1# taken from http://public.kitware.com/Bug/view.php?id=1260 and slightly adjusted
2
3# - Try to find precompiled headers support for GCC 3.4 and 4.x
4# Once done this will define:
5#
6# Variable:
7#   PCHSupport_FOUND
8#
9# Macro:
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
14
15IF(CMAKE_COMPILER_IS_GNUCXX)
16
17    EXEC_PROGRAM(
18        ${CMAKE_CXX_COMPILER}
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)
24    ENDIF()
25
26    SET(_PCH_include_prefix "-I")
27    SET(_PCH_isystem_prefix "-isystem")
28    SET(_PCH_define_prefix "-D")
29
30ELSEIF(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")
35ELSE()
36    SET(PCHSupport_FOUND FALSE)
37ENDIF()
38
39MACRO(_PCH_GET_COMPILE_FLAGS _out_compile_flags)
40
41    STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name)
42    SET(${_out_compile_flags} ${${_flags_var_name}} )
43
44    IF(CMAKE_COMPILER_IS_GNUCXX)
45
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")
49        ENDIF()
50
51        GET_TARGET_PROPERTY(_target_definitions ${_PCH_current_target} COMPILE_DEFINITIONS)
52        if(_target_definitions)
53          foreach(_def ${_target_definitions})
54            LIST(APPEND ${_out_compile_flags} "-D${_def}")
55          endforeach()
56        endif()
57
58    ELSE()
59        ## TODO ... ? or does it work out of the box
60    ENDIF()
61
62    GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES )
63    FOREACH(item ${DIRINC})
64        if(item MATCHES "^${OpenCV_SOURCE_DIR}/modules/")
65          LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}\"${item}\"")
66        else()
67          LIST(APPEND ${_out_compile_flags} "${_PCH_isystem_prefix}\"${item}\"")
68        endif()
69    ENDFOREACH(item)
70
71    get_target_property(DIRINC ${_PCH_current_target} INCLUDE_DIRECTORIES )
72    FOREACH(item ${DIRINC})
73        if(item MATCHES "^${OpenCV_SOURCE_DIR}/modules/")
74          LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}\"${item}\"")
75        else()
76          LIST(APPEND ${_out_compile_flags} "${_PCH_isystem_prefix}\"${item}\"")
77        endif()
78    ENDFOREACH(item)
79
80    GET_DIRECTORY_PROPERTY(_directory_flags DEFINITIONS)
81    GET_DIRECTORY_PROPERTY(_global_definitions DIRECTORY ${OpenCV_SOURCE_DIR} DEFINITIONS)
82    #MESSAGE("_directory_flags ${_directory_flags} ${_global_definitions}" )
83    LIST(APPEND ${_out_compile_flags} ${_directory_flags})
84    LIST(APPEND ${_out_compile_flags} ${_global_definitions})
85    LIST(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS})
86
87    SEPARATE_ARGUMENTS(${_out_compile_flags})
88
89ENDMACRO(_PCH_GET_COMPILE_FLAGS)
90
91
92MACRO(_PCH_WRITE_PCHDEP_CXX _targetName _include_file _dephelp)
93
94    SET(${_dephelp} ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch_dephelp.cxx)
95    IF(CMAKE_HOST_WIN32)
96        ADD_CUSTOM_COMMAND(
97          OUTPUT "${${_dephelp}}"
98          COMMAND ${CMAKE_COMMAND} -E echo "#include \\\"${_include_file}\\\"" >  "${${_dephelp}}"
99          COMMAND ${CMAKE_COMMAND} -E echo "int testfunction();"               >> "${${_dephelp}}"
100          COMMAND ${CMAKE_COMMAND} -E echo "int testfunction()"                >> "${${_dephelp}}"
101          COMMAND ${CMAKE_COMMAND} -E echo "{"                                 >> "${${_dephelp}}"
102          COMMAND ${CMAKE_COMMAND} -E echo "    return 0;"                     >> "${${_dephelp}}"
103          COMMAND ${CMAKE_COMMAND} -E echo "}"                                 >> "${${_dephelp}}"
104          DEPENDS "${_include_file}"
105          )
106    else()
107        ADD_CUSTOM_COMMAND(
108          OUTPUT "${${_dephelp}}"
109          COMMAND ${CMAKE_COMMAND} -E echo "\\#include \\\"${_include_file}\\\"" >  "${${_dephelp}}"
110          COMMAND ${CMAKE_COMMAND} -E echo "int testfunction\\(\\)\\;"         >> "${${_dephelp}}"
111          COMMAND ${CMAKE_COMMAND} -E echo "int testfunction\\(\\)"            >> "${${_dephelp}}"
112          COMMAND ${CMAKE_COMMAND} -E echo "{"                                 >> "${${_dephelp}}"
113          COMMAND ${CMAKE_COMMAND} -E echo "    \\return 0\\;"                 >> "${${_dephelp}}"
114          COMMAND ${CMAKE_COMMAND} -E echo "}"                                 >> "${${_dephelp}}"
115          DEPENDS "${_include_file}"
116          )
117    endif()
118
119ENDMACRO(_PCH_WRITE_PCHDEP_CXX )
120
121MACRO(_PCH_GET_COMPILE_COMMAND out_command _input _output)
122
123    FILE(TO_NATIVE_PATH ${_input} _native_input)
124    FILE(TO_NATIVE_PATH ${_output} _native_output)
125
126    IF(CMAKE_COMPILER_IS_GNUCXX)
127        IF(CMAKE_CXX_COMPILER_ARG1)
128            # remove leading space in compiler argument
129            STRING(REGEX REPLACE "^ +" "" pchsupport_compiler_cxx_arg1 ${CMAKE_CXX_COMPILER_ARG1})
130
131            SET(${out_command}
132              ${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
133              )
134        ELSE(CMAKE_CXX_COMPILER_ARG1)
135            SET(${out_command}
136              ${CMAKE_CXX_COMPILER}  ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
137              )
138        ENDIF(CMAKE_CXX_COMPILER_ARG1)
139    ELSE(CMAKE_COMPILER_IS_GNUCXX)
140
141        SET(_dummy_str "#include <${_input}>")
142        FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pch_dummy.cpp ${_dummy_str})
143
144        SET(${out_command}
145          ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} /c /Fp${_native_output} /Yc${_native_input} pch_dummy.cpp
146          )
147        #/out:${_output}
148
149    ENDIF(CMAKE_COMPILER_IS_GNUCXX)
150
151ENDMACRO(_PCH_GET_COMPILE_COMMAND )
152
153
154MACRO(_PCH_GET_TARGET_COMPILE_FLAGS _cflags  _header_name _pch_path _dowarn )
155
156    FILE(TO_NATIVE_PATH ${_pch_path} _native_pch_path)
157
158    IF(CMAKE_COMPILER_IS_GNUCXX)
159        # for use with distcc and gcc >4.0.1 if preprocessed files are accessible
160        # on all remote machines set
161        # PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess
162        # if you want warnings for invalid header files (which is very inconvenient
163        # if you have different versions of the headers for different build types
164        # you may set _pch_dowarn
165        IF (_dowarn)
166            SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_header_name}\" -Winvalid-pch " )
167        ELSE (_dowarn)
168            SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_header_name}\" " )
169        ENDIF (_dowarn)
170
171    ELSE(CMAKE_COMPILER_IS_GNUCXX)
172
173        set(${_cflags} "/Fp${_native_pch_path} /Yu${_header_name}" )
174
175    ENDIF(CMAKE_COMPILER_IS_GNUCXX)
176
177ENDMACRO(_PCH_GET_TARGET_COMPILE_FLAGS )
178
179
180MACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input _output)
181
182    GET_FILENAME_COMPONENT(_name ${_input} NAME)
183    GET_FILENAME_COMPONENT(_path ${_input} PATH)
184    SET(${_output} "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch/${_targetName}_${CMAKE_BUILD_TYPE}.gch")
185
186ENDMACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input)
187
188
189MACRO(ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use )
190
191    # to do: test whether compiler flags match between target  _targetName
192    # and _pch_output_to_use
193    GET_FILENAME_COMPONENT(_name ${_input} NAME)
194
195    IF(ARGN STREQUAL "0")
196        SET(_dowarn 0)
197    ELSE()
198        SET(_dowarn 1)
199    ENDIF()
200
201    _PCH_GET_TARGET_COMPILE_FLAGS(_target_cflags ${_name} ${_pch_output_to_use} ${_dowarn})
202    #MESSAGE("Add flags ${_target_cflags} to ${_targetName} " )
203
204    GET_TARGET_PROPERTY(_sources ${_targetName} SOURCES)
205    FOREACH(src ${_sources})
206      if(NOT "${src}" MATCHES "\\.mm$")
207        get_source_file_property(_flags "${src}" COMPILE_FLAGS)
208        if(_flags)
209          set(_flags "${_flags} ${_target_cflags}")
210        else()
211          set(_flags "${_target_cflags}")
212        endif()
213
214        set_source_files_properties("${src}" PROPERTIES COMPILE_FLAGS "${_flags}")
215      endif()
216    ENDFOREACH()
217
218    ADD_CUSTOM_TARGET(pch_Generate_${_targetName}
219      DEPENDS ${_pch_output_to_use}
220      )
221
222    ADD_DEPENDENCIES(${_targetName} pch_Generate_${_targetName} )
223
224ENDMACRO(ADD_PRECOMPILED_HEADER_TO_TARGET)
225
226MACRO(ADD_PRECOMPILED_HEADER _targetName _input)
227
228    SET(_PCH_current_target ${_targetName})
229
230    IF(NOT CMAKE_BUILD_TYPE)
231        MESSAGE(FATAL_ERROR
232          "This is the ADD_PRECOMPILED_HEADER macro. "
233          "You must set CMAKE_BUILD_TYPE!"
234          )
235    ENDIF()
236
237    IF(ARGN STREQUAL "0")
238        SET(_dowarn 0)
239    ELSE()
240        SET(_dowarn 1)
241    ENDIF()
242
243    GET_FILENAME_COMPONENT(_name ${_input} NAME)
244    GET_FILENAME_COMPONENT(_path ${_input} PATH)
245    GET_PRECOMPILED_HEADER_OUTPUT( ${_targetName} ${_input} _output)
246
247    _PCH_WRITE_PCHDEP_CXX(${_targetName} "${_input}" _pch_dephelp_cxx)
248
249    ADD_LIBRARY(${_targetName}_pch_dephelp STATIC "${_pch_dephelp_cxx}" "${_input}" )
250
251    set_target_properties(${_targetName}_pch_dephelp PROPERTIES
252      DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
253      ARCHIVE_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_PATH}"
254      )
255
256    _PCH_GET_COMPILE_FLAGS(_compile_FLAGS)
257
258    get_target_property(type ${_targetName} TYPE)
259    if(type STREQUAL "SHARED_LIBRARY")
260        get_target_property(__DEFINES ${_targetName} DEFINE_SYMBOL)
261        if(NOT __DEFINES MATCHES __DEFINES-NOTFOUND)
262            list(APPEND _compile_FLAGS "${_PCH_define_prefix}${__DEFINES}")
263        endif()
264    endif()
265
266    get_target_property(DIRINC ${_targetName} INCLUDE_DIRECTORIES)
267    set_target_properties(${_targetName}_pch_dephelp PROPERTIES INCLUDE_DIRECTORIES "${DIRINC}")
268
269    #MESSAGE("_compile_FLAGS: ${_compile_FLAGS}")
270    #message("COMMAND ${CMAKE_CXX_COMPILER}	${_compile_FLAGS} -x c++-header -o ${_output} ${_input}")
271
272    ADD_CUSTOM_COMMAND(
273      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${_name}"
274      COMMAND ${CMAKE_COMMAND} -E copy  "${_input}" "${CMAKE_CURRENT_BINARY_DIR}/${_name}" # ensure same directory! Required by gcc
275      DEPENDS "${_input}"
276      )
277
278    #message("_command  ${_input} ${_output}")
279    _PCH_GET_COMPILE_COMMAND(_command  ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_output} )
280
281    GET_FILENAME_COMPONENT(_outdir ${_output} PATH)
282    ADD_CUSTOM_COMMAND(
283      OUTPUT "${_output}"
284      COMMAND ${CMAKE_COMMAND} -E make_directory "${_outdir}"
285      COMMAND ${_command}
286      DEPENDS "${_input}"
287      DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_name}"
288      DEPENDS ${_targetName}_pch_dephelp
289      )
290
291    ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_input}  ${_output} ${_dowarn})
292
293ENDMACRO(ADD_PRECOMPILED_HEADER)
294
295
296# Generates the use of precompiled in a target,
297# without using depency targets (2 extra for each target)
298# Using Visual, must also add ${_targetName}_pch to sources
299# Not needed by Xcode
300
301MACRO(GET_NATIVE_PRECOMPILED_HEADER _targetName _input)
302
303    if(CMAKE_GENERATOR MATCHES "^Visual.*$")
304        set(_dummy_str "#include \"${_input}\"\n")
305
306        set(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cpp)
307        if(EXISTS ${${_targetName}_pch})
308            # Check if contents is the same, if not rewrite
309            # todo
310        else()
311            FILE(WRITE ${${_targetName}_pch} ${_dummy_str})
312        endif()
313    endif()
314
315ENDMACRO(GET_NATIVE_PRECOMPILED_HEADER)
316
317
318MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _input)
319
320    IF(ARGN STREQUAL "0")
321        SET(_dowarn 0)
322    ELSE()
323        SET(_dowarn 1)
324    ENDIF()
325
326    if(CMAKE_GENERATOR MATCHES "^Visual.*$")
327
328        # Auto include the precompile (useful for moc processing, since the use of
329        # precompiled is specified at the target level
330        # and I don't want to specifiy /F- for each moc/res/ui generated files (using Qt)
331
332        GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS)
333        if (oldProps MATCHES NOTFOUND)
334            SET(oldProps "")
335        endif()
336
337        SET(newProperties "${oldProps} /Yu\"${_input}\" /FI\"${_input}\"")
338        SET_TARGET_PROPERTIES(${_targetName} PROPERTIES COMPILE_FLAGS "${newProperties}")
339
340        #also inlude ${oldProps} to have the same compile options
341        SET_SOURCE_FILES_PROPERTIES(${${_targetName}_pch} PROPERTIES COMPILE_FLAGS "${oldProps} /Yc\"${_input}\"")
342
343    elseif (CMAKE_GENERATOR MATCHES Xcode)
344
345        # For Xcode, cmake needs my patch to process
346        # GCC_PREFIX_HEADER and GCC_PRECOMPILE_PREFIX_HEADER as target properties
347
348        # When buiding out of the tree, precompiled may not be located
349        # Use full path instead.
350        GET_FILENAME_COMPONENT(fullPath ${_input} ABSOLUTE)
351
352        SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${fullPath}")
353        SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
354
355    else()
356
357        #Fallback to the "old" precompiled suppport
358        #ADD_PRECOMPILED_HEADER(${_targetName} ${_input} ${_dowarn})
359
360    endif()
361
362ENDMACRO(ADD_NATIVE_PRECOMPILED_HEADER)
363
364macro(ocv_add_precompiled_header_to_target the_target pch_header)
365  if(PCHSupport_FOUND AND ENABLE_PRECOMPILED_HEADERS AND EXISTS "${pch_header}")
366    if(CMAKE_GENERATOR MATCHES "^Visual" OR CMAKE_GENERATOR MATCHES Xcode)
367      add_native_precompiled_header(${the_target} ${pch_header})
368    elseif(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_GENERATOR MATCHES "Makefiles|Ninja")
369      add_precompiled_header(${the_target} ${pch_header})
370    endif()
371  endif()
372endmacro()
373