• 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 = 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
174  def GetObjectSourceDependencies(self, gn_target_name, object_dependencies):
175    """All OBJECT libraries whose sources have not been absorbed."""
176    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:
182        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)
192
193  def GetCMakeTargetName(self, gn_target_name):
194    # 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)
224
225
226class Target(object):
227  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)
231    self.gn_type = self.properties.get('type', None)
232    self.cmake_type = cmake_target_types.get(self.gn_type, None)
233
234
235def 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)
244  outputs_name = '${target}__output'
245  SetVariableList(out, outputs_name, outputs)
246
247  out.write('add_custom_command(OUTPUT ')
248  WriteVariable(out, outputs_name)
249  out.write('\n')
250
251  if output_directories:
252    out.write('  COMMAND ${CMAKE_COMMAND} -E make_directory "')
253    out.write('" "'.join(map(CMakeStringEscape, output_directories)))
254    out.write('"\n')
255
256  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('"')
265  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
272  #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)
374  out.write('\n')
375
376  for src, dst in zip(inputs, outputs):
377    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)
386    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, ' ')
393  out.write('\n')
394
395  out.write('  WORKING_DIRECTORY "')
396  out.write(CMakeStringEscape(project.build_path))
397  out.write('"\n')
398
399  out.write('  COMMENT "Copy ${target}"\n')
400
401  out.write('  VERBATIM)\n')
402
403  synthetic_dependencies.add(outputs_name)
404
405
406def WriteCompilerFlags(out, target, project, sources):
407  # Hack, set linker language to c if no c or cxx files present.
408  if not 'c' in sources and not 'cxx' in sources:
409    SetCurrentTargetProperty(out, 'LINKER_LANGUAGE', ['C'])
410
411  # Mark uncompiled sources as uncompiled.
412  if 'input' in sources:
413    SetFilesProperty(out, sources['input'], 'HEADER_FILE_ONLY', ('True',), '')
414  if 'other' in sources:
415    SetFilesProperty(out, sources['other'], 'HEADER_FILE_ONLY', ('True',), '')
416
417  # Mark object sources as linkable.
418  if 'obj' in sources:
419    SetFilesProperty(out, sources['obj'], 'EXTERNAL_OBJECT', ('True',), '')
420
421  # TODO: 'output_name', 'output_dir', 'output_extension'
422  # This includes using 'source_outputs' to direct compiler output.
423
424  # Includes
425  includes = target.properties.get('include_dirs', [])
426  if includes:
427    out.write('set_property(TARGET "${target}" ')
428    out.write('APPEND PROPERTY INCLUDE_DIRECTORIES')
429    for include_dir in includes:
430      out.write('\n  "')
431      out.write(project.GetAbsolutePath(include_dir))
432      out.write('"')
433    out.write(')\n')
434
435  # Defines
436  defines = target.properties.get('defines', [])
437  if defines:
438    SetCurrentTargetProperty(out, 'COMPILE_DEFINITIONS', defines, ';')
439
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', [])
451  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')):
457    flags.extend(cflags_c)
458  elif 'cxx' in sources and not any(k in sources for k in ('asm', 'c', 'objc', 'objcc')):
459    flags.extend(cflags_cxx)
460  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)
464  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, ' ')
473    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, ' ')
477  if flags:
478    SetCurrentTargetProperty(out, 'COMPILE_FLAGS', flags, ' ')
479
480  # Linker flags
481  ldflags = target.properties.get('ldflags', [])
482  if ldflags:
483    SetCurrentTargetProperty(out, 'LINK_FLAGS', ldflags, ' ')
484
485
486gn_target_types_that_absorb_objects = (
487  'executable',
488  'loadable_module',
489  'shared_library',
490  'static_library'
491)
492
493
494def WriteSourceVariables(out, target, project):
495  # 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).
497  source_types = {'cxx':[], 'c':[], 'asm':[], 'objc':[], 'objcc':[],
498                  'obj':[], 'obj_target':[], 'input':[], 'other':[]}
499
500  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
507  # TODO .def files on Windows
508  for source in all_sources:
509    _, ext = posixpath.splitext(source)
510    source_abs_path = project.GetAbsolutePath(source)
511    source_types[source_file_types.get(ext, 'other')].append(source_abs_path)
512
513  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
517  # 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
520  if target.gn_type in gn_target_types_that_absorb_objects:
521    object_dependencies = set()
522    project.GetObjectSourceDependencies(target.gn_name, object_dependencies)
523    for dependency in object_dependencies:
524      cmake_dependency_name = project.GetCMakeTargetName(dependency)
525      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:
531      sources[source_type] = '${target}__' + source_type + '_srcs'
532      SetVariableList(out, sources[source_type], sources_of_type)
533  return sources
534
535
536def WriteTarget(out, target, project):
537  out.write('\n#')
538  out.write(target.gn_name)
539  out.write('\n')
540
541  if target.cmake_type is None:
542    print ('Target %s has unknown target type %s, skipping.' %
543          (        target.gn_name,            target.gn_type ) )
544    return
545
546  SetVariable(out, 'target', target.cmake_name)
547
548  sources = WriteSourceVariables(out, target, project)
549
550  synthetic_dependencies = set()
551  if target.gn_type == 'action':
552    WriteAction(out, target, project, sources, synthetic_dependencies)
553  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)
557
558  out.write(target.cmake_type.command)
559  out.write('("${target}"')
560  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, ' ')
565  if synthetic_dependencies:
566    out.write(' DEPENDS')
567    for synthetic_dependencie in synthetic_dependencies:
568      WriteVariable(out, synthetic_dependencie, ' ')
569  out.write(')\n')
570
571  if target.cmake_type.command != 'add_custom_target':
572    WriteCompilerFlags(out, target, project, sources)
573
574  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
587  for dependency in dependencies:
588    gn_dependency_type = project.targets.get(dependency, {}).get('type', None)
589    cmake_dependency_type = cmake_target_types.get(gn_dependency_type, None)
590    cmake_dependency_name = project.GetCMakeTargetName(dependency)
591    if cmake_dependency_type.command != 'add_library':
592      nonlibraries.add(cmake_dependency_name)
593    elif cmake_dependency_type.modifier != 'OBJECT':
594      if target.cmake_type.is_linkable:
595        libraries.add(cmake_dependency_name)
596      else:
597        nonlibraries.add(cmake_dependency_name)
598
599  # Non-library dependencies.
600  if nonlibraries:
601    out.write('add_dependencies("${target}"')
602    for nonlibrary in nonlibraries:
603      out.write('\n  "')
604      out.write(nonlibrary)
605      out.write('"')
606    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):
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 2.8.8 FATAL_ERROR)\n')
652  out.write('cmake_policy(VERSION 2.8.8)\n\n')
653
654  out.write('file(WRITE "')
655  out.write(CMakeStringEscape(posixpath.join(project.build_path, "empty.cpp")))
656  out.write('")\n')
657
658  # Update the gn generated ninja build.
659  # If a build file has changed, this will update CMakeLists.ext if
660  # gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py
661  # style was used to create this config.
662  out.write('execute_process(COMMAND\n')
663  out.write('  ninja -C "')
664  out.write(CMakeStringEscape(project.build_path))
665  out.write('" build.ninja\n')
666  out.write('  RESULT_VARIABLE ninja_result)\n')
667  out.write('if (ninja_result)\n')
668  out.write('  message(WARNING ')
669  out.write('"Regeneration failed running ninja: ${ninja_result}")\n')
670  out.write('endif()\n')
671
672  out.write('include("')
673  out.write(CMakeStringEscape(extName))
674  out.write('")\n')
675  out.close()
676
677  out = open(extName, 'w+')
678  out.write('# Generated by gn_to_cmake.py.\n')
679  out.write('cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n')
680  out.write('cmake_policy(VERSION 2.8.8)\n')
681
682  # The following appears to be as-yet undocumented.
683  # http://public.kitware.com/Bug/view.php?id=8392
684  out.write('enable_language(ASM)\n\n')
685  # ASM-ATT does not support .S files.
686  # output.write('enable_language(ASM-ATT)\n')
687
688  # Current issues with automatic re-generation:
689  # The gn generated build.ninja target uses build.ninja.d
690  #   but build.ninja.d does not contain the ide or gn.
691  # Currently the ide is not run if the project.json file is not changed
692  #   but the ide needs to be run anyway if it has itself changed.
693  #   This can be worked around by deleting the project.json file.
694  out.write('file(READ "')
695  gn_deps_file = posixpath.join(project.build_path, 'build.ninja.d')
696  out.write(CMakeStringEscape(gn_deps_file))
697  out.write('" "gn_deps_string" OFFSET ')
698  out.write(str(len('build.ninja: ')))
699  out.write(')\n')
700  # One would think this would need to worry about escaped spaces
701  # but gn doesn't escape spaces here (it generates invalid .d files).
702  out.write('string(REPLACE " " ";" "gn_deps" ${gn_deps_string})\n')
703  out.write('foreach("gn_dep" ${gn_deps})\n')
704  out.write('  configure_file("')
705  out.write(CMakeStringEscape(project.build_path))
706  out.write('${gn_dep}" "CMakeLists.devnull" COPYONLY)\n')
707  out.write('endforeach("gn_dep")\n')
708
709  out.write('list(APPEND other_deps "')
710  out.write(CMakeStringEscape(os.path.abspath(__file__)))
711  out.write('")\n')
712  out.write('foreach("other_dep" ${other_deps})\n')
713  out.write('  configure_file("${other_dep}" "CMakeLists.devnull" COPYONLY)\n')
714  out.write('endforeach("other_dep")\n')
715
716  for target_name in project.targets.keys():
717    out.write('\n')
718    WriteTarget(out, Target(target_name, project), project)
719
720
721def main():
722  if len(sys.argv) != 2:
723    print('Usage: ' + sys.argv[0] + ' <json_file_name>')
724    exit(1)
725
726  json_path = sys.argv[1]
727  project = None
728  with open(json_path, 'r') as json_file:
729    project = json.loads(json_file.read())
730
731  WriteProject(Project(project))
732
733
734if __name__ == "__main__":
735  main()
736