Skip to content

cmake: Fix cross-compilation support in add_subdirectory mode - #20874

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:cmake/add-subdirectory-cross-compile
Oct 26, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:cmake/add-subdirectory-cross-compile

Conversation

@Swatantra-66

@Swatantra-66 Swatantra-66 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When NuttX is embedded into a parent CMake project via add_subdirectory(), several issues previously prevented cross-compiling non-sim architectures:

  1. In arch/{arm,arm64,risc-v,tricore,x86_64,xtensa}/src/CMakeLists.txt and boards/CMakeLists.txt, add_subdirectory() inverted the EXCLUDE_FROM_ALL and binary_dir arguments, violating CMake syntax add_subdirectory(source_dir binary_dir [EXCLUDE_FROM_ALL]).
  2. arch/arm/src/cmake/Toolchain.cmake and arch/arm64/src/cmake/Toolchain.cmake used include() without ${CMAKE_CURRENT_LIST_DIR}, failing to locate sibling toolchain files when CMAKE_MODULE_PATH was not inherited.
  3. In subproject mode, CMAKE_TOOLCHAIN_FILE is resolved using a deduplicated macro (nuttx_resolve_toolchain_file) against CMAKE_BINARY_DIR, CMAKE_SOURCE_DIR, and CMAKE_CURRENT_SOURCE_DIR per CMake lookup rules, and safely evaluated after NuttX configuration is loaded.
  4. Active compiler detection prioritizes CMAKE_C_COMPILER over $CACHE{CMAKE_C_COMPILER} to ensure normal-scope toolchain definitions from the parent project take precedence over stale cache entries.
  5. Added a diagnostic check inspecting the active compiler basename against TOOLCHAIN_PREFIX (or verifying clang/armclang for Clang-based targets). If a host compiler was inadvertently initialized by a parent project() call, a clear FATAL_ERROR is raised advising how to configure CMAKE_TOOLCHAIN_FILE before the parent's first project() call.
  6. Exported CMAKE_TOOLCHAIN_FILE, CMAKE_MODULE_PATH, and all CONFIG_* variables to PARENT_SCOPE.

Fixes #19697

Impact

  • Is new feature added? Is existing feature changed?
    Yes, enables cross-compilation support when NuttX is embedded as a subproject via add_subdirectory().
  • User impact:
    Users can now embed NuttX into larger CMake projects (e.g. multi-component firmware or SDKs) targeting embedded architectures (ARM, ARM64, RISC-V, Xtensa, TriCore, etc.).
  • Build process:
    • Standalone NuttX CMake builds (cmake -B build -DBOARD_CONFIG=...) and GNU Make builds are completely unaffected.
    • Subproject builds now correctly resolve toolchain files, inherit compiler definitions, and export configuration variables (CONFIG_*) to the parent project scope.
  • Hardware:
    Applies to all architectures when building with CMake, specifically fixing embedded targets (arm, arm64, risc-v, xtensa, tricore, x86_64).
  • Documentation / Diagnostics:
    Adds a clear diagnostic FATAL_ERROR guiding users to set CMAKE_TOOLCHAIN_FILE before project() if a parent build inadvertently initializes a host compiler.
  • Security / Compatibility:
    No security implications. Fully backward-compatible.

Testing

  • Build Host:
    • OS: Windows 11 / Linux
    • CMake: 3.20+
  • Verification:
    1. Syntax & whitespace: Verified with git diff --check.
    2. Standalone CMake build: Confirmed standalone configuration behavior remains unchanged (cmake -B build -DBOARD_CONFIG=...).
    3. Embedded subproject (add_subdirectory):
      • Verified that chip and board subdirectories are registered with valid binary directories in parent projects without syntax errors (exclude_chip, exclude_board, etc.).
      • Tested CMAKE_TOOLCHAIN_FILE resolution for both relative and absolute paths via the new nuttx_resolve_toolchain_file macro.
      • Verified active compiler evaluation: confirms correct precedence (CMAKE_C_COMPILER before $CACHE{CMAKE_C_COMPILER}) and verifies that matching cross-compilers (arm-none-eabi-*, riscv64-unknown-elf-*, clang, armclang) pass.
      • Verified diagnostic error: tested with mismatched host compilers to confirm clear, actionable guidance is produced without empty string artifacts.

@Swatantra-66
Swatantra-66 force-pushed the cmake/add-subdirectory-cross-compile branch from 74ef3bf to f4512b8 Compare October 21, 2026 20:30
@github-actions github-actions Bot added Area: Build system Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: tricore Issues related to the TriCore architecture from Infineon Arch: x86_64 Issues related to the x86_64 architecture Arch: xtensa Issues related to the Xtensa architecture Size: M The size of the change in this PR is medium labels Oct 21, 2026
@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Comment thread arch/arm/src/cmake/Toolchain.cmake Resolved
Comment thread CMakeLists.txt Resolved
When NuttX is embedded in an external project via add_subdirectory(),
several issues prevented cross-compiling non-sim architectures:

1. In arch/{arm,arm64,risc-v,tricore,x86_64,xtensa}/src/CMakeLists.txt and
   boards/CMakeLists.txt, the arguments to add_subdirectory() had
   EXCLUDE_FROM_ALL and binary_dir inverted, violating CMake syntax
   add_subdirectory(source_dir binary_dir [EXCLUDE_FROM_ALL]).
2. arch/arm/src/cmake/Toolchain.cmake and arch/arm64/src/cmake/Toolchain.cmake
   used include() without CMAKE_CURRENT_LIST_DIR, failing to find sibling
   toolchain files when CMAKE_MODULE_PATH was not set in parent scope.
3. In subproject mode, resolve and safely evaluate CMAKE_TOOLCHAIN_FILE
   (handling relative paths and cache entries per CMake lookup rules) once
   NuttX configuration is exported so architecture settings (e.g. ARCH_SUBDIR,
   ARCHCPUFLAGS) are populated.
4. Export CMAKE_TOOLCHAIN_FILE, CMAKE_MODULE_PATH, and all CONFIG_*
   variables to PARENT_SCOPE so parent projects can access configuration
   and build targets with the cross-compiler.
5. Add a diagnostic check inspecting the active compiler executable
   driver basename against TOOLCHAIN_PREFIX, providing clear guidance
   to configure CMAKE_TOOLCHAIN_FILE before the parent project's first
   project() call if a mismatched compiler is detected.

Fixes apache#19697.

Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the cmake/add-subdirectory-cross-compile branch from f4512b8 to 96574aa Compare October 22, 2026 12:18
@xiaoxiang781216
xiaoxiang781216 merged commit 1fb5dd1 into apache:master Oct 26, 2026
86 of 106 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: tricore Issues related to the TriCore architecture from Infineon Arch: x86_64 Issues related to the x86_64 architecture Arch: xtensa Issues related to the Xtensa architecture Area: Build system Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] error in building add_subdirectory for nuttx

3 participants