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