Clean up CMake build on Mac
[WebKit-https.git] / Source / cmake / WebKitMacros.cmake
1 macro(WEBKIT_INCLUDE_CONFIG_FILES_IF_EXISTS)
2     set(_file ${CMAKE_CURRENT_SOURCE_DIR}/Platform${PORT}.cmake)
3     if (EXISTS ${_file})
4         message(STATUS "Using platform-specific CMakeLists: ${_file}")
5         include(${_file})
6     else ()
7         message(STATUS "Platform-specific CMakeLists not found: ${_file}")
8     endif ()
9 endmacro()
10
11 # Append the given dependencies to the source file
12 macro(ADD_SOURCE_DEPENDENCIES _source _deps)
13     set(_tmp)
14     get_source_file_property(_tmp ${_source} OBJECT_DEPENDS)
15     if (NOT _tmp)
16         set(_tmp "")
17     endif ()
18
19     foreach (f ${_deps})
20         list(APPEND _tmp "${f}")
21     endforeach ()
22
23     set_source_files_properties(${_source} PROPERTIES OBJECT_DEPENDS "${_tmp}")
24 endmacro()
25
26 macro(ADD_PRECOMPILED_HEADER _header _cpp _source)
27     if (MSVC)
28         get_filename_component(PrecompiledBasename ${_header} NAME_WE)
29         set(PrecompiledBinary "${CMAKE_CURRENT_BINARY_DIR}/${PrecompiledBasename}.pch")
30         set(_sources ${${_source}})
31
32         set_source_files_properties(${_cpp}
33             PROPERTIES COMPILE_FLAGS "/Yc\"${_header}\" /Fp\"${PrecompiledBinary}\""
34             OBJECT_OUTPUTS "${PrecompiledBinary}")
35         set_source_files_properties(${_sources}
36             PROPERTIES COMPILE_FLAGS "/Yu\"${_header}\" /FI\"${_header}\" /Fp\"${PrecompiledBinary}\""
37             OBJECT_DEPENDS "${PrecompiledBinary}")
38         list(APPEND ${_source} ${_cpp})
39     endif ()
40     #FIXME: Add support for Xcode.
41 endmacro()
42
43 # Helper macro which wraps generate-bindings.pl script.
44 #   _output_source is a list name which will contain generated sources.(eg. WebCore_SOURCES)
45 #   _input_files are IDL files to generate.
46 #   _base_dir is base directory where script is called.
47 #   _idl_includes is value of --include argument. (eg. --include=${WEBCORE_DIR}/bindings/js)
48 #   _features is a value of --defines argument.
49 #   _destination is a value of --outputDir argument.
50 #   _prefix is a prefix of output files. (eg. JS - it makes JSXXX.cpp JSXXX.h from XXX.idl)
51 #   _generator is a value of --generator argument.
52 #   _supplemental_dependency_file is a value of --supplementalDependencyFile. (optional)
53 macro(GENERATE_BINDINGS _output_source _input_files _base_dir _idl_includes _features _destination _prefix _generator _extension _idl_attributes_file)
54     set(BINDING_GENERATOR ${WEBCORE_DIR}/bindings/scripts/generate-bindings.pl)
55     set(_args ${ARGN})
56     list(LENGTH _args _argCount)
57     if (_argCount GREATER 0)
58         list(GET _args 0 _supplemental_dependency_file)
59         if (_supplemental_dependency_file)
60             set(_supplemental_dependency --supplementalDependencyFile ${_supplemental_dependency_file})
61         endif ()
62         list(GET _args 1 _additional_dependencies)
63     endif ()
64
65     set(COMMON_GENERATOR_DEPENDENCIES
66         ${BINDING_GENERATOR}
67         ${WEBCORE_DIR}/bindings/scripts/CodeGenerator.pm
68         ${SCRIPTS_BINDINGS}
69         ${_supplemental_dependency_file}
70         ${_idl_attributes_file}
71     )
72     list(APPEND COMMON_GENERATOR_DEPENDENCIES ${_additional_dependencies})
73
74     if (EXISTS ${WEBCORE_DIR}/bindings/scripts/CodeGenerator${_generator}.pm)
75         list(APPEND COMMON_GENERATOR_DEPENDENCIES ${WEBCORE_DIR}/bindings/scripts/CodeGenerator${_generator}.pm)
76     endif ()
77
78     foreach (_file ${_input_files})
79         get_filename_component(_name ${_file} NAME_WE)
80
81         # Not all ObjC bindings generate a .mm file, and not all .mm files generated should be compiled.
82         if (${_generator} STREQUAL "ObjC")
83             list(FIND ObjC_BINDINGS_NO_MM ${_name} _no_mm_index)
84             if (${_no_mm_index} EQUAL -1)
85                 set(_no_mm 0)
86             else ()
87                 set(_no_mm 1)
88             endif ()
89         else ()
90             set(_no_mm 0)
91         endif ()
92
93         if (${_no_mm})
94             add_custom_command(
95                 OUTPUT ${_destination}/${_prefix}${_name}.h
96                 MAIN_DEPENDENCY ${_file}
97                 DEPENDS ${COMMON_GENERATOR_DEPENDENCIES}
98                 COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${BINDING_GENERATOR} --defines "${_features}" --generator ${_generator} ${_idl_includes} --outputDir "${_destination}" --preprocessor "${CODE_GENERATOR_PREPROCESSOR}" --idlAttributesFile ${_idl_attributes_file} ${_supplemental_dependency} ${_file}
99                 WORKING_DIRECTORY ${_base_dir}
100                 VERBATIM)
101
102             list(APPEND ${_output_source} ${_destination}/${_prefix}${_name}.h)
103         else ()
104             add_custom_command(
105                 OUTPUT ${_destination}/${_prefix}${_name}.${_extension} ${_destination}/${_prefix}${_name}.h
106                 MAIN_DEPENDENCY ${_file}
107                 DEPENDS ${COMMON_GENERATOR_DEPENDENCIES}
108                 COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${BINDING_GENERATOR} --defines "${_features}" --generator ${_generator} ${_idl_includes} --outputDir "${_destination}" --preprocessor "${CODE_GENERATOR_PREPROCESSOR}" --idlAttributesFile ${_idl_attributes_file} ${_supplemental_dependency} ${_file}
109                 WORKING_DIRECTORY ${_base_dir}
110                 VERBATIM)
111             if (${_generator} STREQUAL "ObjC")
112                 list(APPEND ${_output_source} ${_destination}/${_prefix}${_name}.h)
113             else ()
114                 list(APPEND ${_output_source} ${_destination}/${_prefix}${_name}.${_extension})
115             endif ()
116         endif ()
117     endforeach ()
118 endmacro()
119
120 macro(GENERATE_FONT_NAMES _infile)
121     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_names.pl)
122     set(_arguments  --fonts ${_infile})
123     set(_outputfiles ${DERIVED_SOURCES_WEBCORE_DIR}/WebKitFontFamilyNames.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/WebKitFontFamilyNames.h)
124
125     add_custom_command(
126         OUTPUT  ${_outputfiles}
127         MAIN_DEPENDENCY ${_infile}
128         DEPENDS ${MAKE_NAMES_DEPENDENCIES} ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
129         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR} ${_arguments}
130         VERBATIM)
131 endmacro()
132
133
134 macro(GENERATE_EVENT_FACTORY _infile _outfile)
135     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_event_factory.pl)
136
137     add_custom_command(
138         OUTPUT  ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile}
139         MAIN_DEPENDENCY ${_infile}
140         DEPENDS ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
141         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --input ${_infile} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR}
142         VERBATIM)
143 endmacro()
144
145
146 macro(GENERATE_EXCEPTION_CODE_DESCRIPTION _infile _outfile)
147     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_dom_exceptions.pl)
148
149     add_custom_command(
150         OUTPUT  ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile}
151         MAIN_DEPENDENCY ${_infile}
152         DEPENDS ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
153         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --input ${_infile} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR}
154         VERBATIM)
155 endmacro()
156
157
158 macro(GENERATE_SETTINGS_MACROS _infile _outfile)
159     set(NAMES_GENERATOR ${WEBCORE_DIR}/page/make_settings.pl)
160
161     add_custom_command(
162         OUTPUT ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile} ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.h ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.idl
163         MAIN_DEPENDENCY ${_infile}
164         DEPENDS ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
165         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --input ${_infile} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR}
166         VERBATIM)
167 endmacro()
168
169
170 macro(GENERATE_DOM_NAMES _namespace _attrs)
171     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_names.pl)
172     set(_arguments  --attrs ${_attrs})
173     set(_outputfiles ${DERIVED_SOURCES_WEBCORE_DIR}/${_namespace}Names.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/${_namespace}Names.h)
174     set(_extradef)
175     set(_tags)
176
177     foreach (f ${ARGN})
178         if (_tags)
179             set(_extradef "${_extradef} ${f}")
180         else ()
181             set(_tags ${f})
182         endif ()
183     endforeach ()
184
185     if (_tags)
186         set(_arguments "${_arguments}" --tags ${_tags} --factory --wrapperFactory)
187         set(_outputfiles "${_outputfiles}" ${DERIVED_SOURCES_WEBCORE_DIR}/${_namespace}ElementFactory.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/${_namespace}ElementFactory.h ${DERIVED_SOURCES_WEBCORE_DIR}/JS${_namespace}ElementWrapperFactory.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/JS${_namespace}ElementWrapperFactory.h)
188     endif ()
189
190     if (_extradef)
191         set(_additionArguments "${_additionArguments}" --extraDefines=${_extradef})
192     endif ()
193
194     add_custom_command(
195         OUTPUT  ${_outputfiles}
196         DEPENDS ${MAKE_NAMES_DEPENDENCIES} ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS} ${_attrs} ${_tags}
197         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --preprocessor "${CODE_GENERATOR_PREPROCESSOR_WITH_LINEMARKERS}" --outputDir ${DERIVED_SOURCES_WEBCORE_DIR} ${_arguments} ${_additionArguments}
198         VERBATIM)
199 endmacro()
200
201
202 macro(GENERATE_GRAMMAR _prefix _input _output_header _output_source _features)
203     # This is a workaround for winflexbison, which does not work corretly when
204     # run in a different working directory than the installation directory.
205     get_filename_component(_working_directory ${BISON_EXECUTABLE} PATH)
206
207     add_custom_command(
208         OUTPUT ${_output_header} ${_output_source}
209         MAIN_DEPENDENCY ${_input}
210         DEPENDS ${_input}
211         COMMAND ${PERL_EXECUTABLE} -I ${WEBCORE_DIR}/bindings/scripts ${WEBCORE_DIR}/css/makegrammar.pl --outputDir ${DERIVED_SOURCES_WEBCORE_DIR} --extraDefines "${_features}" --preprocessor "${CODE_GENERATOR_PREPROCESSOR}" --bison "${BISON_EXECUTABLE}" --symbolsPrefix ${_prefix} ${_input}
212         WORKING_DIRECTORY ${_working_directory}
213         VERBATIM)
214 endmacro()
215
216 macro(MAKE_HASH_TOOLS _source)
217     get_filename_component(_name ${_source} NAME_WE)
218
219     if (${_source} STREQUAL "DocTypeStrings")
220         set(_hash_tools_h "${DERIVED_SOURCES_WEBCORE_DIR}/HashTools.h")
221     else ()
222         set(_hash_tools_h "")
223     endif ()
224
225     add_custom_command(
226         OUTPUT ${DERIVED_SOURCES_WEBCORE_DIR}/${_name}.cpp ${_hash_tools_h}
227         MAIN_DEPENDENCY ${_source}.gperf
228         COMMAND ${PERL_EXECUTABLE} ${WEBCORE_DIR}/make-hash-tools.pl ${DERIVED_SOURCES_WEBCORE_DIR} ${_source}.gperf
229         VERBATIM)
230
231     unset(_name)
232     unset(_hash_tools_h)
233 endmacro()
234
235 macro(WEBKIT_WRAP_SOURCELIST)
236     foreach (_file ${ARGN})
237         get_filename_component(_basename ${_file} NAME_WE)
238         get_filename_component(_path ${_file} PATH)
239
240         if (NOT _file MATCHES "${DERIVED_SOURCES_WEBCORE_DIR}")
241             string(REGEX REPLACE "/" "\\\\\\\\" _sourcegroup "${_path}")
242             source_group("${_sourcegroup}" FILES ${_file})
243         endif ()
244     endforeach ()
245
246     source_group("DerivedSources" REGULAR_EXPRESSION "${DERIVED_SOURCES_WEBCORE_DIR}")
247 endmacro()
248
249
250 macro(WEBKIT_CREATE_FORWARDING_HEADER _target_directory _file)
251     get_filename_component(_source_path "${CMAKE_SOURCE_DIR}/Source/" ABSOLUTE)
252     get_filename_component(_absolute "${_file}" ABSOLUTE)
253     get_filename_component(_name "${_file}" NAME)
254     set(_target_filename "${_target_directory}/${_name}")
255
256     # Try to make the path in the forwarding header relative to the Source directory
257     # so that these forwarding headers are compatible with the ones created by the
258     # WebKit2 generate-forwarding-headers script.
259     string(REGEX REPLACE "${_source_path}/" "" _relative ${_absolute})
260
261     set(_content "#include \"${_relative}\"\n")
262
263     if (EXISTS "${_target_filename}")
264         file(READ "${_target_filename}" _old_content)
265     endif ()
266
267     if (NOT _old_content STREQUAL _content)
268         file(WRITE "${_target_filename}" "${_content}")
269     endif ()
270 endmacro()
271
272 macro(WEBKIT_CREATE_FORWARDING_HEADERS _framework)
273     # On Windows, we copy the entire contents of forwarding headers.
274     if (NOT WIN32)
275         set(_processing_directories 0)
276         set(_processing_files 0)
277         set(_target_directory "${DERIVED_SOURCES_DIR}/ForwardingHeaders/${_framework}")
278
279         file(GLOB _files "${_target_directory}/*.h")
280         foreach (_file ${_files})
281             file(READ "${_file}" _content)
282             string(REGEX MATCH "^#include \"([^\"]*)\"" _matched ${_content})
283             if (_matched AND NOT EXISTS "${CMAKE_SOURCE_DIR}/Source/${CMAKE_MATCH_1}")
284                file(REMOVE "${_file}")
285             endif ()
286         endforeach ()
287
288         foreach (_currentArg ${ARGN})
289             if ("${_currentArg}" STREQUAL "DIRECTORIES")
290                 set(_processing_directories 1)
291                 set(_processing_files 0)
292             elseif ("${_currentArg}" STREQUAL "FILES")
293                 set(_processing_directories 0)
294                 set(_processing_files 1)
295             elseif (_processing_directories)
296                 file(GLOB _files "${_currentArg}/*.h")
297                 foreach (_file ${_files})
298                     WEBKIT_CREATE_FORWARDING_HEADER(${_target_directory} ${_file})
299                 endforeach ()
300             elseif (_processing_files)
301                 WEBKIT_CREATE_FORWARDING_HEADER(${_target_directory} ${_currentArg})
302             endif ()
303         endforeach ()
304     endif ()
305 endmacro()
306
307 # Helper macro which wraps generate-message-receiver.py and generate-message-header.py scripts
308 #   _output_source is a list name which will contain generated sources.(eg. WebKit2_SOURCES)
309 #   _input_files are messages.in files to generate.
310 macro(GENERATE_WEBKIT2_MESSAGE_SOURCES _output_source _input_files)
311     foreach (_file ${_input_files})
312         get_filename_component(_name ${_file} NAME_WE)
313         add_custom_command(
314             OUTPUT ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}MessageReceiver.cpp ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}Messages.h
315             MAIN_DEPENDENCY ${_file}
316             DEPENDS ${WEBKIT2_DIR}/Scripts/webkit/__init__.py
317                     ${WEBKIT2_DIR}/Scripts/webkit/messages.py
318                     ${WEBKIT2_DIR}/Scripts/webkit/model.py
319                     ${WEBKIT2_DIR}/Scripts/webkit/parser.py
320             COMMAND ${PYTHON_EXECUTABLE} ${WEBKIT2_DIR}/Scripts/generate-message-receiver.py ${_file} > ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}MessageReceiver.cpp
321             COMMAND ${PYTHON_EXECUTABLE} ${WEBKIT2_DIR}/Scripts/generate-messages-header.py ${_file} > ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}Messages.h
322             WORKING_DIRECTORY ${WEBKIT2_DIR}
323             VERBATIM)
324
325         list(APPEND ${_output_source} ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}MessageReceiver.cpp)
326     endforeach ()
327 endmacro()
328
329 # Helper macro for using all-in-one builds
330 # This macro removes the sources included in the _all_in_one_file from the input _file_list.
331 # _file_list is a list of source files
332 # _all_in_one_file is an all-in-one cpp file includes other cpp files
333 # _result_file_list is the output file list
334 macro(PROCESS_ALLINONE_FILE _file_list _all_in_one_file _result_file_list)
335     file(STRINGS ${_all_in_one_file} _all_in_one_file_content)
336     set(${_result_file_list} ${_file_list})
337     foreach (_line ${_all_in_one_file_content})
338         string(REGEX MATCH "^#include [\"<](.*)[\">]" _found ${_line})
339         if (_found)
340             list(APPEND _allins ${CMAKE_MATCH_1})
341         endif ()
342     endforeach ()
343
344     foreach (_allin ${_allins})
345         string(REGEX REPLACE ";[^;]*/${_allin};" ";" _new_result "${${_result_file_list}}")
346         set(${_result_file_list} ${_new_result})
347     endforeach ()
348
349 endmacro()