1 # taken from http://public.kitware.com/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)
17 IF(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.2.0")
18 SET(PCHSupport_FOUND TRUE)
21 SET(_PCH_include_prefix "-I")
22 SET(_PCH_isystem_prefix "-isystem")
23 SET(_PCH_define_prefix "-D")
25 ELSEIF(CMAKE_GENERATOR MATCHES "^Visual.*$")
26 SET(PCHSupport_FOUND TRUE)
27 SET(_PCH_include_prefix "/I")
28 SET(_PCH_isystem_prefix "/I")
29 SET(_PCH_define_prefix "/D")
31 SET(PCHSupport_FOUND FALSE)
34 MACRO(_PCH_GET_COMPILE_FLAGS _out_compile_flags)
36 STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name)
37 SET(${_out_compile_flags} ${${_flags_var_name}} )
39 IF(CMAKE_COMPILER_IS_GNUCXX)
41 GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
42 IF(${_targetType} STREQUAL SHARED_LIBRARY AND NOT WIN32)
43 LIST(APPEND ${_out_compile_flags} "-fPIC")
46 GET_PROPERTY(_definitions DIRECTORY PROPERTY COMPILE_DEFINITIONS)
48 foreach(_def ${_definitions})
49 LIST(APPEND ${_out_compile_flags} "\"-D${_def}\"")
52 GET_TARGET_PROPERTY(_target_definitions ${_PCH_current_target} COMPILE_DEFINITIONS)
53 if(_target_definitions)
54 foreach(_def ${_target_definitions})
55 LIST(APPEND ${_out_compile_flags} "\"-D${_def}\"")
60 ## TODO ... ? or does it work out of the box
63 GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES )
64 FOREACH(item ${DIRINC})
65 if(item MATCHES "^${OpenCV_SOURCE_DIR}/modules/")
66 LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}\"${item}\"")
68 LIST(APPEND ${_out_compile_flags} "${_PCH_isystem_prefix}\"${item}\"")
72 get_target_property(DIRINC ${_PCH_current_target} INCLUDE_DIRECTORIES )
73 FOREACH(item ${DIRINC})
74 if(item MATCHES "^${OpenCV_SOURCE_DIR}/modules/")
75 LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}\"${item}\"")
77 LIST(APPEND ${_out_compile_flags} "${_PCH_isystem_prefix}\"${item}\"")
81 LIST(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS})
83 SEPARATE_ARGUMENTS(${_out_compile_flags})
85 ENDMACRO(_PCH_GET_COMPILE_FLAGS)
88 MACRO(_PCH_WRITE_PCHDEP_CXX _targetName _include_file _dephelp)
90 SET(${_dephelp} ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch_dephelp.cxx)
93 OUTPUT "${${_dephelp}}"
94 COMMAND ${CMAKE_COMMAND} -E echo "#include \\\"${_include_file}\\\"" > "${${_dephelp}}"
95 COMMAND ${CMAKE_COMMAND} -E echo "int testfunction();" >> "${${_dephelp}}"
96 COMMAND ${CMAKE_COMMAND} -E echo "int testfunction()" >> "${${_dephelp}}"
97 COMMAND ${CMAKE_COMMAND} -E echo "{" >> "${${_dephelp}}"
98 COMMAND ${CMAKE_COMMAND} -E echo " return 0;" >> "${${_dephelp}}"
99 COMMAND ${CMAKE_COMMAND} -E echo "}" >> "${${_dephelp}}"
100 DEPENDS "${_include_file}"
104 OUTPUT "${${_dephelp}}"
105 COMMAND ${CMAKE_COMMAND} -E echo "\\#include \\\"${_include_file}\\\"" > "${${_dephelp}}"
106 COMMAND ${CMAKE_COMMAND} -E echo "int testfunction\\(\\)\\;" >> "${${_dephelp}}"
107 COMMAND ${CMAKE_COMMAND} -E echo "int testfunction\\(\\)" >> "${${_dephelp}}"
108 COMMAND ${CMAKE_COMMAND} -E echo "{" >> "${${_dephelp}}"
109 COMMAND ${CMAKE_COMMAND} -E echo " \\return 0\\;" >> "${${_dephelp}}"
110 COMMAND ${CMAKE_COMMAND} -E echo "}" >> "${${_dephelp}}"
111 DEPENDS "${_include_file}"
115 ENDMACRO(_PCH_WRITE_PCHDEP_CXX )
117 MACRO(_PCH_GET_COMPILE_COMMAND out_command _input _output)
119 FILE(TO_NATIVE_PATH ${_input} _native_input)
120 FILE(TO_NATIVE_PATH ${_output} _native_output)
122 IF(CMAKE_COMPILER_IS_GNUCXX)
123 IF(CMAKE_CXX_COMPILER_ARG1)
124 # remove leading space in compiler argument
125 STRING(REGEX REPLACE "^ +" "" pchsupport_compiler_cxx_arg1 ${CMAKE_CXX_COMPILER_ARG1})
128 ${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
130 ELSE(CMAKE_CXX_COMPILER_ARG1)
132 ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
134 ENDIF(CMAKE_CXX_COMPILER_ARG1)
135 ELSE(CMAKE_COMPILER_IS_GNUCXX)
137 SET(_dummy_str "#include <${_input}>")
138 FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pch_dummy.cpp ${_dummy_str})
141 ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} /c /Fp${_native_output} /Yc${_native_input} pch_dummy.cpp
145 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
147 ENDMACRO(_PCH_GET_COMPILE_COMMAND )
150 MACRO(_PCH_GET_TARGET_COMPILE_FLAGS _cflags _header_name _pch_path _dowarn )
152 FILE(TO_NATIVE_PATH ${_pch_path} _native_pch_path)
154 IF(CMAKE_COMPILER_IS_GNUCXX)
155 # for use with distcc and gcc >4.0.1 if preprocessed files are accessible
156 # on all remote machines set
157 # PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess
158 # if you want warnings for invalid header files (which is very inconvenient
159 # if you have different versions of the headers for different build types
160 # you may set _pch_dowarn
162 SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -Winvalid-pch " )
164 SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} " )
167 ELSE(CMAKE_COMPILER_IS_GNUCXX)
169 set(${_cflags} "/Fp${_native_pch_path} /Yu${_header_name}" )
171 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
173 ENDMACRO(_PCH_GET_TARGET_COMPILE_FLAGS )
176 MACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input _output)
178 GET_FILENAME_COMPONENT(_name ${_input} NAME)
179 GET_FILENAME_COMPONENT(_path ${_input} PATH)
180 SET(${_output} "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch/${_targetName}_${CMAKE_BUILD_TYPE}.gch")
182 ENDMACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input)
185 MACRO(ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use )
187 # to do: test whether compiler flags match between target _targetName
188 # and _pch_output_to_use
189 GET_FILENAME_COMPONENT(_name ${_input} NAME)
191 IF(ARGN STREQUAL "0")
197 _PCH_GET_TARGET_COMPILE_FLAGS(_target_cflags ${_name} ${_pch_output_to_use} ${_dowarn})
198 #MESSAGE("Add flags ${_target_cflags} to ${_targetName} " )
200 GET_TARGET_PROPERTY(_sources ${_targetName} SOURCES)
201 FOREACH(src ${_sources})
202 if(NOT "${src}" MATCHES "\\.mm$")
203 get_source_file_property(_flags "${src}" COMPILE_FLAGS)
205 set(_flags "${_flags} ${_target_cflags}")
207 set(_flags "${_target_cflags}")
210 set_source_files_properties("${src}" PROPERTIES COMPILE_FLAGS "${_flags}")
214 ADD_CUSTOM_TARGET(pch_Generate_${_targetName}
215 DEPENDS ${_pch_output_to_use}
218 ADD_DEPENDENCIES(${_targetName} pch_Generate_${_targetName} )
220 ENDMACRO(ADD_PRECOMPILED_HEADER_TO_TARGET)
222 MACRO(ADD_PRECOMPILED_HEADER _targetName _input)
224 SET(_PCH_current_target ${_targetName})
226 IF(NOT CMAKE_BUILD_TYPE)
228 "This is the ADD_PRECOMPILED_HEADER macro. "
229 "You must set CMAKE_BUILD_TYPE!"
233 IF(ARGN STREQUAL "0")
239 GET_FILENAME_COMPONENT(_name ${_input} NAME)
240 GET_FILENAME_COMPONENT(_path ${_input} PATH)
241 GET_PRECOMPILED_HEADER_OUTPUT( ${_targetName} ${_input} _output)
243 _PCH_WRITE_PCHDEP_CXX(${_targetName} "${_input}" _pch_dephelp_cxx)
245 ADD_LIBRARY(${_targetName}_pch_dephelp STATIC "${_pch_dephelp_cxx}" "${_input}" )
247 set_target_properties(${_targetName}_pch_dephelp PROPERTIES
248 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
249 ARCHIVE_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_PATH}"
252 _PCH_GET_COMPILE_FLAGS(_compile_FLAGS)
254 get_target_property(type ${_targetName} TYPE)
255 if(type STREQUAL "SHARED_LIBRARY")
256 get_target_property(__DEFINES ${_targetName} DEFINE_SYMBOL)
257 if(NOT __DEFINES MATCHES __DEFINES-NOTFOUND)
258 list(APPEND _compile_FLAGS "${_PCH_define_prefix}${__DEFINES}")
262 get_target_property(DIRINC ${_targetName} INCLUDE_DIRECTORIES)
263 set_target_properties(${_targetName}_pch_dephelp PROPERTIES INCLUDE_DIRECTORIES "${DIRINC}")
265 #MESSAGE("_compile_FLAGS: ${_compile_FLAGS}")
266 #message("COMMAND ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}")
269 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${_name}"
270 COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_input}" "${CMAKE_CURRENT_BINARY_DIR}/${_name}" # ensure same directory! Required by gcc
274 #message("_command ${_input} ${_output}")
275 _PCH_GET_COMPILE_COMMAND(_command ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_output} )
277 GET_FILENAME_COMPONENT(_outdir ${_output} PATH)
280 COMMAND ${CMAKE_COMMAND} -E make_directory "${_outdir}"
283 DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_name}"
284 DEPENDS ${_targetName}_pch_dephelp
287 ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_input} ${_output} ${_dowarn})
290 GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS)
291 if (oldProps MATCHES NOTFOUND)
294 SET(newProperties "${oldProps} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_name}\"")
295 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES COMPILE_FLAGS "${newProperties}")
297 ENDMACRO(ADD_PRECOMPILED_HEADER)
300 # Generates the use of precompiled in a target,
301 # without using depency targets (2 extra for each target)
302 # Using Visual, must also add ${_targetName}_pch to sources
303 # Not needed by Xcode
305 MACRO(GET_NATIVE_PRECOMPILED_HEADER _targetName _input)
307 if(CMAKE_GENERATOR MATCHES "^Visual.*$")
308 set(_dummy_str "#include \"${_input}\"\n")
310 set(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cpp)
311 if(EXISTS ${${_targetName}_pch})
312 # Check if contents is the same, if not rewrite
315 FILE(WRITE ${${_targetName}_pch} ${_dummy_str})
319 ENDMACRO(GET_NATIVE_PRECOMPILED_HEADER)
322 MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _input)
324 IF(ARGN STREQUAL "0")
330 if(CMAKE_GENERATOR MATCHES "^Visual.*$")
332 # Auto include the precompile (useful for moc processing, since the use of
333 # precompiled is specified at the target level
334 # and I don't want to specifiy /F- for each moc/res/ui generated files (using Qt)
336 GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS)
337 if (oldProps MATCHES NOTFOUND)
341 SET(newProperties "${oldProps} /Yu\"${_input}\" /FI\"${_input}\"")
342 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES COMPILE_FLAGS "${newProperties}")
344 #also inlude ${oldProps} to have the same compile options
345 SET_SOURCE_FILES_PROPERTIES(${${_targetName}_pch} PROPERTIES COMPILE_FLAGS "${oldProps} /Yc\"${_input}\"")
347 elseif (CMAKE_GENERATOR MATCHES Xcode)
349 # For Xcode, cmake needs my patch to process
350 # GCC_PREFIX_HEADER and GCC_PRECOMPILE_PREFIX_HEADER as target properties
352 # When buiding out of the tree, precompiled may not be located
353 # Use full path instead.
354 GET_FILENAME_COMPONENT(fullPath ${_input} ABSOLUTE)
356 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${fullPath}")
357 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
361 #Fallback to the "old" precompiled suppport
362 #ADD_PRECOMPILED_HEADER(${_targetName} ${_input} ${_dowarn})
366 ENDMACRO(ADD_NATIVE_PRECOMPILED_HEADER)
368 macro(ocv_add_precompiled_header_to_target the_target pch_header)
369 if(PCHSupport_FOUND AND ENABLE_PRECOMPILED_HEADERS AND EXISTS "${pch_header}")
370 if(CMAKE_GENERATOR MATCHES "^Visual" OR CMAKE_GENERATOR MATCHES Xcode)
371 add_native_precompiled_header(${the_target} ${pch_header})
372 elseif(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_GENERATOR MATCHES "Makefiles|Ninja")
373 add_precompiled_header(${the_target} ${pch_header})