Commit 70b843c9 authored by David 'Digit' Turner's avatar David 'Digit' Turner Committed by Commit Bot

build: Add android_debug_resources_temp_dir

Normally, compiling Android resources requires unpacking
and processing resource dependencies into a temporary
directory before sending the result to 'aapt2 compile',
then getting rid of these intermediate files.

To help debug issues related to Android resource compilation,
this CL introduces a new GN variable that can be set in your
args.gn, to force the build to put all such build directories
under a top-level directory, and not removing intermediate
files in it.

This should allow inspecting the intermediate files to better
understand what's going on.

This is needed to debug issues that appeared in my attempt
to change the resources stored in APKs versus bundles, e.g.:
https://chromium-review.googlesource.com/c/chromium/src/+/1270947

BUG=879228,882860,897056
R=agrieve@chromium.org, estevenson@chromium.org,yfriedman@chromium.org

Change-Id: I76c79a893b0bbdb56ba685f4304da929d11c7e05
Reviewed-on: https://chromium-review.googlesource.com/c/1288436Reviewed-by: default avataragrieve <agrieve@chromium.org>
Commit-Queue: David Turner <digit@chromium.org>
Cr-Commit-Position: refs/heads/master@{#601178}
parent b7327db4
...@@ -27,6 +27,11 @@ from xml.etree import ElementTree ...@@ -27,6 +27,11 @@ from xml.etree import ElementTree
from util import build_utils from util import build_utils
from util import resource_utils from util import resource_utils
# Name of environment variable that can be used to force this script to
# put temporary resource files into specific sub-directories, instead of
# temporary ones.
_ENV_DEBUG_VARIABLE = 'ANDROID_DEBUG_TEMP_RESOURCES_DIR'
# Import jinja2 from third_party/jinja2 # Import jinja2 from third_party/jinja2
sys.path.insert(1, os.path.join(build_utils.DIR_SOURCE_ROOT, 'third_party')) sys.path.insert(1, os.path.join(build_utils.DIR_SOURCE_ROOT, 'third_party'))
from jinja2 import Template # pylint: disable=F0401 from jinja2 import Template # pylint: disable=F0401
...@@ -581,8 +586,8 @@ def _WriteFinalRTxtFile(options, aapt_r_txt_path): ...@@ -581,8 +586,8 @@ def _WriteFinalRTxtFile(options, aapt_r_txt_path):
return r_txt_file return r_txt_file
def _OnStaleMd5(options): def _OnStaleMd5(options, debug_temp_resources_dir):
with resource_utils.BuildContext() as build: with resource_utils.BuildContext(debug_temp_resources_dir) as build:
dep_subdirs = resource_utils.ExtractDeps(options.dependencies_res_zips, dep_subdirs = resource_utils.ExtractDeps(options.dependencies_res_zips,
build.deps_dir) build.deps_dir)
...@@ -657,10 +662,17 @@ def main(args): ...@@ -657,10 +662,17 @@ def main(args):
input_strings.extend(_CreateLinkApkArgs(options)) input_strings.extend(_CreateLinkApkArgs(options))
debug_temp_resources_dir = os.environ.get(_ENV_DEBUG_VARIABLE)
if debug_temp_resources_dir:
build_utils.DeleteDirectory(debug_temp_resources_dir)
build_utils.MakeDirectory(debug_temp_resources_dir)
possible_input_paths = [ possible_input_paths = [
options.aapt_path, options.aapt_path,
options.aapt2_path, options.aapt2_path,
options.android_manifest, options.android_manifest,
debug_temp_resources_dir,
options.shared_resources_whitelist, options.shared_resources_whitelist,
] ]
possible_input_paths += options.include_resources possible_input_paths += options.include_resources
...@@ -672,7 +684,7 @@ def main(args): ...@@ -672,7 +684,7 @@ def main(args):
input_paths.append(options.webp_binary) input_paths.append(options.webp_binary)
build_utils.CallAndWriteDepfileIfStale( build_utils.CallAndWriteDepfileIfStale(
lambda: _OnStaleMd5(options), lambda: _OnStaleMd5(options, debug_temp_resources_dir),
options, options,
input_paths=input_paths, input_paths=input_paths,
input_strings=input_strings, input_strings=input_strings,
......
...@@ -407,11 +407,22 @@ def ExtractDeps(dep_zips, deps_dir): ...@@ -407,11 +407,22 @@ def ExtractDeps(dep_zips, deps_dir):
class _ResourceBuildContext(object): class _ResourceBuildContext(object):
"""A temporary directory for packaging and compiling Android resources.""" """A temporary directory for packaging and compiling Android resources.
def __init__(self):
Args:
temp_dir: Optional root build directory path. If None, a temporary
directory will be created, and removed in Close().
"""
def __init__(self, temp_dir=None):
"""Initialized the context.""" """Initialized the context."""
# The top-level temporary directory. # The top-level temporary directory.
self.temp_dir = tempfile.mkdtemp() if temp_dir:
self.temp_dir = temp_dir
self.remove_on_exit = False
else:
self.temp_dir = tempfile.mkdtemp()
self.remove_on_exit = True
# A location to store resources extracted form dependency zip files. # A location to store resources extracted form dependency zip files.
self.deps_dir = os.path.join(self.temp_dir, 'deps') self.deps_dir = os.path.join(self.temp_dir, 'deps')
os.mkdir(self.deps_dir) os.mkdir(self.deps_dir)
...@@ -426,14 +437,15 @@ class _ResourceBuildContext(object): ...@@ -426,14 +437,15 @@ class _ResourceBuildContext(object):
def Close(self): def Close(self):
"""Close the context and destroy all temporary files.""" """Close the context and destroy all temporary files."""
shutil.rmtree(self.temp_dir) if self.remove_on_exit:
shutil.rmtree(self.temp_dir)
@contextlib.contextmanager @contextlib.contextmanager
def BuildContext(): def BuildContext(temp_dir=None):
"""Generator for a _ResourceBuildContext instance.""" """Generator for a _ResourceBuildContext instance."""
try: try:
context = _ResourceBuildContext() context = _ResourceBuildContext(temp_dir)
yield context yield context
finally: finally:
context.Close() context.Close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment