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