1 # taken from http://www.vtk.org/Bug/view.php?id=1260 and slightly adjusted
3 # - Try to find precompiled headers support for GCC 3.4 and 4.x
4 # Once done this will define:
10 # ADD_PRECOMPILED_HEADER _targetName _input _dowarn
11 # ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use _dowarn
12 # ADD_NATIVE_PRECOMPILED_HEADER _targetName _input _dowarn
13 # GET_NATIVE_PRECOMPILED_HEADER _targetName _input
15 IF(CMAKE_COMPILER_IS_GNUCXX)
19 ARGS ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
20 OUTPUT_VARIABLE gcc_compiler_version)
21 #MESSAGE("GCC Version: ${gcc_compiler_version}")
22 IF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
23 SET(PCHSupport_FOUND TRUE)
24 ELSE(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
25 IF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
26 SET(PCHSupport_FOUND TRUE)
27 ENDIF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
28 ENDIF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
30 SET(_PCH_include_prefix "-I")
32 ELSE(CMAKE_COMPILER_IS_GNUCXX)
34 SET(PCHSupport_FOUND TRUE) # for experimental msvc support
35 SET(_PCH_include_prefix "/I")
37 SET(PCHSupport_FOUND FALSE)
39 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
42 MACRO(_PCH_GET_COMPILE_FLAGS _out_compile_flags)
45 STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name)
46 SET(${_out_compile_flags} ${${_flags_var_name}} )
48 IF(CMAKE_COMPILER_IS_GNUCXX)
50 GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
51 IF(${_targetType} STREQUAL SHARED_LIBRARY AND NOT WIN32)
52 LIST(APPEND ${_out_compile_flags} "${${_out_compile_flags}} -fPIC")
55 ELSE(CMAKE_COMPILER_IS_GNUCXX)
56 ## TODO ... ? or does it work out of the box
57 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
59 GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES )
60 FOREACH(item ${DIRINC})
61 LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}${item}")
64 GET_DIRECTORY_PROPERTY(_directory_flags DEFINITIONS)
65 GET_DIRECTORY_PROPERTY(_global_definitions DIRECTORY ${CMAKE_SOURCE_DIR} DEFINITIONS)
66 #MESSAGE("_directory_flags ${_directory_flags} ${_global_definitions}" )
67 LIST(APPEND ${_out_compile_flags} ${_directory_flags})
68 LIST(APPEND ${_out_compile_flags} ${_global_definitions})
69 LIST(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS} )
71 SEPARATE_ARGUMENTS(${_out_compile_flags})
73 ENDMACRO(_PCH_GET_COMPILE_FLAGS)
76 MACRO(_PCH_WRITE_PCHDEP_CXX _targetName _include_file _dephelp)
78 SET(${_dephelp} ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch_dephelp.cxx)
79 FILE(WRITE ${${_dephelp}}
80 "#include \"${_include_file}\"
88 ENDMACRO(_PCH_WRITE_PCHDEP_CXX )
90 MACRO(_PCH_GET_COMPILE_COMMAND out_command _input _output)
92 FILE(TO_NATIVE_PATH ${_input} _native_input)
93 FILE(TO_NATIVE_PATH ${_output} _native_output)
96 IF(CMAKE_COMPILER_IS_GNUCXX)
97 IF(CMAKE_CXX_COMPILER_ARG1)
98 # remove leading space in compiler argument
99 STRING(REGEX REPLACE "^ +" "" pchsupport_compiler_cxx_arg1 ${CMAKE_CXX_COMPILER_ARG1})
102 ${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
104 ELSE(CMAKE_CXX_COMPILER_ARG1)
106 ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
108 ENDIF(CMAKE_CXX_COMPILER_ARG1)
109 ELSE(CMAKE_COMPILER_IS_GNUCXX)
111 SET(_dummy_str "#include <${_input}>")
112 FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pch_dummy.cpp ${_dummy_str})
115 ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} /c /Fp${_native_output} /Yc${_native_input} pch_dummy.cpp
119 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
121 ENDMACRO(_PCH_GET_COMPILE_COMMAND )
125 MACRO(_PCH_GET_TARGET_COMPILE_FLAGS _cflags _header_name _pch_path _dowarn )
127 FILE(TO_NATIVE_PATH ${_pch_path} _native_pch_path)
129 IF(CMAKE_COMPILER_IS_GNUCXX)
130 # for use with distcc and gcc >4.0.1 if preprocessed files are accessible
131 # on all remote machines set
132 # PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess
133 # if you want warnings for invalid header files (which is very inconvenient
134 # if you have different versions of the headers for different build types
135 # you may set _pch_dowarn
137 SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include ${CMAKE_CURRENT_BINARY_DIR}/${_header_name} -Winvalid-pch " )
139 SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include ${CMAKE_CURRENT_BINARY_DIR}/${_header_name} " )
141 ELSE(CMAKE_COMPILER_IS_GNUCXX)
143 set(${_cflags} "/Fp${_native_pch_path} /Yu${_header_name}" )
145 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
147 ENDMACRO(_PCH_GET_TARGET_COMPILE_FLAGS )
149 MACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input _output)
150 GET_FILENAME_COMPONENT(_name ${_input} NAME)
151 GET_FILENAME_COMPONENT(_path ${_input} PATH)
152 SET(_output "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch/${_targetName}_${CMAKE_BUILD_TYPE}.gch")
153 ENDMACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input)
156 MACRO(ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use )
158 # to do: test whether compiler flags match between target _targetName
159 # and _pch_output_to_use
160 GET_FILENAME_COMPONENT(_name ${_input} NAME)
162 IF( "${ARGN}" STREQUAL "0")
164 ELSE( "${ARGN}" STREQUAL "0")
166 ENDIF("${ARGN}" STREQUAL "0")
169 _PCH_GET_TARGET_COMPILE_FLAGS(_target_cflags ${_name} ${_pch_output_to_use} ${_dowarn})
170 # MESSAGE("Add flags ${_target_cflags} to ${_targetName} " )
171 SET_TARGET_PROPERTIES(${_targetName}
173 COMPILE_FLAGS ${_target_cflags}
176 ADD_CUSTOM_TARGET(pch_Generate_${_targetName}
177 DEPENDS ${_pch_output_to_use}
180 ADD_DEPENDENCIES(${_targetName} pch_Generate_${_targetName} )
182 ENDMACRO(ADD_PRECOMPILED_HEADER_TO_TARGET)
184 MACRO(ADD_PRECOMPILED_HEADER _targetName _input)
186 SET(_PCH_current_target ${_targetName})
188 IF(NOT CMAKE_BUILD_TYPE)
190 "This is the ADD_PRECOMPILED_HEADER macro. "
191 "You must set CMAKE_BUILD_TYPE!"
193 ENDIF(NOT CMAKE_BUILD_TYPE)
195 IF( "${ARGN}" STREQUAL "0")
197 ELSE( "${ARGN}" STREQUAL "0")
199 ENDIF("${ARGN}" STREQUAL "0")
202 GET_FILENAME_COMPONENT(_name ${_input} NAME)
203 GET_FILENAME_COMPONENT(_path ${_input} PATH)
204 GET_PRECOMPILED_HEADER_OUTPUT( ${_targetName} ${_input} _output)
206 GET_FILENAME_COMPONENT(_outdir ${_output} PATH )
208 GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
209 _PCH_WRITE_PCHDEP_CXX(${_targetName} ${_input} _pch_dephelp_cxx)
211 IF(${_targetType} STREQUAL SHARED_LIBRARY)
212 ADD_LIBRARY(${_targetName}_pch_dephelp STATIC ${_pch_dephelp_cxx} )
213 ELSE(${_targetType} STREQUAL SHARED_LIBRARY)
214 ADD_LIBRARY(${_targetName}_pch_dephelp STATIC ${_pch_dephelp_cxx})
215 ENDIF(${_targetType} STREQUAL SHARED_LIBRARY)
217 FILE(MAKE_DIRECTORY ${_outdir})
220 _PCH_GET_COMPILE_FLAGS(_compile_FLAGS)
222 #MESSAGE("_compile_FLAGS: ${_compile_FLAGS}")
223 #message("COMMAND ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}")
224 SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/${_name} PROPERTIES GENERATED 1)
226 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_name}
227 COMMAND ${CMAKE_COMMAND} -E copy ${_input} ${CMAKE_CURRENT_BINARY_DIR}/${_name} # ensure same directory! Required by gcc
231 #message("_command ${_input} ${_output}")
232 _PCH_GET_COMPILE_COMMAND(_command ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_output} )
235 #message("_output ${_output}")
240 DEPENDS ${_input} ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_targetName}_pch_dephelp
244 ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_input} ${_output} ${_dowarn})
245 ENDMACRO(ADD_PRECOMPILED_HEADER)
248 # Generates the use of precompiled in a target,
249 # without using depency targets (2 extra for each target)
250 # Using Visual, must also add ${_targetName}_pch to sources
251 # Not needed by Xcode
253 MACRO(GET_NATIVE_PRECOMPILED_HEADER _targetName _input)
255 if(CMAKE_GENERATOR MATCHES Visual*)
257 SET(_dummy_str "#include \"${_input}\"\n"
258 "// This is required to suppress LNK4221. Very annoying.\n"
259 "void *g_${_targetName}Dummy = 0\;\n")
261 # Use of cxx extension for generated files (as Qt does)
262 SET(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cxx)
263 if(EXISTS ${${_targetName}_pch})
264 # Check if contents is the same, if not rewrite
266 else(EXISTS ${${_targetName}_pch})
267 FILE(WRITE ${${_targetName}_pch} ${_dummy_str})
268 endif(EXISTS ${${_targetName}_pch})
269 endif(CMAKE_GENERATOR MATCHES Visual*)
271 ENDMACRO(GET_NATIVE_PRECOMPILED_HEADER)
274 MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _input)
276 IF( "${ARGN}" STREQUAL "0")
278 ELSE( "${ARGN}" STREQUAL "0")
280 ENDIF("${ARGN}" STREQUAL "0")
282 if(CMAKE_GENERATOR MATCHES Visual*)
283 # Auto include the precompile (useful for moc processing, since the use of
284 # precompiled is specified at the target level
285 # and I don't want to specifiy /F- for each moc/res/ui generated files (using Qt)
287 GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS)
288 if (${oldProps} MATCHES NOTFOUND)
290 endif(${oldProps} MATCHES NOTFOUND)
292 SET(newProperties "${oldProps} /Yu\"${_input}\" /FI\"${_input}\"")
293 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES COMPILE_FLAGS "${newProperties}")
295 #also inlude ${oldProps} to have the same compile options
296 SET_SOURCE_FILES_PROPERTIES(${${_targetName}_pch} PROPERTIES COMPILE_FLAGS "${oldProps} /Yc\"${_input}\"")
298 else(CMAKE_GENERATOR MATCHES Visual*)
300 if (CMAKE_GENERATOR MATCHES Xcode)
301 # For Xcode, cmake needs my patch to process
302 # GCC_PREFIX_HEADER and GCC_PRECOMPILE_PREFIX_HEADER as target properties
304 GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS)
305 if (${oldProps} MATCHES NOTFOUND)
307 endif(${oldProps} MATCHES NOTFOUND)
309 # When buiding out of the tree, precompiled may not be located
310 # Use full path instead.
311 GET_FILENAME_COMPONENT(fullPath ${_input} ABSOLUTE)
313 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${fullPath}")
314 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
316 else (CMAKE_GENERATOR MATCHES Xcode)
318 #Fallback to the "old" precompiled suppport
319 #ADD_PRECOMPILED_HEADER(${_targetName} ${_input} ${_dowarn})
320 endif(CMAKE_GENERATOR MATCHES Xcode)
321 endif(CMAKE_GENERATOR MATCHES Visual*)
323 ENDMACRO(ADD_NATIVE_PRECOMPILED_HEADER)