[CMake] Decouple generating bindings of WebCore and WebCoreTestSupport
[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 ${_cpp} NAME_WE)
29         file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${_source}")
30         set(PrecompiledBinary "${CMAKE_CURRENT_BINARY_DIR}/${_source}/${PrecompiledBasename}.pch")
31         set(_sources ${${_source}})
32
33         set_source_files_properties(${_cpp}
34             PROPERTIES COMPILE_FLAGS "/Yc\"${_header}\" /Fp\"${PrecompiledBinary}\""
35             OBJECT_OUTPUTS "${PrecompiledBinary}")
36         set_source_files_properties(${_sources}
37             PROPERTIES COMPILE_FLAGS "/Yu\"${_header}\" /FI\"${_header}\" /Fp\"${PrecompiledBinary}\""
38             OBJECT_DEPENDS "${PrecompiledBinary}")
39         list(APPEND ${_source} ${_cpp})
40     endif ()
41     #FIXME: Add support for Xcode.
42 endmacro()
43
44 # Helper macro which wraps generate-bindings.pl script.
45 #   _output_source is a list name which will contain generated sources.(eg. WebCore_SOURCES)
46 #   _input_files are IDL files to generate.
47 #   _base_dir is base directory where script is called.
48 #   _idl_includes is value of --include argument. (eg. --include=${WEBCORE_DIR}/bindings/js)
49 #   _features is a value of --defines argument.
50 #   _destination is a value of --outputDir argument.
51 #   _prefix is a prefix of output files. (eg. JS - it makes JSXXX.cpp JSXXX.h from XXX.idl)
52 #   _generator is a value of --generator argument.
53 #   _supplemental_dependency_file is a value of --supplementalDependencyFile. (optional)
54 macro(GENERATE_BINDINGS _output_source _input_files _base_dir _idl_includes _features _destination _prefix _generator _extension _idl_attributes_file)
55     set(_supplemental_dependency)
56     set(_supplemental_dependency_file)
57     set(_additional_dependencies)
58     set(BINDING_GENERATOR ${WEBCORE_DIR}/bindings/scripts/generate-bindings.pl)
59     set(_args ${ARGN})
60     list(LENGTH _args _argCount)
61     if (_argCount GREATER 0)
62         list(GET _args 0 _supplemental_dependency_file)
63         if (_supplemental_dependency_file)
64             set(_supplemental_dependency --supplementalDependencyFile ${_supplemental_dependency_file})
65         endif ()
66         list(GET _args 1 _additional_dependencies)
67     endif ()
68
69     set(COMMON_GENERATOR_DEPENDENCIES
70         ${BINDING_GENERATOR}
71         ${WEBCORE_DIR}/bindings/scripts/CodeGenerator.pm
72         ${SCRIPTS_BINDINGS}
73         ${_supplemental_dependency_file}
74         ${_idl_attributes_file}
75     )
76     list(APPEND COMMON_GENERATOR_DEPENDENCIES ${_additional_dependencies})
77
78     if (EXISTS ${WEBCORE_DIR}/bindings/scripts/CodeGenerator${_generator}.pm)
79         list(APPEND COMMON_GENERATOR_DEPENDENCIES ${WEBCORE_DIR}/bindings/scripts/CodeGenerator${_generator}.pm)
80     endif ()
81
82     foreach (_file ${_input_files})
83         get_filename_component(_name ${_file} NAME_WE)
84
85         add_custom_command(
86             OUTPUT ${_destination}/${_prefix}${_name}.${_extension} ${_destination}/${_prefix}${_name}.h
87             MAIN_DEPENDENCY ${_file}
88             DEPENDS ${COMMON_GENERATOR_DEPENDENCIES}
89             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}
90             WORKING_DIRECTORY ${_base_dir}
91             VERBATIM)
92         list(APPEND ${_output_source} ${_destination}/${_prefix}${_name}.${_extension})
93     endforeach ()
94 endmacro()
95
96 macro(GENERATE_FONT_NAMES _infile)
97     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_names.pl)
98     set(_arguments  --fonts ${_infile})
99     set(_outputfiles ${DERIVED_SOURCES_WEBCORE_DIR}/WebKitFontFamilyNames.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/WebKitFontFamilyNames.h)
100
101     add_custom_command(
102         OUTPUT  ${_outputfiles}
103         MAIN_DEPENDENCY ${_infile}
104         DEPENDS ${MAKE_NAMES_DEPENDENCIES} ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
105         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR} ${_arguments}
106         VERBATIM)
107 endmacro()
108
109
110 macro(GENERATE_EVENT_FACTORY _infile _outfile)
111     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_event_factory.pl)
112
113     add_custom_command(
114         OUTPUT  ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile}
115         MAIN_DEPENDENCY ${_infile}
116         DEPENDS ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
117         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --input ${_infile} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR}
118         VERBATIM)
119 endmacro()
120
121
122 macro(GENERATE_EXCEPTION_CODE_DESCRIPTION _infile _outfile)
123     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_dom_exceptions.pl)
124
125     add_custom_command(
126         OUTPUT  ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile}
127         MAIN_DEPENDENCY ${_infile}
128         DEPENDS ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
129         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --input ${_infile} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR}
130         VERBATIM)
131 endmacro()
132
133
134 macro(GENERATE_SETTINGS_MACROS _infile _outfile)
135     set(NAMES_GENERATOR ${WEBCORE_DIR}/page/make_settings.pl)
136
137     add_custom_command(
138         OUTPUT ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile} ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.h ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.idl
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_DOM_NAMES _namespace _attrs)
147     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_names.pl)
148     set(_arguments  --attrs ${_attrs})
149     set(_outputfiles ${DERIVED_SOURCES_WEBCORE_DIR}/${_namespace}Names.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/${_namespace}Names.h)
150     set(_extradef)
151     set(_tags)
152
153     foreach (f ${ARGN})
154         if (_tags)
155             set(_extradef "${_extradef} ${f}")
156         else ()
157             set(_tags ${f})
158         endif ()
159     endforeach ()
160
161     if (_tags)
162         set(_arguments "${_arguments}" --tags ${_tags} --factory --wrapperFactory)
163         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)
164     endif ()
165
166     if (_extradef)
167         set(_additionArguments "${_additionArguments}" --extraDefines=${_extradef})
168     endif ()
169
170     add_custom_command(
171         OUTPUT  ${_outputfiles}
172         DEPENDS ${MAKE_NAMES_DEPENDENCIES} ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS} ${_attrs} ${_tags}
173         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --preprocessor "${CODE_GENERATOR_PREPROCESSOR_WITH_LINEMARKERS}" --outputDir ${DERIVED_SOURCES_WEBCORE_DIR} ${_arguments} ${_additionArguments}
174         VERBATIM)
175 endmacro()
176
177
178 macro(GENERATE_GRAMMAR _prefix _input _output_header _output_source _features)
179     # This is a workaround for winflexbison, which does not work corretly when
180     # run in a different working directory than the installation directory.
181     get_filename_component(_working_directory ${BISON_EXECUTABLE} PATH)
182
183     add_custom_command(
184         OUTPUT ${_output_header} ${_output_source}
185         MAIN_DEPENDENCY ${_input}
186         DEPENDS ${_input}
187         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}
188         WORKING_DIRECTORY ${_working_directory}
189         VERBATIM)
190 endmacro()
191
192 macro(MAKE_HASH_TOOLS _source)
193     get_filename_component(_name ${_source} NAME_WE)
194
195     if (${_source} STREQUAL "DocTypeStrings")
196         set(_hash_tools_h "${DERIVED_SOURCES_WEBCORE_DIR}/HashTools.h")
197     else ()
198         set(_hash_tools_h "")
199     endif ()
200
201     add_custom_command(
202         OUTPUT ${DERIVED_SOURCES_WEBCORE_DIR}/${_name}.cpp ${_hash_tools_h}
203         MAIN_DEPENDENCY ${_source}.gperf
204         COMMAND ${PERL_EXECUTABLE} ${WEBCORE_DIR}/make-hash-tools.pl ${DERIVED_SOURCES_WEBCORE_DIR} ${_source}.gperf ${GPERF_EXECUTABLE}
205         VERBATIM)
206
207     unset(_name)
208     unset(_hash_tools_h)
209 endmacro()
210
211 macro(WEBKIT_WRAP_SOURCELIST)
212     foreach (_file ${ARGN})
213         get_filename_component(_basename ${_file} NAME_WE)
214         get_filename_component(_path ${_file} PATH)
215
216         if (NOT _file MATCHES "${DERIVED_SOURCES_WEBCORE_DIR}")
217             string(REGEX REPLACE "/" "\\\\\\\\" _sourcegroup "${_path}")
218             source_group("${_sourcegroup}" FILES ${_file})
219         endif ()
220     endforeach ()
221
222     source_group("DerivedSources" REGULAR_EXPRESSION "${DERIVED_SOURCES_WEBCORE_DIR}")
223 endmacro()
224
225 macro(WEBKIT_FRAMEWORK _target)
226     include_directories(${${_target}_INCLUDE_DIRECTORIES})
227     include_directories(SYSTEM ${${_target}_SYSTEM_INCLUDE_DIRECTORIES})
228     add_library(${_target} ${${_target}_LIBRARY_TYPE}
229         ${${_target}_HEADERS}
230         ${${_target}_SOURCES}
231     )
232     target_link_libraries(${_target} ${${_target}_LIBRARIES})
233     set_target_properties(${_target} PROPERTIES COMPILE_DEFINITIONS "BUILDING_${_target}")
234
235     if (${_target}_OUTPUT_NAME)
236         set_target_properties(${_target} PROPERTIES OUTPUT_NAME ${${_target}_OUTPUT_NAME})
237     endif ()
238
239     if (${_target}_PRE_BUILD_COMMAND)
240         add_custom_target(_${_target}_PreBuild COMMAND ${${_target}_PRE_BUILD_COMMAND} VERBATIM)
241         add_dependencies(${_target} _${_target}_PreBuild)
242     endif ()
243
244     if (${_target}_POST_BUILD_COMMAND)
245         add_custom_command(TARGET ${_target} POST_BUILD COMMAND ${${_target}_POST_BUILD_COMMAND} VERBATIM)
246     endif ()
247
248     if (APPLE AND NOT PORT STREQUAL "GTK" AND NOT ${_target} STREQUAL "WTF")
249         set_target_properties(${_target} PROPERTIES FRAMEWORK TRUE)
250         install(TARGETS ${_target} FRAMEWORK DESTINATION ${LIB_INSTALL_DIR})
251     endif ()
252 endmacro()
253
254 macro(WEBKIT_CREATE_FORWARDING_HEADER _target_directory _file)
255     get_filename_component(_source_path "${CMAKE_SOURCE_DIR}/Source/" ABSOLUTE)
256     get_filename_component(_absolute "${_file}" ABSOLUTE)
257     get_filename_component(_name "${_file}" NAME)
258     set(_target_filename "${_target_directory}/${_name}")
259
260     # Try to make the path in the forwarding header relative to the Source directory
261     # so that these forwarding headers are compatible with the ones created by the
262     # WebKit2 generate-forwarding-headers script.
263     string(REGEX REPLACE "${_source_path}/" "" _relative ${_absolute})
264
265     set(_content "#include \"${_relative}\"\n")
266
267     if (EXISTS "${_target_filename}")
268         file(READ "${_target_filename}" _old_content)
269     endif ()
270
271     if (NOT _old_content STREQUAL _content)
272         file(WRITE "${_target_filename}" "${_content}")
273     endif ()
274 endmacro()
275
276 macro(WEBKIT_CREATE_FORWARDING_HEADERS _framework)
277     # On Windows, we copy the entire contents of forwarding headers.
278     if (NOT WIN32)
279         set(_processing_directories 0)
280         set(_processing_files 0)
281         set(_target_directory "${DERIVED_SOURCES_DIR}/ForwardingHeaders/${_framework}")
282
283         file(GLOB _files "${_target_directory}/*.h")
284         foreach (_file ${_files})
285             file(READ "${_file}" _content)
286             string(REGEX MATCH "^#include \"([^\"]*)\"" _matched ${_content})
287             if (_matched AND NOT EXISTS "${CMAKE_SOURCE_DIR}/Source/${CMAKE_MATCH_1}")
288                file(REMOVE "${_file}")
289             endif ()
290         endforeach ()
291
292         foreach (_currentArg ${ARGN})
293             if ("${_currentArg}" STREQUAL "DIRECTORIES")
294                 set(_processing_directories 1)
295                 set(_processing_files 0)
296             elseif ("${_currentArg}" STREQUAL "FILES")
297                 set(_processing_directories 0)
298                 set(_processing_files 1)
299             elseif (_processing_directories)
300                 file(GLOB _files "${_currentArg}/*.h")
301                 foreach (_file ${_files})
302                     WEBKIT_CREATE_FORWARDING_HEADER(${_target_directory} ${_file})
303                 endforeach ()
304             elseif (_processing_files)
305                 WEBKIT_CREATE_FORWARDING_HEADER(${_target_directory} ${_currentArg})
306             endif ()
307         endforeach ()
308     endif ()
309 endmacro()
310
311 # Helper macro which wraps generate-message-receiver.py and generate-message-header.py scripts
312 #   _output_source is a list name which will contain generated sources.(eg. WebKit2_SOURCES)
313 #   _input_files are messages.in files to generate.
314 macro(GENERATE_WEBKIT2_MESSAGE_SOURCES _output_source _input_files)
315     foreach (_file ${_input_files})
316         get_filename_component(_name ${_file} NAME_WE)
317         add_custom_command(
318             OUTPUT ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}MessageReceiver.cpp ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}Messages.h
319             MAIN_DEPENDENCY ${_file}
320             DEPENDS ${WEBKIT2_DIR}/Scripts/webkit/__init__.py
321                     ${WEBKIT2_DIR}/Scripts/webkit/messages.py
322                     ${WEBKIT2_DIR}/Scripts/webkit/model.py
323                     ${WEBKIT2_DIR}/Scripts/webkit/parser.py
324             COMMAND ${PYTHON_EXECUTABLE} ${WEBKIT2_DIR}/Scripts/generate-message-receiver.py ${_file} > ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}MessageReceiver.cpp
325             COMMAND ${PYTHON_EXECUTABLE} ${WEBKIT2_DIR}/Scripts/generate-messages-header.py ${_file} > ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}Messages.h
326             WORKING_DIRECTORY ${WEBKIT2_DIR}
327             VERBATIM)
328
329         list(APPEND ${_output_source} ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}MessageReceiver.cpp)
330     endforeach ()
331 endmacro()
332
333 macro(MAKE_JS_FILE_ARRAYS _output_cpp _output_h _scripts _scripts_dependencies)
334     if (WIN32)
335         set(_python_path set "PYTHONPATH=${JavaScriptCore_SCRIPTS_DIR}" COMMAND)
336     else ()
337         set(_python_path "PYTHONPATH=${JavaScriptCore_SCRIPTS_DIR}")
338     endif ()
339
340     add_custom_command(
341         OUTPUT ${_output_h} ${_output_cpp}
342         MAIN_DEPENDENCY ${WEBCORE_DIR}/Scripts/make-js-file-arrays.py
343         DEPENDS ${${_scripts}}
344         COMMAND ${_python_path} ${PYTHON_EXECUTABLE} ${WEBCORE_DIR}/Scripts/make-js-file-arrays.py ${_output_h} ${_output_cpp} ${${_scripts}}
345         VERBATIM)
346     list(APPEND WebCore_DERIVED_SOURCES ${_output_cpp})
347     ADD_SOURCE_DEPENDENCIES(${${_scripts_dependencies}} ${_output_h} ${_output_cpp})
348 endmacro()
349
350 # Helper macro for using all-in-one builds
351 # This macro removes the sources included in the _all_in_one_file from the input _file_list.
352 # _file_list is a list of source files
353 # _all_in_one_file is an all-in-one cpp file includes other cpp files
354 # _result_file_list is the output file list
355 macro(PROCESS_ALLINONE_FILE _file_list _all_in_one_file _result_file_list _no_compile)
356     file(STRINGS ${_all_in_one_file} _all_in_one_file_content)
357     set(${_result_file_list} ${_file_list})
358     set(_allins "")
359     foreach (_line ${_all_in_one_file_content})
360         string(REGEX MATCH "^#include [\"<](.*)[\">]" _found ${_line})
361         if (_found)
362             list(APPEND _allins ${CMAKE_MATCH_1})
363         endif ()
364     endforeach ()
365
366     foreach (_allin ${_allins})
367         if (${_no_compile})
368             # For DerivedSources.cpp, we still need the derived sources to be generated, but we do not want them to be compiled
369             # individually. We add the header to the result file list so that CMake knows to keep generating the files.
370             string(REGEX REPLACE "(.*)\\.cpp" "\\1" _allin_no_ext ${_allin})
371             string(REGEX REPLACE ";([^;]*/)${_allin_no_ext}\\.cpp;" ";\\1${_allin_no_ext}.h;" _new_result "${${_result_file_list}};")
372         else ()
373             string(REGEX REPLACE ";[^;]*/${_allin};" ";" _new_result "${${_result_file_list}};")
374         endif ()
375         set(${_result_file_list} ${_new_result})
376     endforeach ()
377
378 endmacro()