Skip to content

Commit 9e17252

Browse files
committed
improved detect_arm_include_dir()
1 parent 61e3bd3 commit 9e17252

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

build_environment.sh

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,33 @@ detect_arm_include_dir() {
8585
return 1
8686
fi
8787

88-
# Ask the compiler for its sysroot (where its libraries and headers live)
88+
# 1) Try sysroot-based detection first (works for some packaged toolchains)
8989
local sysroot
9090
sysroot=$(arm-none-eabi-gcc -print-sysroot 2>/dev/null || true)
9191

92-
# Some packaged toolchains may return empty or "/" if they do not use a sysroot.
93-
if [ -z "$sysroot" ] || [ "$sysroot" = "/" ]; then
94-
# We do not guess further here to avoid wrong paths.
95-
# The user can always override C_INCLUDE_PATH manually if needed.
96-
return 1
92+
if [ -n "$sysroot" ] && [ "$sysroot" != "/" ]; then
93+
if [ -d "$sysroot/include" ]; then
94+
echo "$sysroot/include"
95+
return 0
96+
fi
97+
if [ -d "$sysroot/arm-none-eabi/include" ]; then
98+
echo "$sysroot/arm-none-eabi/include"
99+
return 0
100+
fi
97101
fi
98102

99-
# Common header locations, tested in order.
100-
if [ -d "$sysroot/include" ]; then
101-
echo "$sysroot/include"
102-
return 0
103-
fi
103+
# 2) Fallback: ask GCC directly for its internal include directory.
104+
# On many Linux distributions, -print-sysroot is empty for bare-metal toolchains,
105+
# but -print-file-name=include still returns a valid include path.
106+
local gcc_include
107+
gcc_include=$(arm-none-eabi-gcc -print-file-name=include 2>/dev/null || true)
104108

105-
if [ -d "$sysroot/arm-none-eabi/include" ]; then
106-
echo "$sysroot/arm-none-eabi/include"
109+
if [ -n "$gcc_include" ] && [ "$gcc_include" != "include" ] && [ -d "$gcc_include" ]; then
110+
echo "$gcc_include"
107111
return 0
108112
fi
109113

110-
# No known include directory found under sysroot.
114+
# Nothing usable found.
111115
return 1
112116
}
113117

@@ -138,7 +142,7 @@ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
138142
export C_INCLUDE_PATH="${arm_inc_dir}"
139143
fi
140144
else
141-
log_warn "Could not auto-detect ARM include directory on Linux. You may need to set C_INCLUDE_PATH manually for your toolchain."
145+
log_warn "Could not auto-detect ARM include directory on Linux (sysroot may be empty for this toolchain). You may need to set C_INCLUDE_PATH manually."
142146
fi
143147

144148
elif [[ "$OSTYPE" == "darwin"* ]]; then

0 commit comments

Comments
 (0)