blob: 665f30927862242e27d8b161e33ce590af09953f [file] [log] [blame]
Ryan Prichard7aea7e92022-01-13 17:30:17 -08001# 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
11include(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
15if(UNIX)
16 set(CMAKE_C_OUTPUT_EXTENSION .o)
17else()
18 set(CMAKE_C_OUTPUT_EXTENSION .obj)
19endif()
20
21set(_INCLUDED_FILE 0)
22
23# Load compiler-specific information.
24if(CMAKE_C_COMPILER_ID)
25 include(Compiler/${CMAKE_C_COMPILER_ID}-C OPTIONAL)
26endif()
27
28set(CMAKE_BASE_NAME)
29get_filename_component(CMAKE_BASE_NAME "${CMAKE_C_COMPILER}" NAME_WE)
30if(CMAKE_COMPILER_IS_GNUCC)
31 set(CMAKE_BASE_NAME gcc)
32endif()
33
34
35# load a hardware specific file, mostly useful for embedded compilers
36if(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 ()
43endif()
44
45
46# load the system- and compiler specific files
47if(CMAKE_C_COMPILER_ID)
48 include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_C_COMPILER_ID}-C
49 OPTIONAL RESULT_VARIABLE _INCLUDED_FILE)
50endif()
51if (NOT _INCLUDED_FILE)
52 include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_BASE_NAME}
53 OPTIONAL RESULT_VARIABLE _INCLUDED_FILE)
54endif ()
55
56# load any compiler-wrapper specific information
57if (CMAKE_C_COMPILER_WRAPPER)
58 __cmake_include_compiler_wrapper(C)
59endif ()
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.
65if (NOT _INCLUDED_FILE)
66 include(Platform/${CMAKE_SYSTEM_NAME} OPTIONAL)
67endif ()
68
69if(CMAKE_C_SIZEOF_DATA_PTR)
70 foreach(f ${CMAKE_C_ABI_FILES})
71 include(${f})
72 endforeach()
73 unset(CMAKE_C_ABI_FILES)
74endif()
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
82if(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}")
86endif()
87
88if(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}")
92endif()
93
94if(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()
101endif()
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
106if(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})
109endif()
110
111set(CMAKE_C_FLAGS_INIT "$ENV{CFLAGS} ${CMAKE_C_FLAGS_INIT}")
112
113cmake_initialize_per_config_variable(CMAKE_C_FLAGS "Flags used by the C compiler")
114
115if(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)
119endif()
120
121if(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.")
124endif()
125
126if(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.")
129endif()
130
131include(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
161if(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>")
164endif()
165
166# create a C shared module just copy the shared library rule
167if(NOT CMAKE_C_CREATE_SHARED_MODULE)
168 set(CMAKE_C_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_LIBRARY})
169endif()
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.
173if(NOT DEFINED CMAKE_C_ARCHIVE_CREATE)
174 set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <LINK_FLAGS> <OBJECTS>")
175endif()
176if(NOT DEFINED CMAKE_C_ARCHIVE_APPEND)
177 set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> q <TARGET> <LINK_FLAGS> <OBJECTS>")
178endif()
179if(NOT DEFINED CMAKE_C_ARCHIVE_FINISH)
180 set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>")
181endif()
182
183# compile a C file into an object file
184if(NOT CMAKE_C_COMPILE_OBJECT)
185 set(CMAKE_C_COMPILE_OBJECT
186 "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
187endif()
188
189if(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>")
192endif()
193
194if(NOT CMAKE_EXECUTABLE_RUNTIME_C_FLAG)
195 set(CMAKE_EXECUTABLE_RUNTIME_C_FLAG ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG})
196endif()
197
198if(NOT CMAKE_EXECUTABLE_RUNTIME_C_FLAG_SEP)
199 set(CMAKE_EXECUTABLE_RUNTIME_C_FLAG_SEP ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP})
200endif()
201
202if(NOT CMAKE_EXECUTABLE_RPATH_LINK_C_FLAG)
203 set(CMAKE_EXECUTABLE_RPATH_LINK_C_FLAG ${CMAKE_SHARED_LIBRARY_RPATH_LINK_C_FLAG})
204endif()
205
206set(CMAKE_C_INFORMATION_LOADED 1)