Skip to content

Commit 2e443a2

Browse files
authored
Update copy-libs.sh
1 parent 75501ac commit 2e443a2

File tree

1 file changed

+156
-40
lines changed

1 file changed

+156
-40
lines changed

tools/copy-libs.sh

Lines changed: 156 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,92 @@ function get_actual_path(){
5252
fi
5353
}
5454

55+
# Include-Pfad-Verkürzung Funktionen (für statische Generierung)
56+
# Das Build-Zeit Framework-Verzeichnis (ESP-IDF Quellen)
57+
if [ -n "$IDF_PATH" ]; then
58+
BUILD_FRAMEWORK_DIR="$IDF_PATH"
59+
elif [ -d "../esp-idf" ]; then
60+
BUILD_FRAMEWORK_DIR="$(realpath ../esp-idf)"
61+
else
62+
BUILD_FRAMEWORK_DIR=$(dirname $(dirname "$PWD"))
63+
fi
64+
65+
is_framework_subfolder() {
66+
local potential_subfolder="$1"
67+
68+
# Prüfe ob absoluter Pfad
69+
if [[ "$potential_subfolder" != /* ]]; then
70+
return 1
71+
fi
72+
73+
# Prüfe ob auf gleichem Laufwerk
74+
local sdk_drive="${BUILD_FRAMEWORK_DIR%%:*}"
75+
local sub_drive="${potential_subfolder%%:*}"
76+
if [[ "$sdk_drive" != "$sub_drive" ]]; then
77+
return 1
78+
fi
79+
80+
# Prüfe ob Unterordner (analog zu Python os.path.commonpath)
81+
case "$potential_subfolder" in
82+
"$BUILD_FRAMEWORK_DIR"*)
83+
return 0
84+
;;
85+
*)
86+
return 1
87+
;;
88+
esac
89+
}
90+
91+
shorten_includes() {
92+
local includes_str="$1"
93+
local shortened_includes=""
94+
local generic_includes=""
95+
96+
# Konvertiere Include-String zu Array
97+
IFS=' ' read -ra includes_array <<< "$includes_str"
98+
99+
for inc in "${includes_array[@]}"; do
100+
if [[ -n "$inc" ]]; then
101+
if is_framework_subfolder "$inc"; then
102+
# Mappe auf die kopierte Struktur im Arduino Framework
103+
local rel_path="${inc#$BUILD_FRAMEWORK_DIR}"
104+
rel_path="${rel_path#/}" # Entferne führenden Slash
105+
106+
# Konvertiere ESP-IDF Pfad zu Arduino Framework Pfad
107+
if [[ "$rel_path" == "components/"* ]]; then
108+
# components/freertos/include -> include/freertos
109+
local component_path="${rel_path#components/}"
110+
local component_name="${component_path%%/*}"
111+
local sub_path="${component_path#*/}"
112+
if [[ "$sub_path" == "include" ]]; then
113+
shortened_includes+=" -iwithprefix/include/$component_name"
114+
else
115+
shortened_includes+=" -iwithprefix/include/$component_name/$sub_path"
116+
fi
117+
elif [[ "$rel_path" == "managed_components/"* ]]; then
118+
# managed_components/esp32-camera/include -> include/esp32-camera
119+
local component_path="${rel_path#managed_components/}"
120+
local component_name="${component_path%%/*}"
121+
local sub_path="${component_path#*/}"
122+
if [[ "$sub_path" == "include" ]]; then
123+
shortened_includes+=" -iwithprefix/include/$component_name"
124+
else
125+
shortened_includes+=" -iwithprefix/include/$component_name/$sub_path"
126+
fi
127+
else
128+
# Andere Pfade direkt mappen
129+
shortened_includes+=" -iwithprefix/$rel_path"
130+
fi
131+
else
132+
generic_includes+=" $inc"
133+
fi
134+
fi
135+
done
136+
137+
# Rückgabe: "generic_includes|shortened_includes"
138+
echo "$generic_includes|$shortened_includes"
139+
}
140+
55141
#
56142
# START OF DATA EXTRACTION FROM CMAKE
57143
#
@@ -61,6 +147,7 @@ CPP_FLAGS=""
61147
AS_FLAGS=""
62148

63149
INCLUDES=""
150+
INCLUDES_RAW=""
64151
DEFINES=""
65152

66153
EXCLUDE_LIBS=";"
@@ -110,16 +197,19 @@ for item in "${@:2:${#@}-5}"; do
110197
if [ "${item:0:1}" = "/" ]; then
111198
item=`get_actual_path $item`
112199
INCLUDES+="$item "
200+
INCLUDES_RAW+="$item "
113201
elif [ "${item:0:2}" = ".." ]; then
114202
if [[ "${item:0:14}" = "../components/" && "${item:0:22}" != "../components/arduino/" ]] || [[ "${item:0:11}" = "../esp-idf/" ]] || [[ "${item:0:22}" = "../managed_components/" ]]; then
115203
item="$PWD${item:2}"
116204
item=`get_actual_path $item`
117205
INCLUDES+="$item "
206+
INCLUDES_RAW+="$item "
118207
fi
119208
else
120209
item="$PWD/build/$item"
121210
item=`get_actual_path $item`
122211
INCLUDES+="$item "
212+
INCLUDES_RAW+="$item "
123213
fi
124214
elif [ "$prefix" = "-D" ]; then
125215
if [[ "${item:2:7}" != "ARDUINO" ]] && [[ "$item" != "-DESP32=ESP32" ]] && [[ "$item" != "-DNDEBUG" ]]; then #skip ARDUINO defines
@@ -134,6 +224,11 @@ for item in "${@:2:${#@}-5}"; do
134224
fi
135225
done
136226

227+
# Wende Include-Verkürzung an
228+
SHORTENED_RESULT=$(shorten_includes "$INCLUDES_RAW")
229+
GENERIC_INCLUDES="${SHORTENED_RESULT%|*}"
230+
SHORTENED_INCLUDES="${SHORTENED_RESULT#*|}"
231+
137232
#collect asm-flags
138233
str=`cat build/compile_commands.json | grep arduino-lib-builder-as.S | grep command | cut -d':' -f2 | cut -d',' -f1`
139234
str="${str:2:${#str}-1}" #remove leading space and quotes
@@ -322,12 +417,25 @@ mkdir -p "$AR_SDK"
322417
AR_PLATFORMIO_PY="$AR_SDK/pioarduino-build.py"
323418
cat configs/pio_start.txt > "$AR_PLATFORMIO_PY"
324419

420+
# ASFLAGS mit statischer Include-Verkürzung
325421
echo " ASFLAGS=[" >> "$AR_PLATFORMIO_PY"
326422
if [ "$IS_XTENSA" = "y" ]; then
327423
echo " \"-mlongcalls\"" >> "$AR_PLATFORMIO_PY"
328424
else
329425
echo " \"-march=rv32imc\"" >> "$AR_PLATFORMIO_PY"
330426
fi
427+
428+
# Füge statische Include-Verkürzung hinzu
429+
if [[ -n "$SHORTENED_INCLUDES" ]]; then
430+
echo " \"-iprefix\", join(FRAMEWORK_DIR, \"tools\", \"sdk\", \"$IDF_TARGET\")," >> "$AR_PLATFORMIO_PY"
431+
IFS=' ' read -ra shortened_array <<< "$SHORTENED_INCLUDES"
432+
for flag in "${shortened_array[@]}"; do
433+
if [[ -n "$flag" ]]; then
434+
echo " \"$flag\"," >> "$AR_PLATFORMIO_PY"
435+
fi
436+
done
437+
fi
438+
331439
echo " ]," >> "$AR_PLATFORMIO_PY"
332440
echo "" >> "$AR_PLATFORMIO_PY"
333441

@@ -364,11 +472,24 @@ echo " \"$last_item\"" >> "$AR_PLATFORMIO_PY"
364472
echo " ]," >> "$AR_PLATFORMIO_PY"
365473
echo "" >> "$AR_PLATFORMIO_PY"
366474

475+
# CCFLAGS mit statischer Include-Verkürzung
367476
echo " CCFLAGS=[" >> "$AR_PLATFORMIO_PY"
368477
set -- $PIO_CC_FLAGS
369478
for item; do
370479
echo " \"$item\"," >> "$AR_PLATFORMIO_PY"
371480
done
481+
482+
# Füge statische Include-Verkürzung hinzu
483+
if [[ -n "$SHORTENED_INCLUDES" ]]; then
484+
echo " \"-iprefix\", join(FRAMEWORK_DIR, \"tools\", \"sdk\", \"$IDF_TARGET\")," >> "$AR_PLATFORMIO_PY"
485+
IFS=' ' read -ra shortened_array <<< "$SHORTENED_INCLUDES"
486+
for flag in "${shortened_array[@]}"; do
487+
if [[ -n "$flag" ]]; then
488+
echo " \"$flag\"," >> "$AR_PLATFORMIO_PY"
489+
fi
490+
done
491+
fi
492+
372493
echo " \"-MMD\"" >> "$AR_PLATFORMIO_PY"
373494
echo " ]," >> "$AR_PLATFORMIO_PY"
374495
echo "" >> "$AR_PLATFORMIO_PY"
@@ -391,9 +512,11 @@ echo " '-Wl,-Map=\"%s\"' % join(\"\${BUILD_DIR}\", \"\${PROGNAME}.map\")"
391512
echo " ]," >> "$AR_PLATFORMIO_PY"
392513
echo "" >> "$AR_PLATFORMIO_PY"
393514

394-
# include dirs
395-
REL_INC=""
515+
# CPPPATH nur mit kopierten Headers
516+
echo " CPPPATH=[" >> "$AR_PLATFORMIO_PY"
396517

518+
# Bestehende CPPPATH-Logik für kopierte Headers
519+
REL_INC=""
397520
set -- $INCLUDES
398521

399522
for item; do
@@ -420,11 +543,11 @@ for item; do
420543
out_cpath="$AR_SDK/include/$fname$out_sub"
421544
REL_INC+="-iwithprefixbefore $fname$out_sub "
422545
if [ "$out_sub" = "" ]; then
423-
echo ""
546+
echo " join($PIO_SDK, \"include\", \"$fname\")," >> "$AR_PLATFORMIO_PY"
424547
else
425548
pio_sub="${out_sub:1}"
426549
pio_sub=`echo $pio_sub | sed 's/\//\\", \\"/g'`
427-
echo ""
550+
echo " join($PIO_SDK, \"include\", \"$fname\", \"$pio_sub\")," >> "$AR_PLATFORMIO_PY"
428551
fi
429552
for f in `find "$item" -name '*.h'`; do
430553
rel_f=${f#*$item}
@@ -451,17 +574,6 @@ for item; do
451574
fi
452575
fi
453576
done
454-
455-
echo " CPPPATH=[" >> "$AR_PLATFORMIO_PY"
456-
457-
set -- $REL_INC
458-
for item; do
459-
if [ "${item:0:17}" = "-iwithprefixbefore" ]; then
460-
include_path="${item:18}"
461-
echo " join($PIO_SDK, \"include\", \"$include_path\")," >> "$AR_PLATFORMIO_PY"
462-
fi
463-
done
464-
465577
echo " join($PIO_SDK, board_config.get(\"build.arduino.memory_type\", (board_config.get(\"build.flash_mode\", \"dio\") + \"_qspi\")), \"include\")," >> "$AR_PLATFORMIO_PY"
466578
echo " join(FRAMEWORK_DIR, \"cores\", board_config.get(\"build.core\"))" >> "$AR_PLATFORMIO_PY"
467579
echo " ]," >> "$AR_PLATFORMIO_PY"
@@ -520,6 +632,35 @@ cat configs/pio_end.txt >> "$AR_PLATFORMIO_PY"
520632
# replace double backslashes with single one
521633
DEFINES=`echo "$DEFINES" | tr -s '\'`
522634

635+
# target flags files
636+
FLAGS_DIR="$AR_SDK/flags"
637+
mkdir -p "$FLAGS_DIR"
638+
echo -n "$DEFINES" > "$FLAGS_DIR/defines"
639+
echo -n "$REL_INC" > "$FLAGS_DIR/includes"
640+
echo -n "$C_FLAGS" > "$FLAGS_DIR/c_flags"
641+
echo -n "$CPP_FLAGS" > "$FLAGS_DIR/cpp_flags"
642+
echo -n "$AS_FLAGS" > "$FLAGS_DIR/S_flags"
643+
echo -n "$LD_FLAGS" > "$FLAGS_DIR/ld_flags"
644+
echo -n "$LD_SCRIPTS" > "$FLAGS_DIR/ld_scripts"
645+
echo -n "$AR_LIBS" > "$FLAGS_DIR/ld_libs"
646+
647+
# Speichere auch die verkürzten Includes in den Flags
648+
echo -n "$SHORTENED_INCLUDES" > "$FLAGS_DIR/shortened_includes"
649+
echo -n "$GENERIC_INCLUDES" > "$FLAGS_DIR/generic_includes"
650+
651+
if [ -d "managed_components/espressif__esp32-camera/driver/private_include/" ]; then
652+
cp -r "managed_components/espressif__esp32-camera/driver/private_include/cam_hal.h" "$AR_SDK/include/espressif__esp32-camera/driver/include/"
653+
fi
654+
655+
# sdkconfig
656+
cp -f "sdkconfig" "$AR_SDK/sdkconfig"
657+
658+
# dependencies.lock
659+
cp -f "dependencies.lock" "$AR_SDK/dependencies.lock"
660+
661+
# gen_esp32part.py
662+
# cp "$IDF_PATH/components/partition_table/gen_esp32part.py" "$AR_GEN_PART_PY"
663+
523664
# copy precompiled libs (if we need them)
524665
function copy_precompiled_lib(){
525666
lib_file="$1"
@@ -549,31 +690,6 @@ for lib in "openthread" "espressif__esp-tflite-micro" "bt" "espressif__esp_modem
549690
fi
550691
done
551692

552-
# target flags files
553-
FLAGS_DIR="$AR_SDK/flags"
554-
mkdir -p "$FLAGS_DIR"
555-
echo -n "$DEFINES" > "$FLAGS_DIR/defines"
556-
echo -n "$REL_INC" > "$FLAGS_DIR/includes"
557-
echo -n "$C_FLAGS" > "$FLAGS_DIR/c_flags"
558-
echo -n "$CPP_FLAGS" > "$FLAGS_DIR/cpp_flags"
559-
echo -n "$AS_FLAGS" > "$FLAGS_DIR/S_flags"
560-
echo -n "$LD_FLAGS" > "$FLAGS_DIR/ld_flags"
561-
echo -n "$LD_SCRIPTS" > "$FLAGS_DIR/ld_scripts"
562-
echo -n "$LD_LIBS" > "$FLAGS_DIR/ld_libs"
563-
564-
if [ -d "managed_components/espressif__esp32-camera/driver/private_include/" ]; then
565-
cp -r "managed_components/espressif__esp32-camera/driver/private_include/cam_hal.h" "$AR_SDK/include/espressif__esp32-camera/driver/include/"
566-
fi
567-
568-
# sdkconfig
569-
cp -f "sdkconfig" "$AR_SDK/sdkconfig"
570-
571-
# dependencies.lock
572-
cp -f "dependencies.lock" "$AR_SDK/dependencies.lock"
573-
574-
# gen_esp32part.py
575-
# cp "$IDF_PATH/components/partition_table/gen_esp32part.py" "$AR_GEN_PART_PY"
576-
577693
# Handle Mem Variants
578694
mkdir -p "$AR_SDK/$MEMCONF/include"
579695
mv "$PWD/build/config/sdkconfig.h" "$AR_SDK/$MEMCONF/include/sdkconfig.h"

0 commit comments

Comments
 (0)