1# SPDX-License-Identifier: Apache-2.0
2
3set_ifndef(C++ g++)
4
5# Configures CMake for using GCC, this script is re-used by several
6# GCC-based toolchains
7
8find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}${CC} PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
9
10if(CONFIG_CPP)
11  set(cplusplus_compiler ${CROSS_COMPILE}${C++})
12else()
13  if(EXISTS ${CROSS_COMPILE}${C++})
14    set(cplusplus_compiler ${CROSS_COMPILE}${C++})
15  else()
16    # When the toolchain doesn't support C++, and we aren't building
17    # with C++ support just set it to something so CMake doesn't
18    # crash, it won't actually be called
19    set(cplusplus_compiler ${CMAKE_C_COMPILER})
20  endif()
21endif()
22find_program(CMAKE_CXX_COMPILER ${cplusplus_compiler} PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
23
24set(NOSTDINC "")
25
26# Note that NOSYSDEF_CFLAG may be an empty string, and
27# set_ifndef() does not work with empty string.
28if(NOT DEFINED NOSYSDEF_CFLAG)
29  set(NOSYSDEF_CFLAG -undef)
30endif()
31
32foreach(file_name include/stddef.h include-fixed/limits.h)
33  execute_process(
34    COMMAND ${CMAKE_C_COMPILER} --print-file-name=${file_name}
35    OUTPUT_VARIABLE _OUTPUT
36    )
37  get_filename_component(_OUTPUT "${_OUTPUT}" DIRECTORY)
38  string(REGEX REPLACE "\n" "" _OUTPUT "${_OUTPUT}")
39
40  list(APPEND NOSTDINC ${_OUTPUT})
41endforeach()
42
43# This libgcc code is partially duplicated in compiler/*/target.cmake
44execute_process(
45  COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-libgcc-file-name
46  OUTPUT_VARIABLE LIBGCC_FILE_NAME
47  OUTPUT_STRIP_TRAILING_WHITESPACE
48  )
49
50get_filename_component(LIBGCC_DIR ${LIBGCC_FILE_NAME} DIRECTORY)
51
52list(APPEND LIB_INCLUDE_DIR "-L\"${LIBGCC_DIR}\"")
53
54# For CMake to be able to test if a compiler flag is supported by the
55# toolchain we need to give CMake the necessary flags to compile and
56# link a dummy C file.
57#
58# CMake checks compiler flags with check_c_compiler_flag() (Which we
59# wrap with target_cc_option() in extensions.cmake)
60foreach(isystem_include_dir ${NOSTDINC})
61  list(APPEND isystem_include_flags -isystem "\"${isystem_include_dir}\"")
62endforeach()
63# The CMAKE_REQUIRED_FLAGS variable is used by check_c_compiler_flag()
64# (and other commands which end up calling check_c_source_compiles())
65# to add additional compiler flags used during checking. These flags
66# are unused during "real" builds of Zephyr source files linked into
67# the final executable.
68#
69# Appending onto any existing values lets users specify
70# toolchain-specific flags at generation time.
71list(APPEND CMAKE_REQUIRED_FLAGS -nostartfiles -nostdlib ${isystem_include_flags} -Wl,--unresolved-symbols=ignore-in-object-files)
72string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
73