1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
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
8 # It also loads a system - compiler - processor (or target hardware)
9 # specific file, which is mainly useful for crosscompiling and embedded systems.
11 include(CMakeLanguageInformation)
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
16 set(CMAKE_C_OUTPUT_EXTENSION .o)
18 set(CMAKE_C_OUTPUT_EXTENSION .obj)
23 # Load compiler-specific information.
24 if(CMAKE_C_COMPILER_ID)
25 include(Compiler/${CMAKE_C_COMPILER_ID}-C OPTIONAL)
29 get_filename_component(CMAKE_BASE_NAME "${CMAKE_C_COMPILER}" NAME_WE)
30 if(CMAKE_COMPILER_IS_GNUCC)
31 set(CMAKE_BASE_NAME gcc)
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)
40 if (NOT _INCLUDED_FILE)
41 include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_BASE_NAME}-${CMAKE_SYSTEM_PROCESSOR} OPTIONAL)
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)
51 if (NOT _INCLUDED_FILE)
52 include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_BASE_NAME}
53 OPTIONAL RESULT_VARIABLE _INCLUDED_FILE)
56 # load any compiler-wrapper specific information
57 if (CMAKE_C_COMPILER_WRAPPER)
58 __cmake_include_compiler_wrapper(C)
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)
69 if(CMAKE_C_SIZEOF_DATA_PTR)
70 foreach(f IN LISTS CMAKE_C_ABI_FILES)
73 unset(CMAKE_C_ABI_FILES)
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.
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}")
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}")
94 set(CMAKE_C_FLAGS_INIT "$ENV{CFLAGS} ${CMAKE_C_FLAGS_INIT}")
96 cmake_initialize_per_config_variable(CMAKE_C_FLAGS "Flags used by the C compiler")
98 if(CMAKE_C_STANDARD_LIBRARIES_INIT)
99 set(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES_INIT}"
100 CACHE STRING "Libraries linked by default with all C applications.")
101 mark_as_advanced(CMAKE_C_STANDARD_LIBRARIES)
104 if(NOT CMAKE_C_COMPILER_LAUNCHER AND DEFINED ENV{CMAKE_C_COMPILER_LAUNCHER})
105 set(CMAKE_C_COMPILER_LAUNCHER "$ENV{CMAKE_C_COMPILER_LAUNCHER}"
106 CACHE STRING "Compiler launcher for C.")
109 if(NOT CMAKE_C_LINKER_LAUNCHER AND DEFINED ENV{CMAKE_C_LINKER_LAUNCHER})
110 set(CMAKE_C_LINKER_LAUNCHER "$ENV{CMAKE_C_LINKER_LAUNCHER}"
111 CACHE STRING "Linker launcher for C.")
114 include(CMakeCommonLanguageInclude)
115 _cmake_common_language_platform_flags(C)
117 # now define the following rule variables
119 # CMAKE_C_CREATE_SHARED_LIBRARY
120 # CMAKE_C_CREATE_SHARED_MODULE
121 # CMAKE_C_COMPILE_OBJECT
122 # CMAKE_C_LINK_EXECUTABLE
124 # variables supplied by the generator at use time
126 # <TARGET_BASE> the target without the suffix
133 # C compiler information
135 # <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS>
136 # <CMAKE_SHARED_MODULE_CREATE_C_FLAGS>
137 # <CMAKE_C_LINK_FLAGS>
139 # Static library tools
144 # create a C shared library
145 if(NOT CMAKE_C_CREATE_SHARED_LIBRARY)
146 set(CMAKE_C_CREATE_SHARED_LIBRARY
147 "<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>")
150 # create a C shared module just copy the shared library rule
151 if(NOT CMAKE_C_CREATE_SHARED_MODULE)
152 set(CMAKE_C_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_LIBRARY})
155 # Create a static archive incrementally for large object file counts.
156 # If CMAKE_C_CREATE_STATIC_LIBRARY is set it will override these.
157 if(NOT DEFINED CMAKE_C_ARCHIVE_CREATE)
158 set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <LINK_FLAGS> <OBJECTS>")
160 if(NOT DEFINED CMAKE_C_ARCHIVE_APPEND)
161 set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> q <TARGET> <LINK_FLAGS> <OBJECTS>")
163 if(NOT DEFINED CMAKE_C_ARCHIVE_FINISH)
164 set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>")
167 # compile a C file into an object file
168 if(NOT CMAKE_C_COMPILE_OBJECT)
169 set(CMAKE_C_COMPILE_OBJECT
170 "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
173 if(NOT CMAKE_C_LINK_EXECUTABLE)
174 set(CMAKE_C_LINK_EXECUTABLE
175 "<CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
178 set(CMAKE_C_USE_LINKER_INFORMATION TRUE)
180 set(CMAKE_C_INFORMATION_LOADED 1)