diff --git a/setup-vscode-macos.sh b/setup-vscode-macos.sh index 0e3524a..e545b73 100755 --- a/setup-vscode-macos.sh +++ b/setup-vscode-macos.sh @@ -112,7 +112,10 @@ done [ -z "$GCC_DIR" ] && die "Dossier 'gcc/' introuvable a la racine du projet ($PROJECT_ROOT). Ce script attend un projet CubeMX genere avec la toolchain 'STM32CubeIDE / Makefile'." info "Dossier de build : $GCC_DIR" -mapfile -t SUBMAKEFILES < <(find "$GCC_DIR" -maxdepth 1 -type f \( -iname "makefile_*" \) | sort) +SUBMAKEFILES=() +while IFS= read -r line; do + SUBMAKEFILES+=("$line") +done < <(find "$GCC_DIR" -maxdepth 1 -type f -iname "makefile_*" | sort) TOP_MAKEFILE="$GCC_DIR/Makefile" [ -f "$TOP_MAKEFILE" ] || die "Makefile principal introuvable dans $GCC_DIR" if [ "${#SUBMAKEFILES[@]}" -eq 0 ]; then @@ -311,19 +314,52 @@ fi # ============================================================================== log "Extraction des chemins d'include / defines (pour l'auto-completion VS Code)" # ============================================================================== -MAIN_APP_MAKEFILE="${SUBMAKEFILES[-1]}" -INCLUDE_PATHS_RAW=$("$GNU_MAKE" -f "$MAIN_APP_MAKEFILE" -C "$GCC_DIR" -p 2>/dev/null | grep -m1 "^include_paths " | sed 's/^include_paths[[:space:]]*:=[[:space:]]*//') -DEFINES_RAW=$("$GNU_MAKE" -f "$MAIN_APP_MAKEFILE" -C "$GCC_DIR" -p 2>/dev/null | grep -m1 "^c_compiler_options " | grep -oE '\-D[A-Za-z0-9_]+' | sed 's/^-D//') +# On essaie chaque makefile jusqu'a en trouver un qui expose "include_paths :=" +# (le plus complet, generalement celui de l'application principale - "Boot" par +# exemple n'a souvent qu'une variante simplifiee sans cette variable). +MAIN_APP_MAKEFILE="" +INCLUDE_PATHS_RAW="" +DEFINES_RAW="" +for candidate in "${SUBMAKEFILES[@]}"; do + candidate_basename="$(basename "$candidate")" + candidate_dump=$(cd "$GCC_DIR" && "$GNU_MAKE" -f "$candidate_basename" -p 2>/dev/null) + candidate_includes=$(echo "$candidate_dump" | grep -m1 "^include_paths :=" | sed 's/^include_paths[[:space:]]*:=[[:space:]]*//') + if [ -n "$candidate_includes" ]; then + MAIN_APP_MAKEFILE="$candidate" + MAKE_DUMP="$candidate_dump" + INCLUDE_PATHS_RAW="$candidate_includes" + DEFINES_RAW=$(echo "$candidate_dump" | grep -m1 "^c_compiler_options =" | grep -oE '\-D[A-Za-z0-9_]+' | sed 's/^-D//' | sort -u) + break + fi +done +if [ -z "$INCLUDE_PATHS_RAW" ]; then + warn "Extraction des chemins d'include incomplete - c_cpp_properties.json pourra necessiter des ajustements manuels." + MAIN_APP_MAKEFILE="${SUBMAKEFILES[0]}" +fi +info "Makefile utilise pour l'extraction : $(basename "$MAIN_APP_MAKEFILE")" -INCLUDE_JSON="" +INCLUDE_ITEMS=() for p in $INCLUDE_PATHS_RAW; do - INCLUDE_JSON="$INCLUDE_JSON \"\${workspaceFolder}/$p\",\n" + INCLUDE_ITEMS+=(" \"\${workspaceFolder}/$p\"") done -DEFINES_JSON="" +DEFINE_ITEMS=() for d in $DEFINES_RAW; do - DEFINES_JSON="$DEFINES_JSON \"$d\",\n" + DEFINE_ITEMS+=(" \"$d\"") done -info "$(echo -e "$INCLUDE_JSON" | grep -c workspaceFolder) chemins d'include et $(echo -e "$DEFINES_JSON" | grep -c '\".*\"') defines extraits." +join_with_commas() { + local first=1 + for item in "$@"; do + [ "$first" -eq 0 ] && printf ",\n" + printf "%s" "$item" + first=0 + done + [ "$#" -gt 0 ] && printf "\n" +} +INCLUDE_JSON="" +[ "${#INCLUDE_ITEMS[@]}" -gt 0 ] && INCLUDE_JSON="$(join_with_commas "${INCLUDE_ITEMS[@]}")" +DEFINES_JSON="" +[ "${#DEFINE_ITEMS[@]}" -gt 0 ] && DEFINES_JSON="$(join_with_commas "${DEFINE_ITEMS[@]}")" +info "${#INCLUDE_ITEMS[@]} chemins d'include et ${#DEFINE_ITEMS[@]} defines extraits." # ============================================================================== log "Detection de la structure du projet (mono-projet ou Boot+Appli)" @@ -361,10 +397,10 @@ GCC_DIR_REL=${GCC_DIR#"$PROJECT_ROOT/"} echo " \"cppStandard\": \"gnu++14\"," echo " \"intelliSenseMode\": \"linux-gcc-arm\"," echo " \"defines\": [" - echo -e "$DEFINES_JSON" | sed '$ s/,$//' + echo "$DEFINES_JSON" echo " ]," echo " \"includePath\": [" - echo -e "$INCLUDE_JSON" | sed '$ s/,$//' + echo "$INCLUDE_JSON" echo " ]" echo " }" echo " ]"