Swatantra-66 opened a new pull request, #20874:
URL: https://github.com/apache/nuttx/pull/20874
## 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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]