CMakeCXXInformation.cmake 10 KB
Newer Older
1
2
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
3

4

5
6
7
# This file sets the basic flags for the C++ language in CMake.
# It also loads the available platform file for the system-compiler
# if it exists.
Alexander Neundorf's avatar
 
Alexander Neundorf committed
8
9
10
# It also loads a system - compiler - processor (or target hardware)
# specific file, which is mainly useful for crosscompiling and embedded systems.

11
12
include(CMakeLanguageInformation)

Alexander Neundorf's avatar
 
Alexander Neundorf committed
13
14
# some compilers use different extensions (e.g. sdcc uses .rel)
# so set the extension here first so it can be overridden by the compiler specific file
15
16
if(UNIX)
  set(CMAKE_CXX_OUTPUT_EXTENSION .o)
17
else()
18
  set(CMAKE_CXX_OUTPUT_EXTENSION .obj)
19
endif()
Alexander Neundorf's avatar
 
Alexander Neundorf committed
20

21
set(_INCLUDED_FILE 0)
22

23
# Load compiler-specific information.
24
25
if(CMAKE_CXX_COMPILER_ID)
  include(Compiler/${CMAKE_CXX_COMPILER_ID}-CXX OPTIONAL)
26
endif()
27

28
set(CMAKE_BASE_NAME)
29
get_filename_component(CMAKE_BASE_NAME "${CMAKE_CXX_COMPILER}" NAME_WE)
Alexander Neundorf's avatar
 
Alexander Neundorf committed
30
# since the gnu compiler has several names force g++
31
32
if(CMAKE_COMPILER_IS_GNUCXX)
  set(CMAKE_BASE_NAME g++)
33
endif()
Alexander Neundorf's avatar
 
Alexander Neundorf committed
34
35


Alexander Neundorf's avatar
 
Alexander Neundorf committed
36
# load a hardware specific file, mostly useful for embedded compilers
37
38
39
if(CMAKE_SYSTEM_PROCESSOR)
  if(CMAKE_CXX_COMPILER_ID)
    include(Platform/${CMAKE_SYSTEM_NAME}-${CMAKE_CXX_COMPILER_ID}-CXX-${CMAKE_SYSTEM_PROCESSOR} OPTIONAL RESULT_VARIABLE _INCLUDED_FILE)
40
  endif()
41
42
  if (NOT _INCLUDED_FILE)
    include(Platform/${CMAKE_SYSTEM_NAME}-${CMAKE_BASE_NAME}-${CMAKE_SYSTEM_PROCESSOR} OPTIONAL)
43
44
  endif ()
endif()
Alexander Neundorf's avatar
 
Alexander Neundorf committed
45
46

# load the system- and compiler specific files
47
48
if(CMAKE_CXX_COMPILER_ID)
  include(Platform/${CMAKE_SYSTEM_NAME}-${CMAKE_CXX_COMPILER_ID}-CXX OPTIONAL RESULT_VARIABLE _INCLUDED_FILE)
49
endif()
50
51
if (NOT _INCLUDED_FILE)
  include(Platform/${CMAKE_SYSTEM_NAME}-${CMAKE_BASE_NAME} OPTIONAL
52
          RESULT_VARIABLE _INCLUDED_FILE)
53
endif ()
54
55
56
57
58
59

# load any compiler-wrapper specific information
if (CMAKE_CXX_COMPILER_WRAPPER)
  __cmake_include_compiler_wrapper(CXX)
endif ()

60
61
62
63
# We specify the compiler information in the system file for some
# platforms, but this language may not have been enabled when the file
# was first included.  Include it again to get the language info.
# Remove this when all compiler info is removed from system files.
64
65
if (NOT _INCLUDED_FILE)
  include(Platform/${CMAKE_SYSTEM_NAME} OPTIONAL)
66
endif ()
Alexander Neundorf's avatar
 
Alexander Neundorf committed
67

68
69
70
71
72
73
if(CMAKE_CXX_SIZEOF_DATA_PTR)
  foreach(f ${CMAKE_CXX_ABI_FILES})
    include(${f})
  endforeach()
  unset(CMAKE_CXX_ABI_FILES)
endif()
Alexander Neundorf's avatar
 
Alexander Neundorf committed
74

75
# This should be included before the _INIT variables are
76
# used to initialize the cache.  Since the rule variables
77
78
79
80
# have if blocks on them, users can still define them here.
# But, it should still be after the platform file so changes can
# be made to those values.

81
if(CMAKE_USER_MAKE_RULES_OVERRIDE)
82
  # Save the full path of the file so try_compile can use it.
83
84
85
  include(${CMAKE_USER_MAKE_RULES_OVERRIDE} RESULT_VARIABLE _override)
  set(CMAKE_USER_MAKE_RULES_OVERRIDE "${_override}")
endif()
86

87
if(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX)
88
  # Save the full path of the file so try_compile can use it.
89
90
91
  include(${CMAKE_USER_MAKE_RULES_OVERRIDE_CXX} RESULT_VARIABLE _override)
  set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX "${_override}")
endif()
92

93
94
95
96

# Create a set of shared library variable specific to C++
# For 90% of the systems, these are the same flags as the C versions
# so if these are not set just copy the flags from the c version
97
98
if(NOT CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS)
  set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS})
99
endif()
100

101
102
if(NOT CMAKE_CXX_COMPILE_OPTIONS_PIC)
  set(CMAKE_CXX_COMPILE_OPTIONS_PIC ${CMAKE_C_COMPILE_OPTIONS_PIC})
103
endif()
104

105
106
if(NOT CMAKE_CXX_COMPILE_OPTIONS_PIE)
  set(CMAKE_CXX_COMPILE_OPTIONS_PIE ${CMAKE_C_COMPILE_OPTIONS_PIE})
107
endif()
108

109
110
if(NOT CMAKE_CXX_COMPILE_OPTIONS_DLL)
  set(CMAKE_CXX_COMPILE_OPTIONS_DLL ${CMAKE_C_COMPILE_OPTIONS_DLL})
111
endif()
112

113
114
if(NOT CMAKE_SHARED_LIBRARY_CXX_FLAGS)
  set(CMAKE_SHARED_LIBRARY_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_C_FLAGS})
115
endif()
116

117
118
if(NOT DEFINED CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS)
  set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS})
119
endif()
120

121
122
if(NOT CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG)
  set(CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG})
123
endif()
124

125
126
if(NOT CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP)
  set(CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP})
127
endif()
128

129
130
if(NOT CMAKE_SHARED_LIBRARY_RPATH_LINK_CXX_FLAG)
  set(CMAKE_SHARED_LIBRARY_RPATH_LINK_CXX_FLAG ${CMAKE_SHARED_LIBRARY_RPATH_LINK_C_FLAG})
131
endif()
132

133
134
135
if(NOT DEFINED CMAKE_EXE_EXPORTS_CXX_FLAG)
  set(CMAKE_EXE_EXPORTS_CXX_FLAG ${CMAKE_EXE_EXPORTS_C_FLAG})
endif()
136

137
138
139
if(NOT DEFINED CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG)
  set(CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG ${CMAKE_SHARED_LIBRARY_SONAME_C_FLAG})
endif()
140

141
142
if(NOT CMAKE_EXECUTABLE_RUNTIME_CXX_FLAG)
  set(CMAKE_EXECUTABLE_RUNTIME_CXX_FLAG ${CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG})
143
endif()
144

145
146
if(NOT CMAKE_EXECUTABLE_RUNTIME_CXX_FLAG_SEP)
  set(CMAKE_EXECUTABLE_RUNTIME_CXX_FLAG_SEP ${CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP})
147
endif()
148

149
150
if(NOT CMAKE_EXECUTABLE_RPATH_LINK_CXX_FLAG)
  set(CMAKE_EXECUTABLE_RPATH_LINK_CXX_FLAG ${CMAKE_SHARED_LIBRARY_RPATH_LINK_CXX_FLAG})
151
endif()
152

153
154
if(NOT DEFINED CMAKE_SHARED_LIBRARY_LINK_CXX_WITH_RUNTIME_PATH)
  set(CMAKE_SHARED_LIBRARY_LINK_CXX_WITH_RUNTIME_PATH ${CMAKE_SHARED_LIBRARY_LINK_C_WITH_RUNTIME_PATH})
155
endif()
156

157
158
if(NOT CMAKE_INCLUDE_FLAG_CXX)
  set(CMAKE_INCLUDE_FLAG_CXX ${CMAKE_INCLUDE_FLAG_C})
159
endif()
160

161
162
if(NOT CMAKE_INCLUDE_FLAG_SEP_CXX)
  set(CMAKE_INCLUDE_FLAG_SEP_CXX ${CMAKE_INCLUDE_FLAG_SEP_C})
163
endif()
164

165
166
167
# for most systems a module is the same as a shared library
# so unless the variable CMAKE_MODULE_EXISTS is set just
# copy the values from the LIBRARY variables
168
169
170
if(NOT CMAKE_MODULE_EXISTS)
  set(CMAKE_SHARED_MODULE_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_CXX_FLAGS})
  set(CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS})
171
endif()
172

173
# repeat for modules
174
175
if(NOT CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS)
  set(CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS ${CMAKE_SHARED_MODULE_CREATE_C_FLAGS})
176
endif()
177

178
179
if(NOT CMAKE_SHARED_MODULE_CXX_FLAGS)
  set(CMAKE_SHARED_MODULE_CXX_FLAGS ${CMAKE_SHARED_MODULE_C_FLAGS})
180
endif()
181

182
# Initialize CXX link type selection flags from C versions.
183
184
185
foreach(type SHARED_LIBRARY SHARED_MODULE EXE)
  if(NOT CMAKE_${type}_LINK_STATIC_CXX_FLAGS)
    set(CMAKE_${type}_LINK_STATIC_CXX_FLAGS
186
      ${CMAKE_${type}_LINK_STATIC_C_FLAGS})
187
  endif()
188
189
  if(NOT CMAKE_${type}_LINK_DYNAMIC_CXX_FLAGS)
    set(CMAKE_${type}_LINK_DYNAMIC_CXX_FLAGS
190
      ${CMAKE_${type}_LINK_DYNAMIC_C_FLAGS})
191
192
  endif()
endforeach()
193

194
195
196
197
# add the flags to the cache based
# on the initial values computed in the platform/*.cmake files
# use _INIT variables so that this only happens the first time
# and you can set these flags in the cmake cache
198
set(CMAKE_CXX_FLAGS_INIT "$ENV{CXXFLAGS} ${CMAKE_CXX_FLAGS_INIT}")
199
200
201
202
203

foreach(c "" _DEBUG _RELEASE _MINSIZEREL _RELWITHDEBINFO)
  string(STRIP "${CMAKE_CXX_FLAGS${c}_INIT}" CMAKE_CXX_FLAGS${c}_INIT)
endforeach()

204
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_INIT}" CACHE STRING
205
206
     "Flags used by the compiler during all build types.")

207
208
if(NOT CMAKE_NOT_USING_CONFIG_FLAGS)
  set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG_INIT}" CACHE STRING
209
     "Flags used by the compiler during debug builds.")
210
  set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL_INIT}" CACHE STRING
211
     "Flags used by the compiler during release builds for minimum size.")
212
  set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE_INIT}" CACHE STRING
213
     "Flags used by the compiler during release builds.")
214
  set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT}" CACHE STRING
215
     "Flags used by the compiler during release builds with debug info.")
216

217
endif()
218

219
220
if(CMAKE_CXX_STANDARD_LIBRARIES_INIT)
  set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES_INIT}"
221
    CACHE STRING "Libraries linked by default with all C++ applications.")
222
  mark_as_advanced(CMAKE_CXX_STANDARD_LIBRARIES)
223
endif()
224

225
include(CMakeCommonLanguageInclude)
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

# now define the following rules:
# CMAKE_CXX_CREATE_SHARED_LIBRARY
# CMAKE_CXX_CREATE_SHARED_MODULE
# CMAKE_CXX_COMPILE_OBJECT
# CMAKE_CXX_LINK_EXECUTABLE

# variables supplied by the generator at use time
# <TARGET>
# <TARGET_BASE> the target without the suffix
# <OBJECTS>
# <OBJECT>
# <LINK_LIBRARIES>
# <FLAGS>
# <LINK_FLAGS>

# CXX compiler information
243
# <CMAKE_CXX_COMPILER>
244
245
246
247
248
# <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS>
# <CMAKE_CXX_SHARED_MODULE_CREATE_FLAGS>
# <CMAKE_CXX_LINK_FLAGS>

# Static library tools
249
# <CMAKE_AR>
250
251
252
253
# <CMAKE_RANLIB>


# create a shared C++ library
254
255
if(NOT CMAKE_CXX_CREATE_SHARED_LIBRARY)
  set(CMAKE_CXX_CREATE_SHARED_LIBRARY
256
      "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <SONAME_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
257
endif()
258
259

# create a c++ shared module copy the shared library rule by default
260
261
if(NOT CMAKE_CXX_CREATE_SHARED_MODULE)
  set(CMAKE_CXX_CREATE_SHARED_MODULE ${CMAKE_CXX_CREATE_SHARED_LIBRARY})
262
endif()
263
264


265
266
# Create a static archive incrementally for large object file counts.
# If CMAKE_CXX_CREATE_STATIC_LIBRARY is set it will override these.
267
if(NOT DEFINED CMAKE_CXX_ARCHIVE_CREATE)
Brad King's avatar
Brad King committed
268
  set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <LINK_FLAGS> <OBJECTS>")
269
270
endif()
if(NOT DEFINED CMAKE_CXX_ARCHIVE_APPEND)
271
  set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> q  <TARGET> <LINK_FLAGS> <OBJECTS>")
272
273
274
275
endif()
if(NOT DEFINED CMAKE_CXX_ARCHIVE_FINISH)
  set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>")
endif()
276
277

# compile a C++ file into an object file
278
279
if(NOT CMAKE_CXX_COMPILE_OBJECT)
  set(CMAKE_CXX_COMPILE_OBJECT
280
    "<CMAKE_CXX_COMPILER>  <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
281
endif()
282

283
284
if(NOT CMAKE_CXX_LINK_EXECUTABLE)
  set(CMAKE_CXX_LINK_EXECUTABLE
285
    "<CMAKE_CXX_COMPILER>  <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS>  -o <TARGET> <LINK_LIBRARIES>")
286
endif()
287

288
mark_as_advanced(
289
CMAKE_VERBOSE_MAKEFILE
290
291
292
293
294
295
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_DEBUG)

296
set(CMAKE_CXX_INFORMATION_LOADED 1)
297