blob: 7dbf82c14e46df83261b502b7a8a9c26259964a3 [file] [log] [blame]
bungeman623ef922016-09-23 08:16:04 -07001#!/usr/bin/env python
2#
3# Copyright 2016 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
bungemane95ea082016-09-28 16:45:35 -04008
bungeman623ef922016-09-23 08:16:04 -07009"""
10Usage: gn_to_cmake.py <json_file_name>
11
12gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py
13
14or
15
16gn gen out/config --ide=json
17python gn/gn_to_cmake.py out/config/project.json
Ben Wagner388faa02016-10-05 17:18:38 -040018
19The first is recommended, as it will auto-update.
bungeman623ef922016-09-23 08:16:04 -070020"""
21
bungemane95ea082016-09-28 16:45:35 -040022
Ben Wagnerdc69dc72016-10-04 16:44:44 -040023import itertools
24import functools
bungeman623ef922016-09-23 08:16:04 -070025import json
26import posixpath
27import os
Ben Wagnerdc69dc72016-10-04 16:44:44 -040028import string
bungeman623ef922016-09-23 08:16:04 -070029import sys
30
bungeman623ef922016-09-23 08:16:04 -070031
bungemane95ea082016-09-28 16:45:35 -040032def CMakeStringEscape(a):
33 """Escapes the string 'a' for use inside a CMake string.
bungeman623ef922016-09-23 08:16:04 -070034
bungemane95ea082016-09-28 16:45:35 -040035 This means escaping
36 '\' otherwise it may be seen as modifying the next character
37 '"' otherwise it will end the string
38 ';' otherwise the string becomes a list
bungeman623ef922016-09-23 08:16:04 -070039
bungemane95ea082016-09-28 16:45:35 -040040 The following do not need to be escaped
41 '#' when the lexer is in string state, this does not start a comment
42 """
43 return a.replace('\\', '\\\\').replace(';', '\\;').replace('"', '\\"')
bungeman623ef922016-09-23 08:16:04 -070044
bungeman623ef922016-09-23 08:16:04 -070045
Ben Wagnerdc69dc72016-10-04 16:44:44 -040046def CMakeTargetEscape(a):
47 """Escapes the string 'a' for use as a CMake target name.
48
49 CMP0037 in CMake 3.0 restricts target names to "^[A-Za-z0-9_.:+-]+$"
50 The ':' is only allowed for imported targets.
51 """
52 def Escape(c):
53 if c in string.ascii_letters or c in string.digits or c in '_.+-':
54 return c
55 else:
56 return '__'
57 return ''.join(map(Escape, a))
58
59
bungemane95ea082016-09-28 16:45:35 -040060def SetVariable(out, variable_name, value):
61 """Sets a CMake variable."""
Ben Wagnerdc69dc72016-10-04 16:44:44 -040062 out.write('set("')
63 out.write(CMakeStringEscape(variable_name))
64 out.write('" "')
bungemane95ea082016-09-28 16:45:35 -040065 out.write(CMakeStringEscape(value))
66 out.write('")\n')
bungeman623ef922016-09-23 08:16:04 -070067
bungemane95ea082016-09-28 16:45:35 -040068
69def SetVariableList(out, variable_name, values):
70 """Sets a CMake variable to a list."""
71 if not values:
72 return SetVariable(out, variable_name, "")
73 if len(values) == 1:
74 return SetVariable(out, variable_name, values[0])
Ben Wagnerdc69dc72016-10-04 16:44:44 -040075 out.write('list(APPEND "')
76 out.write(CMakeStringEscape(variable_name))
77 out.write('"\n "')
bungemane95ea082016-09-28 16:45:35 -040078 out.write('"\n "'.join([CMakeStringEscape(value) for value in values]))
79 out.write('")\n')
80
81
82def SetFilesProperty(output, variable, property_name, values, sep):
83 """Given a set of source files, sets the given property on them."""
84 output.write('set_source_files_properties(')
85 WriteVariable(output, variable)
86 output.write(' PROPERTIES ')
87 output.write(property_name)
88 output.write(' "')
89 for value in values:
90 output.write(CMakeStringEscape(value))
91 output.write(sep)
92 output.write('")\n')
93
94
Ben Wagnerdc69dc72016-10-04 16:44:44 -040095def SetCurrentTargetProperty(out, property_name, values, sep=''):
bungemane95ea082016-09-28 16:45:35 -040096 """Given a target, sets the given property."""
Ben Wagnerdc69dc72016-10-04 16:44:44 -040097 out.write('set_target_properties("${target}" PROPERTIES ')
bungemane95ea082016-09-28 16:45:35 -040098 out.write(property_name)
99 out.write(' "')
100 for value in values:
101 out.write(CMakeStringEscape(value))
102 out.write(sep)
103 out.write('")\n')
104
105
106def WriteVariable(output, variable_name, prepend=None):
107 if prepend:
108 output.write(prepend)
109 output.write('${')
110 output.write(variable_name)
111 output.write('}')
112
113
bungemane95ea082016-09-28 16:45:35 -0400114# See GetSourceFileType in gn
115source_file_types = {
116 '.cc': 'cxx',
117 '.cpp': 'cxx',
118 '.cxx': 'cxx',
Brian Salomon4ce69892019-07-12 14:36:03 -0400119 '.m': 'objc',
120 '.mm': 'objcc',
bungemane95ea082016-09-28 16:45:35 -0400121 '.c': 'c',
122 '.s': 'asm',
123 '.S': 'asm',
124 '.asm': 'asm',
125 '.o': 'obj',
126 '.obj': 'obj',
127}
128
129
130class CMakeTargetType(object):
131 def __init__(self, command, modifier, property_modifier, is_linkable):
132 self.command = command
133 self.modifier = modifier
134 self.property_modifier = property_modifier
135 self.is_linkable = is_linkable
136CMakeTargetType.custom = CMakeTargetType('add_custom_target', 'SOURCES',
137 None, False)
138
139# See GetStringForOutputType in gn
140cmake_target_types = {
141 'unknown': CMakeTargetType.custom,
142 'group': CMakeTargetType.custom,
143 'executable': CMakeTargetType('add_executable', None, 'RUNTIME', True),
144 'loadable_module': CMakeTargetType('add_library', 'MODULE', 'LIBRARY', True),
145 'shared_library': CMakeTargetType('add_library', 'SHARED', 'LIBRARY', True),
Kevin Lubickd5c6e162019-02-01 15:34:04 -0500146 'static_library': CMakeTargetType('add_library', 'STATIC', 'ARCHIVE', True),
bungemane95ea082016-09-28 16:45:35 -0400147 'source_set': CMakeTargetType('add_library', 'OBJECT', None, False),
Ben Wagnerbc344042016-09-29 15:41:53 -0400148 'copy': CMakeTargetType.custom,
bungemane95ea082016-09-28 16:45:35 -0400149 'action': CMakeTargetType.custom,
150 'action_foreach': CMakeTargetType.custom,
151 'bundle_data': CMakeTargetType.custom,
152 'create_bundle': CMakeTargetType.custom,
153}
154
155
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400156def FindFirstOf(s, a):
157 return min(s.find(i) for i in a if i in s)
Ben Wagnerbc344042016-09-29 15:41:53 -0400158
159
160class Project(object):
161 def __init__(self, project_json):
162 self.targets = project_json['targets']
163 build_settings = project_json['build_settings']
164 self.root_path = build_settings['root_path']
165 self.build_path = posixpath.join(self.root_path,
166 build_settings['build_dir'][2:])
167
168 def GetAbsolutePath(self, path):
169 if path.startswith("//"):
170 return self.root_path + "/" + path[2:]
171 else:
172 return path
173
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400174 def GetObjectSourceDependencies(self, gn_target_name, object_dependencies):
175 """All OBJECT libraries whose sources have not been absorbed."""
Ben Wagnerbc344042016-09-29 15:41:53 -0400176 dependencies = self.targets[gn_target_name].get('deps', [])
177 for dependency in dependencies:
178 dependency_type = self.targets[dependency].get('type', None)
179 if dependency_type == 'source_set':
180 object_dependencies.add(dependency)
181 if dependency_type not in gn_target_types_that_absorb_objects:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400182 self.GetObjectSourceDependencies(dependency, object_dependencies)
183
184 def GetObjectLibraryDependencies(self, gn_target_name, object_dependencies):
185 """All OBJECT libraries whose libraries have not been absorbed."""
186 dependencies = self.targets[gn_target_name].get('deps', [])
187 for dependency in dependencies:
188 dependency_type = self.targets[dependency].get('type', None)
189 if dependency_type == 'source_set':
190 object_dependencies.add(dependency)
191 self.GetObjectLibraryDependencies(dependency, object_dependencies)
Ben Wagnerbc344042016-09-29 15:41:53 -0400192
193 def GetCMakeTargetName(self, gn_target_name):
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400194 # See <chromium>/src/tools/gn/label.cc#Resolve
195 # //base/test:test_support(//build/toolchain/win:msvc)
196 path_separator = FindFirstOf(gn_target_name, (':', '('))
197 location = None
198 name = None
199 toolchain = None
200 if not path_separator:
201 location = gn_target_name[2:]
202 else:
203 location = gn_target_name[2:path_separator]
204 toolchain_separator = gn_target_name.find('(', path_separator)
205 if toolchain_separator == -1:
206 name = gn_target_name[path_separator + 1:]
207 else:
208 if toolchain_separator > path_separator:
209 name = gn_target_name[path_separator + 1:toolchain_separator]
210 assert gn_target_name.endswith(')')
211 toolchain = gn_target_name[toolchain_separator + 1:-1]
212 assert location or name
213
214 cmake_target_name = None
215 if location.endswith('/' + name):
216 cmake_target_name = location
217 elif location:
218 cmake_target_name = location + '_' + name
219 else:
220 cmake_target_name = name
221 if toolchain:
222 cmake_target_name += '--' + toolchain
223 return CMakeTargetEscape(cmake_target_name)
Ben Wagnerbc344042016-09-29 15:41:53 -0400224
225
bungemane95ea082016-09-28 16:45:35 -0400226class Target(object):
Ben Wagnerbc344042016-09-29 15:41:53 -0400227 def __init__(self, gn_target_name, project):
228 self.gn_name = gn_target_name
229 self.properties = project.targets[self.gn_name]
230 self.cmake_name = project.GetCMakeTargetName(self.gn_name)
bungemane95ea082016-09-28 16:45:35 -0400231 self.gn_type = self.properties.get('type', None)
232 self.cmake_type = cmake_target_types.get(self.gn_type, None)
233
234
Ben Wagnerbc344042016-09-29 15:41:53 -0400235def WriteAction(out, target, project, sources, synthetic_dependencies):
236 outputs = []
237 output_directories = set()
238 for output in target.properties.get('outputs', []):
239 output_abs_path = project.GetAbsolutePath(output)
240 outputs.append(output_abs_path)
241 output_directory = posixpath.dirname(output_abs_path)
242 if output_directory:
243 output_directories.add(output_directory)
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400244 outputs_name = '${target}__output'
Ben Wagnerbc344042016-09-29 15:41:53 -0400245 SetVariableList(out, outputs_name, outputs)
246
247 out.write('add_custom_command(OUTPUT ')
248 WriteVariable(out, outputs_name)
249 out.write('\n')
250
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400251 if output_directories:
252 out.write(' COMMAND ${CMAKE_COMMAND} -E make_directory "')
253 out.write('" "'.join(map(CMakeStringEscape, output_directories)))
254 out.write('"\n')
Ben Wagnerbc344042016-09-29 15:41:53 -0400255
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400256 script = target.properties['script']
257 arguments = target.properties['args']
258 out.write(' COMMAND python "')
259 out.write(CMakeStringEscape(project.GetAbsolutePath(script)))
260 out.write('"')
261 if arguments:
262 out.write('\n "')
263 out.write('"\n "'.join(map(CMakeStringEscape, arguments)))
264 out.write('"')
Ben Wagnerbc344042016-09-29 15:41:53 -0400265 out.write('\n')
266
267 out.write(' DEPENDS ')
268 for sources_type_name in sources.values():
269 WriteVariable(out, sources_type_name, ' ')
270 out.write('\n')
271
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400272 #TODO: CMake 3.7 is introducing DEPFILE
273
274 out.write(' WORKING_DIRECTORY "')
275 out.write(CMakeStringEscape(project.build_path))
276 out.write('"\n')
277
278 out.write(' COMMENT "Action: ${target}"\n')
279
280 out.write(' VERBATIM)\n')
281
282 synthetic_dependencies.add(outputs_name)
283
284
285def ExpandPlaceholders(source, a):
286 source_dir, source_file_part = posixpath.split(source)
287 source_name_part, _ = posixpath.splitext(source_file_part)
288 #TODO: {{source_gen_dir}}, {{source_out_dir}}, {{response_file_name}}
289 return a.replace('{{source}}', source) \
290 .replace('{{source_file_part}}', source_file_part) \
291 .replace('{{source_name_part}}', source_name_part) \
292 .replace('{{source_dir}}', source_dir) \
293 .replace('{{source_root_relative_dir}}', source_dir)
294
295
296def WriteActionForEach(out, target, project, sources, synthetic_dependencies):
297 all_outputs = target.properties.get('outputs', [])
298 inputs = target.properties.get('sources', [])
299 # TODO: consider expanding 'output_patterns' instead.
300 outputs_per_input = len(all_outputs) / len(inputs)
301 for count, source in enumerate(inputs):
302 source_abs_path = project.GetAbsolutePath(source)
303
304 outputs = []
305 output_directories = set()
306 for output in all_outputs[outputs_per_input * count:
307 outputs_per_input * (count+1)]:
308 output_abs_path = project.GetAbsolutePath(output)
309 outputs.append(output_abs_path)
310 output_directory = posixpath.dirname(output_abs_path)
311 if output_directory:
312 output_directories.add(output_directory)
313 outputs_name = '${target}__output_' + str(count)
314 SetVariableList(out, outputs_name, outputs)
315
316 out.write('add_custom_command(OUTPUT ')
317 WriteVariable(out, outputs_name)
318 out.write('\n')
319
320 if output_directories:
321 out.write(' COMMAND ${CMAKE_COMMAND} -E make_directory "')
322 out.write('" "'.join(map(CMakeStringEscape, output_directories)))
323 out.write('"\n')
324
325 script = target.properties['script']
326 # TODO: need to expand {{xxx}} in arguments
327 arguments = target.properties['args']
328 out.write(' COMMAND python "')
329 out.write(CMakeStringEscape(project.GetAbsolutePath(script)))
330 out.write('"')
331 if arguments:
332 out.write('\n "')
333 expand = functools.partial(ExpandPlaceholders, source_abs_path)
334 out.write('"\n "'.join(map(CMakeStringEscape, map(expand,arguments))))
335 out.write('"')
336 out.write('\n')
337
338 out.write(' DEPENDS')
339 if 'input' in sources:
340 WriteVariable(out, sources['input'], ' ')
341 out.write(' "')
342 out.write(CMakeStringEscape(source_abs_path))
343 out.write('"\n')
344
345 #TODO: CMake 3.7 is introducing DEPFILE
346
347 out.write(' WORKING_DIRECTORY "')
348 out.write(CMakeStringEscape(project.build_path))
349 out.write('"\n')
350
351 out.write(' COMMENT "Action ${target} on ')
352 out.write(CMakeStringEscape(source_abs_path))
353 out.write('"\n')
354
355 out.write(' VERBATIM)\n')
356
357 synthetic_dependencies.add(outputs_name)
358
359
360def WriteCopy(out, target, project, sources, synthetic_dependencies):
361 inputs = target.properties.get('sources', [])
362 raw_outputs = target.properties.get('outputs', [])
363
364 # TODO: consider expanding 'output_patterns' instead.
365 outputs = []
366 for output in raw_outputs:
367 output_abs_path = project.GetAbsolutePath(output)
368 outputs.append(output_abs_path)
369 outputs_name = '${target}__output'
370 SetVariableList(out, outputs_name, outputs)
371
372 out.write('add_custom_command(OUTPUT ')
373 WriteVariable(out, outputs_name)
Ben Wagnerbc344042016-09-29 15:41:53 -0400374 out.write('\n')
375
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400376 for src, dst in zip(inputs, outputs):
Kevin Lubick9a2bb092019-02-01 13:32:44 -0500377 abs_src_path = CMakeStringEscape(project.GetAbsolutePath(src))
378 # CMake distinguishes between copying files and copying directories but
379 # gn does not. We assume if the src has a period in its name then it is
380 # a file and otherwise a directory.
381 if "." in os.path.basename(abs_src_path):
382 out.write(' COMMAND ${CMAKE_COMMAND} -E copy "')
383 else:
384 out.write(' COMMAND ${CMAKE_COMMAND} -E copy_directory "')
385 out.write(abs_src_path)
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400386 out.write('" "')
387 out.write(CMakeStringEscape(dst))
388 out.write('"\n')
389
390 out.write(' DEPENDS ')
391 for sources_type_name in sources.values():
392 WriteVariable(out, sources_type_name, ' ')
Ben Wagnerbc344042016-09-29 15:41:53 -0400393 out.write('\n')
394
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400395 out.write(' WORKING_DIRECTORY "')
396 out.write(CMakeStringEscape(project.build_path))
397 out.write('"\n')
398
399 out.write(' COMMENT "Copy ${target}"\n')
400
Ben Wagnerbc344042016-09-29 15:41:53 -0400401 out.write(' VERBATIM)\n')
402
403 synthetic_dependencies.add(outputs_name)
404
405
406def WriteCompilerFlags(out, target, project, sources):
bungemane95ea082016-09-28 16:45:35 -0400407 # Hack, set linker language to c if no c or cxx files present.
408 if not 'c' in sources and not 'cxx' in sources:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400409 SetCurrentTargetProperty(out, 'LINKER_LANGUAGE', ['C'])
bungemane95ea082016-09-28 16:45:35 -0400410
411 # Mark uncompiled sources as uncompiled.
Ben Wagnerbc344042016-09-29 15:41:53 -0400412 if 'input' in sources:
413 SetFilesProperty(out, sources['input'], 'HEADER_FILE_ONLY', ('True',), '')
bungemane95ea082016-09-28 16:45:35 -0400414 if 'other' in sources:
Ben Wagnerbc344042016-09-29 15:41:53 -0400415 SetFilesProperty(out, sources['other'], 'HEADER_FILE_ONLY', ('True',), '')
bungemane95ea082016-09-28 16:45:35 -0400416
417 # Mark object sources as linkable.
418 if 'obj' in sources:
Ben Wagnerbc344042016-09-29 15:41:53 -0400419 SetFilesProperty(out, sources['obj'], 'EXTERNAL_OBJECT', ('True',), '')
bungemane95ea082016-09-28 16:45:35 -0400420
421 # TODO: 'output_name', 'output_dir', 'output_extension'
Ben Wagnerbc344042016-09-29 15:41:53 -0400422 # This includes using 'source_outputs' to direct compiler output.
bungemane95ea082016-09-28 16:45:35 -0400423
424 # Includes
425 includes = target.properties.get('include_dirs', [])
426 if includes:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400427 out.write('set_property(TARGET "${target}" ')
428 out.write('APPEND PROPERTY INCLUDE_DIRECTORIES')
bungemane95ea082016-09-28 16:45:35 -0400429 for include_dir in includes:
430 out.write('\n "')
Ben Wagnerbc344042016-09-29 15:41:53 -0400431 out.write(project.GetAbsolutePath(include_dir))
bungemane95ea082016-09-28 16:45:35 -0400432 out.write('"')
433 out.write(')\n')
434
435 # Defines
436 defines = target.properties.get('defines', [])
437 if defines:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400438 SetCurrentTargetProperty(out, 'COMPILE_DEFINITIONS', defines, ';')
bungemane95ea082016-09-28 16:45:35 -0400439
440 # Compile flags
441 # "arflags", "asmflags", "cflags",
442 # "cflags_c", "clfags_cc", "cflags_objc", "clfags_objcc"
443 # CMake does not have per target lang compile flags.
444 # TODO: $<$<COMPILE_LANGUAGE:CXX>:cflags_cc style generator expression.
445 # http://public.kitware.com/Bug/view.php?id=14857
446 flags = []
447 flags.extend(target.properties.get('cflags', []))
448 cflags_asm = target.properties.get('asmflags', [])
449 cflags_c = target.properties.get('cflags_c', [])
450 cflags_cxx = target.properties.get('cflags_cc', [])
Brian Salomon4ce69892019-07-12 14:36:03 -0400451 cflags_objc = cflags_c[:]
452 cflags_objc.extend(target.properties.get('cflags_objc', []))
453 cflags_objcc = cflags_cxx[:]
454 cflags_objcc.extend(target.properties.get('cflags_objcc', []))
455
456 if 'c' in sources and not any(k in sources for k in ('asm', 'cxx', 'objc', 'objcc')):
bungemane95ea082016-09-28 16:45:35 -0400457 flags.extend(cflags_c)
Brian Salomon4ce69892019-07-12 14:36:03 -0400458 elif 'cxx' in sources and not any(k in sources for k in ('asm', 'c', 'objc', 'objcc')):
bungemane95ea082016-09-28 16:45:35 -0400459 flags.extend(cflags_cxx)
Brian Salomon4ce69892019-07-12 14:36:03 -0400460 elif 'objc' in sources and not any(k in sources for k in ('asm', 'c', 'cxx', 'objcc')):
461 flags.extend(cflags_objc)
462 elif 'objcc' in sources and not any(k in sources for k in ('asm', 'c', 'cxx', 'objc')):
463 flags.extend(cflags_objcc)
bungemane95ea082016-09-28 16:45:35 -0400464 else:
465 # TODO: This is broken, one cannot generally set properties on files,
466 # as other targets may require different properties on the same files.
467 if 'asm' in sources and cflags_asm:
468 SetFilesProperty(out, sources['asm'], 'COMPILE_FLAGS', cflags_asm, ' ')
469 if 'c' in sources and cflags_c:
470 SetFilesProperty(out, sources['c'], 'COMPILE_FLAGS', cflags_c, ' ')
471 if 'cxx' in sources and cflags_cxx:
472 SetFilesProperty(out, sources['cxx'], 'COMPILE_FLAGS', cflags_cxx, ' ')
Brian Salomon4ce69892019-07-12 14:36:03 -0400473 if 'objc' in sources and cflags_objc:
474 SetFilesProperty(out, sources['objc'], 'COMPILE_FLAGS', cflags_objc, ' ')
475 if 'objcc' in sources and cflags_objcc:
476 SetFilesProperty(out, sources['objcc'], 'COMPILE_FLAGS', cflags_objcc, ' ')
bungemane95ea082016-09-28 16:45:35 -0400477 if flags:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400478 SetCurrentTargetProperty(out, 'COMPILE_FLAGS', flags, ' ')
bungemane95ea082016-09-28 16:45:35 -0400479
480 # Linker flags
481 ldflags = target.properties.get('ldflags', [])
482 if ldflags:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400483 SetCurrentTargetProperty(out, 'LINK_FLAGS', ldflags, ' ')
bungemane95ea082016-09-28 16:45:35 -0400484
485
Ben Wagnerbc344042016-09-29 15:41:53 -0400486gn_target_types_that_absorb_objects = (
487 'executable',
488 'loadable_module',
489 'shared_library',
490 'static_library'
491)
bungemane95ea082016-09-28 16:45:35 -0400492
493
Ben Wagnerbc344042016-09-29 15:41:53 -0400494def WriteSourceVariables(out, target, project):
bungemane95ea082016-09-28 16:45:35 -0400495 # gn separates the sheep from the goats based on file extensions.
496 # A full separation is done here because of flag handing (see Compile flags).
Brian Salomon4ce69892019-07-12 14:36:03 -0400497 source_types = {'cxx':[], 'c':[], 'asm':[], 'objc':[], 'objcc':[],
Ben Wagnerbc344042016-09-29 15:41:53 -0400498 'obj':[], 'obj_target':[], 'input':[], 'other':[]}
499
Herb Derby56992782018-05-18 15:09:41 -0400500 all_sources = target.properties.get('sources', [])
501
502 # As of cmake 3.11 add_library must have sources. If there are
503 # no sources, add empty.cpp as the file to compile.
504 if len(all_sources) == 0:
505 all_sources.append(posixpath.join(project.build_path, 'empty.cpp'))
506
Ben Wagnerbc344042016-09-29 15:41:53 -0400507 # TODO .def files on Windows
Herb Derby56992782018-05-18 15:09:41 -0400508 for source in all_sources:
bungemane95ea082016-09-28 16:45:35 -0400509 _, ext = posixpath.splitext(source)
Ben Wagnerbc344042016-09-29 15:41:53 -0400510 source_abs_path = project.GetAbsolutePath(source)
bungemane95ea082016-09-28 16:45:35 -0400511 source_types[source_file_types.get(ext, 'other')].append(source_abs_path)
512
Ben Wagnerbc344042016-09-29 15:41:53 -0400513 for input_path in target.properties.get('inputs', []):
514 input_abs_path = project.GetAbsolutePath(input_path)
515 source_types['input'].append(input_abs_path)
516
bungemane95ea082016-09-28 16:45:35 -0400517 # OBJECT library dependencies need to be listed as sources.
518 # Only executables and non-OBJECT libraries may reference an OBJECT library.
519 # https://gitlab.kitware.com/cmake/cmake/issues/14778
Ben Wagnerbc344042016-09-29 15:41:53 -0400520 if target.gn_type in gn_target_types_that_absorb_objects:
bungemane95ea082016-09-28 16:45:35 -0400521 object_dependencies = set()
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400522 project.GetObjectSourceDependencies(target.gn_name, object_dependencies)
bungemane95ea082016-09-28 16:45:35 -0400523 for dependency in object_dependencies:
Ben Wagnerbc344042016-09-29 15:41:53 -0400524 cmake_dependency_name = project.GetCMakeTargetName(dependency)
bungemane95ea082016-09-28 16:45:35 -0400525 obj_target_sources = '$<TARGET_OBJECTS:' + cmake_dependency_name + '>'
526 source_types['obj_target'].append(obj_target_sources)
527
528 sources = {}
529 for source_type, sources_of_type in source_types.items():
530 if sources_of_type:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400531 sources[source_type] = '${target}__' + source_type + '_srcs'
bungemane95ea082016-09-28 16:45:35 -0400532 SetVariableList(out, sources[source_type], sources_of_type)
533 return sources
534
535
Ben Wagnerbc344042016-09-29 15:41:53 -0400536def WriteTarget(out, target, project):
bungemane95ea082016-09-28 16:45:35 -0400537 out.write('\n#')
Ben Wagnerbc344042016-09-29 15:41:53 -0400538 out.write(target.gn_name)
bungemane95ea082016-09-28 16:45:35 -0400539 out.write('\n')
540
bungemane95ea082016-09-28 16:45:35 -0400541 if target.cmake_type is None:
542 print ('Target %s has unknown target type %s, skipping.' %
Ben Wagnerbc344042016-09-29 15:41:53 -0400543 ( target.gn_name, target.gn_type ) )
bungemane95ea082016-09-28 16:45:35 -0400544 return
545
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400546 SetVariable(out, 'target', target.cmake_name)
547
Ben Wagnerbc344042016-09-29 15:41:53 -0400548 sources = WriteSourceVariables(out, target, project)
549
550 synthetic_dependencies = set()
551 if target.gn_type == 'action':
552 WriteAction(out, target, project, sources, synthetic_dependencies)
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400553 if target.gn_type == 'action_foreach':
554 WriteActionForEach(out, target, project, sources, synthetic_dependencies)
555 if target.gn_type == 'copy':
556 WriteCopy(out, target, project, sources, synthetic_dependencies)
bungemane95ea082016-09-28 16:45:35 -0400557
558 out.write(target.cmake_type.command)
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400559 out.write('("${target}"')
bungemane95ea082016-09-28 16:45:35 -0400560 if target.cmake_type.modifier is not None:
561 out.write(' ')
562 out.write(target.cmake_type.modifier)
563 for sources_type_name in sources.values():
564 WriteVariable(out, sources_type_name, ' ')
Ben Wagnerbc344042016-09-29 15:41:53 -0400565 if synthetic_dependencies:
566 out.write(' DEPENDS')
567 for synthetic_dependencie in synthetic_dependencies:
568 WriteVariable(out, synthetic_dependencie, ' ')
bungemane95ea082016-09-28 16:45:35 -0400569 out.write(')\n')
570
571 if target.cmake_type.command != 'add_custom_target':
Ben Wagnerbc344042016-09-29 15:41:53 -0400572 WriteCompilerFlags(out, target, project, sources)
bungemane95ea082016-09-28 16:45:35 -0400573
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400574 libraries = set()
575 nonlibraries = set()
576
577 dependencies = set(target.properties.get('deps', []))
578 # Transitive OBJECT libraries are in sources.
579 # Those sources are dependent on the OBJECT library dependencies.
580 # Those sources cannot bring in library dependencies.
581 object_dependencies = set()
582 if target.gn_type != 'source_set':
583 project.GetObjectLibraryDependencies(target.gn_name, object_dependencies)
584 for object_dependency in object_dependencies:
585 dependencies.update(project.targets.get(object_dependency).get('deps', []))
586
bungemane95ea082016-09-28 16:45:35 -0400587 for dependency in dependencies:
Ben Wagnerbc344042016-09-29 15:41:53 -0400588 gn_dependency_type = project.targets.get(dependency, {}).get('type', None)
bungemane95ea082016-09-28 16:45:35 -0400589 cmake_dependency_type = cmake_target_types.get(gn_dependency_type, None)
Ben Wagnerbc344042016-09-29 15:41:53 -0400590 cmake_dependency_name = project.GetCMakeTargetName(dependency)
bungemane95ea082016-09-28 16:45:35 -0400591 if cmake_dependency_type.command != 'add_library':
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400592 nonlibraries.add(cmake_dependency_name)
bungemane95ea082016-09-28 16:45:35 -0400593 elif cmake_dependency_type.modifier != 'OBJECT':
Ben Wagnerbc344042016-09-29 15:41:53 -0400594 if target.cmake_type.is_linkable:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400595 libraries.add(cmake_dependency_name)
Ben Wagnerbc344042016-09-29 15:41:53 -0400596 else:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400597 nonlibraries.add(cmake_dependency_name)
bungemane95ea082016-09-28 16:45:35 -0400598
599 # Non-library dependencies.
600 if nonlibraries:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400601 out.write('add_dependencies("${target}"')
bungemane95ea082016-09-28 16:45:35 -0400602 for nonlibrary in nonlibraries:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400603 out.write('\n "')
Ben Wagnerbc344042016-09-29 15:41:53 -0400604 out.write(nonlibrary)
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400605 out.write('"')
bungemane95ea082016-09-28 16:45:35 -0400606 out.write(')\n')
607
608 # Non-OBJECT library dependencies.
609 external_libraries = target.properties.get('libs', [])
610 if target.cmake_type.is_linkable and (external_libraries or libraries):
Ben Wagner187a7712016-10-27 18:47:54 -0400611 library_dirs = target.properties.get('lib_dirs', [])
612 if library_dirs:
613 SetVariableList(out, '${target}__library_directories', library_dirs)
614
bungemane95ea082016-09-28 16:45:35 -0400615 system_libraries = []
616 for external_library in external_libraries:
617 if '/' in external_library:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400618 libraries.add(project.GetAbsolutePath(external_library))
bungemane95ea082016-09-28 16:45:35 -0400619 else:
620 if external_library.endswith('.framework'):
621 external_library = external_library[:-len('.framework')]
Ben Wagner187a7712016-10-27 18:47:54 -0400622 system_library = 'library__' + external_library
623 if library_dirs:
624 system_library = system_library + '__for_${target}'
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400625 out.write('find_library("')
626 out.write(CMakeStringEscape(system_library))
627 out.write('" "')
628 out.write(CMakeStringEscape(external_library))
Ben Wagner187a7712016-10-27 18:47:54 -0400629 out.write('"')
630 if library_dirs:
631 out.write(' PATHS "')
632 WriteVariable(out, '${target}__library_directories')
633 out.write('"')
634 out.write(')\n')
bungemane95ea082016-09-28 16:45:35 -0400635 system_libraries.append(system_library)
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400636 out.write('target_link_libraries("${target}"')
bungemane95ea082016-09-28 16:45:35 -0400637 for library in libraries:
638 out.write('\n "')
639 out.write(CMakeStringEscape(library))
640 out.write('"')
641 for system_library in system_libraries:
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400642 WriteVariable(out, system_library, '\n "')
643 out.write('"')
bungemane95ea082016-09-28 16:45:35 -0400644 out.write(')\n')
645
646
647def WriteProject(project):
Ben Wagnerbc344042016-09-29 15:41:53 -0400648 out = open(posixpath.join(project.build_path, 'CMakeLists.txt'), 'w+')
Ben Wagner82cecb62018-05-31 15:04:43 -0400649 extName = posixpath.join(project.build_path, 'CMakeLists.ext')
Ben Wagnerdc69dc72016-10-04 16:44:44 -0400650 out.write('# Generated by gn_to_cmake.py.\n')
bungeman623ef922016-09-23 08:16:04 -0700651 out.write('cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n')
Herb Derby2b921422019-09-25 10:37:34 -0400652 out.write('cmake_policy(VERSION 2.8.8)\n')
653 out.write('project(Skia)\n\n')
Ben Wagner388faa02016-10-05 17:18:38 -0400654
Herb Derby56992782018-05-18 15:09:41 -0400655 out.write('file(WRITE "')
Ben Wagner82cecb62018-05-31 15:04:43 -0400656 out.write(CMakeStringEscape(posixpath.join(project.build_path, "empty.cpp")))
Herb Derby56992782018-05-18 15:09:41 -0400657 out.write('")\n')
658
Ben Wagner388faa02016-10-05 17:18:38 -0400659 # Update the gn generated ninja build.
660 # If a build file has changed, this will update CMakeLists.ext if
661 # gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py
662 # style was used to create this config.
Ben Wagner4f0a0112018-06-25 15:35:28 -0400663 out.write('execute_process(COMMAND\n')
664 out.write(' ninja -C "')
Ben Wagner388faa02016-10-05 17:18:38 -0400665 out.write(CMakeStringEscape(project.build_path))
Ben Wagner4f0a0112018-06-25 15:35:28 -0400666 out.write('" build.ninja\n')
667 out.write(' RESULT_VARIABLE ninja_result)\n')
668 out.write('if (ninja_result)\n')
669 out.write(' message(WARNING ')
670 out.write('"Regeneration failed running ninja: ${ninja_result}")\n')
671 out.write('endif()\n')
Ben Wagner388faa02016-10-05 17:18:38 -0400672
Ben Wagner82cecb62018-05-31 15:04:43 -0400673 out.write('include("')
674 out.write(CMakeStringEscape(extName))
675 out.write('")\n')
Ben Wagner388faa02016-10-05 17:18:38 -0400676 out.close()
677
Ben Wagner82cecb62018-05-31 15:04:43 -0400678 out = open(extName, 'w+')
Ben Wagner388faa02016-10-05 17:18:38 -0400679 out.write('# Generated by gn_to_cmake.py.\n')
680 out.write('cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n')
bungeman623ef922016-09-23 08:16:04 -0700681 out.write('cmake_policy(VERSION 2.8.8)\n')
682
bungemane95ea082016-09-28 16:45:35 -0400683 # The following appears to be as-yet undocumented.
684 # http://public.kitware.com/Bug/view.php?id=8392
Ben Wagner388faa02016-10-05 17:18:38 -0400685 out.write('enable_language(ASM)\n\n')
bungemane95ea082016-09-28 16:45:35 -0400686 # ASM-ATT does not support .S files.
687 # output.write('enable_language(ASM-ATT)\n')
bungeman623ef922016-09-23 08:16:04 -0700688
Ben Wagner388faa02016-10-05 17:18:38 -0400689 # Current issues with automatic re-generation:
690 # The gn generated build.ninja target uses build.ninja.d
691 # but build.ninja.d does not contain the ide or gn.
692 # Currently the ide is not run if the project.json file is not changed
693 # but the ide needs to be run anyway if it has itself changed.
694 # This can be worked around by deleting the project.json file.
695 out.write('file(READ "')
696 gn_deps_file = posixpath.join(project.build_path, 'build.ninja.d')
697 out.write(CMakeStringEscape(gn_deps_file))
698 out.write('" "gn_deps_string" OFFSET ')
699 out.write(str(len('build.ninja: ')))
700 out.write(')\n')
701 # One would think this would need to worry about escaped spaces
702 # but gn doesn't escape spaces here (it generates invalid .d files).
703 out.write('string(REPLACE " " ";" "gn_deps" ${gn_deps_string})\n')
704 out.write('foreach("gn_dep" ${gn_deps})\n')
Ben Wagner4f0a0112018-06-25 15:35:28 -0400705 out.write(' configure_file("')
706 out.write(CMakeStringEscape(project.build_path))
707 out.write('${gn_dep}" "CMakeLists.devnull" COPYONLY)\n')
Ben Wagner388faa02016-10-05 17:18:38 -0400708 out.write('endforeach("gn_dep")\n')
709
Ben Wagner4f0a0112018-06-25 15:35:28 -0400710 out.write('list(APPEND other_deps "')
711 out.write(CMakeStringEscape(os.path.abspath(__file__)))
712 out.write('")\n')
713 out.write('foreach("other_dep" ${other_deps})\n')
714 out.write(' configure_file("${other_dep}" "CMakeLists.devnull" COPYONLY)\n')
715 out.write('endforeach("other_dep")\n')
716
Ben Wagnerbc344042016-09-29 15:41:53 -0400717 for target_name in project.targets.keys():
bungeman623ef922016-09-23 08:16:04 -0700718 out.write('\n')
Ben Wagnerbc344042016-09-29 15:41:53 -0400719 WriteTarget(out, Target(target_name, project), project)
bungeman623ef922016-09-23 08:16:04 -0700720
bungeman623ef922016-09-23 08:16:04 -0700721
722def main():
723 if len(sys.argv) != 2:
724 print('Usage: ' + sys.argv[0] + ' <json_file_name>')
725 exit(1)
726
727 json_path = sys.argv[1]
728 project = None
729 with open(json_path, 'r') as json_file:
730 project = json.loads(json_file.read())
731
Ben Wagnerbc344042016-09-29 15:41:53 -0400732 WriteProject(Project(project))
bungemane95ea082016-09-28 16:45:35 -0400733
bungeman623ef922016-09-23 08:16:04 -0700734
735if __name__ == "__main__":
736 main()