1 # - Try to find precompiled headers support for GCC 3.4 and 4.x
2 # Once done this will define:
8 # ADD_PRECOMPILED_HEADER _targetName _input _dowarn
9 # ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use _dowarn
10 # ADD_NATIVE_PRECOMPILED_HEADER _targetName _input _dowarn
11 # GET_NATIVE_PRECOMPILED_HEADER _targetName _input
13 IF(CMAKE_COMPILER_IS_GNUCXX)
17 ARGS ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
18 OUTPUT_VARIABLE gcc_compiler_version)
19 #MESSAGE("GCC Version: ${gcc_compiler_version}")
20 IF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
21 SET(PCHSupport_FOUND TRUE)
22 ELSE(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
23 IF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
24 SET(PCHSupport_FOUND TRUE)
25 ENDIF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
26 ENDIF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
28 SET(_PCH_include_prefix "-I")
30 ELSE(CMAKE_COMPILER_IS_GNUCXX)
32 SET(PCHSupport_FOUND TRUE) # for experimental msvc support
33 SET(_PCH_include_prefix "/I")
35 SET(PCHSupport_FOUND FALSE)
37 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
40 MACRO(_PCH_GET_COMPILE_FLAGS _out_compile_flags)
43 STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name)
44 SET(${_out_compile_flags} ${${_flags_var_name}} )
46 IF(CMAKE_COMPILER_IS_GNUCXX)
48 GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
49 IF(${_targetType} STREQUAL SHARED_LIBRARY)
50 LIST(APPEND ${_out_compile_flags} "${${_out_compile_flags}} -fPIC")
51 ENDIF(${_targetType} STREQUAL SHARED_LIBRARY)
53 ELSE(CMAKE_COMPILER_IS_GNUCXX)
54 ## TODO ... ? or does it work out of the box
55 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
57 GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES )
58 FOREACH(item ${DIRINC})
59 LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}${item}")
62 GET_DIRECTORY_PROPERTY(_directory_flags DEFINITIONS)
63 #MESSAGE("_directory_flags ${_directory_flags}" )
64 LIST(APPEND ${_out_compile_flags} ${_directory_flags})
65 LIST(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS} )
67 SEPARATE_ARGUMENTS(${_out_compile_flags})
69 ENDMACRO(_PCH_GET_COMPILE_FLAGS)
72 MACRO(_PCH_WRITE_PCHDEP_CXX _targetName _include_file _dephelp)
74 SET(${_dephelp} ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch_dephelp.cxx)
75 FILE(WRITE ${${_dephelp}}
76 "#include \"${_include_file}\"
84 ENDMACRO(_PCH_WRITE_PCHDEP_CXX )
86 MACRO(_PCH_GET_COMPILE_COMMAND out_command _input _output)
88 FILE(TO_NATIVE_PATH ${_input} _native_input)
89 FILE(TO_NATIVE_PATH ${_output} _native_output)
92 IF(CMAKE_COMPILER_IS_GNUCXX)
93 IF(CMAKE_CXX_COMPILER_ARG1)
94 # remove leading space in compiler argument
95 STRING(REGEX REPLACE "^ +" "" pchsupport_compiler_cxx_arg1 ${CMAKE_CXX_COMPILER_ARG1})
98 ${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
100 ELSE(CMAKE_CXX_COMPILER_ARG1)
102 ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
104 ENDIF(CMAKE_CXX_COMPILER_ARG1)
105 ELSE(CMAKE_COMPILER_IS_GNUCXX)
107 SET(_dummy_str "#include <${_input}>")
108 FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pch_dummy.cpp ${_dummy_str})
111 ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} /c /Fp${_native_output} /Yc${_native_input} pch_dummy.cpp
115 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
117 ENDMACRO(_PCH_GET_COMPILE_COMMAND )
121 MACRO(_PCH_GET_TARGET_COMPILE_FLAGS _cflags _header_name _pch_path _dowarn )
123 FILE(TO_NATIVE_PATH ${_pch_path} _native_pch_path)
125 IF(CMAKE_COMPILER_IS_GNUCXX)
126 # for use with distcc and gcc >4.0.1 if preprocessed files are accessible
127 # on all remote machines set
128 # PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess
129 # if you want warnings for invalid header files (which is very inconvenient
130 # if you have different versions of the headers for different build types
131 # you may set _pch_dowarn
133 SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include ${CMAKE_CURRENT_BINARY_DIR}/${_header_name} -Winvalid-pch " )
135 SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include ${CMAKE_CURRENT_BINARY_DIR}/${_header_name} " )
137 ELSE(CMAKE_COMPILER_IS_GNUCXX)
139 set(${_cflags} "/Fp${_native_pch_path} /Yu${_header_name}" )
141 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
143 ENDMACRO(_PCH_GET_TARGET_COMPILE_FLAGS )
145 MACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input _output)
146 GET_FILENAME_COMPONENT(_name ${_input} NAME)
147 GET_FILENAME_COMPONENT(_path ${_input} PATH)
148 SET(_output "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch/${_targetName}_${CMAKE_BUILD_TYPE}.h++")
149 ENDMACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input)
152 MACRO(ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use )
154 # to do: test whether compiler flags match between target _targetName
155 # and _pch_output_to_use
156 GET_FILENAME_COMPONENT(_name ${_input} NAME)
158 IF( "${ARGN}" STREQUAL "0")
160 ELSE( "${ARGN}" STREQUAL "0")
162 ENDIF("${ARGN}" STREQUAL "0")
165 _PCH_GET_TARGET_COMPILE_FLAGS(_target_cflags ${_name} ${_pch_output_to_use} ${_dowarn})
166 # MESSAGE("Add flags ${_target_cflags} to ${_targetName} " )
167 SET_TARGET_PROPERTIES(${_targetName}
169 COMPILE_FLAGS ${_target_cflags}
172 ADD_CUSTOM_TARGET(pch_Generate_${_targetName}
173 DEPENDS ${_pch_output_to_use}
176 ADD_DEPENDENCIES(${_targetName} pch_Generate_${_targetName} )
178 ENDMACRO(ADD_PRECOMPILED_HEADER_TO_TARGET)
180 MACRO(ADD_PRECOMPILED_HEADER _targetName _input _gch)
182 SET(_PCH_current_target ${_targetName})
184 IF(NOT CMAKE_BUILD_TYPE)
186 "This is the ADD_PRECOMPILED_HEADER macro. "
187 "You must set CMAKE_BUILD_TYPE!"
189 ENDIF(NOT CMAKE_BUILD_TYPE)
191 IF( "${ARGN}" STREQUAL "0")
193 ELSE( "${ARGN}" STREQUAL "0")
195 ENDIF("${ARGN}" STREQUAL "0")
198 GET_FILENAME_COMPONENT(_name ${_input} NAME)
199 GET_FILENAME_COMPONENT(_path ${_input} PATH)
200 GET_PRECOMPILED_HEADER_OUTPUT( ${_targetName} ${_input} _output)
202 GET_FILENAME_COMPONENT(_outdir ${_output} PATH )
204 GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
205 _PCH_WRITE_PCHDEP_CXX(${_targetName} ${_input} _pch_dephelp_cxx)
207 IF(${_targetType} STREQUAL SHARED_LIBRARY)
208 ADD_LIBRARY(${_targetName}_pch_dephelp SHARED ${_pch_dephelp_cxx} )
209 ELSE(${_targetType} STREQUAL SHARED_LIBRARY)
210 ADD_LIBRARY(${_targetName}_pch_dephelp STATIC ${_pch_dephelp_cxx})
211 ENDIF(${_targetType} STREQUAL SHARED_LIBRARY)
213 FILE(MAKE_DIRECTORY ${_outdir})
216 _PCH_GET_COMPILE_FLAGS(_compile_FLAGS)
218 #MESSAGE("_compile_FLAGS: ${_compile_FLAGS}")
219 #message("COMMAND ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}")
220 SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/${_name} PROPERTIES GENERATED 1)
222 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_name}
223 COMMAND ${CMAKE_COMMAND} -E copy ${_input} ${CMAKE_CURRENT_BINARY_DIR}/${_name} # ensure same directory! Required by gcc
227 #message("_command ${_input} ${_output}")
228 _PCH_GET_COMPILE_COMMAND(_command ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_gch} )
231 #message("_output ${_output}")
236 DEPENDS ${_input} ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_targetName}_pch_dephelp
240 ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_input} ${_output} ${_dowarn})
241 ENDMACRO(ADD_PRECOMPILED_HEADER)
244 # Generates the use of precompiled in a target,
245 # without using depency targets (2 extra for each target)
246 # Using Visual, must also add ${_targetName}_pch to sources
247 # Not needed by Xcode
249 MACRO(GET_NATIVE_PRECOMPILED_HEADER _targetName _input)
251 if(CMAKE_GENERATOR MATCHES Visual*)
253 SET(_dummy_str "#include \"${_input}\"\n"
254 "// This is required to suppress LNK4221. Very annoying.\n"
255 "void *g_${_targetName}Dummy = 0\;\n")
257 # Use of cxx extension for generated files (as Qt does)
258 SET(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cxx)
259 if(EXISTS ${${_targetName}_pch})
260 # Check if contents is the same, if not rewrite
262 else(EXISTS ${${_targetName}_pch})
263 FILE(WRITE ${${_targetName}_pch} ${_dummy_str})
264 endif(EXISTS ${${_targetName}_pch})
265 endif(CMAKE_GENERATOR MATCHES Visual*)
267 ENDMACRO(GET_NATIVE_PRECOMPILED_HEADER)
270 MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _input)
272 IF( "${ARGN}" STREQUAL "0")
274 ELSE( "${ARGN}" STREQUAL "0")
276 ENDIF("${ARGN}" STREQUAL "0")
278 if(CMAKE_GENERATOR MATCHES Visual*)
279 # Auto include the precompile (useful for moc processing, since the use of
280 # precompiled is specified at the target level
281 # and I don't want to specifiy /F- for each moc/res/ui generated files (using Qt)
283 GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS)
284 if (${oldProps} MATCHES NOTFOUND)
286 endif(${oldProps} MATCHES NOTFOUND)
288 SET(newProperties "${oldProps} /Yu\"${_input}\" /FI\"${_input}\"")
289 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES COMPILE_FLAGS "${newProperties}")
291 #also inlude ${oldProps} to have the same compile options
292 SET_SOURCE_FILES_PROPERTIES(${${_targetName}_pch} PROPERTIES COMPILE_FLAGS "${oldProps} /Yc\"${_input}\"")
294 else(CMAKE_GENERATOR MATCHES Visual*)
296 if (CMAKE_GENERATOR MATCHES Xcode)
297 # For Xcode, cmake needs my patch to process
298 # GCC_PREFIX_HEADER and GCC_PRECOMPILE_PREFIX_HEADER as target properties
300 GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS)
301 if (${oldProps} MATCHES NOTFOUND)
303 endif(${oldProps} MATCHES NOTFOUND)
305 # When buiding out of the tree, precompiled may not be located
306 # Use full path instead.
307 GET_FILENAME_COMPONENT(fullPath ${_input} ABSOLUTE)
309 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${fullPath}")
310 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
312 else (CMAKE_GENERATOR MATCHES Xcode)
314 #Fallback to the "old" precompiled suppport
315 #ADD_PRECOMPILED_HEADER(${_targetName} ${_input} ${_dowarn})
316 endif(CMAKE_GENERATOR MATCHES Xcode)
317 endif(CMAKE_GENERATOR MATCHES Visual*)
319 ENDMACRO(ADD_NATIVE_PRECOMPILED_HEADER)