[Settings] Generate Settings.h/cpp
[WebKit-https.git] / Source / WebCore / WebCoreMacros.cmake
1 # Helper macro for using all-in-one builds
2 # This macro removes the sources included in the _all_in_one_file from the input _file_list.
3 # _file_list is a list of source files
4 # _all_in_one_file is an all-in-one cpp file includes other cpp files
5 # _result_file_list is the output file list
6 macro(PROCESS_ALLINONE_FILE _file_list _all_in_one_file _result_file_list _no_compile)
7     file(STRINGS ${_all_in_one_file} _all_in_one_file_content)
8     set(${_result_file_list} ${_file_list})
9     set(_allins "")
10     foreach (_line ${_all_in_one_file_content})
11         string(REGEX MATCH "^#include [\"<](.*)[\">]" _found ${_line})
12         if (_found)
13             list(APPEND _allins ${CMAKE_MATCH_1})
14         endif ()
15     endforeach ()
16
17     foreach (_allin ${_allins})
18         if (${_no_compile})
19             # For DerivedSources.cpp, we still need the derived sources to be generated, but we do not want them to be compiled
20             # individually. We add the header to the result file list so that CMake knows to keep generating the files.
21             string(REGEX REPLACE "(.*)\\.cpp" "\\1" _allin_no_ext ${_allin})
22             string(REGEX REPLACE ";([^;]*/)${_allin_no_ext}\\.cpp;" ";\\1${_allin_no_ext}.h;" _new_result "${${_result_file_list}};")
23         else ()
24             string(REGEX REPLACE ";[^;]*/${_allin};" ";" _new_result "${${_result_file_list}};")
25         endif ()
26         set(${_result_file_list} ${_new_result})
27     endforeach ()
28
29 endmacro()
30
31
32 macro(MAKE_HASH_TOOLS _source)
33     get_filename_component(_name ${_source} NAME_WE)
34
35     if (${_source} STREQUAL "DocTypeStrings")
36         set(_hash_tools_h "${DERIVED_SOURCES_WEBCORE_DIR}/HashTools.h")
37     else ()
38         set(_hash_tools_h "")
39     endif ()
40
41     add_custom_command(
42         OUTPUT ${DERIVED_SOURCES_WEBCORE_DIR}/${_name}.cpp ${_hash_tools_h}
43         MAIN_DEPENDENCY ${_source}.gperf
44         COMMAND ${PERL_EXECUTABLE} ${WEBCORE_DIR}/make-hash-tools.pl ${DERIVED_SOURCES_WEBCORE_DIR} ${_source}.gperf ${GPERF_EXECUTABLE}
45         VERBATIM)
46
47     unset(_name)
48     unset(_hash_tools_h)
49 endmacro()
50
51
52 # Append the given dependencies to the source file
53 # This one consider the given dependencies are in ${DERIVED_SOURCES_WEBCORE_DIR}
54 # and prepends this to every member of dependencies list
55 macro(ADD_SOURCE_WEBCORE_DERIVED_DEPENDENCIES _source _deps)
56     set(_tmp "")
57     foreach (f ${_deps})
58         list(APPEND _tmp "${DERIVED_SOURCES_WEBCORE_DIR}/${f}")
59     endforeach ()
60
61     WEBKIT_ADD_SOURCE_DEPENDENCIES(${_source} ${_tmp})
62     unset(_tmp)
63 endmacro()
64
65
66 macro(MAKE_JS_FILE_ARRAYS _output_cpp _output_h _namespace _scripts _scripts_dependencies)
67     add_custom_command(
68         OUTPUT ${_output_h} ${_output_cpp}
69         DEPENDS ${JavaScriptCore_SCRIPTS_DIR}/make-js-file-arrays.py ${${_scripts}}
70         COMMAND ${PYTHON_EXECUTABLE} ${JavaScriptCore_SCRIPTS_DIR}/make-js-file-arrays.py -n ${_namespace} ${_output_h} ${_output_cpp} ${${_scripts}}
71         VERBATIM)
72     WEBKIT_ADD_SOURCE_DEPENDENCIES(${${_scripts_dependencies}} ${_output_h} ${_output_cpp})
73 endmacro()
74
75
76 option(SHOW_BINDINGS_GENERATION_PROGRESS "Show progress of generating bindings" OFF)
77
78 # Helper macro which wraps generate-bindings-all.pl script.
79 #   target is a new target name to be added
80 #   OUTPUT_SOURCE is a list name which will contain generated sources.(eg. WebCore_SOURCES)
81 #   INPUT_FILES are IDL files to generate.
82 #   BASE_DIR is base directory where script is called.
83 #   IDL_INCLUDES is value of --include argument. (eg. ${WEBCORE_DIR}/bindings/js)
84 #   FEATURES is a value of --defines argument.
85 #   DESTINATION is a value of --outputDir argument.
86 #   GENERATOR is a value of --generator argument.
87 #   SUPPLEMENTAL_DEPFILE is a value of --supplementalDependencyFile. (optional)
88 #   PP_EXTRA_OUTPUT is extra outputs of preprocess-idls.pl. (optional)
89 #   PP_EXTRA_ARGS is extra arguments for preprocess-idls.pl. (optional)
90 function(GENERATE_BINDINGS target)
91     set(options)
92     set(oneValueArgs OUTPUT_SOURCE BASE_DIR FEATURES DESTINATION GENERATOR SUPPLEMENTAL_DEPFILE)
93     set(multiValueArgs INPUT_FILES IDL_INCLUDES PP_EXTRA_OUTPUT PP_EXTRA_ARGS)
94     cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
95     set(binding_generator ${WEBCORE_DIR}/bindings/scripts/generate-bindings-all.pl)
96     set(idl_attributes_file ${WEBCORE_DIR}/bindings/scripts/IDLAttributes.json)
97     set(idl_files_list ${CMAKE_CURRENT_BINARY_DIR}/idl_files_${target}.tmp)
98     set(_supplemental_dependency)
99
100     set(content)
101     foreach (f ${arg_INPUT_FILES})
102         if (NOT IS_ABSOLUTE ${f})
103             set(f ${CMAKE_CURRENT_SOURCE_DIR}/${f})
104         endif ()
105         set(content "${content}${f}\n")
106     endforeach ()
107     file(WRITE ${idl_files_list} ${content})
108
109     set(args
110         --defines ${arg_FEATURES}
111         --generator ${arg_GENERATOR}
112         --outputDir ${arg_DESTINATION}
113         --idlFilesList ${idl_files_list}
114         --preprocessor "${CODE_GENERATOR_PREPROCESSOR}"
115         --idlAttributesFile ${idl_attributes_file}
116     )
117     if (arg_SUPPLEMENTAL_DEPFILE)
118         list(APPEND args --supplementalDependencyFile ${arg_SUPPLEMENTAL_DEPFILE})
119     endif ()
120     ProcessorCount(PROCESSOR_COUNT)
121     if (PROCESSOR_COUNT)
122         list(APPEND args --numOfJobs ${PROCESSOR_COUNT})
123     endif ()
124     foreach (i IN LISTS arg_IDL_INCLUDES)
125         if (IS_ABSOLUTE ${i})
126             list(APPEND args --include ${i})
127         else ()
128             list(APPEND args --include ${CMAKE_CURRENT_SOURCE_DIR}/${i})
129         endif ()
130     endforeach ()
131     foreach (i IN LISTS arg_PP_EXTRA_OUTPUT)
132         list(APPEND args --ppExtraOutput ${i})
133     endforeach ()
134     foreach (i IN LISTS arg_PP_EXTRA_ARGS)
135         list(APPEND args --ppExtraArgs ${i})
136     endforeach ()
137
138     set(common_generator_dependencies
139         ${WEBCORE_DIR}/bindings/scripts/generate-bindings.pl
140         ${SCRIPTS_BINDINGS}
141         # Changing enabled features should trigger recompiling all IDL files
142         # because some of them use #if.
143         ${CMAKE_BINARY_DIR}/cmakeconfig.h
144     )
145     if (EXISTS ${WEBCORE_DIR}/bindings/scripts/CodeGenerator${arg_GENERATOR}.pm)
146         list(APPEND common_generator_dependencies ${WEBCORE_DIR}/bindings/scripts/CodeGenerator${arg_GENERATOR}.pm)
147     endif ()
148     if (EXISTS ${arg_BASE_DIR}/CodeGenerator${arg_GENERATOR}.pm)
149         list(APPEND common_generator_dependencies ${arg_BASE_DIR}/CodeGenerator${arg_GENERATOR}.pm)
150     endif ()
151     foreach (i IN LISTS common_generator_dependencies)
152         list(APPEND args --generatorDependency ${i})
153     endforeach ()
154
155     set(gen_sources)
156     set(gen_headers)
157     foreach (_file ${arg_INPUT_FILES})
158         get_filename_component(_name ${_file} NAME_WE)
159         list(APPEND gen_sources ${arg_DESTINATION}/JS${_name}.cpp)
160         list(APPEND gen_headers ${arg_DESTINATION}/JS${_name}.h)
161     endforeach ()
162     set(${arg_OUTPUT_SOURCE} ${${arg_OUTPUT_SOURCE}} ${gen_sources} PARENT_SCOPE)
163     set(act_args)
164     if (SHOW_BINDINGS_GENERATION_PROGRESS)
165         list(APPEND args --showProgress)
166     endif ()
167     list(APPEND act_args BYPRODUCTS ${gen_sources} ${gen_headers})
168     if (SHOW_BINDINGS_GENERATION_PROGRESS)
169         list(APPEND act_args USES_TERMINAL)
170     endif ()
171     add_custom_target(${target}
172         COMMAND ${PERL_EXECUTABLE} ${binding_generator} ${args}
173         WORKING_DIRECTORY ${arg_BASE_DIR}
174         COMMENT "Generate bindings (${target})"
175         VERBATIM ${act_args})
176 endfunction()
177
178
179 macro(GENERATE_FONT_NAMES _infile)
180     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_names.pl)
181     set(_arguments  --fonts ${_infile})
182     set(_outputfiles ${DERIVED_SOURCES_WEBCORE_DIR}/WebKitFontFamilyNames.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/WebKitFontFamilyNames.h)
183
184     add_custom_command(
185         OUTPUT  ${_outputfiles}
186         MAIN_DEPENDENCY ${_infile}
187         DEPENDS ${MAKE_NAMES_DEPENDENCIES} ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
188         COMMAND ${PERL_EXECUTABLE} ${NAMES_GENERATOR} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR} ${_arguments}
189         VERBATIM)
190 endmacro()
191
192
193 macro(GENERATE_EVENT_FACTORY _infile _outfile)
194     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_event_factory.pl)
195
196     add_custom_command(
197         OUTPUT  ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile}
198         MAIN_DEPENDENCY ${_infile}
199         DEPENDS ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS}
200         COMMAND ${PERL_EXECUTABLE} ${NAMES_GENERATOR} --input ${_infile} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR}
201         VERBATIM)
202 endmacro()
203
204
205 macro(GENERATE_SETTINGS_MACROS _infile _outfile)
206     set(NAMES_GENERATOR ${WEBCORE_DIR}/Scripts/GenerateSettings.py)
207
208     # Do not list the output in more than one independent target that may
209     # build in parallel or the two instances of the rule may conflict.
210     # <https://cmake.org/cmake/help/v3.0/command/add_custom_command.html>
211     set(_extra_output
212         ${DERIVED_SOURCES_WEBCORE_DIR}/Settings.h
213         ${DERIVED_SOURCES_WEBCORE_DIR}/Settings.cpp
214         ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.h
215         ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.cpp
216         ${DERIVED_SOURCES_WEBCORE_DIR}/InternalSettingsGenerated.idl
217     )
218
219     set(GENERATE_SETTINGS_SCRIPTS
220         ${WEBCORE_DIR}/Scripts/GenerateSettings/GenerateInternalSettingsHeaderFile.py
221         ${WEBCORE_DIR}/Scripts/GenerateSettings/GenerateInternalSettingsIDLFile.py
222         ${WEBCORE_DIR}/Scripts/GenerateSettings/GenerateInternalSettingsImplementationFile.py
223         ${WEBCORE_DIR}/Scripts/GenerateSettings/GenerateSettingsHeaderFile.py
224         ${WEBCORE_DIR}/Scripts/GenerateSettings/GenerateSettingsImplementationFile.py
225         ${WEBCORE_DIR}/Scripts/GenerateSettings/GenerateSettingsMacrosHeader.py
226         ${WEBCORE_DIR}/Scripts/GenerateSettings/GenerateSettings.py
227         ${WEBCORE_DIR}/Scripts/GenerateSettings/Settings.py
228         ${WEBCORE_DIR}/Scripts/GenerateSettings/__init__.py
229     )
230
231     set(_args BYPRODUCTS ${_extra_output})
232     add_custom_command(
233         OUTPUT ${DERIVED_SOURCES_WEBCORE_DIR}/${_outfile}
234         MAIN_DEPENDENCY ${_infile}
235         DEPENDS ${NAMES_GENERATOR} ${GENERATE_SETTINGS_SCRIPTS} ${SCRIPTS_BINDINGS}
236         COMMAND ${PYTHON_EXECUTABLE} ${NAMES_GENERATOR} --input ${_infile} --outputDir ${DERIVED_SOURCES_WEBCORE_DIR}
237         VERBATIM ${_args})
238 endmacro()
239
240
241 macro(GENERATE_DOM_NAMES _namespace _attrs)
242     set(NAMES_GENERATOR ${WEBCORE_DIR}/dom/make_names.pl)
243     set(_arguments  --attrs ${_attrs})
244     set(_outputfiles ${DERIVED_SOURCES_WEBCORE_DIR}/${_namespace}Names.cpp ${DERIVED_SOURCES_WEBCORE_DIR}/${_namespace}Names.h)
245     set(_extradef)
246     set(_tags)
247
248     foreach (f ${ARGN})
249         if (_tags)
250             set(_extradef "${_extradef} ${f}")
251         else ()
252             set(_tags ${f})
253         endif ()
254     endforeach ()
255
256     if (_tags)
257         set(_arguments "${_arguments}" --tags ${_tags} --factory --wrapperFactory)
258         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)
259     endif ()
260
261     if (_extradef)
262         set(_additionArguments "${_additionArguments}" --extraDefines=${_extradef})
263     endif ()
264
265     add_custom_command(
266         OUTPUT  ${_outputfiles}
267         DEPENDS ${MAKE_NAMES_DEPENDENCIES} ${NAMES_GENERATOR} ${SCRIPTS_BINDINGS} ${_attrs} ${_tags}
268         COMMAND ${PERL_EXECUTABLE} ${NAMES_GENERATOR} --preprocessor "${CODE_GENERATOR_PREPROCESSOR_WITH_LINEMARKERS}" --outputDir ${DERIVED_SOURCES_WEBCORE_DIR} ${_arguments} ${_additionArguments}
269         VERBATIM)
270 endmacro()