blob: e160665f89fbc20635231c37bdb997ba4d652ae3 [file] [log] [blame]
# 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.
import argparse
import os
import re
import subprocess
import sys
import tempfile
"""Wrapper around actool to compile assets catalog.
The script is a wrapper around actool to compile
assets catalog to that turns warning into errors. It also
fixes some quirks of actool to make it work from ninja (mostly that
actool seems to require absolute path but gn generates command-line
with relative paths).
The wrapper filter out any message that is not a section header and
not a warning or error message, and fails if filtered output is not
empty. This should to treat all warnings as error until actool has
an option to fail with non-zero error code when there are warnings.
# Pattern matching a section header in the output of actool.
SECTION_HEADER = re.compile('^/\\* ([^ ]*) \\*/$')
# Name of the section containing informational messages that can be ignored.
# Regular expressions matching spurious messages from actool that should be
# ignored (as they are bogus). Generally a bug should be filed with Apple
# when adding a pattern here.
re.compile(v) for v in [
#, likely a bug in Xcode 9.1 beta, remove once build
# requires a version of Xcode with a fix.
r'\[\]\[ipad\]\[76x76\]\[\]\[\]\[1x\]\[\]\[\]: notice: \(null\)',
#, likely a bug in Xcode 9.2 beta, remove once build
# requires a version of Xcode with a fix.
r'\[\]\[ipad\]\[76x76\]\[\]\[\]\[1x\]\[\]\[\]: notice: 76x76@1x app'
' icons only apply to iPad apps targeting releases of iOS prior to'
' 10.0.',
# Map special type of asset catalog to the corresponding command-line
# parameter that need to be passed to actool.
'.appiconset': '--app-icon',
'.launchimage': '--launch-image',
def IsSpuriousMessage(line):
"""Returns whether line contains a spurious message that should be ignored."""
for pattern in SPURIOUS_PATTERNS:
match =
if match is not None:
return True
return False
def FilterCompilerOutput(compiler_output, relative_paths):
"""Filers actool compilation output.
The compiler output is composed of multiple sections for each different
level of output (error, warning, notices, ...). Each section starts with
the section name on a single line, followed by all the messages from the
The function filter any lines that are not in or sections (as spurious messages comes
before any section of the output).
See, and for some example
messages that pollute the output of actool and cause flaky builds.
compiler_output: string containing the output generated by the
compiler (contains both stdout and stderr)
relative_paths: mapping from absolute to relative paths used to
convert paths in the warning and error messages (unknown paths
will be left unaltered)
The filtered output of the compiler. If the compilation was a
success, then the output will be empty, otherwise it will use
relative path and omit any irrelevant output.
filtered_output = []
current_section = None
data_in_section = False
for line in compiler_output.splitlines():
match =
if match is not None:
data_in_section = False
current_section =
if current_section and current_section != NOTICE_SECTION:
if IsSpuriousMessage(line):
absolute_path = line.split(':')[0]
relative_path = relative_paths.get(absolute_path, absolute_path)
if absolute_path != relative_path:
line = relative_path + line[len(absolute_path):]
if not data_in_section:
data_in_section = True
filtered_output.append('/* %s */\n' % current_section)
filtered_output.append(line + '\n')
return ''.join(filtered_output)
def CompileAssetCatalog(output, platform, product_type, min_deployment_target,
inputs, compress_pngs, partial_info_plist):
"""Compile the .xcassets bundles to an asset catalog using actool.
output: absolute path to the containing bundle
platform: the targeted platform
product_type: the bundle type
min_deployment_target: minimum deployment target
inputs: list of absolute paths to .xcassets bundles
compress_pngs: whether to enable compression of pngs
partial_info_plist: path to partial Info.plist to generate
command = [
if compress_pngs:
if product_type != '':
command.extend(['--product-type', product_type])
if platform == 'macosx':
command.extend(['--target-device', 'mac'])
command.extend(['--target-device', 'iphone', '--target-device', 'ipad'])
# Scan the input directories for the presence of asset catalog types that
# require special treatment, and if so, add them to the actool command-line.
for relative_path in inputs:
if not os.path.isdir(relative_path):
for file_or_dir_name in os.listdir(relative_path):
if not os.path.isdir(os.path.join(relative_path, file_or_dir_name)):
asset_name, asset_type = os.path.splitext(file_or_dir_name)
if asset_type not in ACTOOL_FLAG_FOR_ASSET_TYPE:
command.extend([ACTOOL_FLAG_FOR_ASSET_TYPE[asset_type], asset_name])
# Always ask actool to generate a partial Info.plist file. If not path
# has been given by the caller, use a temporary file name.
temporary_file = None
if not partial_info_plist:
temporary_file = tempfile.NamedTemporaryFile(suffix='.plist')
partial_info_plist =
command.extend(['--output-partial-info-plist', partial_info_plist])
# Dictionary used to convert absolute paths back to their relative form
# in the output of actool.
relative_paths = {}
# actool crashes if paths are relative, so convert input and output paths
# to absolute paths, and record the relative paths to fix them back when
# filtering the output.
absolute_output = os.path.abspath(output)
relative_paths[output] = absolute_output
relative_paths[os.path.dirname(output)] = os.path.dirname(absolute_output)
command.extend(['--compile', os.path.dirname(os.path.abspath(output))])
for relative_path in inputs:
absolute_path = os.path.abspath(relative_path)
relative_paths[absolute_path] = relative_path
# Run actool and redirect stdout and stderr to the same pipe (as actool
# is confused about what should go to stderr/stdout).
process = subprocess.Popen(command,
stdout, _ = process.communicate()
# Filter the output to remove all garbarge and to fix the paths.
stdout = FilterCompilerOutput(stdout.decode('UTF-8'), relative_paths)
if process.returncode or stdout:
if temporary_file:
def Main():
parser = argparse.ArgumentParser(
description='compile assets catalog for a bundle')
choices=('macosx', 'iphoneos', 'iphonesimulator'),
help='target platform for the compiled assets catalog')
help='minimum deployment target for the compiled assets catalog')
help='path to the compiled assets catalog')
help='recompress PNGs while compiling assets catalog')
help='type of the containing bundle')
help='path to partial info plist to create')
help='path to input assets catalog sources')
args = parser.parse_args()
if os.path.basename(args.output) != '':
sys.stderr.write('output should be path to compiled asset catalog, not '
'to the containing bundle: %s\n' % (args.output, ))
CompileAssetCatalog(args.output, args.platform, args.product_type,
args.minimum_deployment_target, args.inputs,
args.compress_pngs, args.partial_info_plist)
if __name__ == '__main__':