]> git.sesse.net Git - casparcg/blob - CMake/PrecompiledHeader.cmake
[flash] Fixed bug where template-host copying didn't work.
[casparcg] / CMake / PrecompiledHeader.cmake
1 # Function for setting up precompiled headers. Usage:
2 #
3 #   add_library/executable(target
4 #       pchheader.c pchheader.cpp pchheader.h)
5 #
6 #   add_precompiled_header(target pchheader.h
7 #       [FORCEINCLUDE]
8 #       [SOURCE_C pchheader.c]
9 #       [SOURCE_CXX pchheader.cpp])
10 #
11 # Options:
12 #
13 #   FORCEINCLUDE: Add compiler flags to automatically include the
14 #   pchheader.h from every source file. Works with both GCC and
15 #   MSVC. This is recommended.
16 #
17 #   SOURCE_C/CXX: Specifies the .c/.cpp source file that includes
18 #   pchheader.h for generating the pre-compiled header
19 #   output. Defaults to pchheader.c. Only required for MSVC.
20 #
21 # Caveats:
22 #
23 #   * Its not currently possible to use the same precompiled-header in
24 #     more than a single target in the same directory (No way to set
25 #     the source file properties differently for each target).
26 #
27 #   * MSVC: A source file with the same name as the header must exist
28 #     and be included in the target (E.g. header.cpp). Name of file
29 #     can be changed using the SOURCE_CXX/SOURCE_C options.
30 #
31 # License:
32 #
33 # Copyright (C) 2009-2013 Lars Christensen <larsch@belunktum.dk>
34 #
35 # Permission is hereby granted, free of charge, to any person
36 # obtaining a copy of this software and associated documentation files
37 # (the 'Software') deal in the Software without restriction,
38 # including without limitation the rights to use, copy, modify, merge,
39 # publish, distribute, sublicense, and/or sell copies of the Software,
40 # and to permit persons to whom the Software is furnished to do so,
41 # subject to the following conditions:
42 #
43 # The above copyright notice and this permission notice shall be
44 # included in all copies or substantial portions of the Software.
45 #
46 # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
47 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
49 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
50 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
51 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
53 # SOFTWARE.
54
55 include(CMakeParseArguments)
56
57 macro(combine_arguments _variable)
58   set(_result "")
59   foreach(_element ${${_variable}})
60     set(_result "${_result} \"${_element}\"")
61   endforeach()
62   string(STRIP "${_result}" _result)
63   set(${_variable} "${_result}")
64 endmacro()
65
66 function(export_all_flags _filename)
67   set(_include_directories "$<TARGET_PROPERTY:${_target},INCLUDE_DIRECTORIES>")
68   set(_compile_definitions "$<TARGET_PROPERTY:${_target},COMPILE_DEFINITIONS>")
69   set(_compile_flags "$<TARGET_PROPERTY:${_target},COMPILE_FLAGS>")
70   set(_compile_options "$<TARGET_PROPERTY:${_target},COMPILE_OPTIONS>")
71   set(_include_directories "$<$<BOOL:${_include_directories}>:-I$<JOIN:${_include_directories},\n-I>\n>")
72   set(_compile_definitions "$<$<BOOL:${_compile_definitions}>:-D$<JOIN:${_compile_definitions},\n-D>\n>")
73   set(_compile_flags "$<$<BOOL:${_compile_flags}>:$<JOIN:${_compile_flags},\n>\n>")
74   set(_compile_options "$<$<BOOL:${_compile_options}>:$<JOIN:${_compile_options},\n>\n>")
75   file(GENERATE OUTPUT "${_filename}" CONTENT "${_compile_definitions}${_include_directories}${_compile_flags}${_compile_options}\n")
76 endfunction()
77
78 function(add_precompiled_header _target _input)
79   cmake_parse_arguments(_PCH "FORCEINCLUDE" "SOURCE_CXX:SOURCE_C" "" ${ARGN})
80
81   get_filename_component(_input_we ${_input} NAME_WE)
82   if(NOT _PCH_SOURCE_CXX)
83     set(_PCH_SOURCE_CXX "${_input_we}.cpp")
84   endif()
85   if(NOT _PCH_SOURCE_C)
86     set(_PCH_SOURCE_C "${_input_we}.c")
87   endif()
88
89   if(MSVC)
90
91     set(_cxx_path "${CMAKE_CFG_INTDIR}/${_target}_cxx_pch")
92     set(_c_path "${CMAKE_CFG_INTDIR}/${_target}_c_pch")
93     make_directory("${_cxx_path}")
94     make_directory("${_c_path}")
95     set(_pch_cxx_header "${_cxx_path}/${_input}")
96     set(_pch_cxx_pch "${_cxx_path}/${_input_we}.pch")
97     set(_pch_c_header "${_c_path}/${_input}")
98     set(_pch_c_pch "${_c_path}/${_input_we}.pch")
99
100     get_target_property(sources ${_target} SOURCES)
101     foreach(_source ${sources})
102       set(_pch_compile_flags "")
103       if(_source MATCHES \\.\(cc|cxx|cpp|c\)$)
104         if(_source MATCHES \\.\(cpp|cxx|cc\)$)
105           set(_pch_header "${_input}")
106           set(_pch "${_pch_cxx_pch}")
107         else()
108           set(_pch_header "${_input}")
109           set(_pch "${_pch_c_pch}")
110         endif()
111
112         if(_source STREQUAL "${_PCH_SOURCE_CXX}")
113           set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_cxx_pch}\" /Yc${_input}")
114           set(_pch_source_cxx_found TRUE)
115         elseif(_source STREQUAL "${_PCH_SOURCE_C}")
116           set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_c_pch}\" /Yc${_input}")
117           set(_pch_source_c_found TRUE)
118         else()
119           if(_source MATCHES \\.\(cpp|cxx|cc\)$)
120             set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_cxx_pch}\" /Yu${_input}")
121             set(_pch_source_cxx_needed TRUE)
122           else()
123             set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_c_pch}\" /Yu${_input}")
124             set(_pch_source_c_needed TRUE)
125           endif()
126           if(_PCH_FORCEINCLUDE)
127             set(_pch_compile_flags "${_pch_compile_flags} /FI${_input}")
128           endif(_PCH_FORCEINCLUDE)
129         endif()
130
131         get_source_file_property(_object_depends "${_source}" OBJECT_DEPENDS)
132         if(NOT _object_depends)
133           set(_object_depends)
134         endif()
135         if(_PCH_FORCEINCLUDE)
136           if(_source MATCHES \\.\(cc|cxx|cpp\)$)
137             list(APPEND _object_depends "${_pch_header}")
138           else()
139             list(APPEND _object_depends "${_pch_header}")
140           endif()
141         endif()
142
143         set_source_files_properties(${_source} PROPERTIES
144           COMPILE_FLAGS "${_pch_compile_flags}"
145           OBJECT_DEPENDS "${_object_depends}")
146       endif()
147     endforeach()
148
149     if(_pch_source_cxx_needed AND NOT _pch_source_cxx_found)
150       message(FATAL_ERROR "A source file ${_PCH_SOURCE_CXX} for ${_input} is required for MSVC builds. Can be set with the SOURCE_CXX option.")
151     endif()
152     if(_pch_source_c_needed AND NOT _pch_source_c_found)
153       message(FATAL_ERROR "A source file ${_PCH_SOURCE_C} for ${_input} is required for MSVC builds. Can be set with the SOURCE_C option.")
154     endif()
155   endif(MSVC)
156
157   if(CMAKE_COMPILER_IS_GNUCXX)
158     get_filename_component(_name ${_input} NAME)
159     set(_pch_header "${CMAKE_CURRENT_SOURCE_DIR}/${_input}")
160     set(_pch_binary_dir "${CMAKE_CURRENT_BINARY_DIR}/${_target}_pch")
161     set(_pchfile "${_pch_binary_dir}/${_input}")
162     set(_outdir "${CMAKE_CURRENT_BINARY_DIR}/${_target}_pch/${_name}.gch")
163     make_directory(${_outdir})
164     set(_output_cxx "${_outdir}/.c++")
165     set(_output_c "${_outdir}/.c")
166
167     set(_pch_flags_file "${_pch_binary_dir}/compile_flags.rsp")
168     export_all_flags("${_pch_flags_file}")
169     set(_compiler_FLAGS "@${_pch_flags_file}")
170     add_custom_command(
171       OUTPUT "${_pchfile}"
172       COMMAND "${CMAKE_COMMAND}" -E copy "${_pch_header}" "${_pchfile}"
173       DEPENDS "${_pch_header}"
174       COMMENT "Updating ${_name}")
175     add_custom_command(
176       OUTPUT "${_output_cxx}"
177       COMMAND "${CMAKE_CXX_COMPILER}" ${_compiler_FLAGS} -x c++-header -o "${_output_cxx}" "${_pchfile}"
178       DEPENDS "${_pchfile}" "${_pch_flags_file}"
179       COMMENT "Precompiling ${_name} for ${_target} (C++)")
180     add_custom_command(
181       OUTPUT "${_output_c}"
182       COMMAND "${CMAKE_C_COMPILER}" ${_compiler_FLAGS} -x c-header -o "${_output_c}" "${_pchfile}"
183       DEPENDS "${_pchfile}" "${_pch_flags_file}"
184       COMMENT "Precompiling ${_name} for ${_target} (C)")
185
186     get_property(_sources TARGET ${_target} PROPERTY SOURCES)
187     foreach(_source ${_sources})
188       set(_pch_compile_flags "")
189
190       if(_source MATCHES \\.\(cc|cxx|cpp|c\)$)
191         get_source_file_property(_pch_compile_flags "${_source}" COMPILE_FLAGS)
192         if(NOT _pch_compile_flags)
193           set(_pch_compile_flags)
194         endif()
195         separate_arguments(_pch_compile_flags)
196         list(APPEND _pch_compile_flags -Winvalid-pch)
197         if(_PCH_FORCEINCLUDE)
198           list(APPEND _pch_compile_flags -include "${_pchfile}")
199         else(_PCH_FORCEINCLUDE)
200           list(APPEND _pch_compile_flags "-I${_pch_binary_dir}")
201         endif(_PCH_FORCEINCLUDE)
202
203         get_source_file_property(_object_depends "${_source}" OBJECT_DEPENDS)
204         if(NOT _object_depends)
205           set(_object_depends)
206         endif()
207         list(APPEND _object_depends "${_pchfile}")
208         if(_source MATCHES \\.\(cc|cxx|cpp\)$)
209           list(APPEND _object_depends "${_output_cxx}")
210         else()
211           list(APPEND _object_depends "${_output_c}")
212         endif()
213
214         combine_arguments(_pch_compile_flags)
215         message("${_source}" ${_pch_compile_flags})
216         set_source_files_properties(${_source} PROPERTIES
217           COMPILE_FLAGS "${_pch_compile_flags}"
218           OBJECT_DEPENDS "${_object_depends}")
219       endif()
220     endforeach()
221   endif(CMAKE_COMPILER_IS_GNUCXX)
222 endfunction()