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_package(Deprecated COMPONENTS SPARSE)
9find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}${CC} PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
10
11if(${CMAKE_C_COMPILER} STREQUAL CMAKE_C_COMPILER-NOTFOUND)
12  message(FATAL_ERROR "C compiler ${CROSS_COMPILE}${CC} not found - Please check your toolchain installation")
13endif()
14
15if(CONFIG_CPP)
16  set(cplusplus_compiler ${CROSS_COMPILE}${C++})
17else()
18  if(EXISTS ${CROSS_COMPILE}${C++})
19    set(cplusplus_compiler ${CROSS_COMPILE}${C++})
20  else()
21    # When the toolchain doesn't support C++, and we aren't building
22    # with C++ support just set it to something so CMake doesn't
23    # crash, it won't actually be called
24    set(cplusplus_compiler ${CMAKE_C_COMPILER})
25  endif()
26endif()
27find_program(CMAKE_CXX_COMPILER ${cplusplus_compiler} PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
28
29set(NOSTDINC "")
30
31# Note that NOSYSDEF_CFLAG may be an empty string, and
32# set_ifndef() does not work with empty string.
33if(NOT DEFINED NOSYSDEF_CFLAG)
34  set(NOSYSDEF_CFLAG -undef)
35endif()
36
37# GCC-13, does not install limits.h on include-fixed anymore
38# https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git;h=be9dd80f933480
39# Add check for GCC version >= 13.1
40execute_process(
41    COMMAND ${CMAKE_C_COMPILER} -dumpfullversion
42    OUTPUT_VARIABLE temp_compiler_version
43    )
44
45if("${temp_compiler_version}" VERSION_GREATER_EQUAL 13.1.0)
46    set(fix_header_file include/limits.h)
47else()
48    set(fix_header_file include-fixed/limits.h)
49endif()
50
51foreach(file_name include/stddef.h "${fix_header_file}")
52  execute_process(
53    COMMAND ${CMAKE_C_COMPILER} --print-file-name=${file_name}
54    OUTPUT_VARIABLE _OUTPUT
55    )
56  get_filename_component(_OUTPUT "${_OUTPUT}" DIRECTORY)
57  string(REGEX REPLACE "\n" "" _OUTPUT "${_OUTPUT}")
58
59  list(APPEND NOSTDINC ${_OUTPUT})
60endforeach()
61
62include(${ZEPHYR_BASE}/cmake/gcc-m-cpu.cmake)
63include(${ZEPHYR_BASE}/cmake/gcc-m-fpu.cmake)
64
65if("${ARCH}" STREQUAL "arm")
66  include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_arm.cmake)
67elseif("${ARCH}" STREQUAL "arm64")
68  include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_arm64.cmake)
69elseif("${ARCH}" STREQUAL "arc")
70  include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_arc.cmake)
71elseif("${ARCH}" STREQUAL "riscv")
72  include(${CMAKE_CURRENT_LIST_DIR}/target_riscv.cmake)
73elseif("${ARCH}" STREQUAL "x86")
74  include(${CMAKE_CURRENT_LIST_DIR}/target_x86.cmake)
75elseif("${ARCH}" STREQUAL "sparc")
76  include(${CMAKE_CURRENT_LIST_DIR}/target_sparc.cmake)
77elseif("${ARCH}" STREQUAL "mips")
78  include(${CMAKE_CURRENT_LIST_DIR}/target_mips.cmake)
79elseif("${ARCH}" STREQUAL "xtensa")
80  include(${CMAKE_CURRENT_LIST_DIR}/target_xtensa.cmake)
81endif()
82
83if(SYSROOT_DIR)
84  # The toolchain has specified a sysroot dir, pass it to the compiler
85  list(APPEND TOOLCHAIN_C_FLAGS
86    --sysroot=${SYSROOT_DIR}
87    )
88
89  # Use sysroot dir to set the libc path's
90  execute_process(
91    COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-multi-directory
92    OUTPUT_VARIABLE NEWLIB_DIR
93    OUTPUT_STRIP_TRAILING_WHITESPACE
94    )
95
96  set(LIBC_LIBRARY_DIR "\"${SYSROOT_DIR}\"/lib/${NEWLIB_DIR}")
97endif()
98
99# This libgcc code is partially duplicated in compiler/*/target.cmake
100execute_process(
101  COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-libgcc-file-name
102  OUTPUT_VARIABLE LIBGCC_FILE_NAME
103  OUTPUT_STRIP_TRAILING_WHITESPACE
104  )
105
106assert_exists(LIBGCC_FILE_NAME)
107
108get_filename_component(LIBGCC_DIR ${LIBGCC_FILE_NAME} DIRECTORY)
109
110assert_exists(LIBGCC_DIR)
111
112LIST(APPEND LIB_INCLUDE_DIR "-L\"${LIBGCC_DIR}\"")
113LIST(APPEND TOOLCHAIN_LIBS gcc)
114
115# For CMake to be able to test if a compiler flag is supported by the
116# toolchain we need to give CMake the necessary flags to compile and
117# link a dummy C file.
118#
119# CMake checks compiler flags with check_c_compiler_flag() (Which we
120# wrap with target_cc_option() in extensions.cmake)
121foreach(isystem_include_dir ${NOSTDINC})
122  list(APPEND isystem_include_flags -isystem "\"${isystem_include_dir}\"")
123endforeach()
124
125# The CMAKE_REQUIRED_FLAGS variable is used by check_c_compiler_flag()
126# (and other commands which end up calling check_c_source_compiles())
127# to add additional compiler flags used during checking. These flags
128# are unused during "real" builds of Zephyr source files linked into
129# the final executable.
130#
131# Appending onto any existing values lets users specify
132# toolchain-specific flags at generation time.
133list(APPEND CMAKE_REQUIRED_FLAGS
134  -nostartfiles
135  -nostdlib
136  ${isystem_include_flags}
137  -Wl,--unresolved-symbols=ignore-in-object-files
138  -Wl,--entry=0 # Set an entry point to avoid a warning
139  )
140string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
141