Repository navigation
cmake: Fix cross-compilation support in add_subdirectory mode - #20874
Merged
xiaoxiang781216 merged 1 commit intoOct 26, 2026
Merged
xiaoxiang781216 merged 1 commit into
xiaoxiang781216 merged 1 commit into
Conversation
1 task done
Swatantra-66
force-pushed
the
cmake/add-subdirectory-cross-compile
branch
from
October 21, 2026 20:30
74ef3bf to
f4512b8
Compare
|
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
force-pushed
the
cmake/add-subdirectory-cross-compile
branch
from
October 22, 2026 12:18
f4512b8 to
96574aa
Compare
xiaoxiang781216
approved these changes
Oct 22, 2026
GUIDINGLI
approved these changes
Oct 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When NuttX is embedded into a parent CMake project via
add_subdirectory(), several issues previously prevented cross-compiling non-sim architectures:arch/{arm,arm64,risc-v,tricore,x86_64,xtensa}/src/CMakeLists.txtandboards/CMakeLists.txt,add_subdirectory()inverted theEXCLUDE_FROM_ALLandbinary_dirarguments, violating CMake syntaxadd_subdirectory(source_dir binary_dir [EXCLUDE_FROM_ALL]).arch/arm/src/cmake/Toolchain.cmakeandarch/arm64/src/cmake/Toolchain.cmakeusedinclude()without${CMAKE_CURRENT_LIST_DIR}, failing to locate sibling toolchain files whenCMAKE_MODULE_PATHwas not inherited.CMAKE_TOOLCHAIN_FILEis resolved using a deduplicated macro (nuttx_resolve_toolchain_file) againstCMAKE_BINARY_DIR,CMAKE_SOURCE_DIR, andCMAKE_CURRENT_SOURCE_DIRper CMake lookup rules, and safely evaluated after NuttX configuration is loaded.CMAKE_C_COMPILERover$CACHE{CMAKE_C_COMPILER}to ensure normal-scope toolchain definitions from the parent project take precedence over stale cache entries.TOOLCHAIN_PREFIX(or verifyingclang/armclangfor Clang-based targets). If a host compiler was inadvertently initialized by a parentproject()call, a clearFATAL_ERRORis raised advising how to configureCMAKE_TOOLCHAIN_FILEbefore the parent's firstproject()call.CMAKE_TOOLCHAIN_FILE,CMAKE_MODULE_PATH, and allCONFIG_*variables toPARENT_SCOPE.Fixes #19697
Impact
Yes, enables cross-compilation support when NuttX is embedded as a subproject via
add_subdirectory().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.).
cmake -B build -DBOARD_CONFIG=...) and GNU Make builds are completely unaffected.CONFIG_*) to the parent project scope.Applies to all architectures when building with CMake, specifically fixing embedded targets (
arm,arm64,risc-v,xtensa,tricore,x86_64).Adds a clear diagnostic
FATAL_ERRORguiding users to setCMAKE_TOOLCHAIN_FILEbeforeproject()if a parent build inadvertently initializes a host compiler.No security implications. Fully backward-compatible.
Testing
git diff --check.cmake -B build -DBOARD_CONFIG=...).add_subdirectory):exclude_chip,exclude_board, etc.).CMAKE_TOOLCHAIN_FILEresolution for both relative and absolute paths via the newnuttx_resolve_toolchain_filemacro.CMAKE_C_COMPILERbefore$CACHE{CMAKE_C_COMPILER}) and verifies that matching cross-compilers (arm-none-eabi-*,riscv64-unknown-elf-*,clang,armclang) pass.