forked from tuya/tuyaopen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tos
executable file
·457 lines (417 loc) · 14.9 KB
/
tos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/env bash
OPEN_SDK_ROOT=$(cd "$(dirname "$0")" && pwd)
PROJECT_ROOT=$(pwd)
. $OPEN_SDK_ROOT/tools/util_tools.sh
LISTMENU=$OPEN_SDK_ROOT/tools/listmenu.sh
OPEN_BUILD=$(basename "$0")
PROJECT_INI=$PROJECT_ROOT/project_build.ini
KCONFIG_TOOLS=$OPEN_SDK_ROOT/tools/kconfiglib
# ==============================================================================
# executing define
# ------------------------------------------------------------------------------
function check_ini()
{
if [ ! -f $PROJECT_INI ]; then
show "${fore[yellow]}" "Not found [$PROJECT_INI]."
show "${fore[green]}" "You could run [$OPEN_BUILD new your_project_name]."
exit 0
fi
}
function check_hooks()
{
if [ -d $OPEN_SDK_ROOT/.git/hooks ]; then
if [ ! -f $OPEN_SDK_ROOT/.git/hooks/pre-commit ]; then
cp -rf $OPEN_SDK_ROOT/tools/hooks/pre-commit $OPEN_SDK_ROOT/.git/hooks/pre-commit
fi
fi
}
function check_submodules()
{
check_hooks
cd $OPEN_SDK_ROOT
submodules=($(grep "path" .gitmodules | sed 's/^[ \t]*path = //'))
for submodule in "${submodules[@]}"
do
if [ -z "$(ls -A $submodule)" ]; then
echo "$submodule is empty, initializing submodule..."
git submodule update --init $submodule
fi
done
cd - > /dev/null
}
function check_arduino()
{
ARDUINO_REPO="https://github.com/tuya/arduino-tuyaopen.git"
ARDUINO_PATH=$OPEN_SDK_ROOT/arduino-tuyaopen
cd $OPEN_SDK_ROOT
if [ -d $ARDUINO_PATH ]; then
show "" "Skip download, exist [$ARDUINO_PATH]."
show "${fore[yellow]}" "If you want to download it again, please remove it."
return 0
fi
git clone $ARDUINO_REPO
if [ ! "0" = "$?" ]; then
show "${fore[red]}" "Clone repo [$ARDUINO_REPO] failed, please try again."
if [ -d "$ARDUINO_PATH" ]; then
show "${fore[yellow]}" "Delete [$ARDUINO_PATH] first."
fi
exit 1
fi
cd $ARDUINO_PATH
TAG=`git describe --tags --abbrev=0`
git checkout $TAG
git submodule update --init
}
function version_exec()
{
show "" "1.0.0"
}
function check_exec()
{
show "" "Check command and version ..."
check_command_version "python3" "3.6.0" "python3 --version"
check_command_version "git" "2.0.0" "git --version"
check_command_version "cmake" "3.16.0" "cmake --version"
check_command_version "ccmake" "3.16.0" "ccmake --version" # sudo apt-get install cmake-curses-gui
check_command_version "lcov" "1.14" "lcov --version"
check_submodules
which $OPEN_BUILD > /dev/null
if [ "1" = "$?" ]; then
show "${fore[yellow]}" "Select a way to configure the OpenSDK tool:"
show "${fore[yellow]}" "1. export PATH=\$PATH:$OPEN_SDK_ROOT"
show "${fore[yellow]}" "2. set step1 to .bashrc / .zshrc / .profile"
fi
}
function download_platform()
{
PLATFORMS_DIR=$OPEN_SDK_ROOT/platform
PLATFORM_PATH="${PLATFORMS_DIR}/${PLATFORM_NAME}"
DEFAULT_CONFIG_NAME="default.config"
if [ -d "$PLATFORM_PATH" ]; then
show "" "Skip download, exist [$PLATFORM_PATH]."
show "${fore[yellow]}" "If you want to download it again, please remove it."
else
show "" "Download platform ..."
git clone "$PLATFORM_REPO" "$PLATFORM_PATH"
if [ ! "0" = "$?" ]; then
show "${fore[red]}" "Clone repo [$PLATFORM_REPO] failed, please try again."
if [ -d "$PLATFORM_PATH" ]; then
show "${fore[yellow]}" "Delete [$PLATFORM_PATH] first."
fi
exit 1
fi
cd "$PLATFORM_PATH"
git checkout -q "$PLATFORM_COMMIT"
fi
if [ -f "$PLATFORM_PATH/platform_prepare.sh" ]; then
show "" "Run [$PLATFORM_PATH/platform_prepare.sh]."
cd "$PLATFORM_PATH"
./platform_prepare.sh $PLATFORM_CHIP
fi
if [ ! -f "$PLATFORM_PATH/$DEFAULT_CONFIG_NAME" ]; then
show "${fore[red]}" "Error: not found [$PLATFORM_PATH/$DEFAULT_CONFIG_NAME]."
exit 1
fi
}
function download_platform_by_name()
{
PLATFORM_NAME=$1
PLATFORM_CHIP=$2
echo "PLATFORM_NAME: $PLATFORM_NAME"
PLATFORMS_DIR=$OPEN_SDK_ROOT/platform
PLATFORM_YAML=$PLATFORMS_DIR/platform_config.yaml
PLATFORM_INFO=$(cat $PLATFORM_YAML | awk "/^- name: $PLATFORM_NAME[[:space:]]*$/{flag=1; next} /^- name:.*$/{flag=0} flag")
PLATFORM_REPO=$(echo "$PLATFORM_INFO" | grep -oP '(?<=repo: ).*(?=$)')
PLATFORM_COMMIT=$(echo "$PLATFORM_INFO" | grep -oP '(?<=commit: ).*(?=$)')
assert_non_null "$PLATFORM_NAME" "Can't find the [platform name]."
assert_non_null "$PLATFORM_REPO" "Can't find the [platform repo]."
assert_non_null "$PLATFORM_COMMIT" "Can't find the [platform commit]."
if [ -z $PLATFORM_NAME ] || [ -z $PLATFORM_REPO ] || [ -z $PLATFORM_COMMIT ]; then
exit 1
fi
download_platform PLATFORM_NAME=$PLATFORM_NAME PLATFORM_REPO=$PLATFORM_REPO PLATFORM_COMMIT=$PLATFORM_COMMIT PLATFORM_CHIP=$PLATFORM_CHIP
}
function new_exec()
{
TEMPLATE_NAME=$1
if [ -z "$TEMPLATE_NAME" ]; then
TEMPLATE_NAME="base"
fi
TEMPLATE_PATH=$OPEN_SDK_ROOT/tools/app_template/$TEMPLATE_NAME
if [ ! -d $TEMPLATE_PATH ]; then
NOTE="No template named [$TEMPLATE_NAME] exists."
show "${fore[red]}" "$NOTE"
exit 1
fi
read -p "Input project name: " PROJ_NAME
if [ -z "$PROJ_NAME" ]; then
NOTE="Project name is empty."
show "${fore[yellow]}" "$NOTE"
exit 1
fi
if [ -d $PROJECT_ROOT/$PROJ_NAME ]; then
NOTE="[$PROJ_NAME] already exists, rename new project or delete the directory."
show "${fore[yellow]}" "$NOTE"
exit 1
fi
show "" "Choice platform ..."
PLATFORMS_DIR=$OPEN_SDK_ROOT/platform
PLATFORM_YAML=$PLATFORMS_DIR/platform_config.yaml
PLATFORM_NAMES=$(cat $PLATFORM_YAML | grep -oP '(?<=^- name: ).*(?=$)')
PLATFORM_NAME=$(echo "$PLATFORM_NAMES" | bash $LISTMENU "Platforms" 20 60 13)
PLATFORM_INFO=$(cat $PLATFORM_YAML | awk "/^- name: $PLATFORM_NAME[[:space:]]*$/{flag=1; next} /^- name:.*$/{flag=0} flag")
PLATFORM_REPO=$(echo "$PLATFORM_INFO" | grep -oP '(?<=repo: ).*(?=$)')
PLATFORM_COMMIT=$(echo "$PLATFORM_INFO" | grep -oP '(?<=commit: ).*(?=$)')
PLATFORM_CHIPS=$(echo "$PLATFORM_INFO" | awk '/^[[:space:]]*chip:[[:space:]]*$/{flag=1; next} flag{gsub(/^[[:space:]-]+/, ""); print}')
if [ ! x"$PLATFORM_CHIPS" = x"" ]; then
PLATFORM_CHIP=$(echo "$PLATFORM_CHIPS" | bash $LISTMENU "Chips" 20 60 13)
fi
assert_non_null "$PLATFORM_NAME" "Can't find the [platform name]."
assert_non_null "$PLATFORM_REPO" "Can't find the [platform repo]."
assert_non_null "$PLATFORM_COMMIT" "Can't find the [platform commit]."
PLATFORM_STR="platform: $PLATFORM_NAME
repo: $PLATFORM_REPO
commit: $PLATFORM_COMMIT
chip: $PLATFORM_CHIP" # end PLATFORM_STR
show "" "$PLATFORM_STR"
cp -a $TEMPLATE_PATH $PROJECT_ROOT/$PROJ_NAME
sed -i "s/###PROJECT_NAME###/${PROJ_NAME}_${PLATFORM_NAME}/g" $PROJECT_ROOT/$PROJ_NAME/project_build.ini
sed -i "s/###PLATFORM_NAME###/$PLATFORM_NAME/g" $PROJECT_ROOT/$PROJ_NAME/project_build.ini
if [ -n "$PLATFORM_CHIP" ]; then
echo "chip = $PLATFORM_CHIP" >> $PROJECT_ROOT/$PROJ_NAME/project_build.ini
fi
NOTE="You can modify the file [project_build.ini] to configure the project."
show "${fore[green]}" "$NOTE"
download_platform PLATFORM_NAME=$PLATFORM_NAME PLATFORM_REPO=$PLATFORM_REPO PLATFORM_COMMIT=$PLATFORM_COMMIT PLATFORM_CHIP=$PLATFORM_CHIP
}
function build_exec()
{
check_ini
check_submodules
PROJECT_NAMES=$1
if [ -z "$PROJECT_NAMES" ]; then
PROJECT_NAMES=$(cat $PROJECT_INI | grep -oP '(?<=^\[project\:).*(?=\])')
fi
RET=0
for PROJ in $PROJECT_NAMES; do
PROJECT_INFO=$(cat $PROJECT_INI | awk "/^\[project:$PROJ]$/{flag=1; next} /^\[project:.*$/{flag=0} flag")
if [ -z "$PROJECT_INFO" ]; then
NOTE="Can't found project [$PROJ]."
show "${fore[red]}" "$NOTE"
RET=1
continue
fi
PROJECT_PLATFORM=$(echo "$PROJECT_INFO" | grep -oP '(?<=platform = ).*(?=$)')
if [ -z "$PROJECT_PLATFORM" ]; then
NOTE="Can't found project [$PROJ] platform."
show "${fore[red]}" "$NOTE"
RET=1
continue
fi
PROJECT_FRAMEWORK=$(echo "$PROJECT_INFO" | grep -oP '(?<=framework = ).*(?=$)')
if [ -z "$PROJECT_FRAMEWORK" ]; then
PROJECT_FRAMEWORK="base"
fi
if [ "$PROJECT_FRAMEWORK" = "arduino" ]; then
check_arduino
fi
PROJECT_CHIP=$(echo "$PROJECT_INFO" | grep -oP '(?<=chip = ).*(?=$)')
PLATFORM_PATH=$OPEN_SDK_ROOT/platform/$PROJECT_PLATFORM
if [ ! -d "$PLATFORM_PATH" ]; then
download_platform_by_name $PROJECT_PLATFORM $PROJECT_CHIP
if [ "0" != "$?" ]; then
RET=1
continue
fi
fi
mkdir -p $PROJECT_ROOT/.build/$PROJ
cd $PROJECT_ROOT/.build/$PROJ
if [ -f "$PLATFORM_PATH/build_setup.sh" ]; then
$PLATFORM_PATH/build_setup.sh "${PROJ}" "${PROJECT_PLATFORM}" "${PROJECT_CHIP}" "${PROJECT_FRAMEWORK}"
fi
cmake $OPEN_SDK_ROOT -DTOS_PROJECT_NAME=$PROJ -DTOS_PROJECT_ROOT=$PROJECT_ROOT -DTOS_PROJECT_PLATFORM=$PROJECT_PLATFORM -DTOS_FRAMEWORK=$PROJECT_FRAMEWORK -DTOS_PROJECT_CHIP=$PROJECT_CHIP
make example
if [ ! "0" = "$?" ]; then
RET=1
fi
if [ -f "$PLATFORM_PATH/build_hock.sh" ]; then
$PLATFORM_PATH/build_hock.sh "${PROJ}" "${PROJECT_PLATFORM}" "${PROJECT_CHIP}" "${PROJECT_FRAMEWORK}"
fi
cd - > /dev/null
done
return "$RET"
}
function clean_exec()
{
check_ini
PROJECT_NAMES=$1
if [ -z "$PROJECT_NAMES" ]; then
PROJECT_NAMES=$(cat $PROJECT_INI | grep -oP '(?<=^\[project\:).*(?=\])')
fi
for PROJ in $PROJECT_NAMES; do
if [ ! -d ".build/$PROJ" ]; then
NOTE="The project does not exist yet, run [$OPEN_BUILD build $PROJ] first"
show "${fore[yellow]}" "$NOTE"
continue
fi
cd $PROJECT_ROOT/.build/$PROJ
make clean_all
cd - > /dev/null
done
}
function menuconfig_exec()
{
check_ini
PROJ=$1
if [ -z "$PROJ" ]; then
PROJECT_NAMES=$(cat $PROJECT_INI | grep -oP '(?<=^\[project\:).*(?=\])')
if [ $(echo "$PROJECT_NAMES" | wc -w) = "1" ]; then
PROJ=$PROJECT_NAMES
else
PROJ=$(echo "$PROJECT_NAMES" | bash $LISTMENU "Projects" 20 60 13)
fi
fi
if [ -z "$PROJ" ]; then
NOTE="The menuconfig requires one parameter [project name]"
show "${fore[yellow]}" "$NOTE"
exit 1
fi
PROJECT_INFO=$(cat $PROJECT_INI | awk "/^\[project:$PROJ]$/{flag=1; next} /^\[project:.*$/{flag=0} flag")
if [ -z "$PROJECT_INFO" ]; then
NOTE="Can't found project [$PROJ]."
show "${fore[red]}" "$NOTE"
exit 1
fi
PROJECT_PLATFORM=$(echo "$PROJECT_INFO" | grep -oP '(?<=platform = ).*(?=$)')
PLATFORMS_DIR="$OPEN_SDK_ROOT/platform"
PLATFORM_PATH="${PLATFORMS_DIR}/${PROJECT_PLATFORM}"
if [ ! -d "$PLATFORM_PATH" ]; then
download_platform_by_name $PROJECT_PLATFORM $PROJECT_CHIP
fi
DOT_CONFIG_DIR="$PROJECT_ROOT/.build/$PROJ/cache"
KCONFIG_CATALOG="CatalogKconfig"
DOT_CONFIG="using.config"
CMAKE_CONFIG="using.cmake"
HEADER_DIR="$PROJECT_ROOT/.build/$PROJ/include"
HEADER_FILE="tuya_kconfig.h"
HEADER_IN_PATH="${KCONFIG_TOOLS}/config.h.in"
mkdir -p ${DOT_CONFIG_DIR}
cd ${DOT_CONFIG_DIR}
if [ ! -f $DOT_CONFIG ]; then
cp $PLATFORM_PATH/default.config $DOT_CONFIG
fi
python3 $KCONFIG_TOOLS/set_catalog_config.py -b $PLATFORM_PATH -s $OPEN_SDK_ROOT/src -a $PROJECT_ROOT -o $KCONFIG_CATALOG
bash ${KCONFIG_TOOLS}/run_menuconfig.sh "${KCONFIG_CATALOG}" "$DOT_CONFIG"
python3 ${KCONFIG_TOOLS}/conf2cmake.py -c "${DOT_CONFIG_DIR}/${DOT_CONFIG}" -o "${CMAKE_CONFIG}"
mkdir -p ${HEADER_DIR}
cd ${HEADER_DIR}
python3 ${KCONFIG_TOOLS}/conf2h.py -c "${DOT_CONFIG_DIR}/${DOT_CONFIG}" -o "${HEADER_FILE}" -i "${HEADER_IN_PATH}"
cd - > /dev/null
}
function _build_all()
{
PROJECTS_DIR=$(cd "$1" && pwd)
MAX_DEEP=$(expr $2 - 1)
PROJECT_INI="$PROJECTS_DIR/project_build.ini"
if [ -f "$PROJECT_INI" ]; then
PROJECT_ROOT=$PROJECTS_DIR
PROJECT_INI=$PROJECT_INI
build_exec
if [ "0" = "$?" ]; then
BUILD_INFORMATION+="\033[32mBuild Success [${PROJECT_ROOT}].\033[0m\n"
else
BUILD_INFORMATION+="\033[31mBuild Failed [${PROJECT_ROOT}].\033[0m\n"
fi
elif [ "$MAX_DEEP" -gt "0" ]; then
SUB_DIRS=$(ls -d $PROJECTS_DIR/*/)
for dir in $SUB_DIRS; do
d=$(cd "$dir" && pwd)
_build_all "$d" "$MAX_DEEP"
done
fi
MAX_DEEP="$2"
}
function build_all_exec()
{
PROJECTS_DIR=$1
if [ -z "$PROJECTS_DIR" ]; then
NOTE="build_all command need an argument [PROJECTS_DIR]"
show "${fore[yellow]}" "$NOTE"
exit 1
fi
if [ ! -d "$PROJECTS_DIR" ]; then
NOTE="Path not exists: [$PROJECTS_DIR]"
show "${fore[yellow]}" "$NOTE"
exit 1
fi
BUILD_INFORMATION="\n----------Build All Result----------\n"
_build_all "$PROJECTS_DIR" "3"
printf "${BUILD_INFORMATION}"
}
function new_platform_exec()
{
PLATFORM_NAME=$1
if [ -z "$PLATFORM_NAME" ]; then
NOTE="new_platform command need an argument [PLATFORM_NAME]"
show "${fore[yellow]}" "$NOTE"
exit 1
fi
${OPEN_SDK_ROOT}/tools/kernel_porting.sh ${PLATFORM_NAME}
}
function help_exec()
{
NOTE="
Usage: $OPEN_BUILD COMMAND [ARGS]...
Commands:
version - Show TOS verson
check - Check command and version
new - New project [base(default) / auduino]
build - Build specific project or all projects
[name] (default build all project by project_build.ini)
clean - Clean specific project or all projects
[name] (default clean all project by project_build.ini)
menuconfig - Configuration project features
build_all - Build all projects in the directory
[*path]
new_platform - New platform [platform_name]
help - Help information
"
show "" "$NOTE"
}
# ==============================================================================
# executing define finished
# ------------------------------------------------------------------------------
case $1 in
version)
version_exec
;;
check)
check_exec
;;
new)
new_exec $2
;;
build)
build_exec $2
;;
clean)
clean_exec $2
;;
menuconfig)
menuconfig_exec $2
;;
build_all)
build_all_exec $2
;;
new_platform)
new_platform_exec $2
;;
help)
help_exec
;;
*)
show "${fore[yellow]}" "Unknow command [$1]"
help_exec
;;
esac
exit 0