-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrecompiledHeader.cmake
More file actions
271 lines (260 loc) · 11.2 KB
/
Copy pathPrecompiledHeader.cmake
File metadata and controls
271 lines (260 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# Copyright (C) 2012 Yuchen Deng
# Contact: loaden@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# TODO: missing clang support
cmake_minimum_required(VERSION 2.8.9 FATAL_ERROR)
macro(set_precompiled_header target language precompiledHeader pchSourceVariable)
if(TARGET ${target})
message(FATAL_ERROR "The first argument must be an non-existent target.")
endif()
set(${target}_PCH_LANGUAGE ${language}
CACHE INTERNAL "Which language (C/CXX) using for PCH support"
)
get_source_file_property(pchHeaderFile ${precompiledHeader} LOCATION)
if(MSVC)
if(${language} STREQUAL CXX)
set(${pchSourceVariable} ${target}_PCH.cpp)
elseif(${language} STREQUAL C)
set(${pchSourceVariable} ${target}_PCH.c)
else()
message(FATAL_ERROR "[${target}] Unknown language \"${language}\"")
return()
endif()
set(${target}_PCH_SOURCE_VAR ${pchSourceVariable}
CACHE INTERNAL "Precompiled source file variable"
)
if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${${pchSourceVariable}} OR
NOT ${${target}_PCH_HEADER_FILE} STREQUAL ${pchHeaderFile})
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${${pchSourceVariable}}
"/* This file is autogenerated, do not edit! */\n"
"#include \"${pchHeaderFile}\"\n"
)
endif()
add_custom_target(${target}_pch)
add_custom_command(TARGET ${target}_pch POST_BUILD
COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_CURRENT_BINARY_DIR}/${${pchSourceVariable}}
COMMENT "[${target}] Update precompiled source - done"
)
endif()
message(STATUS "[${target}] Precompiled header is \"${pchHeaderFile}\"")
set(${target}_PCH_HEADER_FILE ${pchHeaderFile}
CACHE INTERNAL "Precompiled header file"
)
endmacro()
macro(use_precompiled_header target)
if(NOT TARGET ${target})
message(FATAL_ERROR "The first argument must be an existing target.")
endif()
get_target_property(targetAutomoc ${target} AUTOMOC)
if(CMAKE_AUTOMOC OR targetAutomoc)
set(automocFile ${target}_automoc.cpp)
if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${automocFile})
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${automocFile}
"/* This file is autogenerated, do not edit! */\n"
)
endif()
endif()
if(MSVC)
add_msvc_precompiled_header(${target})
use_msvc_precompiled_header(${target} ${ARGN} ${automocFile})
elseif(CMAKE_COMPILER_IS_GNUC OR CMAKE_COMPILER_IS_GNUCXX OR MINGW)
add_gcc_precompiled_header(${target})
use_gcc_precompiled_header(${target} ${ARGN} ${automocFile})
endif()
endmacro()
function(add_msvc_precompiled_header target)
set(pchBinaryFile ${CMAKE_CURRENT_BINARY_DIR}/${target}_PCH.pch)
set_source_files_properties(${${${target}_PCH_SOURCE_VAR}} PROPERTIES
COMPILE_FLAGS "/Yc\"${${target}_PCH_HEADER_FILE}\" /Fp\"${pchBinaryFile}\""
OBJECT_OUTPUTS ${pchBinaryFile}
)
message(STATUS "[${target}] Precompiled binary is \"${pchBinaryFile}\"")
set(${target}_PCH_BINARY_FILE ${pchBinaryFile}
CACHE INTERNAL "Precompiled binary file"
)
endfunction()
function(use_msvc_precompiled_header target)
if(NOT ${target}_PCH_BINARY_FILE)
message(FATAL_ERROR "[${target}] Precompiled binary does not exist")
return()
endif()
get_target_property(targetSources ${target} SOURCES)
list(FIND targetSources ${${${target}_PCH_SOURCE_VAR}} result)
if(result EQUAL -1)
message(FATAL_ERROR "[${target}] Please add '\${${${target}_PCH_SOURCE_VAR}}' or '${${${target}_PCH_SOURCE_VAR}}' to target")
return()
endif()
set(pchBinaryFile ${${target}_PCH_BINARY_FILE})
set_property(SOURCE ${ARGN} APPEND_STRING PROPERTY
COMPILE_FLAGS "/Yu\"${pchBinaryFile}\" /FI\"${pchBinaryFile}\" /Fp\"${pchBinaryFile}\""
)
set_property(SOURCE ${ARGN} APPEND PROPERTY
OBJECT_DEPENDS ${pchBinaryFile}
)
endfunction()
function(add_gcc_precompiled_header target)
set(origPCHHeaderFile ${${target}_PCH_HEADER_FILE})
get_filename_component(pchHeaderPath ${origPCHHeaderFile} PATH)
if(NOT ${pchHeaderPath} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
set(pchSearchPath -I"${pchHeaderPath}")
separate_arguments(pchSearchPath)
endif()
set(pchHeaderName ${target}_PCH.h)
set(pchHeaderFile ${CMAKE_CURRENT_BINARY_DIR}/${pchHeaderName})
set(pchBinaryFile ${CMAKE_CURRENT_BINARY_DIR}/${pchHeaderName}.gch)
add_custom_command(OUTPUT ${pchHeaderFile}
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${origPCHHeaderFile} ${pchHeaderFile}
DEPENDS ${origPCHHeaderFile}
COMMENT "[${target}] Update precompiled header - done"
)
if(${${target}_PCH_LANGUAGE} STREQUAL CXX)
set(pchGenerator -x c++-header -c ${pchHeaderFile} -o ${pchBinaryFile})
else(${${target}_PCH_LANGUAGE} STREQUAL C)
set(pchGenerator -x c-header -c ${pchHeaderFile} -o ${pchBinaryFile})
else()
message(FATAL_ERROR "[${target}] Unknown language \"${${target}_PCH_LANGUAGE}\"")
return()
endif()
get_gcc_compile_flags(${target} compileFlags)
add_custom_command(OUTPUT ${pchBinaryFile}
COMMAND ${CMAKE_CXX_COMPILER} ${${target}_PCH_FLAGS} ${compileFlags} ${pchSearchPath} ${pchGenerator}
DEPENDS ${pchHeaderFile}
COMMENT "[${target}] Compile precompiled binary - done"
)
add_custom_target(${target}_pch
DEPENDS ${pchHeaderFile} ${pchBinaryFile}
)
add_dependencies(${target} ${target}_pch)
message(STATUS "[${target}] Precompiled binary is \"${pchBinaryFile}\"")
set(${target}_PCH_NEW_HEADER_FILE ${pchHeaderFile}
CACHE INTERNAL "Precompiled header file copy"
)
endfunction()
function(use_gcc_precompiled_header target)
if(NOT ${target}_PCH_NEW_HEADER_FILE)
message(FATAL_ERROR "[${target}] Precompiled header copy does not exist")
return()
endif()
set(pchHeaderFile ${${target}_PCH_NEW_HEADER_FILE})
set_property(SOURCE ${ARGN} APPEND_STRING PROPERTY
COMPILE_FLAGS "-include \"${pchHeaderFile}\" -Winvalid-pch"
)
set_property(SOURCE ${ARGN} APPEND PROPERTY
OBJECT_DEPENDS ${pchHeaderFile}
)
endfunction()
function(get_gcc_compile_flags target flagsVar)
string(TOUPPER "${CMAKE_BUILD_TYPE}" config)
set(language ${${target}_PCH_LANGUAGE})
# Collect options from CMake language variables
if(CMAKE_${language}_FLAGS)
list(APPEND compileFlags ${CMAKE_${language}_FLAGS})
endif()
if(CMAKE_${language}_FLAGS_${config})
list(APPEND compileFlags ${CMAKE_${language}_FLAGS_${config}})
endif()
# Add option from CMake target type variable
get_target_property(targetType ${target} TYPE)
# handle POSITION_INDEPENDENT_CODE property introduced with CMake 2.8.9 if policy CMP0018 is turned on
cmake_policy(GET CMP0018 _PIC_Policy)
# Honor the POSITION_INDEPENDENT_CODE target property
get_target_property(targetPIC ${target} POSITION_INDEPENDENT_CODE)
if(targetPIC)
if(targetType STREQUAL SHARED_LIBRARY)
list(APPEND compileFlags ${CMAKE_${language}_COMPILE_OPTIONS_PIC})
elseif(targetType STREQUAL EXECUTABLE)
list(APPEND compileFlags ${CMAKE_${language}_COMPILE_OPTIONS_PIE})
endif()
endif()
# Platform specific flags
if(APPLE)
get_target_property(architectures ${target} OSX_ARCHITECTURES_${config})
if(NOT architectures)
get_target_property(architectures ${target} OSX_ARCHITECTURES)
endif()
foreach(arch ${architectures})
list(APPEND compileFlags -arch ${arch})
endforeach()
if(CMAKE_OSX_SYSROOT AND CMAKE_OSX_SYSROOT_DEFAULT AND CMAKE_${language}_HAS_ISYSROOT)
if(NOT ${CMAKE_OSX_SYSROOT} STREQUAL ${CMAKE_OSX_SYSROOT_DEFAULT})
list(APPEND compileFlags -isysroot ${CMAKE_OSX_SYSROOT})
endif()
endif()
if(CMAKE_OSX_DEPLOYMENT_TARGET AND CMAKE_${language}_OSX_DEPLOYMENT_TARGET_FLAG)
list(APPEND compileFlags ${CMAKE_${language}_OSX_DEPLOYMENT_TARGET_FLAG} ${CMAKE_OSX_DEPLOYMENT_TARGET})
endif()
endif()
# Add current dir in the first order
if(CMAKE_INCLUDE_CURRENT_DIR)
list(APPEND compileFlags -I"${CMAKE_CURRENT_BINARY_DIR}")
list(APPEND compileFlags -I"${CMAKE_CURRENT_SOURCE_DIR}")
endif()
# Add directory properties
get_directory_property(defines COMPILE_DEFINITIONS)
if(defines)
foreach(item ${defines})
list(APPEND compileFlags -D${item})
endforeach()
endif()
get_directory_property(defines COMPILE_DEFINITIONS_${config})
if(defines)
foreach(item ${defines})
list(APPEND compileFlags -D${item})
endforeach()
endif()
get_directory_property(value ${target} INCLUDE_DIRECTORIES)
if(value)
foreach(item ${value})
list(APPEND compileFlags -I"${item}")
endforeach()
endif()
# Add target compile options
get_target_property(targetflags ${target} COMPILE_FLAGS)
if(targetflags)
list(APPEND compileFlags ${targetflags})
endif()
# Add target compile definitions
get_target_property(defines ${target} COMPILE_DEFINITIONS)
if(defines)
foreach(item ${defines})
list(APPEND compileFlags -D${item})
endforeach()
endif()
get_target_property(defines ${target} COMPILE_DEFINITIONS_${config})
if(defines)
foreach(item ${defines})
list(APPEND compileFlags -D${item})
endforeach()
endif()
# Add target include directories
get_target_property(value ${target} INCLUDE_DIRECTORIES)
if(value)
foreach(item ${value})
list(APPEND compileFlags -I"${item}")
endforeach()
endif()
separate_arguments(compileFlags)
set(${flagsVar} ${compileFlags} PARENT_SCOPE)
endfunction()
function(add_pch_compile_flags target)
if(CMAKE_COMPILER_IS_GNUC OR CMAKE_COMPILER_IS_GNUCXX OR MINGW)
set(${target}_PCH_FLAGS ${ARGN}
CACHE INTERNAL "Prepend additional compile flags for Precompiled Header support"
)
endif()
endfunction()