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