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 GCC_COMPILER_VERSION
43    )
44
45if("${GCC_COMPILER_VERSION}" VERSION_LESS 4.3.0 OR
46    "${GCC_COMPILER_VERSION}" VERSION_GREATER_EQUAL 13.1.0)
47    set(fix_header_file include/limits.h)
48else()
49    set(fix_header_file include-fixed/limits.h)
50endif()
51
52foreach(file_name include/stddef.h "${fix_header_file}")
53  execute_process(
54    COMMAND ${CMAKE_C_COMPILER} --print-file-name=${file_name}
55    OUTPUT_VARIABLE _OUTPUT
56    )
57  get_filename_component(_OUTPUT "${_OUTPUT}" DIRECTORY)
58  string(REGEX REPLACE "\n" "" _OUTPUT "${_OUTPUT}")
59
60  list(APPEND NOSTDINC ${_OUTPUT})
61endforeach()
62
63include(${ZEPHYR_BASE}/cmake/gcc-m-cpu.cmake)
64include(${ZEPHYR_BASE}/cmake/gcc-m-fpu.cmake)
65
66if("${ARCH}" STREQUAL "arm")
67  include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_arm.cmake)
68elseif("${ARCH}" STREQUAL "arm64")
69  include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_arm64.cmake)
70elseif("${ARCH}" STREQUAL "arc")
71  include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_arc.cmake)
72elseif("${ARCH}" STREQUAL "riscv")
73  include(${CMAKE_CURRENT_LIST_DIR}/target_riscv.cmake)
74elseif("${ARCH}" STREQUAL "x86")
75  include(${CMAKE_CURRENT_LIST_DIR}/target_x86.cmake)
76elseif("${ARCH}" STREQUAL "sparc")
77  include(${CMAKE_CURRENT_LIST_DIR}/target_sparc.cmake)
78elseif("${ARCH}" STREQUAL "mips")
79  include(${CMAKE_CURRENT_LIST_DIR}/target_mips.cmake)
80elseif("${ARCH}" STREQUAL "xtensa")
81  include(${CMAKE_CURRENT_LIST_DIR}/target_xtensa.cmake)
82endif()
83
84if(SYSROOT_DIR)
85  # The toolchain has specified a sysroot dir, pass it to the compiler
86  list(APPEND TOOLCHAIN_C_FLAGS
87    --sysroot=${SYSROOT_DIR}
88    )
89
90  # Use sysroot dir to set the libc path's
91  execute_process(
92    COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-multi-directory
93    OUTPUT_VARIABLE NEWLIB_DIR
94    OUTPUT_STRIP_TRAILING_WHITESPACE
95    )
96
97  set(LIBC_LIBRARY_DIR "\"${SYSROOT_DIR}\"/lib/${NEWLIB_DIR}")
98endif()
99
100# This libgcc code is partially duplicated in compiler/*/target.cmake
101execute_process(
102  COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-libgcc-file-name
103  OUTPUT_VARIABLE LIBGCC_FILE_NAME
104  OUTPUT_STRIP_TRAILING_WHITESPACE
105  )
106
107assert_exists(LIBGCC_FILE_NAME)
108
109get_filename_component(LIBGCC_DIR ${LIBGCC_FILE_NAME} DIRECTORY)
110
111assert_exists(LIBGCC_DIR)
112
113set_linker_property(PROPERTY lib_include_dir "-L\"${LIBGCC_DIR}\"")
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