From d17956a074e869baa1a157a833991d3b9f6a122e Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Thu, 22 May 2025 16:03:09 +0000 Subject: [PATCH] build: Support SYSROOT_DIR in native_sim and clang When running naive sim builds using clang, respect user passed SYSROOT_DIR values. Additionally, allow overriding the archive tool when calling Make for the native simulator. Add the sysroot (if available) to the native_simulator target and TOOLCHAIN_C_FLAGS and TOOLCHAIN_LD_FLAGS. Additionally pass CMAKE_AR to NSI_AR. Signed-off-by: Yuval Peress --- boards/native/common/natsim_config.cmake | 7 +++++++ cmake/compiler/clang/target.cmake | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/boards/native/common/natsim_config.cmake b/boards/native/common/natsim_config.cmake index d9decf2cacd..6345ef6cd6e 100644 --- a/boards/native/common/natsim_config.cmake +++ b/boards/native/common/natsim_config.cmake @@ -7,8 +7,15 @@ get_property(CCACHE GLOBAL PROPERTY RULE_LAUNCH_COMPILE) target_link_options(native_simulator INTERFACE "-T ${ZEPHYR_BASE}/boards/native/common/natsim_linker_script.ld") +if(SYSROOT_DIR) + message(NOTICE "Appending --sysroot=${SYSROOT_DIR} to native_simulator") + target_compile_options(native_simulator INTERFACE "--sysroot=${SYSROOT_DIR}") + target_link_options(native_simulator INTERFACE "--sysroot=${SYSROOT_DIR}") +endif() + set(nsi_config_content ${nsi_config_content} + "NSI_AR:=${CMAKE_AR}" "NSI_BUILD_OPTIONS:=$,\ >" "NSI_BUILD_PATH:=${zephyr_build_path}/NSI" "NSI_CC:=${CCACHE} ${CMAKE_C_COMPILER}" diff --git a/cmake/compiler/clang/target.cmake b/cmake/compiler/clang/target.cmake index 62c39b0ec6f..0381ddcfc3b 100644 --- a/cmake/compiler/clang/target.cmake +++ b/cmake/compiler/clang/target.cmake @@ -18,6 +18,12 @@ endif() find_program(CMAKE_C_COMPILER clang ${find_program_clang_args}) find_program(CMAKE_CXX_COMPILER clang++ ${find_program_clang_args}) +if(SYSROOT_DIR) + # The toolchain has specified a sysroot dir, pass it to the compiler + list(APPEND TOOLCHAIN_C_FLAGS "--sysroot=${SYSROOT_DIR}") + list(APPEND TOOLCHAIN_LD_FLAGS "--sysroot=${SYSROOT_DIR}") +endif() + if(NOT "${ARCH}" STREQUAL "posix") include(${ZEPHYR_BASE}/cmake/gcc-m-cpu.cmake) include(${ZEPHYR_BASE}/cmake/gcc-m-fpu.cmake) @@ -72,7 +78,8 @@ if(NOT "${ARCH}" STREQUAL "posix") # # Other clang/LLVM distributions may come with other pre-built C libraries. # clang/LLVM supports using an alternative C library, either by direct linking, - # or by specifying '--sysroot '. + # or by specifying '--sysroot '. Sysroot can also be passed using the + # CMake variable SYSROOT_DIR which will append the '--sysroot ' flags. # # LLVM for Arm provides a 'newlib.cfg' file for newlib C selection. # Let us support this principle by looking for a dedicated 'newlib.cfg' or