forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reland of Enable whitelist generation for official builds.
Previous: https://codereview.chromium.org/2175413004/ Currently, all resources are included in PAK files when Chrome is built locally. Only official_buildbot.sh uses a resource whitelist. This CL enables local builds to use resource whitelisting by setting the enable_resource_whitelist_generation gn flag to true, or by building an official build. This will allow developers to more easily monitor the changes in APK size for each commit they make. However, a large amount of output is generated (_pragma is used to create warnings to allow whitelisted resources to be listed), so for now the whitelist will only be generated for official builds. This change results in a ~1.5 mb difference when calculating the APK size with resource_sizes.py. BUG=632385 Review-Url: https://codereview.chromium.org/2241383004 Cr-Commit-Position: refs/heads/master@{#413584}
- Loading branch information
estevenson
authored and
Commit bot
committed
Aug 22, 2016
1 parent
75f7dde
commit e19e708
Showing
12 changed files
with
308 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/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. | ||
|
||
"""Runs a compilation command. | ||
This script exists to avoid using complex shell commands in | ||
gcc_toolchain.gni's tool("cxx") and tool("cc") in case the host running the | ||
compiler does not have a POSIX-like shell (e.g. Windows). | ||
""" | ||
|
||
import argparse | ||
import sys | ||
|
||
import wrapper_utils | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument('--resource-whitelist', | ||
help='Generate a resource whitelist for this target.', | ||
metavar='PATH') | ||
parser.add_argument('command', nargs=argparse.REMAINDER, | ||
help='Compilation command') | ||
args = parser.parse_args() | ||
|
||
returncode, stderr = wrapper_utils.CaptureCommandStderr( | ||
wrapper_utils.CommandToRun(args.command)) | ||
|
||
used_resources = wrapper_utils.ExtractResourceIdsFromPragmaWarnings(stderr) | ||
sys.stderr.write(stderr) | ||
|
||
if args.resource_whitelist: | ||
with open(args.resource_whitelist, 'w') as f: | ||
if used_resources: | ||
f.write('\n'.join(str(resource) for resource in used_resources)) | ||
f.write('\n') | ||
|
||
|
||
return returncode | ||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright (c) 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. | ||
|
||
"""Helper functions for gcc_toolchain.gni wrappers.""" | ||
|
||
import os | ||
import re | ||
import subprocess | ||
import shlex | ||
import sys | ||
|
||
_BAT_PREFIX = 'cmd /c call ' | ||
_WHITELIST_RE = re.compile('whitelisted_resource_(?P<resource_id>[0-9]+)') | ||
|
||
|
||
def CommandToRun(command): | ||
"""Generates commands compatible with Windows. | ||
When running on a Windows host and using a toolchain whose tools are | ||
actually wrapper scripts (i.e. .bat files on Windows) rather than binary | ||
executables, the |command| to run has to be prefixed with this magic. | ||
The GN toolchain definitions take care of that for when GN/Ninja is | ||
running the tool directly. When that command is passed in to this | ||
script, it appears as a unitary string but needs to be split up so that | ||
just 'cmd' is the actual command given to Python's subprocess module. | ||
Args: | ||
command: List containing the UNIX style |command|. | ||
Returns: | ||
A list containing the Windows version of the |command|. | ||
""" | ||
if command[0].startswith(_BAT_PREFIX): | ||
command = command[0].split(None, 3) + command[1:] | ||
return command | ||
|
||
|
||
def ResolveRspLinks(inputs): | ||
"""Return a list of files contained in a response file. | ||
Args: | ||
inputs: A command containing rsp files. | ||
Returns: | ||
A set containing the rsp file content.""" | ||
rspfiles = [a[1:] for a in inputs if a.startswith('@')] | ||
resolved = set() | ||
for rspfile in rspfiles: | ||
with open(rspfile, 'r') as f: | ||
resolved.update(shlex.split(f.read())) | ||
|
||
return resolved | ||
|
||
|
||
def CombineResourceWhitelists(whitelist_candidates, outfile): | ||
"""Combines all whitelists for a resource file into a single whitelist. | ||
Args: | ||
whitelist_candidates: List of paths to rsp files containing all targets. | ||
outfile: Path to save the combined whitelist. | ||
""" | ||
whitelists = ('%s.whitelist' % candidate for candidate in whitelist_candidates | ||
if os.path.exists('%s.whitelist' % candidate)) | ||
|
||
resources = set() | ||
for whitelist in whitelists: | ||
with open(whitelist, 'r') as f: | ||
resources.update(f.readlines()) | ||
|
||
with open(outfile, 'w') as f: | ||
f.writelines(resources) | ||
|
||
|
||
def ExtractResourceIdsFromPragmaWarnings(text): | ||
"""Returns set of resource IDs that are inside unknown pragma warnings. | ||
Args: | ||
text: The text that will be scanned for unknown pragma warnings. | ||
Returns: | ||
A set containing integers representing resource IDs. | ||
""" | ||
used_resources = set() | ||
lines = text.splitlines() | ||
for ln in lines: | ||
match = _WHITELIST_RE.search(ln) | ||
if match: | ||
resource_id = int(match.group('resource_id')) | ||
used_resources.add(resource_id) | ||
|
||
return used_resources | ||
|
||
|
||
def CaptureCommandStderr(command): | ||
"""Returns the stderr of a command. | ||
Args: | ||
args: A list containing the command and arguments. | ||
cwd: The working directory from where the command should be made. | ||
env: Environment variables for the new process. | ||
""" | ||
child = subprocess.Popen(command, stderr=subprocess.PIPE) | ||
_, stderr = child.communicate() | ||
return child.returncode, stderr |
Oops, something went wrong.