From 146e22fca4b56d401b7f58e42dbb34d362df92db Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Tue, 27 May 2025 11:46:41 -0700 Subject: [PATCH] cmake: xcc/xt-clang: fix for proper system include paths Although xt-clang is based on clang, for some reason, it still lists xcc system include path as the first search path (e.g. for stddef.h), and the clang system include path as last. This creates a big issue when the code starts to use any standards past C89 (since xcc is based on GCC 4.2). We can use compiler property nostdin_include to add -isystem to compiler options. However, some modules (e.g. picolibcs) somehow ignore this. So we also need to forcibly do add_compile_options() to make sure the clang system include path is placed before the xcc system include path. Signed-off-by: Daniel Leung --- cmake/compiler/xcc/target.cmake | 17 +++++++++++++++- cmake/compiler/xt-clang/compiler_flags.cmake | 21 +++----------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/cmake/compiler/xcc/target.cmake b/cmake/compiler/xcc/target.cmake index f9e9c721a15..56d15451d3d 100644 --- a/cmake/compiler/xcc/target.cmake +++ b/cmake/compiler/xcc/target.cmake @@ -37,7 +37,22 @@ foreach(file_name include/stddef.h include-fixed/limits.h) get_filename_component(_OUTPUT "${_OUTPUT}" DIRECTORY) string(REGEX REPLACE "\n" "" _OUTPUT "${_OUTPUT}") - list(APPEND NOSTDINC ${_OUTPUT}) + # Need to make sure the path exists before we add it to ${NOSTDINC}. + # For example, include-fixed is in xcc but not xt-clang. + if(EXISTS "${_OUTPUT}") + list(APPEND NOSTDINC ${_OUTPUT}) + + if("${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "xt-clang") + # This forcibly adds -isystem so that the xt-clang system + # include paths are before the xcc include paths. + # For some reason, xt-clang places xcc include paths before + # xt-clang ones so we need to force it. + # + # Some modules ignores the compiler property nostdinc_include + # so we need to make sure -isystem is used there. + add_compile_options(-isystem ${_OUTPUT}) + endif() + endif() endforeach() # This libgcc code is partially duplicated in compiler/*/target.cmake diff --git a/cmake/compiler/xt-clang/compiler_flags.cmake b/cmake/compiler/xt-clang/compiler_flags.cmake index c70832ebc0f..54451194c6d 100644 --- a/cmake/compiler/xt-clang/compiler_flags.cmake +++ b/cmake/compiler/xt-clang/compiler_flags.cmake @@ -2,25 +2,10 @@ include(${ZEPHYR_BASE}/cmake/compiler/clang/compiler_flags.cmake) -# nostdinc_include contains path to llvm headers and also relative -# path of "include-fixed". -# Clear "nostdinc" and nostdinc_include +# nostdinc needs to be cleared as it is needed for xtensa/config/core.h. +# nostdinc_include contains path to llvm headers. set_compiler_property(PROPERTY nostdinc) -set_compiler_property(PROPERTY nostdinc_include) - -# For C++ code, re-add the standard includes directory which was -# cleared up from nostdinc_inlcude in above lines with no -# "include-fixed" this time" -if(CONFIG_CPP) - execute_process( - COMMAND ${CMAKE_C_COMPILER} --print-file-name=include/stddef.h - OUTPUT_VARIABLE _OUTPUT - COMMAND_ERROR_IS_FATAL ANY - ) - get_filename_component(_OUTPUT "${_OUTPUT}" DIRECTORY) - string(REGEX REPLACE "\n" "" _OUTPUT "${_OUTPUT}") - set_compiler_property(PROPERTY nostdinc_include "${_OUTPUT}") -endif() +set_compiler_property(APPEND PROPERTY nostdinc_include ${NOSTDINC}) if($ENV{XCC_NO_G_FLAG}) # Older xcc/clang cannot use "-g" due to this bug: