2016-09-23 15:16:04 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright 2016 Google Inc.
|
|
|
|
#
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
2016-09-28 20:45:35 +00:00
|
|
|
|
2016-09-23 15:16:04 +00:00
|
|
|
"""
|
|
|
|
Usage: gn_to_cmake.py <json_file_name>
|
|
|
|
|
|
|
|
gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
gn gen out/config --ide=json
|
|
|
|
python gn/gn_to_cmake.py out/config/project.json
|
2016-10-05 21:18:38 +00:00
|
|
|
|
|
|
|
The first is recommended, as it will auto-update.
|
2016-09-23 15:16:04 +00:00
|
|
|
"""
|
|
|
|
|
2016-09-28 20:45:35 +00:00
|
|
|
|
2016-10-04 20:44:44 +00:00
|
|
|
import itertools
|
|
|
|
import functools
|
2016-09-23 15:16:04 +00:00
|
|
|
import json
|
|
|
|
import posixpath
|
|
|
|
import os
|
2016-10-04 20:44:44 +00:00
|
|
|
import string
|
2016-09-23 15:16:04 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
|
2016-09-28 20:45:35 +00:00
|
|
|
def CMakeStringEscape(a):
|
|
|
|
"""Escapes the string 'a' for use inside a CMake string.
|
|
|
|
|
|
|
|
This means escaping
|
|
|
|
'\' otherwise it may be seen as modifying the next character
|
|
|
|
'"' otherwise it will end the string
|
|
|
|
';' otherwise the string becomes a list
|
|
|
|
|
|
|
|
The following do not need to be escaped
|
|
|
|
'#' when the lexer is in string state, this does not start a comment
|
|
|
|
"""
|
|
|
|
return a.replace('\\', '\\\\').replace(';', '\\;').replace('"', '\\"')
|
|
|
|
|
|
|
|
|
2016-10-04 20:44:44 +00:00
|
|
|
def CMakeTargetEscape(a):
|
|
|
|
"""Escapes the string 'a' for use as a CMake target name.
|
|
|
|
|
|
|
|
CMP0037 in CMake 3.0 restricts target names to "^[A-Za-z0-9_.:+-]+$"
|
|
|
|
The ':' is only allowed for imported targets.
|
|
|
|
"""
|
|
|
|
def Escape(c):
|
|
|
|
if c in string.ascii_letters or c in string.digits or c in '_.+-':
|
|
|
|
return c
|
|
|
|
else:
|
|
|
|
return '__'
|
|
|
|
return ''.join(map(Escape, a))
|
|
|
|
|
|
|
|
|
2016-09-28 20:45:35 +00:00
|
|
|
def SetVariable(out, variable_name, value):
|
|
|
|
"""Sets a CMake variable."""
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write('set("')
|
|
|
|
out.write(CMakeStringEscape(variable_name))
|
|
|
|
out.write('" "')
|
2016-09-28 20:45:35 +00:00
|
|
|
out.write(CMakeStringEscape(value))
|
|
|
|
out.write('")\n')
|
|
|
|
|
|
|
|
|
|
|
|
def SetVariableList(out, variable_name, values):
|
|
|
|
"""Sets a CMake variable to a list."""
|
|
|
|
if not values:
|
|
|
|
return SetVariable(out, variable_name, "")
|
|
|
|
if len(values) == 1:
|
|
|
|
return SetVariable(out, variable_name, values[0])
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write('list(APPEND "')
|
|
|
|
out.write(CMakeStringEscape(variable_name))
|
|
|
|
out.write('"\n "')
|
2016-09-28 20:45:35 +00:00
|
|
|
out.write('"\n "'.join([CMakeStringEscape(value) for value in values]))
|
|
|
|
out.write('")\n')
|
|
|
|
|
|
|
|
|
|
|
|
def SetFilesProperty(output, variable, property_name, values, sep):
|
|
|
|
"""Given a set of source files, sets the given property on them."""
|
|
|
|
output.write('set_source_files_properties(')
|
|
|
|
WriteVariable(output, variable)
|
|
|
|
output.write(' PROPERTIES ')
|
|
|
|
output.write(property_name)
|
|
|
|
output.write(' "')
|
|
|
|
for value in values:
|
|
|
|
output.write(CMakeStringEscape(value))
|
|
|
|
output.write(sep)
|
|
|
|
output.write('")\n')
|
|
|
|
|
|
|
|
|
2016-10-04 20:44:44 +00:00
|
|
|
def SetCurrentTargetProperty(out, property_name, values, sep=''):
|
2016-09-28 20:45:35 +00:00
|
|
|
"""Given a target, sets the given property."""
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write('set_target_properties("${target}" PROPERTIES ')
|
2016-09-28 20:45:35 +00:00
|
|
|
out.write(property_name)
|
|
|
|
out.write(' "')
|
|
|
|
for value in values:
|
|
|
|
out.write(CMakeStringEscape(value))
|
|
|
|
out.write(sep)
|
|
|
|
out.write('")\n')
|
|
|
|
|
|
|
|
|
|
|
|
def WriteVariable(output, variable_name, prepend=None):
|
|
|
|
if prepend:
|
|
|
|
output.write(prepend)
|
|
|
|
output.write('${')
|
|
|
|
output.write(variable_name)
|
|
|
|
output.write('}')
|
|
|
|
|
|
|
|
|
|
|
|
# See GetSourceFileType in gn
|
|
|
|
source_file_types = {
|
|
|
|
'.cc': 'cxx',
|
|
|
|
'.cpp': 'cxx',
|
|
|
|
'.cxx': 'cxx',
|
2019-07-12 18:36:03 +00:00
|
|
|
'.m': 'objc',
|
|
|
|
'.mm': 'objcc',
|
2016-09-28 20:45:35 +00:00
|
|
|
'.c': 'c',
|
|
|
|
'.s': 'asm',
|
|
|
|
'.S': 'asm',
|
|
|
|
'.asm': 'asm',
|
|
|
|
'.o': 'obj',
|
|
|
|
'.obj': 'obj',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class CMakeTargetType(object):
|
|
|
|
def __init__(self, command, modifier, property_modifier, is_linkable):
|
|
|
|
self.command = command
|
|
|
|
self.modifier = modifier
|
|
|
|
self.property_modifier = property_modifier
|
|
|
|
self.is_linkable = is_linkable
|
|
|
|
CMakeTargetType.custom = CMakeTargetType('add_custom_target', 'SOURCES',
|
|
|
|
None, False)
|
|
|
|
|
|
|
|
# See GetStringForOutputType in gn
|
|
|
|
cmake_target_types = {
|
|
|
|
'unknown': CMakeTargetType.custom,
|
|
|
|
'group': CMakeTargetType.custom,
|
|
|
|
'executable': CMakeTargetType('add_executable', None, 'RUNTIME', True),
|
|
|
|
'loadable_module': CMakeTargetType('add_library', 'MODULE', 'LIBRARY', True),
|
|
|
|
'shared_library': CMakeTargetType('add_library', 'SHARED', 'LIBRARY', True),
|
2019-02-01 20:34:04 +00:00
|
|
|
'static_library': CMakeTargetType('add_library', 'STATIC', 'ARCHIVE', True),
|
2016-09-28 20:45:35 +00:00
|
|
|
'source_set': CMakeTargetType('add_library', 'OBJECT', None, False),
|
2016-09-29 19:41:53 +00:00
|
|
|
'copy': CMakeTargetType.custom,
|
2016-09-28 20:45:35 +00:00
|
|
|
'action': CMakeTargetType.custom,
|
|
|
|
'action_foreach': CMakeTargetType.custom,
|
|
|
|
'bundle_data': CMakeTargetType.custom,
|
|
|
|
'create_bundle': CMakeTargetType.custom,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-04 20:44:44 +00:00
|
|
|
def FindFirstOf(s, a):
|
|
|
|
return min(s.find(i) for i in a if i in s)
|
2016-09-29 19:41:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Project(object):
|
|
|
|
def __init__(self, project_json):
|
|
|
|
self.targets = project_json['targets']
|
|
|
|
build_settings = project_json['build_settings']
|
|
|
|
self.root_path = build_settings['root_path']
|
2020-01-13 19:43:20 +00:00
|
|
|
self.build_path = self.GetAbsolutePath(build_settings['build_dir'])
|
2016-09-29 19:41:53 +00:00
|
|
|
|
|
|
|
def GetAbsolutePath(self, path):
|
2020-01-13 19:43:20 +00:00
|
|
|
if path.startswith('//'):
|
|
|
|
return posixpath.join(self.root_path, path[2:])
|
2016-09-29 19:41:53 +00:00
|
|
|
else:
|
|
|
|
return path
|
|
|
|
|
2016-10-04 20:44:44 +00:00
|
|
|
def GetObjectSourceDependencies(self, gn_target_name, object_dependencies):
|
|
|
|
"""All OBJECT libraries whose sources have not been absorbed."""
|
2016-09-29 19:41:53 +00:00
|
|
|
dependencies = self.targets[gn_target_name].get('deps', [])
|
|
|
|
for dependency in dependencies:
|
|
|
|
dependency_type = self.targets[dependency].get('type', None)
|
|
|
|
if dependency_type == 'source_set':
|
|
|
|
object_dependencies.add(dependency)
|
|
|
|
if dependency_type not in gn_target_types_that_absorb_objects:
|
2016-10-04 20:44:44 +00:00
|
|
|
self.GetObjectSourceDependencies(dependency, object_dependencies)
|
|
|
|
|
|
|
|
def GetObjectLibraryDependencies(self, gn_target_name, object_dependencies):
|
|
|
|
"""All OBJECT libraries whose libraries have not been absorbed."""
|
|
|
|
dependencies = self.targets[gn_target_name].get('deps', [])
|
|
|
|
for dependency in dependencies:
|
|
|
|
dependency_type = self.targets[dependency].get('type', None)
|
|
|
|
if dependency_type == 'source_set':
|
|
|
|
object_dependencies.add(dependency)
|
|
|
|
self.GetObjectLibraryDependencies(dependency, object_dependencies)
|
2016-09-29 19:41:53 +00:00
|
|
|
|
|
|
|
def GetCMakeTargetName(self, gn_target_name):
|
2016-10-04 20:44:44 +00:00
|
|
|
# See <chromium>/src/tools/gn/label.cc#Resolve
|
|
|
|
# //base/test:test_support(//build/toolchain/win:msvc)
|
|
|
|
path_separator = FindFirstOf(gn_target_name, (':', '('))
|
|
|
|
location = None
|
|
|
|
name = None
|
|
|
|
toolchain = None
|
|
|
|
if not path_separator:
|
|
|
|
location = gn_target_name[2:]
|
|
|
|
else:
|
|
|
|
location = gn_target_name[2:path_separator]
|
|
|
|
toolchain_separator = gn_target_name.find('(', path_separator)
|
|
|
|
if toolchain_separator == -1:
|
|
|
|
name = gn_target_name[path_separator + 1:]
|
|
|
|
else:
|
|
|
|
if toolchain_separator > path_separator:
|
|
|
|
name = gn_target_name[path_separator + 1:toolchain_separator]
|
|
|
|
assert gn_target_name.endswith(')')
|
|
|
|
toolchain = gn_target_name[toolchain_separator + 1:-1]
|
|
|
|
assert location or name
|
|
|
|
|
|
|
|
cmake_target_name = None
|
|
|
|
if location.endswith('/' + name):
|
|
|
|
cmake_target_name = location
|
|
|
|
elif location:
|
|
|
|
cmake_target_name = location + '_' + name
|
|
|
|
else:
|
|
|
|
cmake_target_name = name
|
|
|
|
if toolchain:
|
|
|
|
cmake_target_name += '--' + toolchain
|
|
|
|
return CMakeTargetEscape(cmake_target_name)
|
2016-09-29 19:41:53 +00:00
|
|
|
|
|
|
|
|
2016-09-28 20:45:35 +00:00
|
|
|
class Target(object):
|
2016-09-29 19:41:53 +00:00
|
|
|
def __init__(self, gn_target_name, project):
|
|
|
|
self.gn_name = gn_target_name
|
|
|
|
self.properties = project.targets[self.gn_name]
|
|
|
|
self.cmake_name = project.GetCMakeTargetName(self.gn_name)
|
2016-09-28 20:45:35 +00:00
|
|
|
self.gn_type = self.properties.get('type', None)
|
|
|
|
self.cmake_type = cmake_target_types.get(self.gn_type, None)
|
|
|
|
|
|
|
|
|
2016-09-29 19:41:53 +00:00
|
|
|
def WriteAction(out, target, project, sources, synthetic_dependencies):
|
|
|
|
outputs = []
|
|
|
|
output_directories = set()
|
|
|
|
for output in target.properties.get('outputs', []):
|
|
|
|
output_abs_path = project.GetAbsolutePath(output)
|
|
|
|
outputs.append(output_abs_path)
|
|
|
|
output_directory = posixpath.dirname(output_abs_path)
|
|
|
|
if output_directory:
|
|
|
|
output_directories.add(output_directory)
|
2016-10-04 20:44:44 +00:00
|
|
|
outputs_name = '${target}__output'
|
2016-09-29 19:41:53 +00:00
|
|
|
SetVariableList(out, outputs_name, outputs)
|
|
|
|
|
|
|
|
out.write('add_custom_command(OUTPUT ')
|
|
|
|
WriteVariable(out, outputs_name)
|
|
|
|
out.write('\n')
|
|
|
|
|
2016-10-04 20:44:44 +00:00
|
|
|
if output_directories:
|
|
|
|
out.write(' COMMAND ${CMAKE_COMMAND} -E make_directory "')
|
|
|
|
out.write('" "'.join(map(CMakeStringEscape, output_directories)))
|
|
|
|
out.write('"\n')
|
|
|
|
|
|
|
|
script = target.properties['script']
|
|
|
|
arguments = target.properties['args']
|
|
|
|
out.write(' COMMAND python "')
|
|
|
|
out.write(CMakeStringEscape(project.GetAbsolutePath(script)))
|
|
|
|
out.write('"')
|
|
|
|
if arguments:
|
|
|
|
out.write('\n "')
|
|
|
|
out.write('"\n "'.join(map(CMakeStringEscape, arguments)))
|
|
|
|
out.write('"')
|
2016-09-29 19:41:53 +00:00
|
|
|
out.write('\n')
|
|
|
|
|
|
|
|
out.write(' DEPENDS ')
|
|
|
|
for sources_type_name in sources.values():
|
|
|
|
WriteVariable(out, sources_type_name, ' ')
|
|
|
|
out.write('\n')
|
|
|
|
|
2016-10-04 20:44:44 +00:00
|
|
|
#TODO: CMake 3.7 is introducing DEPFILE
|
|
|
|
|
|
|
|
out.write(' WORKING_DIRECTORY "')
|
|
|
|
out.write(CMakeStringEscape(project.build_path))
|
|
|
|
out.write('"\n')
|
|
|
|
|
|
|
|
out.write(' COMMENT "Action: ${target}"\n')
|
|
|
|
|
|
|
|
out.write(' VERBATIM)\n')
|
|
|
|
|
|
|
|
synthetic_dependencies.add(outputs_name)
|
|
|
|
|
|
|
|
|
|
|
|
def ExpandPlaceholders(source, a):
|
|
|
|
source_dir, source_file_part = posixpath.split(source)
|
|
|
|
source_name_part, _ = posixpath.splitext(source_file_part)
|
|
|
|
#TODO: {{source_gen_dir}}, {{source_out_dir}}, {{response_file_name}}
|
|
|
|
return a.replace('{{source}}', source) \
|
|
|
|
.replace('{{source_file_part}}', source_file_part) \
|
|
|
|
.replace('{{source_name_part}}', source_name_part) \
|
|
|
|
.replace('{{source_dir}}', source_dir) \
|
|
|
|
.replace('{{source_root_relative_dir}}', source_dir)
|
|
|
|
|
|
|
|
|
|
|
|
def WriteActionForEach(out, target, project, sources, synthetic_dependencies):
|
|
|
|
all_outputs = target.properties.get('outputs', [])
|
|
|
|
inputs = target.properties.get('sources', [])
|
|
|
|
# TODO: consider expanding 'output_patterns' instead.
|
|
|
|
outputs_per_input = len(all_outputs) / len(inputs)
|
|
|
|
for count, source in enumerate(inputs):
|
|
|
|
source_abs_path = project.GetAbsolutePath(source)
|
|
|
|
|
|
|
|
outputs = []
|
|
|
|
output_directories = set()
|
|
|
|
for output in all_outputs[outputs_per_input * count:
|
|
|
|
outputs_per_input * (count+1)]:
|
|
|
|
output_abs_path = project.GetAbsolutePath(output)
|
|
|
|
outputs.append(output_abs_path)
|
|
|
|
output_directory = posixpath.dirname(output_abs_path)
|
|
|
|
if output_directory:
|
|
|
|
output_directories.add(output_directory)
|
|
|
|
outputs_name = '${target}__output_' + str(count)
|
|
|
|
SetVariableList(out, outputs_name, outputs)
|
|
|
|
|
|
|
|
out.write('add_custom_command(OUTPUT ')
|
|
|
|
WriteVariable(out, outputs_name)
|
|
|
|
out.write('\n')
|
|
|
|
|
|
|
|
if output_directories:
|
|
|
|
out.write(' COMMAND ${CMAKE_COMMAND} -E make_directory "')
|
|
|
|
out.write('" "'.join(map(CMakeStringEscape, output_directories)))
|
|
|
|
out.write('"\n')
|
|
|
|
|
|
|
|
script = target.properties['script']
|
|
|
|
# TODO: need to expand {{xxx}} in arguments
|
|
|
|
arguments = target.properties['args']
|
|
|
|
out.write(' COMMAND python "')
|
|
|
|
out.write(CMakeStringEscape(project.GetAbsolutePath(script)))
|
|
|
|
out.write('"')
|
|
|
|
if arguments:
|
|
|
|
out.write('\n "')
|
|
|
|
expand = functools.partial(ExpandPlaceholders, source_abs_path)
|
|
|
|
out.write('"\n "'.join(map(CMakeStringEscape, map(expand,arguments))))
|
|
|
|
out.write('"')
|
|
|
|
out.write('\n')
|
|
|
|
|
|
|
|
out.write(' DEPENDS')
|
|
|
|
if 'input' in sources:
|
|
|
|
WriteVariable(out, sources['input'], ' ')
|
|
|
|
out.write(' "')
|
|
|
|
out.write(CMakeStringEscape(source_abs_path))
|
|
|
|
out.write('"\n')
|
|
|
|
|
|
|
|
#TODO: CMake 3.7 is introducing DEPFILE
|
|
|
|
|
|
|
|
out.write(' WORKING_DIRECTORY "')
|
|
|
|
out.write(CMakeStringEscape(project.build_path))
|
|
|
|
out.write('"\n')
|
|
|
|
|
|
|
|
out.write(' COMMENT "Action ${target} on ')
|
|
|
|
out.write(CMakeStringEscape(source_abs_path))
|
|
|
|
out.write('"\n')
|
|
|
|
|
|
|
|
out.write(' VERBATIM)\n')
|
|
|
|
|
|
|
|
synthetic_dependencies.add(outputs_name)
|
|
|
|
|
|
|
|
|
|
|
|
def WriteCopy(out, target, project, sources, synthetic_dependencies):
|
|
|
|
inputs = target.properties.get('sources', [])
|
|
|
|
raw_outputs = target.properties.get('outputs', [])
|
|
|
|
|
|
|
|
# TODO: consider expanding 'output_patterns' instead.
|
|
|
|
outputs = []
|
|
|
|
for output in raw_outputs:
|
|
|
|
output_abs_path = project.GetAbsolutePath(output)
|
|
|
|
outputs.append(output_abs_path)
|
|
|
|
outputs_name = '${target}__output'
|
|
|
|
SetVariableList(out, outputs_name, outputs)
|
|
|
|
|
|
|
|
out.write('add_custom_command(OUTPUT ')
|
|
|
|
WriteVariable(out, outputs_name)
|
2016-09-29 19:41:53 +00:00
|
|
|
out.write('\n')
|
|
|
|
|
2016-10-04 20:44:44 +00:00
|
|
|
for src, dst in zip(inputs, outputs):
|
2019-02-01 18:32:44 +00:00
|
|
|
abs_src_path = CMakeStringEscape(project.GetAbsolutePath(src))
|
|
|
|
# CMake distinguishes between copying files and copying directories but
|
|
|
|
# gn does not. We assume if the src has a period in its name then it is
|
|
|
|
# a file and otherwise a directory.
|
|
|
|
if "." in os.path.basename(abs_src_path):
|
|
|
|
out.write(' COMMAND ${CMAKE_COMMAND} -E copy "')
|
|
|
|
else:
|
|
|
|
out.write(' COMMAND ${CMAKE_COMMAND} -E copy_directory "')
|
|
|
|
out.write(abs_src_path)
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write('" "')
|
|
|
|
out.write(CMakeStringEscape(dst))
|
|
|
|
out.write('"\n')
|
|
|
|
|
|
|
|
out.write(' DEPENDS ')
|
|
|
|
for sources_type_name in sources.values():
|
|
|
|
WriteVariable(out, sources_type_name, ' ')
|
2016-09-29 19:41:53 +00:00
|
|
|
out.write('\n')
|
|
|
|
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write(' WORKING_DIRECTORY "')
|
|
|
|
out.write(CMakeStringEscape(project.build_path))
|
|
|
|
out.write('"\n')
|
|
|
|
|
|
|
|
out.write(' COMMENT "Copy ${target}"\n')
|
|
|
|
|
2016-09-29 19:41:53 +00:00
|
|
|
out.write(' VERBATIM)\n')
|
|
|
|
|
|
|
|
synthetic_dependencies.add(outputs_name)
|
|
|
|
|
|
|
|
|
|
|
|
def WriteCompilerFlags(out, target, project, sources):
|
2016-09-28 20:45:35 +00:00
|
|
|
# Hack, set linker language to c if no c or cxx files present.
|
|
|
|
if not 'c' in sources and not 'cxx' in sources:
|
2016-10-04 20:44:44 +00:00
|
|
|
SetCurrentTargetProperty(out, 'LINKER_LANGUAGE', ['C'])
|
2016-09-28 20:45:35 +00:00
|
|
|
|
|
|
|
# Mark uncompiled sources as uncompiled.
|
2016-09-29 19:41:53 +00:00
|
|
|
if 'input' in sources:
|
|
|
|
SetFilesProperty(out, sources['input'], 'HEADER_FILE_ONLY', ('True',), '')
|
2016-09-28 20:45:35 +00:00
|
|
|
if 'other' in sources:
|
2016-09-29 19:41:53 +00:00
|
|
|
SetFilesProperty(out, sources['other'], 'HEADER_FILE_ONLY', ('True',), '')
|
2016-09-28 20:45:35 +00:00
|
|
|
|
|
|
|
# Mark object sources as linkable.
|
|
|
|
if 'obj' in sources:
|
2016-09-29 19:41:53 +00:00
|
|
|
SetFilesProperty(out, sources['obj'], 'EXTERNAL_OBJECT', ('True',), '')
|
2016-09-28 20:45:35 +00:00
|
|
|
|
|
|
|
# TODO: 'output_name', 'output_dir', 'output_extension'
|
2016-09-29 19:41:53 +00:00
|
|
|
# This includes using 'source_outputs' to direct compiler output.
|
2016-09-28 20:45:35 +00:00
|
|
|
|
|
|
|
# Includes
|
|
|
|
includes = target.properties.get('include_dirs', [])
|
|
|
|
if includes:
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write('set_property(TARGET "${target}" ')
|
|
|
|
out.write('APPEND PROPERTY INCLUDE_DIRECTORIES')
|
2016-09-28 20:45:35 +00:00
|
|
|
for include_dir in includes:
|
|
|
|
out.write('\n "')
|
2016-09-29 19:41:53 +00:00
|
|
|
out.write(project.GetAbsolutePath(include_dir))
|
2016-09-28 20:45:35 +00:00
|
|
|
out.write('"')
|
|
|
|
out.write(')\n')
|
2016-09-23 15:16:04 +00:00
|
|
|
|
2016-09-28 20:45:35 +00:00
|
|
|
# Defines
|
|
|
|
defines = target.properties.get('defines', [])
|
|
|
|
if defines:
|
2016-10-04 20:44:44 +00:00
|
|
|
SetCurrentTargetProperty(out, 'COMPILE_DEFINITIONS', defines, ';')
|
2016-09-28 20:45:35 +00:00
|
|
|
|
|
|
|
# Compile flags
|
|
|
|
# "arflags", "asmflags", "cflags",
|
|
|
|
# "cflags_c", "clfags_cc", "cflags_objc", "clfags_objcc"
|
|
|
|
# CMake does not have per target lang compile flags.
|
|
|
|
# TODO: $<$<COMPILE_LANGUAGE:CXX>:cflags_cc style generator expression.
|
|
|
|
# http://public.kitware.com/Bug/view.php?id=14857
|
|
|
|
flags = []
|
|
|
|
flags.extend(target.properties.get('cflags', []))
|
|
|
|
cflags_asm = target.properties.get('asmflags', [])
|
|
|
|
cflags_c = target.properties.get('cflags_c', [])
|
|
|
|
cflags_cxx = target.properties.get('cflags_cc', [])
|
2019-07-12 18:36:03 +00:00
|
|
|
cflags_objc = cflags_c[:]
|
|
|
|
cflags_objc.extend(target.properties.get('cflags_objc', []))
|
|
|
|
cflags_objcc = cflags_cxx[:]
|
|
|
|
cflags_objcc.extend(target.properties.get('cflags_objcc', []))
|
|
|
|
|
|
|
|
if 'c' in sources and not any(k in sources for k in ('asm', 'cxx', 'objc', 'objcc')):
|
2016-09-28 20:45:35 +00:00
|
|
|
flags.extend(cflags_c)
|
2019-07-12 18:36:03 +00:00
|
|
|
elif 'cxx' in sources and not any(k in sources for k in ('asm', 'c', 'objc', 'objcc')):
|
2016-09-28 20:45:35 +00:00
|
|
|
flags.extend(cflags_cxx)
|
2019-07-12 18:36:03 +00:00
|
|
|
elif 'objc' in sources and not any(k in sources for k in ('asm', 'c', 'cxx', 'objcc')):
|
|
|
|
flags.extend(cflags_objc)
|
|
|
|
elif 'objcc' in sources and not any(k in sources for k in ('asm', 'c', 'cxx', 'objc')):
|
|
|
|
flags.extend(cflags_objcc)
|
2016-09-28 20:45:35 +00:00
|
|
|
else:
|
|
|
|
# TODO: This is broken, one cannot generally set properties on files,
|
|
|
|
# as other targets may require different properties on the same files.
|
|
|
|
if 'asm' in sources and cflags_asm:
|
|
|
|
SetFilesProperty(out, sources['asm'], 'COMPILE_FLAGS', cflags_asm, ' ')
|
|
|
|
if 'c' in sources and cflags_c:
|
|
|
|
SetFilesProperty(out, sources['c'], 'COMPILE_FLAGS', cflags_c, ' ')
|
|
|
|
if 'cxx' in sources and cflags_cxx:
|
|
|
|
SetFilesProperty(out, sources['cxx'], 'COMPILE_FLAGS', cflags_cxx, ' ')
|
2019-07-12 18:36:03 +00:00
|
|
|
if 'objc' in sources and cflags_objc:
|
|
|
|
SetFilesProperty(out, sources['objc'], 'COMPILE_FLAGS', cflags_objc, ' ')
|
|
|
|
if 'objcc' in sources and cflags_objcc:
|
|
|
|
SetFilesProperty(out, sources['objcc'], 'COMPILE_FLAGS', cflags_objcc, ' ')
|
2016-09-28 20:45:35 +00:00
|
|
|
if flags:
|
2016-10-04 20:44:44 +00:00
|
|
|
SetCurrentTargetProperty(out, 'COMPILE_FLAGS', flags, ' ')
|
2016-09-28 20:45:35 +00:00
|
|
|
|
|
|
|
# Linker flags
|
|
|
|
ldflags = target.properties.get('ldflags', [])
|
|
|
|
if ldflags:
|
2016-10-04 20:44:44 +00:00
|
|
|
SetCurrentTargetProperty(out, 'LINK_FLAGS', ldflags, ' ')
|
2016-09-28 20:45:35 +00:00
|
|
|
|
|
|
|
|
2016-09-29 19:41:53 +00:00
|
|
|
gn_target_types_that_absorb_objects = (
|
|
|
|
'executable',
|
|
|
|
'loadable_module',
|
|
|
|
'shared_library',
|
|
|
|
'static_library'
|
|
|
|
)
|
2016-09-28 20:45:35 +00:00
|
|
|
|
|
|
|
|
2016-09-29 19:41:53 +00:00
|
|
|
def WriteSourceVariables(out, target, project):
|
2016-09-28 20:45:35 +00:00
|
|
|
# gn separates the sheep from the goats based on file extensions.
|
|
|
|
# A full separation is done here because of flag handing (see Compile flags).
|
2019-07-12 18:36:03 +00:00
|
|
|
source_types = {'cxx':[], 'c':[], 'asm':[], 'objc':[], 'objcc':[],
|
2016-09-29 19:41:53 +00:00
|
|
|
'obj':[], 'obj_target':[], 'input':[], 'other':[]}
|
|
|
|
|
2018-05-18 19:09:41 +00:00
|
|
|
all_sources = target.properties.get('sources', [])
|
|
|
|
|
|
|
|
# As of cmake 3.11 add_library must have sources. If there are
|
|
|
|
# no sources, add empty.cpp as the file to compile.
|
|
|
|
if len(all_sources) == 0:
|
|
|
|
all_sources.append(posixpath.join(project.build_path, 'empty.cpp'))
|
|
|
|
|
2016-09-29 19:41:53 +00:00
|
|
|
# TODO .def files on Windows
|
2018-05-18 19:09:41 +00:00
|
|
|
for source in all_sources:
|
2016-09-28 20:45:35 +00:00
|
|
|
_, ext = posixpath.splitext(source)
|
2016-09-29 19:41:53 +00:00
|
|
|
source_abs_path = project.GetAbsolutePath(source)
|
2016-09-28 20:45:35 +00:00
|
|
|
source_types[source_file_types.get(ext, 'other')].append(source_abs_path)
|
|
|
|
|
2016-09-29 19:41:53 +00:00
|
|
|
for input_path in target.properties.get('inputs', []):
|
|
|
|
input_abs_path = project.GetAbsolutePath(input_path)
|
|
|
|
source_types['input'].append(input_abs_path)
|
|
|
|
|
2016-09-28 20:45:35 +00:00
|
|
|
# OBJECT library dependencies need to be listed as sources.
|
|
|
|
# Only executables and non-OBJECT libraries may reference an OBJECT library.
|
|
|
|
# https://gitlab.kitware.com/cmake/cmake/issues/14778
|
2016-09-29 19:41:53 +00:00
|
|
|
if target.gn_type in gn_target_types_that_absorb_objects:
|
2016-09-28 20:45:35 +00:00
|
|
|
object_dependencies = set()
|
2016-10-04 20:44:44 +00:00
|
|
|
project.GetObjectSourceDependencies(target.gn_name, object_dependencies)
|
2016-09-28 20:45:35 +00:00
|
|
|
for dependency in object_dependencies:
|
2016-09-29 19:41:53 +00:00
|
|
|
cmake_dependency_name = project.GetCMakeTargetName(dependency)
|
2016-09-28 20:45:35 +00:00
|
|
|
obj_target_sources = '$<TARGET_OBJECTS:' + cmake_dependency_name + '>'
|
|
|
|
source_types['obj_target'].append(obj_target_sources)
|
|
|
|
|
|
|
|
sources = {}
|
|
|
|
for source_type, sources_of_type in source_types.items():
|
|
|
|
if sources_of_type:
|
2016-10-04 20:44:44 +00:00
|
|
|
sources[source_type] = '${target}__' + source_type + '_srcs'
|
2016-09-28 20:45:35 +00:00
|
|
|
SetVariableList(out, sources[source_type], sources_of_type)
|
|
|
|
return sources
|
|
|
|
|
|
|
|
|
2016-09-29 19:41:53 +00:00
|
|
|
def WriteTarget(out, target, project):
|
2016-09-28 20:45:35 +00:00
|
|
|
out.write('\n#')
|
2016-09-29 19:41:53 +00:00
|
|
|
out.write(target.gn_name)
|
2016-09-28 20:45:35 +00:00
|
|
|
out.write('\n')
|
|
|
|
|
|
|
|
if target.cmake_type is None:
|
|
|
|
print ('Target %s has unknown target type %s, skipping.' %
|
2016-09-29 19:41:53 +00:00
|
|
|
( target.gn_name, target.gn_type ) )
|
2016-09-28 20:45:35 +00:00
|
|
|
return
|
|
|
|
|
2016-10-04 20:44:44 +00:00
|
|
|
SetVariable(out, 'target', target.cmake_name)
|
|
|
|
|
2016-09-29 19:41:53 +00:00
|
|
|
sources = WriteSourceVariables(out, target, project)
|
|
|
|
|
|
|
|
synthetic_dependencies = set()
|
|
|
|
if target.gn_type == 'action':
|
|
|
|
WriteAction(out, target, project, sources, synthetic_dependencies)
|
2016-10-04 20:44:44 +00:00
|
|
|
if target.gn_type == 'action_foreach':
|
|
|
|
WriteActionForEach(out, target, project, sources, synthetic_dependencies)
|
|
|
|
if target.gn_type == 'copy':
|
|
|
|
WriteCopy(out, target, project, sources, synthetic_dependencies)
|
2016-09-28 20:45:35 +00:00
|
|
|
|
|
|
|
out.write(target.cmake_type.command)
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write('("${target}"')
|
2016-09-28 20:45:35 +00:00
|
|
|
if target.cmake_type.modifier is not None:
|
|
|
|
out.write(' ')
|
|
|
|
out.write(target.cmake_type.modifier)
|
|
|
|
for sources_type_name in sources.values():
|
|
|
|
WriteVariable(out, sources_type_name, ' ')
|
2016-09-29 19:41:53 +00:00
|
|
|
if synthetic_dependencies:
|
|
|
|
out.write(' DEPENDS')
|
|
|
|
for synthetic_dependencie in synthetic_dependencies:
|
|
|
|
WriteVariable(out, synthetic_dependencie, ' ')
|
2016-09-28 20:45:35 +00:00
|
|
|
out.write(')\n')
|
|
|
|
|
|
|
|
if target.cmake_type.command != 'add_custom_target':
|
2016-09-29 19:41:53 +00:00
|
|
|
WriteCompilerFlags(out, target, project, sources)
|
2016-09-28 20:45:35 +00:00
|
|
|
|
2016-10-04 20:44:44 +00:00
|
|
|
libraries = set()
|
|
|
|
nonlibraries = set()
|
|
|
|
|
|
|
|
dependencies = set(target.properties.get('deps', []))
|
|
|
|
# Transitive OBJECT libraries are in sources.
|
|
|
|
# Those sources are dependent on the OBJECT library dependencies.
|
|
|
|
# Those sources cannot bring in library dependencies.
|
|
|
|
object_dependencies = set()
|
|
|
|
if target.gn_type != 'source_set':
|
|
|
|
project.GetObjectLibraryDependencies(target.gn_name, object_dependencies)
|
|
|
|
for object_dependency in object_dependencies:
|
|
|
|
dependencies.update(project.targets.get(object_dependency).get('deps', []))
|
|
|
|
|
2016-09-28 20:45:35 +00:00
|
|
|
for dependency in dependencies:
|
2016-09-29 19:41:53 +00:00
|
|
|
gn_dependency_type = project.targets.get(dependency, {}).get('type', None)
|
2016-09-28 20:45:35 +00:00
|
|
|
cmake_dependency_type = cmake_target_types.get(gn_dependency_type, None)
|
2016-09-29 19:41:53 +00:00
|
|
|
cmake_dependency_name = project.GetCMakeTargetName(dependency)
|
2016-09-28 20:45:35 +00:00
|
|
|
if cmake_dependency_type.command != 'add_library':
|
2016-10-04 20:44:44 +00:00
|
|
|
nonlibraries.add(cmake_dependency_name)
|
2016-09-28 20:45:35 +00:00
|
|
|
elif cmake_dependency_type.modifier != 'OBJECT':
|
2016-09-29 19:41:53 +00:00
|
|
|
if target.cmake_type.is_linkable:
|
2016-10-04 20:44:44 +00:00
|
|
|
libraries.add(cmake_dependency_name)
|
2016-09-29 19:41:53 +00:00
|
|
|
else:
|
2016-10-04 20:44:44 +00:00
|
|
|
nonlibraries.add(cmake_dependency_name)
|
2016-09-28 20:45:35 +00:00
|
|
|
|
|
|
|
# Non-library dependencies.
|
|
|
|
if nonlibraries:
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write('add_dependencies("${target}"')
|
2016-09-28 20:45:35 +00:00
|
|
|
for nonlibrary in nonlibraries:
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write('\n "')
|
2016-09-29 19:41:53 +00:00
|
|
|
out.write(nonlibrary)
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write('"')
|
2016-09-28 20:45:35 +00:00
|
|
|
out.write(')\n')
|
|
|
|
|
|
|
|
# Non-OBJECT library dependencies.
|
2020-08-12 14:45:00 +00:00
|
|
|
combined_library_lists = [target.properties.get(key, []) for key in ['libs', 'frameworks']]
|
|
|
|
external_libraries = list(itertools.chain(*combined_library_lists))
|
2016-09-28 20:45:35 +00:00
|
|
|
if target.cmake_type.is_linkable and (external_libraries or libraries):
|
2016-10-27 22:47:54 +00:00
|
|
|
library_dirs = target.properties.get('lib_dirs', [])
|
|
|
|
if library_dirs:
|
|
|
|
SetVariableList(out, '${target}__library_directories', library_dirs)
|
|
|
|
|
2016-09-28 20:45:35 +00:00
|
|
|
system_libraries = []
|
|
|
|
for external_library in external_libraries:
|
|
|
|
if '/' in external_library:
|
2016-10-04 20:44:44 +00:00
|
|
|
libraries.add(project.GetAbsolutePath(external_library))
|
2016-09-28 20:45:35 +00:00
|
|
|
else:
|
|
|
|
if external_library.endswith('.framework'):
|
|
|
|
external_library = external_library[:-len('.framework')]
|
2016-10-27 22:47:54 +00:00
|
|
|
system_library = 'library__' + external_library
|
|
|
|
if library_dirs:
|
|
|
|
system_library = system_library + '__for_${target}'
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write('find_library("')
|
|
|
|
out.write(CMakeStringEscape(system_library))
|
|
|
|
out.write('" "')
|
|
|
|
out.write(CMakeStringEscape(external_library))
|
2016-10-27 22:47:54 +00:00
|
|
|
out.write('"')
|
|
|
|
if library_dirs:
|
|
|
|
out.write(' PATHS "')
|
|
|
|
WriteVariable(out, '${target}__library_directories')
|
|
|
|
out.write('"')
|
|
|
|
out.write(')\n')
|
2016-09-28 20:45:35 +00:00
|
|
|
system_libraries.append(system_library)
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write('target_link_libraries("${target}"')
|
2016-09-28 20:45:35 +00:00
|
|
|
for library in libraries:
|
|
|
|
out.write('\n "')
|
|
|
|
out.write(CMakeStringEscape(library))
|
|
|
|
out.write('"')
|
|
|
|
for system_library in system_libraries:
|
2016-10-04 20:44:44 +00:00
|
|
|
WriteVariable(out, system_library, '\n "')
|
|
|
|
out.write('"')
|
2016-09-28 20:45:35 +00:00
|
|
|
out.write(')\n')
|
|
|
|
|
|
|
|
|
|
|
|
def WriteProject(project):
|
2016-09-29 19:41:53 +00:00
|
|
|
out = open(posixpath.join(project.build_path, 'CMakeLists.txt'), 'w+')
|
2018-05-31 19:04:43 +00:00
|
|
|
extName = posixpath.join(project.build_path, 'CMakeLists.ext')
|
2016-10-04 20:44:44 +00:00
|
|
|
out.write('# Generated by gn_to_cmake.py.\n')
|
2021-04-09 16:47:36 +00:00
|
|
|
out.write('cmake_minimum_required(VERSION 3.7 FATAL_ERROR)\n')
|
|
|
|
out.write('cmake_policy(VERSION 3.7)\n')
|
2019-09-25 14:37:34 +00:00
|
|
|
out.write('project(Skia)\n\n')
|
2016-10-05 21:18:38 +00:00
|
|
|
|
2018-05-18 19:09:41 +00:00
|
|
|
out.write('file(WRITE "')
|
2018-05-31 19:04:43 +00:00
|
|
|
out.write(CMakeStringEscape(posixpath.join(project.build_path, "empty.cpp")))
|
2018-05-18 19:09:41 +00:00
|
|
|
out.write('")\n')
|
|
|
|
|
2016-10-05 21:18:38 +00:00
|
|
|
# Update the gn generated ninja build.
|
|
|
|
# If a build file has changed, this will update CMakeLists.ext if
|
|
|
|
# gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py
|
|
|
|
# style was used to create this config.
|
2018-06-25 19:35:28 +00:00
|
|
|
out.write('execute_process(COMMAND\n')
|
|
|
|
out.write(' ninja -C "')
|
2016-10-05 21:18:38 +00:00
|
|
|
out.write(CMakeStringEscape(project.build_path))
|
2018-06-25 19:35:28 +00:00
|
|
|
out.write('" build.ninja\n')
|
|
|
|
out.write(' RESULT_VARIABLE ninja_result)\n')
|
|
|
|
out.write('if (ninja_result)\n')
|
|
|
|
out.write(' message(WARNING ')
|
|
|
|
out.write('"Regeneration failed running ninja: ${ninja_result}")\n')
|
|
|
|
out.write('endif()\n')
|
2016-10-05 21:18:38 +00:00
|
|
|
|
2018-05-31 19:04:43 +00:00
|
|
|
out.write('include("')
|
|
|
|
out.write(CMakeStringEscape(extName))
|
|
|
|
out.write('")\n')
|
2021-04-16 15:47:25 +00:00
|
|
|
# This lets Clion find the emscripten header files when working with CanvasKit.
|
|
|
|
out.write('include_directories(SYSTEM $ENV{EMSDK}/upstream/emscripten/system/include/)\n')
|
2016-10-05 21:18:38 +00:00
|
|
|
out.close()
|
|
|
|
|
2018-05-31 19:04:43 +00:00
|
|
|
out = open(extName, 'w+')
|
2016-10-05 21:18:38 +00:00
|
|
|
out.write('# Generated by gn_to_cmake.py.\n')
|
2021-04-09 16:47:36 +00:00
|
|
|
out.write('cmake_minimum_required(VERSION 3.7 FATAL_ERROR)\n')
|
|
|
|
out.write('cmake_policy(VERSION 3.7)\n')
|
2016-09-23 15:16:04 +00:00
|
|
|
|
2016-09-28 20:45:35 +00:00
|
|
|
# The following appears to be as-yet undocumented.
|
|
|
|
# http://public.kitware.com/Bug/view.php?id=8392
|
2016-10-05 21:18:38 +00:00
|
|
|
out.write('enable_language(ASM)\n\n')
|
2016-09-28 20:45:35 +00:00
|
|
|
# ASM-ATT does not support .S files.
|
|
|
|
# output.write('enable_language(ASM-ATT)\n')
|
2016-09-23 15:16:04 +00:00
|
|
|
|
2016-10-05 21:18:38 +00:00
|
|
|
# Current issues with automatic re-generation:
|
|
|
|
# The gn generated build.ninja target uses build.ninja.d
|
|
|
|
# but build.ninja.d does not contain the ide or gn.
|
|
|
|
# Currently the ide is not run if the project.json file is not changed
|
|
|
|
# but the ide needs to be run anyway if it has itself changed.
|
|
|
|
# This can be worked around by deleting the project.json file.
|
|
|
|
out.write('file(READ "')
|
|
|
|
gn_deps_file = posixpath.join(project.build_path, 'build.ninja.d')
|
|
|
|
out.write(CMakeStringEscape(gn_deps_file))
|
|
|
|
out.write('" "gn_deps_string" OFFSET ')
|
|
|
|
out.write(str(len('build.ninja: ')))
|
|
|
|
out.write(')\n')
|
|
|
|
# One would think this would need to worry about escaped spaces
|
|
|
|
# but gn doesn't escape spaces here (it generates invalid .d files).
|
|
|
|
out.write('string(REPLACE " " ";" "gn_deps" ${gn_deps_string})\n')
|
|
|
|
out.write('foreach("gn_dep" ${gn_deps})\n')
|
2018-06-25 19:35:28 +00:00
|
|
|
out.write(' configure_file("')
|
|
|
|
out.write(CMakeStringEscape(project.build_path))
|
|
|
|
out.write('${gn_dep}" "CMakeLists.devnull" COPYONLY)\n')
|
2016-10-05 21:18:38 +00:00
|
|
|
out.write('endforeach("gn_dep")\n')
|
|
|
|
|
2018-06-25 19:35:28 +00:00
|
|
|
out.write('list(APPEND other_deps "')
|
|
|
|
out.write(CMakeStringEscape(os.path.abspath(__file__)))
|
|
|
|
out.write('")\n')
|
|
|
|
out.write('foreach("other_dep" ${other_deps})\n')
|
|
|
|
out.write(' configure_file("${other_dep}" "CMakeLists.devnull" COPYONLY)\n')
|
|
|
|
out.write('endforeach("other_dep")\n')
|
|
|
|
|
2016-09-29 19:41:53 +00:00
|
|
|
for target_name in project.targets.keys():
|
2016-09-23 15:16:04 +00:00
|
|
|
out.write('\n')
|
2016-09-29 19:41:53 +00:00
|
|
|
WriteTarget(out, Target(target_name, project), project)
|
2016-09-23 15:16:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print('Usage: ' + sys.argv[0] + ' <json_file_name>')
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
json_path = sys.argv[1]
|
|
|
|
project = None
|
|
|
|
with open(json_path, 'r') as json_file:
|
|
|
|
project = json.loads(json_file.read())
|
|
|
|
|
2016-09-29 19:41:53 +00:00
|
|
|
WriteProject(Project(project))
|
2016-09-28 20:45:35 +00:00
|
|
|
|
2016-09-23 15:16:04 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|