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 ocv_is_opencv_directory(__result ${item})
67 LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}\"${item}\"")
68 elseif(CMAKE_COMPILER_IS_GNUCXX AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0" AND
69 item MATCHES "/usr/include$")
70 # workaround for GCC 6.x bug
72 LIST(APPEND ${_out_compile_flags} "${_PCH_isystem_prefix}\"${item}\"")
76 get_target_property(DIRINC ${_PCH_current_target} INCLUDE_DIRECTORIES )
77 FOREACH(item ${DIRINC})
78 ocv_is_opencv_directory(__result ${item})
80 LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}\"${item}\"")
81 elseif(CMAKE_COMPILER_IS_GNUCXX AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0" AND
82 item MATCHES "/usr/include$")
83 # workaround for GCC 6.x bug
85 LIST(APPEND ${_out_compile_flags} "${_PCH_isystem_prefix}\"${item}\"")
89 LIST(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS})
91 SEPARATE_ARGUMENTS(${_out_compile_flags})
93 ENDMACRO(_PCH_GET_COMPILE_FLAGS)
96 MACRO(_PCH_WRITE_PCHDEP_CXX _targetName _include_file _dephelp)
98 set(${_dephelp} "${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch_dephelp.cxx")
100 if(EXISTS "${${_dephelp}}")
101 file(READ "${${_dephelp}}" _content)
104 "#include \"${_include_file}\"
111 if(NOT _content STREQUAL _dummy_str)
112 file(WRITE "${${_dephelp}}" "${_dummy_str}")
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} " )
199 if(CMAKE_COMPILER_IS_GNUCXX)
200 set(_target_cflags "${_target_cflags} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_name}\"")
203 GET_TARGET_PROPERTY(_sources ${_targetName} SOURCES)
204 FOREACH(src ${_sources})
205 if(NOT "${src}" MATCHES "\\.mm$")
206 get_source_file_property(_flags "${src}" COMPILE_FLAGS)
207 get_source_file_property(_flags2 "${src}" COMPILE_DEFINITIONS)
208 if(NOT _flags AND NOT _flags2)
209 set_source_files_properties("${src}" PROPERTIES COMPILE_FLAGS "${_target_cflags}")
211 #ocv_debug_message("Skip PCH, flags: ${oldProps} defines: ${oldProps2}, file: ${src}")
216 ADD_CUSTOM_TARGET(pch_Generate_${_targetName}
217 DEPENDS ${_pch_output_to_use}
220 ADD_DEPENDENCIES(${_targetName} pch_Generate_${_targetName} )
222 ENDMACRO(ADD_PRECOMPILED_HEADER_TO_TARGET)
224 MACRO(ADD_PRECOMPILED_HEADER _targetName _input)
226 SET(_PCH_current_target ${_targetName})
228 IF(NOT CMAKE_BUILD_TYPE)
230 "This is the ADD_PRECOMPILED_HEADER macro. "
231 "You must set CMAKE_BUILD_TYPE!"
235 IF(ARGN STREQUAL "0")
241 GET_FILENAME_COMPONENT(_name ${_input} NAME)
242 GET_FILENAME_COMPONENT(_path ${_input} PATH)
243 GET_PRECOMPILED_HEADER_OUTPUT( ${_targetName} ${_input} _output)
245 _PCH_WRITE_PCHDEP_CXX(${_targetName} "${_input}" _pch_dephelp_cxx)
247 ADD_LIBRARY(${_targetName}_pch_dephelp STATIC "${_pch_dephelp_cxx}" "${_input}" )
249 set_target_properties(${_targetName}_pch_dephelp PROPERTIES
250 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
251 ARCHIVE_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_PATH}"
254 _PCH_GET_COMPILE_FLAGS(_compile_FLAGS)
256 get_target_property(type ${_targetName} TYPE)
257 if(type STREQUAL "SHARED_LIBRARY")
258 get_target_property(__DEFINES ${_targetName} DEFINE_SYMBOL)
259 if(NOT __DEFINES MATCHES __DEFINES-NOTFOUND)
260 list(APPEND _compile_FLAGS "${_PCH_define_prefix}${__DEFINES}")
264 get_target_property(DIRINC ${_targetName} INCLUDE_DIRECTORIES)
265 set_target_properties(${_targetName}_pch_dephelp PROPERTIES INCLUDE_DIRECTORIES "${DIRINC}")
267 #MESSAGE("_compile_FLAGS: ${_compile_FLAGS}")
268 #message("COMMAND ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}")
271 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${_name}"
272 COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_input}" "${CMAKE_CURRENT_BINARY_DIR}/${_name}" # ensure same directory! Required by gcc
276 #message("_command ${_input} ${_output}")
277 _PCH_GET_COMPILE_COMMAND(_command ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_output} )
279 GET_FILENAME_COMPONENT(_outdir ${_output} PATH)
282 COMMAND ${CMAKE_COMMAND} -E make_directory "${_outdir}"
285 DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_name}"
286 DEPENDS ${_targetName}_pch_dephelp
289 ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_input} ${_output} ${_dowarn})
291 ENDMACRO(ADD_PRECOMPILED_HEADER)
294 # Generates the use of precompiled in a target,
295 # without using depency targets (2 extra for each target)
296 # Using Visual, must also add ${_targetName}_pch to sources
297 # Not needed by Xcode
299 MACRO(GET_NATIVE_PRECOMPILED_HEADER _targetName _input)
301 if(ENABLE_PRECOMPILED_HEADERS)
302 if(CMAKE_GENERATOR MATCHES "^Visual.*$")
303 set(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cpp)
307 ENDMACRO(GET_NATIVE_PRECOMPILED_HEADER)
310 MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _input)
312 IF(ARGN STREQUAL "0")
318 if(CMAKE_GENERATOR MATCHES "^Visual.*$")
320 # Auto include the precompile (useful for moc processing, since the use of
321 # precompiled is specified at the target level
322 # and I don't want to specifiy /F- for each moc/res/ui generated files (using Qt)
324 get_target_property(_sources ${_targetName} SOURCES)
325 foreach(src ${_sources})
326 if(NOT "${src}" MATCHES "\\.mm$"
327 AND NOT "${src}" MATCHES "\\.h$" AND NOT "${src}" MATCHES "\\.hpp$" # header files
328 AND NOT "${src}" MATCHES "^\$" # CMake generator expressions
330 get_source_file_property(oldProps "${src}" COMPILE_FLAGS)
331 get_source_file_property(oldProps2 "${src}" COMPILE_DEFINITIONS)
332 if(NOT oldProps AND NOT oldProps2)
333 set(newProperties "/Yu\"${_input}\" /FI\"${_input}\"")
334 set_source_files_properties("${src}" PROPERTIES COMPILE_FLAGS "${newProperties}")
336 ocv_debug_message("Skip PCH, flags: ${oldProps} defines: ${oldProps2}, file: ${src}")
341 #also inlude ${oldProps} to have the same compile options
342 GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS)
343 if (oldProps MATCHES NOTFOUND)
346 SET_SOURCE_FILES_PROPERTIES(${${_targetName}_pch} PROPERTIES COMPILE_FLAGS "${oldProps} /Yc\"${_input}\"")
348 set(_dummy_str "#include \"${_input}\"\n")
349 set(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cpp)
350 if(EXISTS ${${_targetName}_pch})
351 file(READ "${${_targetName}_pch}" _contents)
353 if(NOT _dummy_str STREQUAL "${_contents}")
354 file(WRITE ${${_targetName}_pch} ${_dummy_str})
357 elseif (CMAKE_GENERATOR MATCHES Xcode)
359 # For Xcode, cmake needs my patch to process
360 # GCC_PREFIX_HEADER and GCC_PRECOMPILE_PREFIX_HEADER as target properties
362 # When buiding out of the tree, precompiled may not be located
363 # Use full path instead.
364 GET_FILENAME_COMPONENT(fullPath ${_input} ABSOLUTE)
366 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${fullPath}")
367 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
371 #Fallback to the "old" precompiled suppport
372 #ADD_PRECOMPILED_HEADER(${_targetName} ${_input} ${_dowarn})
376 ENDMACRO(ADD_NATIVE_PRECOMPILED_HEADER)
378 macro(ocv_add_precompiled_header_to_target the_target pch_header)
379 if(PCHSupport_FOUND AND ENABLE_PRECOMPILED_HEADERS AND EXISTS "${pch_header}")
380 if(CMAKE_GENERATOR MATCHES "^Visual" OR CMAKE_GENERATOR MATCHES Xcode)
381 add_native_precompiled_header(${the_target} ${pch_header})
382 elseif(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_GENERATOR MATCHES "Makefiles|Ninja")
383 add_precompiled_header(${the_target} ${pch_header})