Update to 2.0.0 tree from current Fremantle build
[opencv] / OpenCVPCHSupport.cmake
1 # taken from http://www.vtk.org/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
15 IF(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 MATCHES "4\\.[0-9]\\.[0-9]")
23         SET(PCHSupport_FOUND TRUE)
24     ELSE(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
25         IF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
26             SET(PCHSupport_FOUND TRUE)
27         ENDIF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
28     ENDIF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
29
30         SET(_PCH_include_prefix "-I")
31
32 ELSE(CMAKE_COMPILER_IS_GNUCXX)
33         IF(WIN32)
34                 SET(PCHSupport_FOUND TRUE) # for experimental msvc support
35                 SET(_PCH_include_prefix "/I")
36         ELSE(WIN32)
37                 SET(PCHSupport_FOUND FALSE)
38         ENDIF(WIN32)
39 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
40
41
42 MACRO(_PCH_GET_COMPILE_FLAGS _out_compile_flags)
43
44
45   STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name)
46   SET(${_out_compile_flags} ${${_flags_var_name}} )
47
48   IF(CMAKE_COMPILER_IS_GNUCXX)
49
50     GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
51     IF(${_targetType} STREQUAL SHARED_LIBRARY AND NOT WIN32)
52         LIST(APPEND ${_out_compile_flags} "${${_out_compile_flags}} -fPIC")
53     ENDIF()
54
55   ELSE(CMAKE_COMPILER_IS_GNUCXX)
56     ## TODO ... ? or does it work out of the box
57   ENDIF(CMAKE_COMPILER_IS_GNUCXX)
58
59   GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES )
60   FOREACH(item ${DIRINC})
61     LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}${item}")
62   ENDFOREACH(item)
63
64   GET_DIRECTORY_PROPERTY(_directory_flags DEFINITIONS)
65   GET_DIRECTORY_PROPERTY(_global_definitions DIRECTORY ${CMAKE_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} )
70
71   SEPARATE_ARGUMENTS(${_out_compile_flags})
72
73 ENDMACRO(_PCH_GET_COMPILE_FLAGS)
74
75
76 MACRO(_PCH_WRITE_PCHDEP_CXX _targetName _include_file _dephelp)
77
78   SET(${_dephelp} ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch_dephelp.cxx)
79   FILE(WRITE  ${${_dephelp}}
80 "#include \"${_include_file}\"
81 int testfunction()
82 {
83     return 0;
84 }
85 "
86     )
87
88 ENDMACRO(_PCH_WRITE_PCHDEP_CXX )
89
90 MACRO(_PCH_GET_COMPILE_COMMAND out_command _input _output)
91
92         FILE(TO_NATIVE_PATH ${_input} _native_input)
93         FILE(TO_NATIVE_PATH ${_output} _native_output)
94
95
96         IF(CMAKE_COMPILER_IS_GNUCXX)
97           IF(CMAKE_CXX_COMPILER_ARG1)
98             # remove leading space in compiler argument
99             STRING(REGEX REPLACE "^ +" "" pchsupport_compiler_cxx_arg1 ${CMAKE_CXX_COMPILER_ARG1})
100
101             SET(${out_command}
102               ${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS}   -x c++-header -o ${_output} ${_input}
103               )
104           ELSE(CMAKE_CXX_COMPILER_ARG1)
105             SET(${out_command}
106               ${CMAKE_CXX_COMPILER}  ${_compile_FLAGS}  -x c++-header -o ${_output} ${_input}
107               )
108           ENDIF(CMAKE_CXX_COMPILER_ARG1)
109         ELSE(CMAKE_COMPILER_IS_GNUCXX)
110
111                 SET(_dummy_str "#include <${_input}>")
112                 FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pch_dummy.cpp ${_dummy_str})
113
114                 SET(${out_command}
115                         ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} /c /Fp${_native_output} /Yc${_native_input} pch_dummy.cpp
116                 )
117                 #/out:${_output}
118
119         ENDIF(CMAKE_COMPILER_IS_GNUCXX)
120
121 ENDMACRO(_PCH_GET_COMPILE_COMMAND )
122
123
124
125 MACRO(_PCH_GET_TARGET_COMPILE_FLAGS _cflags  _header_name _pch_path _dowarn )
126
127   FILE(TO_NATIVE_PATH ${_pch_path} _native_pch_path)
128
129   IF(CMAKE_COMPILER_IS_GNUCXX)
130     # for use with distcc and gcc >4.0.1 if preprocessed files are accessible
131     # on all remote machines set
132     # PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess
133     # if you want warnings for invalid header files (which is very inconvenient
134     # if you have different versions of the headers for different build types
135     # you may set _pch_dowarn
136     IF (_dowarn)
137       SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include ${CMAKE_CURRENT_BINARY_DIR}/${_header_name} -Winvalid-pch " )
138     ELSE (_dowarn)
139       SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include ${CMAKE_CURRENT_BINARY_DIR}/${_header_name} " )
140     ENDIF (_dowarn)
141   ELSE(CMAKE_COMPILER_IS_GNUCXX)
142
143     set(${_cflags} "/Fp${_native_pch_path} /Yu${_header_name}" )
144
145   ENDIF(CMAKE_COMPILER_IS_GNUCXX)
146
147 ENDMACRO(_PCH_GET_TARGET_COMPILE_FLAGS )
148
149 MACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input _output)
150   GET_FILENAME_COMPONENT(_name ${_input} NAME)
151   GET_FILENAME_COMPONENT(_path ${_input} PATH)
152   SET(_output "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch/${_targetName}_${CMAKE_BUILD_TYPE}.gch")
153 ENDMACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input)
154
155
156 MACRO(ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use )
157
158   # to do: test whether compiler flags match between target  _targetName
159   # and _pch_output_to_use
160   GET_FILENAME_COMPONENT(_name ${_input} NAME)
161
162   IF( "${ARGN}" STREQUAL "0")
163     SET(_dowarn 0)
164   ELSE( "${ARGN}" STREQUAL "0")
165     SET(_dowarn 1)
166   ENDIF("${ARGN}" STREQUAL "0")
167
168
169   _PCH_GET_TARGET_COMPILE_FLAGS(_target_cflags ${_name} ${_pch_output_to_use} ${_dowarn})
170   #   MESSAGE("Add flags ${_target_cflags} to ${_targetName} " )
171   SET_TARGET_PROPERTIES(${_targetName}
172     PROPERTIES
173     COMPILE_FLAGS ${_target_cflags}
174     )
175
176   ADD_CUSTOM_TARGET(pch_Generate_${_targetName}
177     DEPENDS     ${_pch_output_to_use}
178     )
179
180   ADD_DEPENDENCIES(${_targetName} pch_Generate_${_targetName} )
181
182 ENDMACRO(ADD_PRECOMPILED_HEADER_TO_TARGET)
183
184 MACRO(ADD_PRECOMPILED_HEADER _targetName _input)
185
186   SET(_PCH_current_target ${_targetName})
187
188   IF(NOT CMAKE_BUILD_TYPE)
189     MESSAGE(FATAL_ERROR
190       "This is the ADD_PRECOMPILED_HEADER macro. "
191       "You must set CMAKE_BUILD_TYPE!"
192       )
193   ENDIF(NOT CMAKE_BUILD_TYPE)
194
195   IF( "${ARGN}" STREQUAL "0")
196     SET(_dowarn 0)
197   ELSE( "${ARGN}" STREQUAL "0")
198     SET(_dowarn 1)
199   ENDIF("${ARGN}" STREQUAL "0")
200
201
202   GET_FILENAME_COMPONENT(_name ${_input} NAME)
203   GET_FILENAME_COMPONENT(_path ${_input} PATH)
204   GET_PRECOMPILED_HEADER_OUTPUT( ${_targetName} ${_input} _output)
205
206   GET_FILENAME_COMPONENT(_outdir ${_output} PATH )
207
208   GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
209    _PCH_WRITE_PCHDEP_CXX(${_targetName} ${_input} _pch_dephelp_cxx)
210
211   IF(${_targetType} STREQUAL SHARED_LIBRARY)
212     ADD_LIBRARY(${_targetName}_pch_dephelp STATIC ${_pch_dephelp_cxx} )
213   ELSE(${_targetType} STREQUAL SHARED_LIBRARY)
214     ADD_LIBRARY(${_targetName}_pch_dephelp STATIC ${_pch_dephelp_cxx})
215   ENDIF(${_targetType} STREQUAL SHARED_LIBRARY)
216
217   FILE(MAKE_DIRECTORY ${_outdir})
218
219
220   _PCH_GET_COMPILE_FLAGS(_compile_FLAGS)
221
222   #MESSAGE("_compile_FLAGS: ${_compile_FLAGS}")
223   #message("COMMAND ${CMAKE_CXX_COMPILER}       ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}")
224   SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/${_name} PROPERTIES GENERATED 1)
225   ADD_CUSTOM_COMMAND(
226    OUTPUT       ${CMAKE_CURRENT_BINARY_DIR}/${_name}
227    COMMAND ${CMAKE_COMMAND} -E copy  ${_input} ${CMAKE_CURRENT_BINARY_DIR}/${_name} # ensure same directory! Required by gcc
228    DEPENDS ${_input}
229   )
230
231   #message("_command  ${_input} ${_output}")
232   _PCH_GET_COMPILE_COMMAND(_command  ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_output} )
233
234   #message(${_input} )
235   #message("_output ${_output}")
236
237   ADD_CUSTOM_COMMAND(
238     OUTPUT ${_output}
239     COMMAND ${_command}
240     DEPENDS ${_input}   ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_targetName}_pch_dephelp
241    )
242
243
244   ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_input}  ${_output} ${_dowarn})
245 ENDMACRO(ADD_PRECOMPILED_HEADER)
246
247
248 # Generates the use of precompiled in a target,
249 # without using depency targets (2 extra for each target)
250 # Using Visual, must also add ${_targetName}_pch to sources
251 # Not needed by Xcode
252
253 MACRO(GET_NATIVE_PRECOMPILED_HEADER _targetName _input)
254
255         if(CMAKE_GENERATOR MATCHES Visual*)
256
257                 SET(_dummy_str "#include \"${_input}\"\n"
258                                                                                 "// This is required to suppress LNK4221.  Very annoying.\n"
259                                                                                 "void *g_${_targetName}Dummy = 0\;\n")
260
261                 # Use of cxx extension for generated files (as Qt does)
262                 SET(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cxx)
263                 if(EXISTS ${${_targetName}_pch})
264                         # Check if contents is the same, if not rewrite
265                         # todo
266                 else(EXISTS ${${_targetName}_pch})
267                         FILE(WRITE ${${_targetName}_pch} ${_dummy_str})
268                 endif(EXISTS ${${_targetName}_pch})
269         endif(CMAKE_GENERATOR MATCHES Visual*)
270
271 ENDMACRO(GET_NATIVE_PRECOMPILED_HEADER)
272
273
274 MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _input)
275
276         IF( "${ARGN}" STREQUAL "0")
277                 SET(_dowarn 0)
278         ELSE( "${ARGN}" STREQUAL "0")
279                 SET(_dowarn 1)
280         ENDIF("${ARGN}" STREQUAL "0")
281
282         if(CMAKE_GENERATOR MATCHES Visual*)
283                 # Auto include the precompile (useful for moc processing, since the use of
284                 # precompiled is specified at the target level
285                 # and I don't want to specifiy /F- for each moc/res/ui generated files (using Qt)
286
287                 GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS)
288                 if (${oldProps} MATCHES NOTFOUND)
289                         SET(oldProps "")
290                 endif(${oldProps} MATCHES NOTFOUND)
291
292                 SET(newProperties "${oldProps} /Yu\"${_input}\" /FI\"${_input}\"")
293                 SET_TARGET_PROPERTIES(${_targetName} PROPERTIES COMPILE_FLAGS "${newProperties}")
294
295                 #also inlude ${oldProps} to have the same compile options
296                 SET_SOURCE_FILES_PROPERTIES(${${_targetName}_pch} PROPERTIES COMPILE_FLAGS "${oldProps} /Yc\"${_input}\"")
297
298         else(CMAKE_GENERATOR MATCHES Visual*)
299
300                 if (CMAKE_GENERATOR MATCHES Xcode)
301                         # For Xcode, cmake needs my patch to process
302                         # GCC_PREFIX_HEADER and GCC_PRECOMPILE_PREFIX_HEADER as target properties
303
304                         GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS)
305                         if (${oldProps} MATCHES NOTFOUND)
306                                 SET(oldProps "")
307                         endif(${oldProps} MATCHES NOTFOUND)
308
309                         # When buiding out of the tree, precompiled may not be located
310                         # Use full path instead.
311                         GET_FILENAME_COMPONENT(fullPath ${_input} ABSOLUTE)
312
313                         SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${fullPath}")
314                         SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
315
316                 else (CMAKE_GENERATOR MATCHES Xcode)
317
318                         #Fallback to the "old" precompiled suppport
319                         #ADD_PRECOMPILED_HEADER(${_targetName} ${_input} ${_dowarn})
320                 endif(CMAKE_GENERATOR MATCHES Xcode)
321         endif(CMAKE_GENERATOR MATCHES Visual*)
322
323 ENDMACRO(ADD_NATIVE_PRECOMPILED_HEADER)