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)
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 VERSION_GREATER "4.2.-1")
23 SET(PCHSupport_FOUND TRUE)
26 SET(_PCH_include_prefix "-I")
27 SET(_PCH_isystem_prefix "-isystem")
28 SET(_PCH_define_prefix "-D")
30 ELSEIF(CMAKE_GENERATOR MATCHES "^Visual.*$")
31 SET(PCHSupport_FOUND TRUE)
32 SET(_PCH_include_prefix "/I")
33 SET(_PCH_isystem_prefix "/I")
34 SET(_PCH_define_prefix "/D")
36 SET(PCHSupport_FOUND FALSE)
39 MACRO(_PCH_GET_COMPILE_FLAGS _out_compile_flags)
41 STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name)
42 SET(${_out_compile_flags} ${${_flags_var_name}} )
44 IF(CMAKE_COMPILER_IS_GNUCXX)
46 GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
47 IF(${_targetType} STREQUAL SHARED_LIBRARY AND NOT WIN32)
48 LIST(APPEND ${_out_compile_flags} "-fPIC")
52 ## TODO ... ? or does it work out of the box
55 GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES )
56 FOREACH(item ${DIRINC})
57 if(item MATCHES "^${OpenCV_SOURCE_DIR}/modules/")
58 LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}\"${item}\"")
60 LIST(APPEND ${_out_compile_flags} "${_PCH_isystem_prefix}\"${item}\"")
64 GET_DIRECTORY_PROPERTY(_directory_flags DEFINITIONS)
65 GET_DIRECTORY_PROPERTY(_global_definitions DIRECTORY ${OpenCV_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)
81 OUTPUT "${${_dephelp}}"
82 COMMAND ${CMAKE_COMMAND} -E echo "#include \\\"${_include_file}\\\"" > "${${_dephelp}}"
83 COMMAND ${CMAKE_COMMAND} -E echo "int testfunction();" >> "${${_dephelp}}"
84 COMMAND ${CMAKE_COMMAND} -E echo "int testfunction()" >> "${${_dephelp}}"
85 COMMAND ${CMAKE_COMMAND} -E echo "{" >> "${${_dephelp}}"
86 COMMAND ${CMAKE_COMMAND} -E echo " return 0;" >> "${${_dephelp}}"
87 COMMAND ${CMAKE_COMMAND} -E echo "}" >> "${${_dephelp}}"
88 DEPENDS "${_include_file}"
92 OUTPUT "${${_dephelp}}"
93 COMMAND ${CMAKE_COMMAND} -E echo "\\#include \\\"${_include_file}\\\"" > "${${_dephelp}}"
94 COMMAND ${CMAKE_COMMAND} -E echo "int testfunction\\(\\)\\;" >> "${${_dephelp}}"
95 COMMAND ${CMAKE_COMMAND} -E echo "int testfunction\\(\\)" >> "${${_dephelp}}"
96 COMMAND ${CMAKE_COMMAND} -E echo "{" >> "${${_dephelp}}"
97 COMMAND ${CMAKE_COMMAND} -E echo " \\return 0\\;" >> "${${_dephelp}}"
98 COMMAND ${CMAKE_COMMAND} -E echo "}" >> "${${_dephelp}}"
99 DEPENDS "${_include_file}"
103 ENDMACRO(_PCH_WRITE_PCHDEP_CXX )
105 MACRO(_PCH_GET_COMPILE_COMMAND out_command _input _output)
107 FILE(TO_NATIVE_PATH ${_input} _native_input)
108 FILE(TO_NATIVE_PATH ${_output} _native_output)
110 IF(CMAKE_COMPILER_IS_GNUCXX)
111 IF(CMAKE_CXX_COMPILER_ARG1)
112 # remove leading space in compiler argument
113 STRING(REGEX REPLACE "^ +" "" pchsupport_compiler_cxx_arg1 ${CMAKE_CXX_COMPILER_ARG1})
116 ${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
118 ELSE(CMAKE_CXX_COMPILER_ARG1)
120 ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
122 ENDIF(CMAKE_CXX_COMPILER_ARG1)
123 ELSE(CMAKE_COMPILER_IS_GNUCXX)
125 SET(_dummy_str "#include <${_input}>")
126 FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pch_dummy.cpp ${_dummy_str})
129 ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} /c /Fp${_native_output} /Yc${_native_input} pch_dummy.cpp
133 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
135 ENDMACRO(_PCH_GET_COMPILE_COMMAND )
138 MACRO(_PCH_GET_TARGET_COMPILE_FLAGS _cflags _header_name _pch_path _dowarn )
140 FILE(TO_NATIVE_PATH ${_pch_path} _native_pch_path)
142 IF(CMAKE_COMPILER_IS_GNUCXX)
143 # for use with distcc and gcc >4.0.1 if preprocessed files are accessible
144 # on all remote machines set
145 # PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess
146 # if you want warnings for invalid header files (which is very inconvenient
147 # if you have different versions of the headers for different build types
148 # you may set _pch_dowarn
150 SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_header_name}\" -Winvalid-pch " )
152 SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_header_name}\" " )
155 ELSE(CMAKE_COMPILER_IS_GNUCXX)
157 set(${_cflags} "/Fp${_native_pch_path} /Yu${_header_name}" )
159 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
161 ENDMACRO(_PCH_GET_TARGET_COMPILE_FLAGS )
164 MACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input _output)
166 GET_FILENAME_COMPONENT(_name ${_input} NAME)
167 GET_FILENAME_COMPONENT(_path ${_input} PATH)
168 SET(${_output} "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch/${_targetName}_${CMAKE_BUILD_TYPE}.gch")
170 ENDMACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input)
173 MACRO(ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use )
175 # to do: test whether compiler flags match between target _targetName
176 # and _pch_output_to_use
177 GET_FILENAME_COMPONENT(_name ${_input} NAME)
179 IF(ARGN STREQUAL "0")
185 _PCH_GET_TARGET_COMPILE_FLAGS(_target_cflags ${_name} ${_pch_output_to_use} ${_dowarn})
186 #MESSAGE("Add flags ${_target_cflags} to ${_targetName} " )
188 GET_TARGET_PROPERTY(_sources ${_targetName} SOURCES)
189 FOREACH(src ${_sources})
190 if(NOT "${src}" MATCHES "\\.mm$")
191 get_source_file_property(_flags "${src}" COMPILE_FLAGS)
193 set(_flags "${_flags} ${_target_cflags}")
195 set(_flags "${_target_cflags}")
198 set_source_files_properties("${src}" PROPERTIES COMPILE_FLAGS "${_flags}")
202 ADD_CUSTOM_TARGET(pch_Generate_${_targetName}
203 DEPENDS ${_pch_output_to_use}
206 ADD_DEPENDENCIES(${_targetName} pch_Generate_${_targetName} )
208 ENDMACRO(ADD_PRECOMPILED_HEADER_TO_TARGET)
210 MACRO(ADD_PRECOMPILED_HEADER _targetName _input)
212 SET(_PCH_current_target ${_targetName})
214 IF(NOT CMAKE_BUILD_TYPE)
216 "This is the ADD_PRECOMPILED_HEADER macro. "
217 "You must set CMAKE_BUILD_TYPE!"
221 IF(ARGN STREQUAL "0")
227 GET_FILENAME_COMPONENT(_name ${_input} NAME)
228 GET_FILENAME_COMPONENT(_path ${_input} PATH)
229 GET_PRECOMPILED_HEADER_OUTPUT( ${_targetName} ${_input} _output)
231 _PCH_WRITE_PCHDEP_CXX(${_targetName} "${_input}" _pch_dephelp_cxx)
233 ADD_LIBRARY(${_targetName}_pch_dephelp STATIC "${_pch_dephelp_cxx}" "${_input}" )
235 set_target_properties(${_targetName}_pch_dephelp PROPERTIES
236 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
237 ARCHIVE_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_PATH}"
240 _PCH_GET_COMPILE_FLAGS(_compile_FLAGS)
242 get_target_property(type ${_targetName} TYPE)
243 if(type STREQUAL "SHARED_LIBRARY")
244 get_target_property(__DEFINES ${_targetName} DEFINE_SYMBOL)
245 if(NOT __DEFINES MATCHES __DEFINES-NOTFOUND)
246 list(APPEND _compile_FLAGS "${_PCH_define_prefix}${__DEFINES}")
250 #MESSAGE("_compile_FLAGS: ${_compile_FLAGS}")
251 #message("COMMAND ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}")
254 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${_name}"
255 COMMAND ${CMAKE_COMMAND} -E copy "${_input}" "${CMAKE_CURRENT_BINARY_DIR}/${_name}" # ensure same directory! Required by gcc
259 #message("_command ${_input} ${_output}")
260 _PCH_GET_COMPILE_COMMAND(_command ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_output} )
262 GET_FILENAME_COMPONENT(_outdir ${_output} PATH)
265 COMMAND ${CMAKE_COMMAND} -E make_directory "${_outdir}"
268 DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_name}"
269 DEPENDS ${_targetName}_pch_dephelp
272 ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_input} ${_output} ${_dowarn})
274 ENDMACRO(ADD_PRECOMPILED_HEADER)
277 # Generates the use of precompiled in a target,
278 # without using depency targets (2 extra for each target)
279 # Using Visual, must also add ${_targetName}_pch to sources
280 # Not needed by Xcode
282 MACRO(GET_NATIVE_PRECOMPILED_HEADER _targetName _input)
284 if(CMAKE_GENERATOR MATCHES "^Visual.*$")
285 set(_dummy_str "#include \"${_input}\"\n")
287 set(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cpp)
288 if(EXISTS ${${_targetName}_pch})
289 # Check if contents is the same, if not rewrite
292 FILE(WRITE ${${_targetName}_pch} ${_dummy_str})
296 ENDMACRO(GET_NATIVE_PRECOMPILED_HEADER)
299 MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _input)
301 IF(ARGN STREQUAL "0")
307 if(CMAKE_GENERATOR MATCHES "^Visual.*$")
309 # Auto include the precompile (useful for moc processing, since the use of
310 # precompiled is specified at the target level
311 # and I don't want to specifiy /F- for each moc/res/ui generated files (using Qt)
313 GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS)
314 if (oldProps MATCHES NOTFOUND)
318 SET(newProperties "${oldProps} /Yu\"${_input}\" /FI\"${_input}\"")
319 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES COMPILE_FLAGS "${newProperties}")
321 #also inlude ${oldProps} to have the same compile options
322 SET_SOURCE_FILES_PROPERTIES(${${_targetName}_pch} PROPERTIES COMPILE_FLAGS "${oldProps} /Yc\"${_input}\"")
324 elseif (CMAKE_GENERATOR MATCHES Xcode)
326 # For Xcode, cmake needs my patch to process
327 # GCC_PREFIX_HEADER and GCC_PRECOMPILE_PREFIX_HEADER as target properties
329 # When buiding out of the tree, precompiled may not be located
330 # Use full path instead.
331 GET_FILENAME_COMPONENT(fullPath ${_input} ABSOLUTE)
333 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${fullPath}")
334 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
338 #Fallback to the "old" precompiled suppport
339 #ADD_PRECOMPILED_HEADER(${_targetName} ${_input} ${_dowarn})
343 ENDMACRO(ADD_NATIVE_PRECOMPILED_HEADER)
345 macro(ocv_add_precompiled_header_to_target the_target pch_header)
346 if(PCHSupport_FOUND AND ENABLE_PRECOMPILED_HEADERS AND EXISTS "${pch_header}")
347 if(CMAKE_GENERATOR MATCHES "^Visual" OR CMAKE_GENERATOR MATCHES Xcode)
348 add_native_precompiled_header(${the_target} ${pch_header})
349 elseif(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_GENERATOR MATCHES "Makefiles|Ninja")
350 add_precompiled_header(${the_target} ${pch_header})