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