arch/arm/ameba: resolve the asdk toolchain per-IC

Each ameba IC pins its asdk (arm-none-eabi) version in the SDK
(component/soc/<soc>/project/CMakeLists.txt): RTL8720F needs 12.3.1 while
RTL8721Dx needs 10.3.1.  Both share the same arm-none-eabi triple and differ
only by install directory.  The make + cmake setup previously read only the
global default, so the NuttX objects for an IC pinning a newer asdk were built
with the wrong compiler (not the one its SDK archives were built with).

Resolve the version per-IC from a single helper (ameba_asdk_version.sh, keyed
by the SoC) used by every caller:
  - toolchain.mk / ameba_board.mk: make picks the right asdk automatically
    (it already knows the SoC), no user action.
  - ameba_fetch_toolchain.sh: fetches the version the SoC pins.
  - tools/ameba/env.sh: takes an optional board (`. tools/ameba/env.sh
    <board>`) so cmake, which locks the compiler at project() time, gets the
    matching asdk on PATH before it probes.  No board -> the global default.
  - ameba_sdk.cmake: fail configure with a fix-it message if the on-PATH
    compiler is not the version this IC needs (e.g. a stale env from another
    board), instead of silently mis-compiling.

Assisted-by: Claude Code:claude-opus-4-8
Signed-off-by: raul_chen <raul_chen@realsil.com.cn>
This commit is contained in:
raul_chen 2026-07-16 18:04:31 +08:00 committed by Alan C. Assis
parent 54213a13b3
commit 84106b9152
7 changed files with 163 additions and 19 deletions

View file

@ -60,6 +60,43 @@ AMEBA_COMMON="$AMEBA_NUTTX/arch/arm/src/common/ameba"
AMEBA_TOOLS="$AMEBA_COMMON/tools"
AMEBA_TOOLCHAIN_DIR="${AMEBA_TOOLCHAIN_DIR:-$HOME/rtk-toolchain}"
# --- Optional target board: `. tools/ameba/env.sh <board>[:config]` ----------
# CMake locks the compiler at project() time (before the board arch is
# processed), so the per-IC asdk toolchain must be on PATH *before* cmake.
# Passing the board here lets env.sh resolve its SoC and fetch/PATH the matching
# asdk version (e.g. rtl8720f_evb -> RTL8720F -> 12.3.1). With no argument the
# global default is used (correct for the RTL8721Dx boards, e.g. pke8721daf).
# The boards this script knows: every board under an ameba arch chip (its arch
# CMakeLists declares AMEBA_SOC_NAME). Enumerated from the tree, so new ICs
# appear automatically.
_ameba_board_list() {
for _cml in "$AMEBA_NUTTX"/arch/arm/src/*/CMakeLists.txt; do
grep -q AMEBA_SOC_NAME "$_cml" 2>/dev/null || continue
_c=$(basename "$(dirname "$_cml")")
for _b in "$AMEBA_NUTTX"/boards/arm/"$_c"/*/; do
[ -d "$_b" ] && printf '%s ' "$(basename "$_b")"
done
done
}
_ameba_board="${1%%:*}"
_ameba_soc=
if [ -n "$_ameba_board" ]; then
_ameba_bdir=$(cd "$AMEBA_NUTTX" 2>/dev/null &&
ls -d boards/arm/*/"$_ameba_board" 2>/dev/null | head -1)
if [ -n "$_ameba_bdir" ]; then
_ameba_chip=$(basename "$(dirname "$AMEBA_NUTTX/$_ameba_bdir")")
_ameba_soc=$(sed -n \
's/.*set(AMEBA_SOC_NAME[ \t][ \t]*\([A-Za-z0-9_]*\).*/\1/p' \
"$AMEBA_NUTTX/arch/arm/src/$_ameba_chip/CMakeLists.txt" 2>/dev/null | head -1)
else
echo "ameba env.sh: unknown board '$_ameba_board'." >&2
echo " known ameba boards: $(_ameba_board_list)" >&2
echo " continuing with the default asdk." >&2
fi
fi
# --- 1. Resolve / fetch the SDK (pin comes from ameba_sdk.mk, single source) --
if [ -n "$AMEBA_SDK" ] && [ -d "$AMEBA_SDK/component/soc" ]; then
@ -79,11 +116,10 @@ export AMEBA_TOOLCHAIN_DIR
# --- 2. asdk toolchain: fetch if absent, prepend to PATH, pin GCCVER ---------
sh "$AMEBA_TOOLS/ameba_fetch_toolchain.sh" "$AMEBA_SDK" "$AMEBA_TOOLCHAIN_DIR" \
sh "$AMEBA_TOOLS/ameba_fetch_toolchain.sh" "$AMEBA_SDK" "$AMEBA_TOOLCHAIN_DIR" "$_ameba_soc" \
|| return 1 2>/dev/null || exit 1
_ver=$(sed -n 's/.*v_ASDK_VER[ \t][ \t]*\([0-9.][0-9.]*\).*/\1/p' \
"$AMEBA_SDK/cmake/global_define.cmake")
_ver=$(sh "$AMEBA_TOOLS/ameba_asdk_version.sh" "$AMEBA_SDK" "$_ameba_soc")
_build=$(sed -n 's/.*ToolChainVerMinor[ \t][ \t]*\([0-9][0-9]*\).*/\1/p' \
"$AMEBA_SDK/cmake/toolchain/ameba-toolchain-asdk-$_ver.cmake")
_asdk_bin="$AMEBA_TOOLCHAIN_DIR/asdk-$_ver-$_build/linux/newlib/bin"
@ -98,8 +134,27 @@ _pbv=$(sed -n 's/^PREBUILTS_VERSION=//p' "$AMEBA_SDK/env.sh" | head -1)
_prebuilts_bin="$AMEBA_TOOLCHAIN_DIR/prebuilts-linux-$_pbv/bin"
_venv_bin="$(cat "$AMEBA_SDK/.amebapy/bindir" 2>/dev/null)"
# --- 4. Prepend to PATH (idempotent) -----------------------------------------
# --- 4. Prepend to PATH ------------------------------------------------------
# First strip any asdk bin from OUR toolchain dir already on PATH: re-sourcing
# for a different IC (e.g. RTL8720F 12.3.1 -> RTL8721Dx 10.3.1) must move THIS
# board's asdk to the front, but a plain "prepend if absent" would leave the
# previous version ahead and cmake's compiler probe would pick the wrong one.
# Anchored to $AMEBA_TOOLCHAIN_DIR so only our own asdk installs are touched --
# never a system/third-party toolchain elsewhere on PATH.
_newpath=
_oldifs=$IFS
IFS=:
for _p in $PATH; do
case "$_p" in
"$AMEBA_TOOLCHAIN_DIR"/asdk-*/linux/newlib/bin) ;; # our stale asdk -- drop
*) _newpath="${_newpath:+$_newpath:}$_p" ;;
esac
done
IFS=$_oldifs
PATH="$_newpath"
# venv + prebuilts are version-independent, so prepend-if-absent is fine; the
# asdk bin was just stripped, so it is prepended here and lands at the front.
for _d in "$_venv_bin" "$_prebuilts_bin" "$_asdk_bin"; do
[ -n "$_d" ] || continue
case ":$PATH:" in
@ -111,5 +166,12 @@ export PATH
echo "ameba: environment ready"
echo " AMEBA_SDK = $AMEBA_SDK"
echo " asdk = $_asdk_bin"
echo " asdk = $_asdk_bin (${_ameba_soc:-default}: $_ver)"
if [ -z "$_ameba_soc" ]; then
echo " note: no board given -> default asdk $_ver. For an IC that pins a"
echo " newer asdk (e.g. rtl8720f_evb), pass the board so cmake builds"
echo " NuttX with the matching toolchain:"
echo " . tools/ameba/env.sh <board>"
[ -z "$_ameba_board" ] && echo " boards: $(_ameba_board_list)"
fi
echo " now build with cmake (source once) or make"