Progress towards CMake on Mac.
[WebKit-https.git] / Source / cmake / WebKitMacros.cmake
1 macro(INCLUDE_IF_EXISTS _file)
2     if (EXISTS ${_file})
3         message(STATUS "Using platform-specific CMakeLists: ${_file}")
4         include(${_file})
5     else ()
6         message(STATUS "Platform-specific CMakeLists not found: ${_file}")
7     endif ()
8 endmacro()
9
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
27 # Helper macro which wraps generate-bindings.pl script.
28 #   _output_source is a list name which will contain generated sources.(eg. WebCore_SOURCES)
29 #   _input_files are IDL files to generate.
30 #   _base_dir is base directory where script is called.
31 #   _idl_includes is value of --include argument. (eg. --include=${WEBCORE_DIR}/bindings/js)
32 #   _features is a value of --defines argument.
33 #   _destination is a value of --outputDir argument.
34 #   _prefix is a prefix of output files. (eg. JS - it makes JSXXX.cpp JSXXX.h from XXX.idl)
35 #   _generator is a value of --generator argument.
36 #   _supplemental_dependency_file is a value of --supplementalDependencyFile. (optional)
37 macro(GENERATE_BINDINGS _output_source _input_files _base_dir _idl_includes _features _destination _prefix _generator _extension _idl_attributes_file)
38     set(BINDING_GENERATOR ${WEBCORE_DIR}/bindings/scripts/generate-bindings.pl)
39     set(_args ${ARGN})
40     list(LENGTH _args _argCount)
41     if (_argCount EQUAL 5)
42         list(GET _args 0 _supplemental_dependency_file)
43         if (_supplemental_dependency_file)
44             set(_supplemental_dependency --supplementalDependencyFile ${_supplemental_dependency_file})
45         endif ()
46         list(GET _args 1 _window_constructors_file)
47         list(GET _args 2 _workerglobalscope_constructors_file)
48         list(GET _args 3 _sharedworkerglobalscope_constructors_file)
49         list(GET _args 4 _dedicatedworkerglobalscope_constructors_file)
50     endif ()
51
52     set(COMMON_GENERATOR_DEPENDENCIES
53         ${BINDING_GENERATOR}
54         ${WEBCORE_DIR}/bindings/scripts/CodeGenerator.pm
55         ${SCRIPTS_BINDINGS}
56         ${_supplemental_dependency_file}
57         ${_idl_attributes_file}
58         ${_window_constructors_file}
59         ${_workerglobalscope_constructors_file}
60         ${_sharedworkerglobalscope_constructors_file}
61         ${_dedicatedworkerglobalscope_constructors_file}
62     )
63
64     if (EXISTS ${WEBCORE_DIR}/bindings/scripts/CodeGenerator${_generator}.pm)
65         list(APPEND COMMON_GENERATOR_DEPENDENCIES ${WEBCORE_DIR}/bindings/scripts/CodeGenerator${_generator}.pm)
66     endif ()
67
68     foreach (_file ${_input_files})
69         get_filename_component(_name ${_file} NAME_WE)
70
71         add_custom_command(
72             OUTPUT ${_destination}/${_prefix}${_name}.${_extension} ${_destination}/${_prefix}${_name}.h
73             MAIN_DEPENDENCY ${_file}
74             DEPENDS ${COMMON_GENERATOR_DEPENDENCIES}
75             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}
76             WORKING_DIRECTORY ${_base_dir}
77             VERBATIM)
78
79         list(APPEND ${_output_source} ${_destination}/${_prefix}${_name}.${_extension})
80     endforeach ()
81 endmacro()
82
83
84 macro(GENERATE_FONT_NAMES _infile)
85     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_names.pl)
86     set(_arguments  --fonts ${_infile})
87     set(_outputfiles ${DERIVED_SOURCES_WEBCORE_DIR}/WebKitFontFamilyNames.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/WebKitFontFamilyNames.h)
88
89     add_custom_command(
90         OUTPUT  ${_outputfiles}
91         MAIN_DEPENDENCY ${_infile}
92         DEPENDS ${MAKE_NAMES_DEPENDENCIES} ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
93         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR} ${_arguments}
94         VERBATIM)
95 endmacro()
96
97
98 macro(GENERATE_EVENT_FACTORY _infile _outfile)
99     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_event_factory.pl)
100
101     add_custom_command(
102         OUTPUT  ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile}
103         MAIN_DEPENDENCY ${_infile}
104         DEPENDS ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
105         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --input ${_infile} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR}
106         VERBATIM)
107 endmacro()
108
109
110 macro(GENERATE_EXCEPTION_CODE_DESCRIPTION _infile _outfile)
111     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_dom_exceptions.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_SETTINGS_MACROS _infile _outfile)
123     set(NAMES_GENERATOR ${WEBCORE_DIR}/page/make_settings.pl)
124
125     add_custom_command(
126         OUTPUT ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile} ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.h ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.idl
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_DOM_NAMES _namespace _attrs)
135     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_names.pl)
136     set(_arguments  --attrs ${_attrs})
137     set(_outputfiles ${DERIVED_SOURCES_WEBCORE_DIR}/${_namespace}Names.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/${_namespace}Names.h)
138     set(_extradef)
139     set(_tags)
140
141     foreach (f ${ARGN})
142         if (_tags)
143             set(_extradef "${_extradef} ${f}")
144         else ()
145             set(_tags ${f})
146         endif ()
147     endforeach ()
148
149     if (_tags)
150         set(_arguments "${_arguments}" --tags ${_tags} --factory --wrapperFactory)
151         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)
152     endif ()
153
154     if (_extradef)
155         set(_additionArguments "${_additionArguments}" --extraDefines=${_extradef})
156     endif ()
157
158     add_custom_command(
159         OUTPUT  ${_outputfiles}
160         DEPENDS ${MAKE_NAMES_DEPENDENCIES} ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS} ${_attrs} ${_tags}
161         COMMAND ${PERL_EXECUTABLE} -I${WEBCORE_DIR}/bindings/scripts ${NAMES_GENERATOR} --preprocessor "${CODE_GENERATOR_PREPROCESSOR_WITH_LINEMARKERS}" --outputDir ${DERIVED_SOURCES_WEBCORE_DIR} ${_arguments} ${_additionArguments}
162         VERBATIM)
163 endmacro()
164
165
166 macro(GENERATE_GRAMMAR _prefix _input _output_header _output_source _features)
167     # This is a workaround for winflexbison, which does not work corretly when
168     # run in a different working directory than the installation directory.
169     get_filename_component(_working_directory ${BISON_EXECUTABLE} PATH)
170
171     add_custom_command(
172         OUTPUT ${_output_header} ${_output_source}
173         MAIN_DEPENDENCY ${_input}
174         DEPENDS ${_input}
175         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}
176         WORKING_DIRECTORY ${_working_directory}
177         VERBATIM)
178 endmacro()
179
180 macro(MAKE_HASH_TOOLS _source)
181     get_filename_component(_name ${_source} NAME_WE)
182
183     if (${_source} STREQUAL "DocTypeStrings")
184         set(_hash_tools_h "${DERIVED_SOURCES_WEBCORE_DIR}/HashTools.h")
185     else ()
186         set(_hash_tools_h "")
187     endif ()
188
189     add_custom_command(
190         OUTPUT ${DERIVED_SOURCES_WEBCORE_DIR}/${_name}.cpp ${_hash_tools_h}
191         MAIN_DEPENDENCY ${_source}.gperf
192         COMMAND ${PERL_EXECUTABLE} ${WEBCORE_DIR}/make-hash-tools.pl ${DERIVED_SOURCES_WEBCORE_DIR} ${_source}.gperf
193         VERBATIM)
194
195     unset(_name)
196     unset(_hash_tools_h)
197 endmacro()
198
199 macro(WEBKIT_INCLUDE_CONFIG_FILES_IF_EXISTS)
200     if (PORT_FALLBACK)
201         INCLUDE_IF_EXISTS(${CMAKE_CURRENT_SOURCE_DIR}/Platform${PORT_FALLBACK}.cmake)
202     endif ()
203     INCLUDE_IF_EXISTS(${CMAKE_CURRENT_SOURCE_DIR}/Platform${PORT}.cmake)
204 endmacro()
205
206 macro(WEBKIT_WRAP_SOURCELIST)
207     foreach (_file ${ARGN})
208         get_filename_component(_basename ${_file} NAME_WE)
209         get_filename_component(_path ${_file} PATH)
210
211         if (NOT _file MATCHES "${DERIVED_SOURCES_WEBCORE_DIR}")
212             string(REGEX REPLACE "/" "\\\\\\\\" _sourcegroup "${_path}")
213             source_group("${_sourcegroup}" FILES ${_file})
214         endif ()
215     endforeach ()
216
217     source_group("DerivedSources" REGULAR_EXPRESSION "${DERIVED_SOURCES_WEBCORE_DIR}")
218 endmacro()
219
220
221 macro(WEBKIT_CREATE_FORWARDING_HEADER _target_directory _file)
222     get_filename_component(_source_path "${CMAKE_SOURCE_DIR}/Source/" ABSOLUTE)
223     get_filename_component(_absolute "${_file}" ABSOLUTE)
224     get_filename_component(_name "${_file}" NAME)
225     set(_target_filename "${_target_directory}/${_name}")
226
227     # Try to make the path in the forwarding header relative to the Source directory
228     # so that these forwarding headers are compatible with the ones created by the
229     # WebKit2 generate-forwarding-headers script.
230     string(REGEX REPLACE "${_source_path}/" "" _relative ${_absolute})
231
232     set(_content "#include \"${_relative}\"\n")
233
234     if (EXISTS "${_target_filename}")
235         file(READ "${_target_filename}" _old_content)
236     endif ()
237
238     if (NOT _old_content STREQUAL _content)
239         file(WRITE "${_target_filename}" "${_content}")
240     endif ()
241 endmacro()
242
243 macro(WEBKIT_CREATE_FORWARDING_HEADERS _framework)
244     set(_processing_directories 0)
245     set(_processing_files 0)
246     set(_target_directory "${DERIVED_SOURCES_DIR}/ForwardingHeaders/${_framework}")
247
248     file(GLOB _files "${_target_directory}/*.h")
249     foreach (_file ${_files})
250         file(READ "${_file}" _content)
251         string(REGEX MATCH "^#include \"([^\"]*)\"" _matched ${_content})
252         if (_matched AND NOT EXISTS "${CMAKE_SOURCE_DIR}/Source/${CMAKE_MATCH_1}")
253            file(REMOVE "${_file}")
254         endif ()
255     endforeach ()
256
257     foreach (_currentArg ${ARGN})
258         if ("${_currentArg}" STREQUAL "DIRECTORIES")
259             set(_processing_directories 1)
260             set(_processing_files 0)
261         elseif ("${_currentArg}" STREQUAL "FILES")
262             set(_processing_directories 0)
263             set(_processing_files 1)
264         elseif (_processing_directories)
265             file(GLOB _files "${_currentArg}/*.h")
266             foreach (_file ${_files})
267                 WEBKIT_CREATE_FORWARDING_HEADER(${_target_directory} ${_file})
268             endforeach ()
269         elseif (_processing_files)
270             WEBKIT_CREATE_FORWARDING_HEADER(${_target_directory} ${_currentArg})
271         endif ()
272     endforeach ()
273 endmacro()
274
275 # Helper macro which wraps generate-message-receiver.py and generate-message-header.py scripts
276 #   _output_source is a list name which will contain generated sources.(eg. WebKit2_SOURCES)
277 #   _input_files are messages.in files to generate.
278 macro(GENERATE_WEBKIT2_MESSAGE_SOURCES _output_source _input_files)
279     foreach (_file ${_input_files})
280         get_filename_component(_name ${_file} NAME_WE)
281         add_custom_command(
282             OUTPUT ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}MessageReceiver.cpp ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}Messages.h
283             MAIN_DEPENDENCY ${_file}
284             DEPENDS ${WEBKIT2_DIR}/Scripts/webkit2/__init__.py
285                     ${WEBKIT2_DIR}/Scripts/webkit2/messages.py
286                     ${WEBKIT2_DIR}/Scripts/webkit2/model.py
287                     ${WEBKIT2_DIR}/Scripts/webkit2/parser.py
288             COMMAND ${PYTHON_EXECUTABLE} ${WEBKIT2_DIR}/Scripts/generate-message-receiver.py ${_file} > ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}MessageReceiver.cpp
289             COMMAND ${PYTHON_EXECUTABLE} ${WEBKIT2_DIR}/Scripts/generate-messages-header.py ${_file} > ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}Messages.h
290             WORKING_DIRECTORY ${WEBKIT2_DIR}
291             VERBATIM)
292
293         list(APPEND ${_output_source} ${DERIVED_SOURCES_WEBKIT2_DIR}/${_name}MessageReceiver.cpp)
294     endforeach ()
295 endmacro()