[Win] CMake build fix after r188098.
[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         add_custom_command(
82             OUTPUT ${_destination}/${_prefix}${_name}.${_extension} ${_destination}/${_prefix}${_name}.h
83             MAIN_DEPENDENCY ${_file}
84             DEPENDS ${COMMON_GENERATOR_DEPENDENCIES}
85             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}
86             WORKING_DIRECTORY ${_base_dir}
87             VERBATIM)
88
89         list(APPEND ${_output_source} ${_destination}/${_prefix}${_name}.${_extension})
90     endforeach ()
91 endmacro()
92
93
94 macro(GENERATE_FONT_NAMES _infile)
95     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_names.pl)
96     set(_arguments  --fonts ${_infile})
97     set(_outputfiles ${DERIVED_SOURCES_WEBCORE_DIR}/WebKitFontFamilyNames.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/WebKitFontFamilyNames.h)
98
99     add_custom_command(
100         OUTPUT  ${_outputfiles}
101         MAIN_DEPENDENCY ${_infile}
102         DEPENDS ${MAKE_NAMES_DEPENDENCIES} ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
103         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR} ${_arguments}
104         VERBATIM)
105 endmacro()
106
107
108 macro(GENERATE_EVENT_FACTORY _infile _outfile)
109     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_event_factory.pl)
110
111     add_custom_command(
112         OUTPUT  ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile}
113         MAIN_DEPENDENCY ${_infile}
114         DEPENDS ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
115         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --input ${_infile} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR}
116         VERBATIM)
117 endmacro()
118
119
120 macro(GENERATE_EXCEPTION_CODE_DESCRIPTION _infile _outfile)
121     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_dom_exceptions.pl)
122
123     add_custom_command(
124         OUTPUT  ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile}
125         MAIN_DEPENDENCY ${_infile}
126         DEPENDS ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
127         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --input ${_infile} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR}
128         VERBATIM)
129 endmacro()
130
131
132 macro(GENERATE_SETTINGS_MACROS _infile _outfile)
133     set(NAMES_GENERATOR ${WEBCORE_DIR}/page/make_settings.pl)
134
135     add_custom_command(
136         OUTPUT ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile} ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.h ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.idl
137         MAIN_DEPENDENCY ${_infile}
138         DEPENDS ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
139         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --input ${_infile} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR}
140         VERBATIM)
141 endmacro()
142
143
144 macro(GENERATE_DOM_NAMES _namespace _attrs)
145     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_names.pl)
146     set(_arguments  --attrs ${_attrs})
147     set(_outputfiles ${DERIVED_SOURCES_WEBCORE_DIR}/${_namespace}Names.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/${_namespace}Names.h)
148     set(_extradef)
149     set(_tags)
150
151     foreach (f ${ARGN})
152         if (_tags)
153             set(_extradef "${_extradef} ${f}")
154         else ()
155             set(_tags ${f})
156         endif ()
157     endforeach ()
158
159     if (_tags)
160         set(_arguments "${_arguments}" --tags ${_tags} --factory --wrapperFactory)
161         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)
162     endif ()
163
164     if (_extradef)
165         set(_additionArguments "${_additionArguments}" --extraDefines=${_extradef})
166     endif ()
167
168     add_custom_command(
169         OUTPUT  ${_outputfiles}
170         DEPENDS ${MAKE_NAMES_DEPENDENCIES} ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS} ${_attrs} ${_tags}
171         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --preprocessor "${CODE_GENERATOR_PREPROCESSOR_WITH_LINEMARKERS}" --outputDir ${DERIVED_SOURCES_WEBCORE_DIR} ${_arguments} ${_additionArguments}
172         VERBATIM)
173 endmacro()
174
175
176 macro(GENERATE_GRAMMAR _prefix _input _output_header _output_source _features)
177     # This is a workaround for winflexbison, which does not work corretly when
178     # run in a different working directory than the installation directory.
179     get_filename_component(_working_directory ${BISON_EXECUTABLE} PATH)
180
181     add_custom_command(
182         OUTPUT ${_output_header} ${_output_source}
183         MAIN_DEPENDENCY ${_input}
184         DEPENDS ${_input}
185         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}
186         WORKING_DIRECTORY ${_working_directory}
187         VERBATIM)
188 endmacro()
189
190 macro(MAKE_HASH_TOOLS _source)
191     get_filename_component(_name ${_source} NAME_WE)
192
193     if (${_source} STREQUAL "DocTypeStrings")
194         set(_hash_tools_h "${DERIVED_SOURCES_WEBCORE_DIR}/HashTools.h")
195     else ()
196         set(_hash_tools_h "")
197     endif ()
198
199     add_custom_command(
200         OUTPUT ${DERIVED_SOURCES_WEBCORE_DIR}/${_name}.cpp ${_hash_tools_h}
201         MAIN_DEPENDENCY ${_source}.gperf
202         COMMAND ${PERL_EXECUTABLE} ${WEBCORE_DIR}/make-hash-tools.pl ${DERIVED_SOURCES_WEBCORE_DIR} ${_source}.gperf
203         VERBATIM)
204
205     unset(_name)
206     unset(_hash_tools_h)
207 endmacro()
208
209 macro(WEBKIT_WRAP_SOURCELIST)
210     foreach (_file ${ARGN})
211         get_filename_component(_basename ${_file} NAME_WE)
212         get_filename_component(_path ${_file} PATH)
213
214         if (NOT _file MATCHES "${DERIVED_SOURCES_WEBCORE_DIR}")
215             string(REGEX REPLACE "/" "\\\\\\\\" _sourcegroup "${_path}")
216             source_group("${_sourcegroup}" FILES ${_file})
217         endif ()
218     endforeach ()
219
220     source_group("DerivedSources" REGULAR_EXPRESSION "${DERIVED_SOURCES_WEBCORE_DIR}")
221 endmacro()
222
223
224 macro(WEBKIT_CREATE_FORWARDING_HEADER _target_directory _file)
225     get_filename_component(_source_path "${CMAKE_SOURCE_DIR}/Source/" ABSOLUTE)
226     get_filename_component(_absolute "${_file}" ABSOLUTE)
227     get_filename_component(_name "${_file}" NAME)
228     set(_target_filename "${_target_directory}/${_name}")
229
230     # Try to make the path in the forwarding header relative to the Source directory
231     # so that these forwarding headers are compatible with the ones created by the
232     # WebKit2 generate-forwarding-headers script.
233     string(REGEX REPLACE "${_source_path}/" "" _relative ${_absolute})
234
235     set(_content "#include \"${_relative}\"\n")
236
237     if (EXISTS "${_target_filename}")
238         file(READ "${_target_filename}" _old_content)
239     endif ()
240
241     if (NOT _old_content STREQUAL _content)
242         file(WRITE "${_target_filename}" "${_content}")
243     endif ()
244 endmacro()
245
246 macro(WEBKIT_CREATE_FORWARDING_HEADERS _framework)
247     set(_processing_directories 0)
248     set(_processing_files 0)
249     set(_target_directory "${DERIVED_SOURCES_DIR}/ForwardingHeaders/${_framework}")
250
251     file(GLOB _files "${_target_directory}/*.h")
252     foreach (_file ${_files})
253         file(READ "${_file}" _content)
254         string(REGEX MATCH "^#include \"([^\"]*)\"" _matched ${_content})
255         if (_matched AND NOT EXISTS "${CMAKE_SOURCE_DIR}/Source/${CMAKE_MATCH_1}")
256            file(REMOVE "${_file}")
257         endif ()
258     endforeach ()
259
260     foreach (_currentArg ${ARGN})
261         if ("${_currentArg}" STREQUAL "DIRECTORIES")
262             set(_processing_directories 1)
263             set(_processing_files 0)
264         elseif ("${_currentArg}" STREQUAL "FILES")
265             set(_processing_directories 0)
266             set(_processing_files 1)
267         elseif (_processing_directories)
268             file(GLOB _files "${_currentArg}/*.h")
269             foreach (_file ${_files})
270                 WEBKIT_CREATE_FORWARDING_HEADER(${_target_directory} ${_file})
271             endforeach ()
272         elseif (_processing_files)
273             WEBKIT_CREATE_FORWARDING_HEADER(${_target_directory} ${_currentArg})
274         endif ()
275     endforeach ()
276 endmacro()
277
278 # Helper macro which wraps generate-message-receiver.py and generate-message-header.py scripts
279 #   _output_source is a list name which will contain generated sources.(eg. WebKit2_SOURCES)
280 #   _input_files are messages.in files to generate.
281 macro(GENERATE_WEBKIT2_MESSAGE_SOURCES _output_source _input_files)
282     foreach (_file ${_input_files})
283         get_filename_component(_name ${_file} NAME_WE)
284         add_custom_command(
285             OUTPUT ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}MessageReceiver.cpp ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}Messages.h
286             MAIN_DEPENDENCY ${_file}
287             DEPENDS ${WEBKIT2_DIR}/Scripts/webkit/__init__.py
288                     ${WEBKIT2_DIR}/Scripts/webkit/messages.py
289                     ${WEBKIT2_DIR}/Scripts/webkit/model.py
290                     ${WEBKIT2_DIR}/Scripts/webkit/parser.py
291             COMMAND ${PYTHON_EXECUTABLE} ${WEBKIT2_DIR}/Scripts/generate-message-receiver.py ${_file} > ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}MessageReceiver.cpp
292             COMMAND ${PYTHON_EXECUTABLE} ${WEBKIT2_DIR}/Scripts/generate-messages-header.py ${_file} > ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}Messages.h
293             WORKING_DIRECTORY ${WEBKIT2_DIR}
294             VERBATIM)
295
296         list(APPEND ${_output_source} ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}MessageReceiver.cpp)
297     endforeach ()
298 endmacro()
299
300 # Helper macro for using all-in-one builds
301 # This macro removes the sources included in the _all_in_one_file from the input _file_list.
302 # _file_list is a list of source files
303 # _all_in_one_file is an all-in-one cpp file includes other cpp files
304 # _result_file_list is the output file list
305 macro(PROCESS_ALLINONE_FILE _file_list _all_in_one_file _result_file_list)
306     file(STRINGS ${_all_in_one_file} _all_in_one_file_content)
307     set(${_result_file_list} ${_file_list})
308     foreach (_line ${_all_in_one_file_content})
309         string(REGEX MATCH "^#include [\"<](.*)[\">]" _found ${_line})
310         if (_found)
311             list(APPEND _allins ${CMAKE_MATCH_1})
312         endif ()
313     endforeach ()
314
315     foreach (_allin ${_allins})
316         string(REGEX REPLACE ";[^;]*/${_allin};" ";" _new_result "${${_result_file_list}}")
317         set(${_result_file_list} ${_new_result})
318     endforeach ()
319
320 endmacro()