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