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 <daniel.leung@intel.com>
88 lines
3.2 KiB
CMake
88 lines
3.2 KiB
CMake
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
set_ifndef(C++ g++)
|
|
|
|
# Configures CMake for using GCC, this script is re-used by several
|
|
# GCC-based toolchains
|
|
|
|
find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}${CC} PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
|
|
|
|
if(CONFIG_CPP)
|
|
set(cplusplus_compiler ${CROSS_COMPILE}${C++})
|
|
else()
|
|
if(EXISTS ${CROSS_COMPILE}${C++})
|
|
set(cplusplus_compiler ${CROSS_COMPILE}${C++})
|
|
else()
|
|
# When the toolchain doesn't support C++, and we aren't building
|
|
# with C++ support just set it to something so CMake doesn't
|
|
# crash, it won't actually be called
|
|
set(cplusplus_compiler ${CMAKE_C_COMPILER})
|
|
endif()
|
|
endif()
|
|
find_program(CMAKE_CXX_COMPILER ${cplusplus_compiler} PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
|
|
|
|
set(NOSTDINC "")
|
|
|
|
# Note that NOSYSDEF_CFLAG may be an empty string, and
|
|
# set_ifndef() does not work with empty string.
|
|
if(NOT DEFINED NOSYSDEF_CFLAG)
|
|
set(NOSYSDEF_CFLAG -undef)
|
|
endif()
|
|
|
|
foreach(file_name include/stddef.h include-fixed/limits.h)
|
|
execute_process(
|
|
COMMAND ${CMAKE_C_COMPILER} --print-file-name=${file_name}
|
|
OUTPUT_VARIABLE _OUTPUT
|
|
)
|
|
get_filename_component(_OUTPUT "${_OUTPUT}" DIRECTORY)
|
|
string(REGEX REPLACE "\n" "" _OUTPUT "${_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
|
|
execute_process(
|
|
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-libgcc-file-name
|
|
OUTPUT_VARIABLE LIBGCC_FILE_NAME
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
)
|
|
|
|
get_filename_component(LIBGCC_DIR ${LIBGCC_FILE_NAME} DIRECTORY)
|
|
|
|
list(APPEND LIB_INCLUDE_DIR "-L\"${LIBGCC_DIR}\"")
|
|
|
|
# For CMake to be able to test if a compiler flag is supported by the
|
|
# toolchain we need to give CMake the necessary flags to compile and
|
|
# link a dummy C file.
|
|
#
|
|
# CMake checks compiler flags with check_c_compiler_flag() (Which we
|
|
# wrap with target_cc_option() in extensions.cmake)
|
|
foreach(isystem_include_dir ${NOSTDINC})
|
|
list(APPEND isystem_include_flags -isystem "\"${isystem_include_dir}\"")
|
|
endforeach()
|
|
# The CMAKE_REQUIRED_FLAGS variable is used by check_c_compiler_flag()
|
|
# (and other commands which end up calling check_c_source_compiles())
|
|
# to add additional compiler flags used during checking. These flags
|
|
# are unused during "real" builds of Zephyr source files linked into
|
|
# the final executable.
|
|
#
|
|
# Appending onto any existing values lets users specify
|
|
# toolchain-specific flags at generation time.
|
|
list(APPEND CMAKE_REQUIRED_FLAGS -nostartfiles -nostdlib ${isystem_include_flags} -Wl,--unresolved-symbols=ignore-in-object-files)
|
|
string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
|