• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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
8
9"""
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
18
19The first is recommended, as it will auto-update.
20"""
21
22
23import itertools
24import functools
25import json
26import posixpath
27import os
28import string
29import sys
30
31
32def CMakeStringEscape(a):
33  """Escapes the string 'a' for use inside a CMake string.
34
35  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
39
40  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('"', '\\"')
44
45
46def 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
60def SetVariable(out, variable_name, value):
61  """Sets a CMake variable."""
62  out.write('set("')
63  out.write(CMakeStringEscape(variable_name))
64  out.write('" "')
65  out.write(CMakeStringEscape(value))
66  out.write('")\n')
67
68
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])
75  out.write('list(APPEND "')
76  out.write(CMakeStringEscape(variable_name))
77  out.write('"\n  "')
78  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
95def SetCurrentTargetProperty(out, property_name, values, sep=''):
96  """Given a target, sets the given property."""
97  out.write('set_target_properties("${target}" PROPERTIES ')
98  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
114# See GetSourceFileType in gn
115source_file_types = {
116  '.cc': 'cxx',
117  '.cpp': 'cxx',
118  '.cxx': 'cxx',
119  '.m': 'objc',
120  '.mm': 'objcc',
121  '.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),
146  'static_library': CMakeTargetType('add_library', 'STATIC', 'ARCHIVE', True),
147  'source_set': CMakeTargetType('add_library', 'OBJECT', None, False),
148  'copy': CMakeTargetType.custom,
149  'action': CMakeTargetType.custom,
150  'action_foreach': CMakeTargetType.custom,
151  'bundle_data': CMakeTargetType.custom,
152  'create_bundle': CMakeTargetType.custom,
153}
154
155
156def FindFirstOf(s, a):
157  return min(s.find(i) for i in a if i in s)
158
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 = self.GetAbsolutePath(build_settings['build_dir'])
166
167  def GetAbsolutePath(self, path):
168    if path.startswith('//'):
169      return posixpath.join(self.root_path, path[2:])
170    else:
171      return path
172
173  def GetObjectSourceDependencies(self, gn_target_name, object_dependencies):
174    """All OBJECT libraries whose sources have not been absorbed."""
175    dependencies = self.targets[gn_target_name].get('deps', [])
176    for dependency in dependencies:
177      dependency_type = self.targets[dependency].get('type', None)
178      if dependency_type == 'source_set':
179        object_dependencies.add(dependency)
180      if dependency_type not in gn_target_types_that_absorb_objects:
181        self.GetObjectSourceDependencies(dependency, object_dependencies)
182
183  def GetObjectLibraryDependencies(self, gn_target_name, object_dependencies):
184    """All OBJECT libraries whose libraries have not been absorbed."""
185    dependencies = self.targets[gn_target_name].get('deps', [])
186    for dependency in dependencies:
187      dependency_type = self.targets[dependency].get('type', None)
188      if dependency_type == 'source_set':
189        object_dependencies.add(dependency)
190        self.GetObjectLibraryDependencies(dependency, object_dependencies)
191
192  def GetCMakeTargetName(self, gn_target_name):
193    # See <chromium>/src/tools/gn/label.cc#Resolve
194    # //base/test:test_support(//build/toolchain/win:msvc)
195    path_separator = FindFirstOf(gn_target_name, (':', '('))
196    location = None
197    name = None
198    toolchain = None
199    if not path_separator:
200      location = gn_target_name[2:]
201    else:
202      location = gn_target_name[2:path_separator]
203      toolchain_separator = gn_target_name.find('(', path_separator)
204      if toolchain_separator == -1:
205        name = gn_target_name[path_separator + 1:]
206      else:
207        if toolchain_separator > path_separator:
208          name = gn_target_name[path_separator + 1:toolchain_separator]
209        assert gn_target_name.endswith(')')
210        toolchain = gn_target_name[toolchain_separator + 1:-1]
211    assert location or name
212
213    cmake_target_name = None
214    if location.endswith('/' + name):
215      cmake_target_name = location
216    elif location:
217      cmake_target_name = location + '_' + name
218    else:
219      cmake_target_name = name
220    if toolchain:
221      cmake_target_name += '--' + toolchain
222    return CMakeTargetEscape(cmake_target_name)
223
224
225class Target(object):
226  def __init__(self, gn_target_name, project):
227    self.gn_name = gn_target_name
228    self.properties = project.targets[self.gn_name]
229    self.cmake_name = project.GetCMakeTargetName(self.gn_name)
230    self.gn_type = self.properties.get('type', None)
231    self.cmake_type = cmake_target_types.get(self.gn_type, None)
232
233
234def WriteAction(out, target, project, sources, synthetic_dependencies):
235  outputs = []
236  output_directories = set()
237  for output in target.properties.get('outputs', []):
238    output_abs_path = project.GetAbsolutePath(output)
239    outputs.append(output_abs_path)
240    output_directory = posixpath.dirname(output_abs_path)
241    if output_directory:
242      output_directories.add(output_directory)
243  outputs_name = '${target}__output'
244  SetVariableList(out, outputs_name, outputs)
245
246  out.write('add_custom_command(OUTPUT ')
247  WriteVariable(out, outputs_name)
248  out.write('\n')
249
250  if output_directories:
251    out.write('  COMMAND ${CMAKE_COMMAND} -E make_directory "')
252    out.write('" "'.join(map(CMakeStringEscape, output_directories)))
253    out.write('"\n')
254
255  script = target.properties['script']
256  arguments = target.properties['args']
257  out.write('  COMMAND python "')
258  out.write(CMakeStringEscape(project.GetAbsolutePath(script)))
259  out.write('"')
260  if arguments:
261    out.write('\n    "')
262    out.write('"\n    "'.join(map(CMakeStringEscape, arguments)))
263    out.write('"')
264  out.write('\n')
265
266  out.write('  DEPENDS ')
267  for sources_type_name in sources.values():
268    WriteVariable(out, sources_type_name, ' ')
269  out.write('\n')
270
271  #TODO: CMake 3.7 is introducing DEPFILE
272
273  out.write('  WORKING_DIRECTORY "')
274  out.write(CMakeStringEscape(project.build_path))
275  out.write('"\n')
276
277  out.write('  COMMENT "Action: ${target}"\n')
278
279  out.write('  VERBATIM)\n')
280
281  synthetic_dependencies.add(outputs_name)
282
283
284def ExpandPlaceholders(source, a):
285  source_dir, source_file_part = posixpath.split(source)
286  source_name_part, _ = posixpath.splitext(source_file_part)
287  #TODO: {{source_gen_dir}}, {{source_out_dir}}, {{response_file_name}}
288  return a.replace('{{source}}', source) \
289          .replace('{{source_file_part}}', source_file_part) \
290          .replace('{{source_name_part}}', source_name_part) \
291          .replace('{{source_dir}}', source_dir) \
292          .replace('{{source_root_relative_dir}}', source_dir)
293
294
295def WriteActionForEach(out, target, project, sources, synthetic_dependencies):
296  all_outputs = target.properties.get('outputs', [])
297  inputs = target.properties.get('sources', [])
298  # TODO: consider expanding 'output_patterns' instead.
299  outputs_per_input = len(all_outputs) / len(inputs)
300  for count, source in enumerate(inputs):
301    source_abs_path = project.GetAbsolutePath(source)
302
303    outputs = []
304    output_directories = set()
305    for output in all_outputs[outputs_per_input *  count:
306                              outputs_per_input * (count+1)]:
307      output_abs_path = project.GetAbsolutePath(output)
308      outputs.append(output_abs_path)
309      output_directory = posixpath.dirname(output_abs_path)
310      if output_directory:
311        output_directories.add(output_directory)
312    outputs_name = '${target}__output_' + str(count)
313    SetVariableList(out, outputs_name, outputs)
314
315    out.write('add_custom_command(OUTPUT ')
316    WriteVariable(out, outputs_name)
317    out.write('\n')
318
319    if output_directories:
320      out.write('  COMMAND ${CMAKE_COMMAND} -E make_directory "')
321      out.write('" "'.join(map(CMakeStringEscape, output_directories)))
322      out.write('"\n')
323
324    script = target.properties['script']
325    # TODO: need to expand {{xxx}} in arguments
326    arguments = target.properties['args']
327    out.write('  COMMAND python "')
328    out.write(CMakeStringEscape(project.GetAbsolutePath(script)))
329    out.write('"')
330    if arguments:
331      out.write('\n    "')
332      expand = functools.partial(ExpandPlaceholders, source_abs_path)
333      out.write('"\n    "'.join(map(CMakeStringEscape, map(expand,arguments))))
334      out.write('"')
335    out.write('\n')
336
337    out.write('  DEPENDS')
338    if 'input' in sources:
339      WriteVariable(out, sources['input'], ' ')
340    out.write(' "')
341    out.write(CMakeStringEscape(source_abs_path))
342    out.write('"\n')
343
344    #TODO: CMake 3.7 is introducing DEPFILE
345
346    out.write('  WORKING_DIRECTORY "')
347    out.write(CMakeStringEscape(project.build_path))
348    out.write('"\n')
349
350    out.write('  COMMENT "Action ${target} on ')
351    out.write(CMakeStringEscape(source_abs_path))
352    out.write('"\n')
353
354    out.write('  VERBATIM)\n')
355
356    synthetic_dependencies.add(outputs_name)
357
358
359def WriteCopy(out, target, project, sources, synthetic_dependencies):
360  inputs = target.properties.get('sources', [])
361  raw_outputs = target.properties.get('outputs', [])
362
363  # TODO: consider expanding 'output_patterns' instead.
364  outputs = []
365  for output in raw_outputs:
366    output_abs_path = project.GetAbsolutePath(output)
367    outputs.append(output_abs_path)
368  outputs_name = '${target}__output'
369  SetVariableList(out, outputs_name, outputs)
370
371  out.write('add_custom_command(OUTPUT ')
372  WriteVariable(out, outputs_name)
373  out.write('\n')
374
375  for src, dst in zip(inputs, outputs):
376    abs_src_path = CMakeStringEscape(project.GetAbsolutePath(src))
377    # CMake distinguishes between copying files and copying directories but
378    # gn does not. We assume if the src has a period in its name then it is
379    # a file and otherwise a directory.
380    if "." in os.path.basename(abs_src_path):
381      out.write('  COMMAND ${CMAKE_COMMAND} -E copy "')
382    else:
383      out.write('  COMMAND ${CMAKE_COMMAND} -E copy_directory "')
384    out.write(abs_src_path)
385    out.write('" "')
386    out.write(CMakeStringEscape(dst))
387    out.write('"\n')
388
389  out.write('  DEPENDS ')
390  for sources_type_name in sources.values():
391    WriteVariable(out, sources_type_name, ' ')
392  out.write('\n')
393
394  out.write('  WORKING_DIRECTORY "')
395  out.write(CMakeStringEscape(project.build_path))
396  out.write('"\n')
397
398  out.write('  COMMENT "Copy ${target}"\n')
399
400  out.write('  VERBATIM)\n')
401
402  synthetic_dependencies.add(outputs_name)
403
404
405def WriteCompilerFlags(out, target, project, sources):
406  # Hack, set linker language to c if no c or cxx files present.
407  if not 'c' in sources and not 'cxx' in sources:
408    SetCurrentTargetProperty(out, 'LINKER_LANGUAGE', ['C'])
409
410  # Mark uncompiled sources as uncompiled.
411  if 'input' in sources:
412    SetFilesProperty(out, sources['input'], 'HEADER_FILE_ONLY', ('True',), '')
413  if 'other' in sources:
414    SetFilesProperty(out, sources['other'], 'HEADER_FILE_ONLY', ('True',), '')
415
416  # Mark object sources as linkable.
417  if 'obj' in sources:
418    SetFilesProperty(out, sources['obj'], 'EXTERNAL_OBJECT', ('True',), '')
419
420  # TODO: 'output_name', 'output_dir', 'output_extension'
421  # This includes using 'source_outputs' to direct compiler output.
422
423  # Includes
424  includes = target.properties.get('include_dirs', [])
425  if includes:
426    out.write('set_property(TARGET "${target}" ')
427    out.write('APPEND PROPERTY INCLUDE_DIRECTORIES')
428    for include_dir in includes:
429      out.write('\n  "')
430      out.write(project.GetAbsolutePath(include_dir))
431      out.write('"')
432    out.write(')\n')
433
434  # Defines
435  defines = target.properties.get('defines', [])
436  if defines:
437    SetCurrentTargetProperty(out, 'COMPILE_DEFINITIONS', defines, ';')
438
439  # Compile flags
440  # "arflags", "asmflags", "cflags",
441  # "cflags_c", "clfags_cc", "cflags_objc", "clfags_objcc"
442  # CMake does not have per target lang compile flags.
443  # TODO: $<$<COMPILE_LANGUAGE:CXX>:cflags_cc style generator expression.
444  #       http://public.kitware.com/Bug/view.php?id=14857
445  flags = []
446  flags.extend(target.properties.get('cflags', []))
447  cflags_asm = target.properties.get('asmflags', [])
448  cflags_c = target.properties.get('cflags_c', [])
449  cflags_cxx = target.properties.get('cflags_cc', [])
450  cflags_objc = cflags_c[:]
451  cflags_objc.extend(target.properties.get('cflags_objc', []))
452  cflags_objcc = cflags_cxx[:]
453  cflags_objcc.extend(target.properties.get('cflags_objcc', []))
454
455  if 'c' in sources and not any(k in sources for k in ('asm', 'cxx', 'objc', 'objcc')):
456    flags.extend(cflags_c)
457  elif 'cxx' in sources and not any(k in sources for k in ('asm', 'c', 'objc', 'objcc')):
458    flags.extend(cflags_cxx)
459  elif 'objc' in sources and not any(k in sources for k in ('asm', 'c', 'cxx', 'objcc')):
460    flags.extend(cflags_objc)
461  elif 'objcc' in sources and not any(k in sources for k in ('asm', 'c', 'cxx', 'objc')):
462    flags.extend(cflags_objcc)
463  else:
464    # TODO: This is broken, one cannot generally set properties on files,
465    # as other targets may require different properties on the same files.
466    if 'asm' in sources and cflags_asm:
467      SetFilesProperty(out, sources['asm'], 'COMPILE_FLAGS', cflags_asm, ' ')
468    if 'c' in sources and cflags_c:
469      SetFilesProperty(out, sources['c'], 'COMPILE_FLAGS', cflags_c, ' ')
470    if 'cxx' in sources and cflags_cxx:
471      SetFilesProperty(out, sources['cxx'], 'COMPILE_FLAGS', cflags_cxx, ' ')
472    if 'objc' in sources and cflags_objc:
473      SetFilesProperty(out, sources['objc'], 'COMPILE_FLAGS', cflags_objc, ' ')
474    if 'objcc' in sources and cflags_objcc:
475      SetFilesProperty(out, sources['objcc'], 'COMPILE_FLAGS', cflags_objcc, ' ')
476  if flags:
477    SetCurrentTargetProperty(out, 'COMPILE_FLAGS', flags, ' ')
478
479  # Linker flags
480  ldflags = target.properties.get('ldflags', [])
481  if ldflags:
482    SetCurrentTargetProperty(out, 'LINK_FLAGS', ldflags, ' ')
483
484
485gn_target_types_that_absorb_objects = (
486  'executable',
487  'loadable_module',
488  'shared_library',
489  'static_library'
490)
491
492
493def WriteSourceVariables(out, target, project):
494  # gn separates the sheep from the goats based on file extensions.
495  # A full separation is done here because of flag handing (see Compile flags).
496  source_types = {'cxx':[], 'c':[], 'asm':[], 'objc':[], 'objcc':[],
497                  'obj':[], 'obj_target':[], 'input':[], 'other':[]}
498
499  all_sources = target.properties.get('sources', [])
500
501  # As of cmake 3.11 add_library must have sources. If there are
502  # no sources, add empty.cpp as the file to compile.
503  if len(all_sources) == 0:
504    all_sources.append(posixpath.join(project.build_path, 'empty.cpp'))
505
506  # TODO .def files on Windows
507  for source in all_sources:
508    _, ext = posixpath.splitext(source)
509    source_abs_path = project.GetAbsolutePath(source)
510    source_types[source_file_types.get(ext, 'other')].append(source_abs_path)
511
512  for input_path in target.properties.get('inputs', []):
513    input_abs_path = project.GetAbsolutePath(input_path)
514    source_types['input'].append(input_abs_path)
515
516  # OBJECT library dependencies need to be listed as sources.
517  # Only executables and non-OBJECT libraries may reference an OBJECT library.
518  # https://gitlab.kitware.com/cmake/cmake/issues/14778
519  if target.gn_type in gn_target_types_that_absorb_objects:
520    object_dependencies = set()
521    project.GetObjectSourceDependencies(target.gn_name, object_dependencies)
522    for dependency in object_dependencies:
523      cmake_dependency_name = project.GetCMakeTargetName(dependency)
524      obj_target_sources = '$<TARGET_OBJECTS:' + cmake_dependency_name + '>'
525      source_types['obj_target'].append(obj_target_sources)
526
527  sources = {}
528  for source_type, sources_of_type in source_types.items():
529    if sources_of_type:
530      sources[source_type] = '${target}__' + source_type + '_srcs'
531      SetVariableList(out, sources[source_type], sources_of_type)
532  return sources
533
534
535def WriteTarget(out, target, project):
536  out.write('\n#')
537  out.write(target.gn_name)
538  out.write('\n')
539
540  if target.cmake_type is None:
541    print ('Target %s has unknown target type %s, skipping.' %
542          (        target.gn_name,            target.gn_type ) )
543    return
544
545  SetVariable(out, 'target', target.cmake_name)
546
547  sources = WriteSourceVariables(out, target, project)
548
549  synthetic_dependencies = set()
550  if target.gn_type == 'action':
551    WriteAction(out, target, project, sources, synthetic_dependencies)
552  if target.gn_type == 'action_foreach':
553    WriteActionForEach(out, target, project, sources, synthetic_dependencies)
554  if target.gn_type == 'copy':
555    WriteCopy(out, target, project, sources, synthetic_dependencies)
556
557  out.write(target.cmake_type.command)
558  out.write('("${target}"')
559  if target.cmake_type.modifier is not None:
560    out.write(' ')
561    out.write(target.cmake_type.modifier)
562  for sources_type_name in sources.values():
563    WriteVariable(out, sources_type_name, ' ')
564  if synthetic_dependencies:
565    out.write(' DEPENDS')
566    for synthetic_dependencie in synthetic_dependencies:
567      WriteVariable(out, synthetic_dependencie, ' ')
568  out.write(')\n')
569
570  if target.cmake_type.command != 'add_custom_target':
571    WriteCompilerFlags(out, target, project, sources)
572
573  libraries = set()
574  nonlibraries = set()
575
576  dependencies = set(target.properties.get('deps', []))
577  # Transitive OBJECT libraries are in sources.
578  # Those sources are dependent on the OBJECT library dependencies.
579  # Those sources cannot bring in library dependencies.
580  object_dependencies = set()
581  if target.gn_type != 'source_set':
582    project.GetObjectLibraryDependencies(target.gn_name, object_dependencies)
583  for object_dependency in object_dependencies:
584    dependencies.update(project.targets.get(object_dependency).get('deps', []))
585
586  for dependency in dependencies:
587    gn_dependency_type = project.targets.get(dependency, {}).get('type', None)
588    cmake_dependency_type = cmake_target_types.get(gn_dependency_type, None)
589    cmake_dependency_name = project.GetCMakeTargetName(dependency)
590    if cmake_dependency_type.command != 'add_library':
591      nonlibraries.add(cmake_dependency_name)
592    elif cmake_dependency_type.modifier != 'OBJECT':
593      if target.cmake_type.is_linkable:
594        libraries.add(cmake_dependency_name)
595      else:
596        nonlibraries.add(cmake_dependency_name)
597
598  # Non-library dependencies.
599  if nonlibraries:
600    out.write('add_dependencies("${target}"')
601    for nonlibrary in nonlibraries:
602      out.write('\n  "')
603      out.write(nonlibrary)
604      out.write('"')
605    out.write(')\n')
606
607  # Non-OBJECT library dependencies.
608  combined_library_lists = [target.properties.get(key, []) for key in ['libs', 'frameworks']]
609  external_libraries = list(itertools.chain(*combined_library_lists))
610  if target.cmake_type.is_linkable and (external_libraries or libraries):
611    library_dirs = target.properties.get('lib_dirs', [])
612    if library_dirs:
613      SetVariableList(out, '${target}__library_directories', library_dirs)
614
615    system_libraries = []
616    for external_library in external_libraries:
617      if '/' in external_library:
618        libraries.add(project.GetAbsolutePath(external_library))
619      else:
620        if external_library.endswith('.framework'):
621          external_library = external_library[:-len('.framework')]
622        system_library = 'library__' + external_library
623        if library_dirs:
624          system_library = system_library + '__for_${target}'
625        out.write('find_library("')
626        out.write(CMakeStringEscape(system_library))
627        out.write('" "')
628        out.write(CMakeStringEscape(external_library))
629        out.write('"')
630        if library_dirs:
631          out.write(' PATHS "')
632          WriteVariable(out, '${target}__library_directories')
633          out.write('"')
634        out.write(')\n')
635        system_libraries.append(system_library)
636    out.write('target_link_libraries("${target}"')
637    for library in libraries:
638      out.write('\n  "')
639      out.write(CMakeStringEscape(library))
640      out.write('"')
641    for system_library in system_libraries:
642      WriteVariable(out, system_library, '\n  "')
643      out.write('"')
644    out.write(')\n')
645
646
647def WriteProject(project):
648  out = open(posixpath.join(project.build_path, 'CMakeLists.txt'), 'w+')
649  extName = posixpath.join(project.build_path, 'CMakeLists.ext')
650  out.write('# Generated by gn_to_cmake.py.\n')
651  out.write('cmake_minimum_required(VERSION 3.7 FATAL_ERROR)\n')
652  out.write('cmake_policy(VERSION 3.7)\n')
653  out.write('project(Skia)\n\n')
654
655  out.write('file(WRITE "')
656  out.write(CMakeStringEscape(posixpath.join(project.build_path, "empty.cpp")))
657  out.write('")\n')
658
659  # 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.
663  out.write('execute_process(COMMAND\n')
664  out.write('  ninja -C "')
665  out.write(CMakeStringEscape(project.build_path))
666  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')
672
673  out.write('include("')
674  out.write(CMakeStringEscape(extName))
675  out.write('")\n')
676  # This lets Clion find the emscripten header files when working with CanvasKit.
677  out.write('include_directories(SYSTEM $ENV{EMSDK}/upstream/emscripten/system/include/)\n')
678  out.close()
679
680  out = open(extName, 'w+')
681  out.write('# Generated by gn_to_cmake.py.\n')
682  out.write('cmake_minimum_required(VERSION 3.7 FATAL_ERROR)\n')
683  out.write('cmake_policy(VERSION 3.7)\n')
684
685  # The following appears to be as-yet undocumented.
686  # http://public.kitware.com/Bug/view.php?id=8392
687  out.write('enable_language(ASM)\n\n')
688  # ASM-ATT does not support .S files.
689  # output.write('enable_language(ASM-ATT)\n')
690
691  # Current issues with automatic re-generation:
692  # The gn generated build.ninja target uses build.ninja.d
693  #   but build.ninja.d does not contain the ide or gn.
694  # Currently the ide is not run if the project.json file is not changed
695  #   but the ide needs to be run anyway if it has itself changed.
696  #   This can be worked around by deleting the project.json file.
697  out.write('file(READ "')
698  gn_deps_file = posixpath.join(project.build_path, 'build.ninja.d')
699  out.write(CMakeStringEscape(gn_deps_file))
700  out.write('" "gn_deps_string" OFFSET ')
701  out.write(str(len('build.ninja: ')))
702  out.write(')\n')
703  # One would think this would need to worry about escaped spaces
704  # but gn doesn't escape spaces here (it generates invalid .d files).
705  out.write('string(REPLACE " " ";" "gn_deps" ${gn_deps_string})\n')
706  out.write('foreach("gn_dep" ${gn_deps})\n')
707  out.write('  configure_file("')
708  out.write(CMakeStringEscape(project.build_path))
709  out.write('${gn_dep}" "CMakeLists.devnull" COPYONLY)\n')
710  out.write('endforeach("gn_dep")\n')
711
712  out.write('list(APPEND other_deps "')
713  out.write(CMakeStringEscape(os.path.abspath(__file__)))
714  out.write('")\n')
715  out.write('foreach("other_dep" ${other_deps})\n')
716  out.write('  configure_file("${other_dep}" "CMakeLists.devnull" COPYONLY)\n')
717  out.write('endforeach("other_dep")\n')
718
719  for target_name in project.targets.keys():
720    out.write('\n')
721    WriteTarget(out, Target(target_name, project), project)
722
723
724def main():
725  if len(sys.argv) != 2:
726    print('Usage: ' + sys.argv[0] + ' <json_file_name>')
727    exit(1)
728
729  json_path = sys.argv[1]
730  project = None
731  with open(json_path, 'r') as json_file:
732    project = json.loads(json_file.read())
733
734  WriteProject(Project(project))
735
736
737if __name__ == "__main__":
738  main()
739