Ryan Prichard | 7aea7e9 | 2022-01-13 17:30:17 -0800 | [diff] [blame] | 1 | # Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 | # file Copyright.txt or https://cmake.org/licensing for details. |
| 3 | |
| 4 | |
| 5 | # This file sets the basic flags for the C language in CMake. |
| 6 | # It also loads the available platform file for the system-compiler |
| 7 | # if it exists. |
| 8 | # It also loads a system - compiler - processor (or target hardware) |
| 9 | # specific file, which is mainly useful for crosscompiling and embedded systems. |
| 10 | |
| 11 | include(CMakeLanguageInformation) |
| 12 | |
| 13 | # some compilers use different extensions (e.g. sdcc uses .rel) |
| 14 | # so set the extension here first so it can be overridden by the compiler specific file |
| 15 | if(UNIX) |
| 16 | set(CMAKE_C_OUTPUT_EXTENSION .o) |
| 17 | else() |
| 18 | set(CMAKE_C_OUTPUT_EXTENSION .obj) |
| 19 | endif() |
| 20 | |
| 21 | set(_INCLUDED_FILE 0) |
| 22 | |
| 23 | # Load compiler-specific information. |
| 24 | if(CMAKE_C_COMPILER_ID) |
| 25 | include(Compiler/${CMAKE_C_COMPILER_ID}-C OPTIONAL) |
| 26 | endif() |
| 27 | |
| 28 | set(CMAKE_BASE_NAME) |
| 29 | get_filename_component(CMAKE_BASE_NAME "${CMAKE_C_COMPILER}" NAME_WE) |
| 30 | if(CMAKE_COMPILER_IS_GNUCC) |
| 31 | set(CMAKE_BASE_NAME gcc) |
| 32 | endif() |
| 33 | |
| 34 | |
| 35 | # load a hardware specific file, mostly useful for embedded compilers |
| 36 | if(CMAKE_SYSTEM_PROCESSOR) |
| 37 | if(CMAKE_C_COMPILER_ID) |
| 38 | include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_C_COMPILER_ID}-C-${CMAKE_SYSTEM_PROCESSOR} OPTIONAL RESULT_VARIABLE _INCLUDED_FILE) |
| 39 | endif() |
| 40 | if (NOT _INCLUDED_FILE) |
| 41 | include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_BASE_NAME}-${CMAKE_SYSTEM_PROCESSOR} OPTIONAL) |
| 42 | endif () |
| 43 | endif() |
| 44 | |
| 45 | |
| 46 | # load the system- and compiler specific files |
| 47 | if(CMAKE_C_COMPILER_ID) |
| 48 | include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_C_COMPILER_ID}-C |
| 49 | OPTIONAL RESULT_VARIABLE _INCLUDED_FILE) |
| 50 | endif() |
| 51 | if (NOT _INCLUDED_FILE) |
| 52 | include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_BASE_NAME} |
| 53 | OPTIONAL RESULT_VARIABLE _INCLUDED_FILE) |
| 54 | endif () |
| 55 | |
| 56 | # load any compiler-wrapper specific information |
| 57 | if (CMAKE_C_COMPILER_WRAPPER) |
| 58 | __cmake_include_compiler_wrapper(C) |
| 59 | endif () |
| 60 | |
| 61 | # We specify the compiler information in the system file for some |
| 62 | # platforms, but this language may not have been enabled when the file |
| 63 | # was first included. Include it again to get the language info. |
| 64 | # Remove this when all compiler info is removed from system files. |
| 65 | if (NOT _INCLUDED_FILE) |
| 66 | include(Platform/${CMAKE_SYSTEM_NAME} OPTIONAL) |
| 67 | endif () |
| 68 | |
| 69 | if(CMAKE_C_SIZEOF_DATA_PTR) |
| 70 | foreach(f ${CMAKE_C_ABI_FILES}) |
| 71 | include(${f}) |
| 72 | endforeach() |
| 73 | unset(CMAKE_C_ABI_FILES) |
| 74 | endif() |
| 75 | |
| 76 | # This should be included before the _INIT variables are |
| 77 | # used to initialize the cache. Since the rule variables |
| 78 | # have if blocks on them, users can still define them here. |
| 79 | # But, it should still be after the platform file so changes can |
| 80 | # be made to those values. |
| 81 | |
| 82 | if(CMAKE_USER_MAKE_RULES_OVERRIDE) |
| 83 | # Save the full path of the file so try_compile can use it. |
| 84 | include(${CMAKE_USER_MAKE_RULES_OVERRIDE} RESULT_VARIABLE _override) |
| 85 | set(CMAKE_USER_MAKE_RULES_OVERRIDE "${_override}") |
| 86 | endif() |
| 87 | |
| 88 | if(CMAKE_USER_MAKE_RULES_OVERRIDE_C) |
| 89 | # Save the full path of the file so try_compile can use it. |
| 90 | include(${CMAKE_USER_MAKE_RULES_OVERRIDE_C} RESULT_VARIABLE _override) |
| 91 | set(CMAKE_USER_MAKE_RULES_OVERRIDE_C "${_override}") |
| 92 | endif() |
| 93 | |
| 94 | if(CMAKE_EXECUTABLE_FORMAT STREQUAL "ELF") |
| 95 | if(NOT DEFINED CMAKE_C_LINK_WHAT_YOU_USE_FLAG) |
| 96 | set(CMAKE_C_LINK_WHAT_YOU_USE_FLAG "LINKER:--no-as-needed") |
| 97 | endif() |
| 98 | if(NOT DEFINED CMAKE_LINK_WHAT_YOU_USE_CHECK) |
| 99 | set(CMAKE_LINK_WHAT_YOU_USE_CHECK ldd -u -r) |
| 100 | endif() |
| 101 | endif() |
| 102 | |
| 103 | # for most systems a module is the same as a shared library |
| 104 | # so unless the variable CMAKE_MODULE_EXISTS is set just |
| 105 | # copy the values from the LIBRARY variables |
| 106 | if(NOT CMAKE_MODULE_EXISTS) |
| 107 | set(CMAKE_SHARED_MODULE_C_FLAGS ${CMAKE_SHARED_LIBRARY_C_FLAGS}) |
| 108 | set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS}) |
| 109 | endif() |
| 110 | |
| 111 | set(CMAKE_C_FLAGS_INIT "$ENV{CFLAGS} ${CMAKE_C_FLAGS_INIT}") |
| 112 | |
| 113 | cmake_initialize_per_config_variable(CMAKE_C_FLAGS "Flags used by the C compiler") |
| 114 | |
| 115 | if(CMAKE_C_STANDARD_LIBRARIES_INIT) |
| 116 | set(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES_INIT}" |
| 117 | CACHE STRING "Libraries linked by default with all C applications.") |
| 118 | mark_as_advanced(CMAKE_C_STANDARD_LIBRARIES) |
| 119 | endif() |
| 120 | |
| 121 | if(NOT CMAKE_C_COMPILER_LAUNCHER AND DEFINED ENV{CMAKE_C_COMPILER_LAUNCHER}) |
| 122 | set(CMAKE_C_COMPILER_LAUNCHER "$ENV{CMAKE_C_COMPILER_LAUNCHER}" |
| 123 | CACHE STRING "Compiler launcher for C.") |
| 124 | endif() |
| 125 | |
| 126 | if(NOT CMAKE_C_LINKER_LAUNCHER AND DEFINED ENV{CMAKE_C_LINKER_LAUNCHER}) |
| 127 | set(CMAKE_C_LINKER_LAUNCHER "$ENV{CMAKE_C_LINKER_LAUNCHER}" |
| 128 | CACHE STRING "Linker launcher for C.") |
| 129 | endif() |
| 130 | |
| 131 | include(CMakeCommonLanguageInclude) |
| 132 | |
| 133 | # now define the following rule variables |
| 134 | |
| 135 | # CMAKE_C_CREATE_SHARED_LIBRARY |
| 136 | # CMAKE_C_CREATE_SHARED_MODULE |
| 137 | # CMAKE_C_COMPILE_OBJECT |
| 138 | # CMAKE_C_LINK_EXECUTABLE |
| 139 | |
| 140 | # variables supplied by the generator at use time |
| 141 | # <TARGET> |
| 142 | # <TARGET_BASE> the target without the suffix |
| 143 | # <OBJECTS> |
| 144 | # <OBJECT> |
| 145 | # <LINK_LIBRARIES> |
| 146 | # <FLAGS> |
| 147 | # <LINK_FLAGS> |
| 148 | |
| 149 | # C compiler information |
| 150 | # <CMAKE_C_COMPILER> |
| 151 | # <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> |
| 152 | # <CMAKE_SHARED_MODULE_CREATE_C_FLAGS> |
| 153 | # <CMAKE_C_LINK_FLAGS> |
| 154 | |
| 155 | # Static library tools |
| 156 | # <CMAKE_AR> |
| 157 | # <CMAKE_RANLIB> |
| 158 | |
| 159 | |
| 160 | # create a C shared library |
| 161 | if(NOT CMAKE_C_CREATE_SHARED_LIBRARY) |
| 162 | set(CMAKE_C_CREATE_SHARED_LIBRARY |
| 163 | "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> <SONAME_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>") |
| 164 | endif() |
| 165 | |
| 166 | # create a C shared module just copy the shared library rule |
| 167 | if(NOT CMAKE_C_CREATE_SHARED_MODULE) |
| 168 | set(CMAKE_C_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_LIBRARY}) |
| 169 | endif() |
| 170 | |
| 171 | # Create a static archive incrementally for large object file counts. |
| 172 | # If CMAKE_C_CREATE_STATIC_LIBRARY is set it will override these. |
| 173 | if(NOT DEFINED CMAKE_C_ARCHIVE_CREATE) |
| 174 | set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <LINK_FLAGS> <OBJECTS>") |
| 175 | endif() |
| 176 | if(NOT DEFINED CMAKE_C_ARCHIVE_APPEND) |
| 177 | set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> q <TARGET> <LINK_FLAGS> <OBJECTS>") |
| 178 | endif() |
| 179 | if(NOT DEFINED CMAKE_C_ARCHIVE_FINISH) |
| 180 | set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>") |
| 181 | endif() |
| 182 | |
| 183 | # compile a C file into an object file |
| 184 | if(NOT CMAKE_C_COMPILE_OBJECT) |
| 185 | set(CMAKE_C_COMPILE_OBJECT |
| 186 | "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>") |
| 187 | endif() |
| 188 | |
| 189 | if(NOT CMAKE_C_LINK_EXECUTABLE) |
| 190 | set(CMAKE_C_LINK_EXECUTABLE |
| 191 | "<CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>") |
| 192 | endif() |
| 193 | |
| 194 | if(NOT CMAKE_EXECUTABLE_RUNTIME_C_FLAG) |
| 195 | set(CMAKE_EXECUTABLE_RUNTIME_C_FLAG ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG}) |
| 196 | endif() |
| 197 | |
| 198 | if(NOT CMAKE_EXECUTABLE_RUNTIME_C_FLAG_SEP) |
| 199 | set(CMAKE_EXECUTABLE_RUNTIME_C_FLAG_SEP ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP}) |
| 200 | endif() |
| 201 | |
| 202 | if(NOT CMAKE_EXECUTABLE_RPATH_LINK_C_FLAG) |
| 203 | set(CMAKE_EXECUTABLE_RPATH_LINK_C_FLAG ${CMAKE_SHARED_LIBRARY_RPATH_LINK_C_FLAG}) |
| 204 | endif() |
| 205 | |
| 206 | set(CMAKE_C_INFORMATION_LOADED 1) |