Skip to content

Commit 0d75abb

Browse files
authored
Update copy-libs.sh
1 parent b5c65b1 commit 0d75abb

File tree

1 file changed

+45
-59
lines changed

1 file changed

+45
-59
lines changed

tools/copy-libs.sh

Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ source ./tools/config.sh
1717

1818
echo "IDF_TARGET: $IDF_TARGET, MEMCONF: $MEMCONF, PWD: $PWD, OUT: $AR_SDK"
1919

20-
# clean previous
20+
# Clean previous build artifacts
2121
if [ -e "$AR_SDK/sdkconfig" ]; then
2222
rm -rf "$AR_SDK/sdkconfig"
2323
fi
@@ -43,6 +43,7 @@ fi
4343
mkdir -p "$AR_SDK"
4444
mkdir -p "$AR_SDK/lib"
4545

46+
# Helper function to resolve actual path
4647
function get_actual_path(){
4748
d="$1";
4849
if [ -d "$d" ]; then
@@ -87,7 +88,7 @@ else
8788
TOOLCHAIN="riscv32-esp-elf"
8889
fi
8990

90-
# copy zigbee + zboss lib
91+
# Copy zigbee + zboss libraries if available
9192
if [ -d "managed_components/espressif__esp-zigbee-lib/lib/$IDF_TARGET/" ]; then
9293
cp -r "managed_components/espressif__esp-zigbee-lib/lib/$IDF_TARGET"/* "$AR_SDK/lib/"
9394
EXCLUDE_LIBS+="esp_zb_api.ed;esp_zb_api.zczr;"
@@ -98,10 +99,10 @@ if [ -d "managed_components/espressif__esp-zboss-lib/lib/$IDF_TARGET/" ]; then
9899
EXCLUDE_LIBS+="zboss_stack.ed;zboss_stack.zczr;zboss_port.native;zboss_port.native.debug;zboss_port.remote;zboss_port.remote.debug;"
99100
fi
100101

101-
#collect includes, defines and c-flags
102+
# Extract includes, defines and c-flags from compile_commands.json
102103
str=`cat build/compile_commands.json | grep arduino-lib-builder-gcc.c | grep command | cut -d':' -f2 | cut -d',' -f1`
103-
str="${str:2:${#str}-1}" #remove leading space and quotes
104-
str=`printf '%b' "$str"` #unescape the string
104+
str="${str:2:${#str}-1}"
105+
str=`printf '%b' "$str"`
105106
set -- $str
106107
for item in "${@:2:${#@}-5}"; do
107108
prefix="${item:0:2}"
@@ -122,7 +123,7 @@ for item in "${@:2:${#@}-5}"; do
122123
INCLUDES+="$item "
123124
fi
124125
elif [ "$prefix" = "-D" ]; then
125-
if [[ "${item:2:7}" != "ARDUINO" ]] && [[ "$item" != "-DESP32=ESP32" ]] && [[ "$item" != "-DNDEBUG" ]]; then #skip ARDUINO defines
126+
if [[ "${item:2:7}" != "ARDUINO" ]] && [[ "$item" != "-DESP32=ESP32" ]] && [[ "$item" != "-DNDEBUG" ]]; then
126127
DEFINES+="$item "
127128
fi
128129
elif [ "$prefix" = "-O" ]; then
@@ -134,10 +135,10 @@ for item in "${@:2:${#@}-5}"; do
134135
fi
135136
done
136137

137-
#collect asm-flags
138+
# Extract assembly flags from compile_commands.json
138139
str=`cat build/compile_commands.json | grep arduino-lib-builder-as.S | grep command | cut -d':' -f2 | cut -d',' -f1`
139-
str="${str:2:${#str}-1}" #remove leading space and quotes
140-
str=`printf '%b' "$str"` #unescape the string
140+
str="${str:2:${#str}-1}"
141+
str=`printf '%b' "$str"`
141142
set -- $str
142143
for item in "${@:2:${#@}-5}"; do
143144
prefix="${item:0:2}"
@@ -153,10 +154,10 @@ for item in "${@:2:${#@}-5}"; do
153154
fi
154155
done
155156

156-
#collect cpp-flags
157+
# Extract C++ flags from compile_commands.json
157158
str=`cat build/compile_commands.json | grep arduino-lib-builder-cpp.cpp | grep command | cut -d':' -f2 | cut -d',' -f1`
158-
str="${str:2:${#str}-1}" #remove leading space and quotes
159-
str=`printf '%b' "$str"` #unescape the string
159+
str="${str:2:${#str}-1}"
160+
str=`printf '%b' "$str"`
160161
set -- $str
161162
for item in "${@:2:${#@}-5}"; do
162163
prefix="${item:0:2}"
@@ -177,7 +178,7 @@ for item; do
177178
fi
178179
done
179180

180-
#parse link command to extract libs and flags
181+
# Parse link command to extract libraries and flags
181182
add_next=0
182183
is_dir=0
183184
is_script=0
@@ -207,26 +208,25 @@ for item; do
207208
prefix="${item:0:1}"
208209
if [ "$prefix" = "-" ]; then
209210
if [ "${item:0:10}" != "-Wl,--Map=" ]; then
210-
if [ "$item" = "-L" ]; then # -L /path
211+
if [ "$item" = "-L" ]; then
211212
add_next=1
212213
is_dir=1
213-
elif [ "${item:0:2}" = "-L" ]; then # -L/path
214+
elif [ "${item:0:2}" = "-L" ]; then
214215
LD_SCRIPT_DIRS+="${item:2} "
215-
elif [ "$item" = "-T" ]; then # -T script.ld
216+
elif [ "$item" = "-T" ]; then
216217
add_next=1
217218
is_script=1
218219
LD_SCRIPTS+="$item "
219-
elif [ "$item" = "-u" ]; then # -u function_name
220+
elif [ "$item" = "-u" ]; then
220221
add_next=1
221222
LD_FLAGS+="$item "
222-
elif [ "${item:0:2}" = "-l" ]; then # -l[lib_name]
223+
elif [ "${item:0:2}" = "-l" ]; then
223224
short_name="${item:2}"
224225
if [[ $EXCLUDE_LIBS != *";$short_name;"* ]]; then
225226
LD_LIBS+="$item "
226227
exclude_libs=";m;c;gcc;stdc++;"
227228
if [[ $exclude_libs != *";$short_name;"* && $LD_LIBS_SEARCH != *"lib$short_name.a"* ]]; then
228229
LD_LIBS_SEARCH+="lib$short_name.a "
229-
#echo "1. lib add: $item"
230230
fi
231231
fi
232232
elif [ "$item" = "-o" ]; then
@@ -262,29 +262,26 @@ for item; do
262262
if [[ "$lname" != "main" && "$lname" != "arduino" ]]; then
263263
lsize=$($SSTAT "$item")
264264
if (( lsize > 8 )); then
265-
# do we already have this file?
265+
# Check if we already have this file
266266
if [[ $LD_LIB_FILES != *"$item"* ]]; then
267-
# do we already have lib with the same name?
267+
# Check if we already have lib with the same name
268268
if [[ $LD_LIBS != *"-l$lname"* ]]; then
269269
if [[ $EXCLUDE_LIBS != *";$lname;"* ]]; then
270-
#echo "2. collecting lib '$lname' and file: $item"
271270
LD_LIB_FILES+="$item "
272271
LD_LIBS+="-l$lname "
273272
fi
274273
else
275-
# echo "!!! need to rename: '$lname'"
274+
# Need to rename library to avoid conflicts
276275
for i in {2..9}; do
277276
n_item="${item:0:${#item}-2}_$i.a"
278277
n_name=$lname"_$i"
279278
if [ -f "$n_item" ]; then
280279
if [[ $EXCLUDE_LIBS != *";$lname;"* ]]; then
281-
#echo "3. renamed add: -l$n_name"
282280
LD_LIBS+="-l$n_name "
283281
fi
284282
break
285283
elif [[ $LD_LIB_FILES != *"$n_item"* && $LD_LIBS != *"-l$n_name"* ]]; then
286284
if [[ $EXCLUDE_LIBS != *";$lname;"* ]]; then
287-
#echo "4. Renaming '$lname' to '$n_name': $item"
288285
cp -f "$item" "$n_item"
289286
LD_LIB_FILES+="$n_item "
290287
LD_LIBS+="-l$n_name "
@@ -295,7 +292,6 @@ for item; do
295292
fi
296293
else
297294
if [[ $EXCLUDE_LIBS != *";$lname;"* ]]; then
298-
#echo "5. just add: -l$lname"
299295
LD_LIBS+="-l$lname "
300296
fi
301297
fi
@@ -318,11 +314,11 @@ done
318314

319315
mkdir -p "$AR_SDK"
320316

321-
# start generation of pioarduino-build.py
317+
# Start generation of pioarduino-build.py
322318
AR_PLATFORMIO_PY="$AR_SDK/pioarduino-build.py"
323319
cat configs/pio_start.txt > "$AR_PLATFORMIO_PY"
324320

325-
# include dirs - SAMMLE REL_INC für Include-Verkürzung UND kopiere Header
321+
# Process include directories - collect shortened include paths and copy headers
326322
REL_INC=""
327323
set -- $INCLUDES
328324

@@ -350,7 +346,7 @@ for item; do
350346
out_cpath="$AR_SDK/include/$fname$out_sub"
351347
REL_INC+="-iwithprefixbefore $fname$out_sub "
352348

353-
# KOPIERE HEADER-DATEIEN
349+
# Copy header files to output directory
354350
for f in `find "$item" -name '*.h'`; do
355351
rel_f=${f#*$item}
356352
rel_p=${rel_f%/*}
@@ -377,28 +373,25 @@ for item; do
377373
fi
378374
done
379375

380-
# ASFLAGS mit korrigierter Include-Verkürzung (ein String wie Python-Version)
376+
# Generate ASFLAGS with shortened include paths
381377
echo " ASFLAGS=[" >> "$AR_PLATFORMIO_PY"
382378
if [ "$IS_XTENSA" = "y" ]; then
383379
echo " \"-mlongcalls\"," >> "$AR_PLATFORMIO_PY"
384380
else
385381
echo " \"-march=rv32imc\"," >> "$AR_PLATFORMIO_PY"
386382
fi
387383

388-
# Füge Include-Verkürzung aus REL_INC hinzu - KORRIGIERT wie Python-Version
384+
# Add shortened include paths for assembly
389385
if [[ -n "$REL_INC" ]]; then
390386
echo " \"-iprefix\", join(FRAMEWORK_DIR, \"tools\", \"sdk\", \"$IDF_TARGET\")," >> "$AR_PLATFORMIO_PY"
391-
# Verarbeite REL_INC korrekt - Flag und Pfad sind getrennt
392387
IFS=' ' read -ra rel_inc_array <<< "$REL_INC"
393388
i=0
394389
while [ $i -lt ${#rel_inc_array[@]} ]; do
395390
if [[ "${rel_inc_array[$i]}" == "-iwithprefixbefore" ]]; then
396-
# Nächstes Element ist der Pfad
397391
if [ $((i+1)) -lt ${#rel_inc_array[@]} ]; then
398392
path="${rel_inc_array[$((i+1))]}"
399-
# KORRIGIERT: Ein String wie Python-Version
400393
echo " \"-iwithprefix/include/$path\"," >> "$AR_PLATFORMIO_PY"
401-
i=$((i+2)) # Überspringe Flag und Pfad
394+
i=$((i+2))
402395
else
403396
i=$((i+1))
404397
fi
@@ -444,27 +437,24 @@ echo " \"$last_item\"" >> "$AR_PLATFORMIO_PY"
444437
echo " ]," >> "$AR_PLATFORMIO_PY"
445438
echo "" >> "$AR_PLATFORMIO_PY"
446439

447-
# CCFLAGS mit korrigierter Include-Verkürzung (ein String wie Python-Version)
440+
# Generate CCFLAGS with shortened include paths
448441
echo " CCFLAGS=[" >> "$AR_PLATFORMIO_PY"
449442
set -- $PIO_CC_FLAGS
450443
for item; do
451444
echo " \"$item\"," >> "$AR_PLATFORMIO_PY"
452445
done
453446

454-
# Füge Include-Verkürzung aus REL_INC hinzu - KORRIGIERT wie Python-Version
447+
# Add shortened include paths for C/C++ compilation
455448
if [[ -n "$REL_INC" ]]; then
456449
echo " \"-iprefix\", join(FRAMEWORK_DIR, \"tools\", \"sdk\", \"$IDF_TARGET\")," >> "$AR_PLATFORMIO_PY"
457-
# Verarbeite REL_INC korrekt - Flag und Pfad sind getrennt
458450
IFS=' ' read -ra rel_inc_array <<< "$REL_INC"
459451
i=0
460452
while [ $i -lt ${#rel_inc_array[@]} ]; do
461453
if [[ "${rel_inc_array[$i]}" == "-iwithprefixbefore" ]]; then
462-
# Nächstes Element ist der Pfad
463454
if [ $((i+1)) -lt ${#rel_inc_array[@]} ]; then
464455
path="${rel_inc_array[$((i+1))]}"
465-
# KORRIGIERT: Ein String wie Python-Version
466-
echo " \"-iwithprefix/include/$path\"," >> "$AR_PLATFORMIO_PY"
467-
i=$((i+2)) # Überspringe Flag und Pfad
456+
echo " \"$iwithprefix/include/$path\"," >> "$AR_PLATFORMIO_PY"
457+
i=$((i+2))
468458
else
469459
i=$((i+1))
470460
fi
@@ -496,7 +486,7 @@ echo " '-Wl,-Map=\"%s\"' % join(\"\${BUILD_DIR}\", \"\${PROGNAME}.map\")"
496486
echo " ]," >> "$AR_PLATFORMIO_PY"
497487
echo "" >> "$AR_PLATFORMIO_PY"
498488

499-
# CPPPATH - NUR Arduino Core (KEINE Framework-Headers!)
489+
# CPPPATH - Only Arduino Core (NO Framework headers!)
500490
echo " CPPPATH=[" >> "$AR_PLATFORMIO_PY"
501491
echo " join($PIO_SDK, board_config.get(\"build.arduino.memory_type\", (board_config.get(\"build.flash_mode\", \"dio\") + \"_qspi\")), \"include\")," >> "$AR_PLATFORMIO_PY"
502492
echo " join(FRAMEWORK_DIR, \"cores\", board_config.get(\"build.core\"))" >> "$AR_PLATFORMIO_PY"
@@ -513,9 +503,9 @@ for item; do
513503
PIO_LIBS+="\"$item\""
514504
done
515505

506+
# Strip and copy library files
516507
set -- $LD_LIB_FILES
517508
for item; do
518-
#echo "***** Stripping $item"
519509
"$TOOLCHAIN-strip" -g "$item"
520510
cp "$item" "$AR_SDK/lib/"
521511
done
@@ -535,8 +525,8 @@ echo "" >> "$AR_PLATFORMIO_PY"
535525
echo " CPPDEFINES=[" >> "$AR_PLATFORMIO_PY"
536526
set -- $DEFINES
537527
for item; do
538-
item="${item:2}" #remove -D
539-
item="${item/NDEBUG}" #remove NDEBUG
528+
item="${item:2}"
529+
item="${item/NDEBUG}"
540530
if [[ $item == *"="* ]]; then
541531
item=(${item//=/ })
542532
re='^[+-]?[0-9]+([.][0-9]+)?$'
@@ -550,13 +540,13 @@ for item; do
550540
fi
551541
done
552542

553-
# end generation of platformio-build.py
543+
# End generation of platformio-build.py
554544
cat configs/pio_end.txt >> "$AR_PLATFORMIO_PY"
555545

556-
# replace double backslashes with single one
546+
# Replace double backslashes with single one
557547
DEFINES=`echo "$DEFINES" | tr -s '\'`
558548

559-
# target flags files
549+
# Generate target flags files
560550
FLAGS_DIR="$AR_SDK/flags"
561551
mkdir -p "$FLAGS_DIR"
562552
echo -n "$DEFINES" > "$FLAGS_DIR/defines"
@@ -572,16 +562,11 @@ if [ -d "managed_components/espressif__esp32-camera/driver/private_include/" ];
572562
cp -r "managed_components/espressif__esp32-camera/driver/private_include/cam_hal.h" "$AR_SDK/include/espressif__esp32-camera/driver/include/"
573563
fi
574564

575-
# sdkconfig
565+
# Copy configuration files
576566
cp -f "sdkconfig" "$AR_SDK/sdkconfig"
577-
578-
# dependencies.lock
579567
cp -f "dependencies.lock" "$AR_SDK/dependencies.lock"
580568

581-
# gen_esp32part.py
582-
# cp "$IDF_PATH/components/partition_table/gen_esp32part.py" "$AR_GEN_PART_PY"
583-
584-
# copy precompiled libs (if we need them)
569+
# Helper function to copy precompiled libraries if needed
585570
function copy_precompiled_lib(){
586571
lib_file="$1"
587572
lib_name="$(basename $lib_file)"
@@ -591,7 +576,7 @@ function copy_precompiled_lib(){
591576
fi
592577
}
593578

594-
# idf ld scripts
579+
# Copy IDF linker scripts and precompiled libraries
595580
mkdir -p "$AR_SDK/ld"
596581
set -- $LD_SCRIPT_DIRS
597582
for item; do
@@ -603,14 +588,15 @@ for item; do
603588
done
604589
done
605590

591+
# Strip specific libraries
606592
for lib in "openthread" "espressif__esp-tflite-micro" "bt" "espressif__esp_modem" "espressif__esp-zboss-lib" "espressif__esp-zigbee-lib" "espressif__mdns" "espressif__esp-dsp" "espressif__esp32-camera" "joltwallet__littlefs"; do
607593
if [ -f "$AR_SDK/lib/lib$lib.a" ]; then
608594
echo "Stripping $AR_SDK/lib/lib$lib.a"
609595
"$TOOLCHAIN-strip" -g "$AR_SDK/lib/lib$lib.a"
610596
fi
611597
done
612598

613-
# Handle Mem Variants
599+
# Handle memory configuration variants
614600
mkdir -p "$AR_SDK/$MEMCONF/include"
615601
mv "$PWD/build/config/sdkconfig.h" "$AR_SDK/$MEMCONF/include/sdkconfig.h"
616602
for mem_variant in `jq -c '.mem_variants_files[]' configs/builds.json`; do
@@ -629,6 +615,6 @@ for mem_variant in `jq -c '.mem_variants_files[]' configs/builds.json`; do
629615
fi
630616
done;
631617

632-
# Add IDF versions to sdkconfig
618+
# Add IDF version information to sdkconfig
633619
echo "#define CONFIG_ARDUINO_IDF_COMMIT \"$IDF_COMMIT\"" >> "$AR_SDK/$MEMCONF/include/sdkconfig.h"
634620
echo "#define CONFIG_ARDUINO_IDF_BRANCH \"$IDF_BRANCH\"" >> "$AR_SDK/$MEMCONF/include/sdkconfig.h"

0 commit comments

Comments
 (0)