blob: 2b88f453058c8af71b2b46bfd25b2a08ce175c6f [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Convert GN Xcode projects to platform and configuration independent targets.
GN generates Xcode projects that build one configuration only. However, typical
iOS development involves using the Xcode IDE to toggle the platform and
configuration. This script replaces the 'gn' configuration with 'Debug',
'Release' and 'Profile', and changes the ninja invocation to honor these
import argparse
import collections
import copy
import filecmp
import hashlib
import json
import os
import re
import shutil
import string
import subprocess
import sys
import tempfile
import xml.etree.ElementTree
class Template(string.Template):
"""A subclass of string.Template that changes delimiter."""
delimiter = '@'
def LoadSchemeTemplate(root):
"""Return a string.Template object for scheme file loaded relative to root."""
path = os.path.join(root, 'ios', 'build', 'tools', 'xcodescheme.template')
with open(path) as file:
return Template(
def CreateIdentifier(str_id):
"""Return a 24 characters string that can be used as an identifier."""
return hashlib.sha1(str_id.encode("utf-8")).hexdigest()[:24].upper()
def GenerateSchemeForTarget(project, old_project, name, product_name, template):
"""Generates the .xcsheme file for target named |name|.
The file is generated in the new project schemes directory from a template.
If there is an existing previous project, then the old scheme file is copied
and the lldbinit setting is set. If lldbinit setting is already correct, the
file is not modified, just copied.
relative_path = os.path.join('xcshareddata', 'xcschemes', name + '.xcscheme')
identifier = CreateIdentifier('%s %s' % (name, product_name))
scheme_path = os.path.join(project, relative_path)
if not os.path.isdir(os.path.dirname(scheme_path)):
old_scheme_path = os.path.join(old_project, relative_path)
if os.path.exists(old_scheme_path):
made_changes = False
tree = xml.etree.ElementTree.parse(old_scheme_path)
root = tree.getroot()
for reference in root.findall('.//BuildableReference'):
for (attr, value) in (
('BuildableName', product_name),
('BlueprintName', name),
('BlueprintIdentifier', identifier)):
if reference.get(attr) != value:
reference.set(attr, value)
made_changes = True
for child in root:
if child.tag not in ('TestAction', 'LaunchAction'):
if child.get('customLLDBInitFile') != LLDBINIT_PATH:
child.set('customLLDBInitFile', LLDBINIT_PATH)
made_changes = True
if made_changes:
tree.write(scheme_path, xml_declaration=True, encoding='UTF-8')
shutil.copyfile(old_scheme_path, scheme_path)
with open(scheme_path, 'w') as scheme_file:
class XcodeProject(object):
def __init__(self, objects, counter = 0):
self.objects = objects
self.counter = 0
def AddObject(self, parent_name, obj):
while True:
self.counter += 1
str_id = "%s %s %d" % (parent_name, obj['isa'], self.counter)
new_id = CreateIdentifier(str_id)
# Make sure ID is unique. It's possible there could be an id conflict
# since this is run after GN runs.
if new_id not in self.objects:
self.objects[new_id] = obj
return new_id
def check_output(command):
"""Wrapper around subprocess.check_output that decode output as utf-8."""
return subprocess.check_output(command).decode('utf-8')
def CopyFileIfChanged(source_path, target_path):
"""Copy |source_path| to |target_path| if different."""
target_dir = os.path.dirname(target_path)
if not os.path.isdir(target_dir):
if not os.path.exists(target_path) or \
not filecmp.cmp(source_path, target_path):
shutil.copyfile(source_path, target_path)
def CopyTreeIfChanged(source, target):
"""Copy |source| to |target| recursively; files are copied iff changed."""
if os.path.isfile(source):
return CopyFileIfChanged(source, target)
if not os.path.isdir(target):
for name in os.listdir(source):
os.path.join(source, name),
os.path.join(target, name))
def LoadXcodeProjectAsJSON(project_dir):
"""Return Xcode project at |path| as a JSON string."""
return check_output([
'plutil', '-convert', 'json', '-o', '-',
os.path.join(project_dir, 'project.pbxproj')])
def WriteXcodeProject(output_path, json_string):
"""Save Xcode project to |output_path| as XML."""
with tempfile.NamedTemporaryFile() as temp_file:
subprocess.check_call(['plutil', '-convert', 'xml1',])
os.path.join(output_path, 'project.pbxproj'))
def UpdateXcodeProject(project_dir, old_project_dir, configurations, root_dir):
"""Update inplace Xcode project to support multiple configurations.
project_dir: path to the input Xcode project
configurations: list of string corresponding to the configurations that
need to be supported by the tweaked Xcode projects, must contains at
least one value.
root_dir: path to the root directory used to find markdown files
json_data = json.loads(LoadXcodeProjectAsJSON(project_dir))
project = XcodeProject(json_data['objects'])
schemes_template = None
objects_to_remove = []
for value in list(project.objects.values()):
isa = value['isa']
# Teach build shell script to look for the configuration and platform.
if isa == 'PBXShellScriptBuildPhase':
shell_path = value['shellPath']
if shell_path.endswith('/sh'):
value['shellScript'] = value['shellScript'].replace(
'ninja -C .',
elif'[ /]python[23]?$', shell_path):
value['shellScript'] = value['shellScript'].replace(
'ninja_params = [ \'-C\', \'.\' ]',
'ninja_params = [ \'-C\', \'../\' + os.environ[\'CONFIGURATION\']'
' + os.environ[\'EFFECTIVE_PLATFORM_NAME\'] ]')
# Add new configuration, using the first one as default.
if isa == 'XCConfigurationList':
value['defaultConfigurationName'] = configurations[0]
build_config_template = project.objects[value['buildConfigurations'][0]]
build_config_template['buildSettings']['CONFIGURATION_BUILD_DIR'] = \
value['buildConfigurations'] = []
for configuration in configurations:
new_build_config = copy.copy(build_config_template)
new_build_config['name'] = configuration
project.AddObject('products', new_build_config))
# Create scheme files for application, extensions and framework targets.
if isa == 'PBXNativeTarget':
product_type = value['productType']
if product_type not in (
if schemes_template is None:
schemes_template = LoadSchemeTemplate(root_dir)
product = project.objects[value['productReference']]
project_dir, old_project_dir, value['name'],
product['path'], schemes_template)
for object_id in objects_to_remove:
del project.objects[object_id]
source = GetOrCreateRootGroup(project, json_data['rootObject'], 'Source')
AddMarkdownToProject(project, root_dir, source)
SortFileReferencesByName(project, source)
objects = collections.OrderedDict(sorted(project.objects.items()))
WriteXcodeProject(project_dir, json.dumps(json_data))
def CreateGroup(project, parent_group, group_name, path=None):
group_object = {
'children': [],
'isa': 'PBXGroup',
'name': group_name,
'sourceTree': '<group>',
if path is not None:
group_object['path'] = path
parent_group_name = parent_group.get('name', '')
group_object_key = project.AddObject(parent_group_name, group_object)
return group_object
def GetOrCreateRootGroup(project, root_object, group_name):
main_group = project.objects[project.objects[root_object]['mainGroup']]
for child_key in main_group['children']:
child = project.objects[child_key]
if child['name'] == group_name:
return child
return CreateGroup(project, main_group, group_name, path='../..')
class ObjectKey(object):
"""Wrapper around PBXFileReference and PBXGroup for sorting.
A PBXGroup represents a "directory" containing a list of files in an
Xcode project; it can contain references to a list of directories or
A PBXFileReference represents a "file".
The type is stored in the object "isa" property as a string. Since we
want to sort all directories before all files, the < and > operators
are defined so that if "isa" is different, they are sorted in the
reverse of alphabetic ordering, otherwise the name (or path) property
is checked and compared in alphabetic order.
def __init__(self, obj):
self.isa = obj['isa']
if 'name' in obj: = obj['name']
else: = obj['path']
def __lt__(self, other):
if self.isa != other.isa:
return self.isa > other.isa
return <
def __gt__(self, other):
if self.isa != other.isa:
return self.isa < other.isa
return >
def __eq__(self, other):
return self.isa == other.isa and ==
def SortFileReferencesByName(project, group_object):
project, group_object, lambda ref: ObjectKey(project.objects[ref]))
def SortFileReferencesByNameWithSortKey(project, group_object, sort_key):
for key in group_object['children']:
child = project.objects[key]
if child['isa'] == 'PBXGroup':
SortFileReferencesByNameWithSortKey(project, child, sort_key)
def AddMarkdownToProject(project, root_dir, group_object):
list_files_cmd = ['git', '-C', root_dir, 'ls-files', '*.md']
paths = check_output(list_files_cmd).splitlines()
ios_internal_dir = os.path.join(root_dir, 'ios_internal')
if os.path.exists(ios_internal_dir):
list_files_cmd = ['git', '-C', ios_internal_dir, 'ls-files', '*.md']
ios_paths = check_output(list_files_cmd).splitlines()
paths.extend([os.path.join("ios_internal", path) for path in ios_paths])
for path in paths:
new_markdown_entry = {
"fileEncoding": "4",
"isa": "PBXFileReference",
"lastKnownFileType": "net.daringfireball.markdown",
"name": os.path.basename(path),
"path": path,
"sourceTree": "<group>"
new_markdown_entry_id = project.AddObject('sources', new_markdown_entry)
folder = GetFolderForPath(project, group_object, os.path.dirname(path))
def GetFolderForPath(project, group_object, path):
objects = project.objects
if not path:
return group_object
for folder in path.split('/'):
children = group_object['children']
new_root = None
for child in children:
if objects[child]['isa'] == 'PBXGroup' and \
objects[child]['name'] == folder:
new_root = objects[child]
if not new_root:
# If the folder isn't found we could just cram it into the leaf existing
# folder, but that leads to folders with tons of inside.
new_root = CreateGroup(project, group_object, folder)
group_object = new_root
return group_object
def ConvertGnXcodeProject(root_dir, proj_name, input_dir, output_dir, configs):
'''Tweak the Xcode project generated by gn to support multiple configurations.
The Xcode projects generated by "gn gen --ide" only supports a single
platform and configuration (as the platform and configuration are set
per output directory). This method takes as input such projects and
add support for multiple configurations and platforms (to allow devs
to select them in Xcode).
root_dir: directory that is the root of the project
proj_name: name of the Xcode project "file" (usually `all.xcodeproj`)
input_dir: directory containing the XCode projects created by "gn gen --ide"
output_dir: directory where the tweaked Xcode projects will be saved
configs: list of string corresponding to the configurations that need to be
supported by the tweaked Xcode projects, must contains at least one
os.path.join(input_dir, proj_name),
os.path.join(output_dir, proj_name),
configs, root_dir)
CopyTreeIfChanged(os.path.join(input_dir, proj_name),
os.path.join(output_dir, proj_name))
def Main(args):
parser = argparse.ArgumentParser(
description='Convert GN Xcode projects for iOS.')
help='directory containing [product|all] Xcode projects.')
help='directory where to generate the iOS configuration.')
'--add-config', dest='configurations', default=[], action='append',
help='configuration to add to the Xcode project')
'--root', type=os.path.abspath, required=True,
help='root directory of the project')
'--project-name', default='all.xcodeproj', dest='proj_name',
help='name of the Xcode project (default: %(default)s)')
args = parser.parse_args(args)
if not os.path.isdir(args.input):
sys.stderr.write('Input directory does not exists.\n')
return 1
if args.project_name not in os.listdir(args.input):
'Input directory does not contain the Xcode project.\n')
return 1
if not args.configurations:
sys.stderr.write('At least one configuration required, see --add-config.\n')
return 1
if __name__ == '__main__':