summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xbuild/android/gyp/mirror_images.py278
-rwxr-xr-xbuild/android/gyp/process_resources.py50
-rwxr-xr-xbuild/install-build-deps-android.sh11
-rw-r--r--build/java.gypi22
4 files changed, 345 insertions, 16 deletions
diff --git a/build/android/gyp/mirror_images.py b/build/android/gyp/mirror_images.py
new file mode 100755
index 0000000..697fc85
--- /dev/null
+++ b/build/android/gyp/mirror_images.py
@@ -0,0 +1,278 @@
+#!/usr/bin/env python
+#
+# Copyright 2014 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.
+
+"""Mirrors (i.e. horizontally flips) images in the Android res folder for use
+in right-to-left (RTL) mode on Android.
+
+Only some images are mirrored, as determined by the config file, typically
+named mirror_images_config. The config file uses python syntax to define
+two lists of image names: images_to_mirror and images_not_to_mirror. Images in
+images_to_mirror will be mirrored by this tool. To ensure every image has been
+considered for mirroring, the remaining images must be listed in
+images_not_to_mirror.
+
+Mirrorable images include directional images (e.g. back and forward buttons) and
+asymmetric UI elements (e.g. a left-to-right fade image). Non-mirrorable images
+include images with text (e.g. the Chrome logo), symmetric images (e.g. a star
+or X button), and images whose asymmetry is not critical to their meaning. Most
+images do not need to be mirrored.
+
+Example mirror_images_config:
+
+ images_to_mirror = ['back.png', 'forward.png', 'left_to_right_fade.png']
+ images_not_to_mirror = ['star.png', 'chrome_logo.png', 'blank_page.png']
+
+Source images are taken from input_dir/res/drawable-* folders, and the
+generated images are saved into output_dir/res/drawable-ldrtl-* folders. For
+example: input_dir/res/drawable-hdpi/back.png would be mirrored into
+output_dir/res/drawable-ldrtl-hdpi/back.png.
+"""
+
+import errno
+import multiprocessing.pool
+import optparse
+import os
+import subprocess
+import sys
+
+from util import build_utils
+
+BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..')
+sys.path.append(BUILD_ANDROID_DIR)
+
+from pylib import constants
+
+
+class Image(object):
+ """Represents an image in the Android res directory."""
+
+ def __init__(self, drawable_dir, name):
+ # The image's directory, e.g. drawable-hdpi
+ self.drawable_dir = drawable_dir
+ # The image's filename, e.g. star.png
+ self.name = name
+
+
+class ConfigError(Exception):
+ """Error raised when images can't be mirrored because of problems with the
+ config file."""
+
+ def __init__(self, error_messages):
+ # A list of strings describing the errors in the config file.
+ self.error_messages = error_messages
+
+
+class Project(object):
+ """This class knows how to read the config file and mirror images in an
+ Android project."""
+
+ def __init__(self, config_file, input_res_dir, output_res_dir):
+ """Args:
+ config_file: The config file specifying which images will be mirrored.
+ input_res_dir: The directory containing source images to be mirrored.
+ output_res_dir: The directory into which mirrored images can be saved.
+ """
+ self.config_file = config_file
+ self.input_res_dir = input_res_dir
+ self.output_res_dir = output_res_dir
+
+ # List of names of images that will be mirrored, from config file.
+ self.images_to_mirror = None
+ # List of names of images that will not be mirrored, from config file.
+ self.images_not_to_mirror = None
+ # List of all images found in res/drawable* directories.
+ self.images = None
+
+ def mirror_images(self):
+ """Mirrors images in the project according to the configuration.
+
+ If the project configuration contains any errors, this will raise a
+ ConfigError exception containing a list of errors that must be addressed
+ manually before any images can be mirrored.
+ """
+ self._read_config_file()
+ self._read_drawable_dirs()
+ self._verify_config()
+ self._mirror_images()
+
+ def _read_config_file(self):
+ """Reads the lists of images that should and should not be mirrored from the
+ config file.
+ """
+ exec_env = {}
+ execfile(self.config_file, exec_env)
+ self.images_to_mirror = exec_env.get('images_to_mirror')
+ self.images_not_to_mirror = exec_env.get('images_not_to_mirror')
+ errors = []
+ self._verify_config_list_well_formed(self.images_to_mirror,
+ 'images_to_mirror', errors)
+ self._verify_config_list_well_formed(self.images_not_to_mirror,
+ 'images_not_to_mirror', errors)
+ if errors:
+ raise ConfigError(errors)
+
+ def _verify_config_list_well_formed(self, config_list, list_name, errors):
+ """Checks that config_list is a list of strings. If not, appends error
+ messages to errors."""
+ if type(config_list) != list:
+ errors.append('The config file must contain a list named ' + list_name)
+ else:
+ for item in config_list:
+ if not isinstance(item, basestring):
+ errors.append('List {0} contains a non-string item: {1}'
+ .format(list_name, item))
+
+ def _read_drawable_dirs(self):
+ """Gets the list of images in the input drawable directories."""
+ self.images = []
+
+ for dir_name in os.listdir(self.input_res_dir):
+ dir_components = dir_name.split('-')
+ if dir_components[0] != 'drawable' or 'ldrtl' in dir_components[1:]:
+ continue
+ dir_path = os.path.join(self.input_res_dir, dir_name)
+ if not os.path.isdir(dir_path):
+ continue
+
+ for image_name in os.listdir(dir_path):
+ if image_name.endswith('.png'):
+ self.images.append(Image(dir_name, image_name))
+
+ def _verify_config(self):
+ """Checks the config file for errors. Raises a ConfigError if errors are
+ found."""
+ errors = []
+
+ # Ensure images_to_mirror and images_not_to_mirror are sorted with no
+ # duplicates.
+ for l in self.images_to_mirror, self.images_not_to_mirror:
+ for i in range(len(l) - 1):
+ if l[i + 1] == l[i]:
+ errors.append(l[i + 1] + ' is listed multiple times')
+ elif l[i + 1] < l[i]:
+ errors.append(l[i + 1] + ' is not in sorted order')
+
+ # Ensure no overlap between images_to_mirror and images_not_to_mirror.
+ overlap = set(self.images_to_mirror).intersection(self.images_not_to_mirror)
+ for item in sorted(overlap):
+ errors.append(item + ' appears in both lists.')
+
+ # Ensure all images in res/drawable* folders are listed in config file.
+ images_in_config = set(self.images_to_mirror + self.images_not_to_mirror)
+ images_in_res_dir = [i.name for i in self.images]
+ images_missing_from_config = set(images_in_res_dir).difference(
+ images_in_config)
+ for image_name in sorted(images_missing_from_config):
+ errors.append(image_name + ' exists in a res/drawable* folder but is not '
+ 'listed in the config file. Update the config file to specify '
+ 'whether this image should be mirrored for right-to-left layouts (or '
+ 'delete the image).')
+
+ # Ensure only images in res/drawable* folders are listed in config file.
+ images_not_in_res_dir = set(images_in_config).difference(
+ images_in_res_dir)
+ for image_name in sorted(images_not_in_res_dir):
+ errors.append(image_name + ' is listed in the config file, but does not '
+ 'exist in any res/drawable* folders. Remove this image name from the '
+ 'config file (or add the image to a drawable folder).')
+
+ if errors:
+ raise ConfigError(errors)
+
+ def _mirror_image(self, image):
+ ltr_path = os.path.join(self.input_res_dir, image.drawable_dir, image.name)
+ rtl_path = os.path.join(self.output_res_dir,
+ get_rtl_dir(image.drawable_dir), image.name)
+ build_utils.MakeDirectory(os.path.dirname(rtl_path))
+ mirror_image(ltr_path, rtl_path)
+
+ def _mirror_images(self):
+ pool = multiprocessing.pool.ThreadPool()
+ images_to_mirror = [i for i in self.images if
+ i.name in self.images_to_mirror]
+ pool.map(self._mirror_image, images_to_mirror)
+
+
+def get_rtl_dir(drawable_dir_ltr):
+ """Returns the RTL drawable directory corresponding to drawable_dir_ltr.
+
+ Example:
+ drawable-hdpi -> drawable-ldrtl-hdpi
+ """
+ dir_components = drawable_dir_ltr.split('-')
+ assert 'ldrtl' not in dir_components
+ # ldrtl is always the first qualifier, as long as mobile country code or
+ # language and region aren't used as qualifiers:
+ # http://developer.android.com/guide/topics/resources/providing-resources.html
+ dir_components.insert(1, 'ldrtl')
+ return '-'.join(dir_components)
+
+
+def mirror_image(src_path, dst_path):
+ """Mirrors a single image horizontally. 9-patches are treated specially: the
+ left and right edges of a 9-patch are not mirrored.
+
+ Args:
+ src_path: The image to be mirrored.
+ dst_path: The path where the mirrored image will be saved.
+ """
+ try:
+ if src_path.endswith('.9.png'):
+ build_utils.CheckOutput(['convert', src_path,
+ '(', '+clone', '-flop', '-shave', '1x0', ')',
+ '-compose', 'Copy',
+ '-geometry', '+1+0',
+ '-composite', dst_path])
+ else:
+ build_utils.CheckOutput(['convert', '-flop', src_path, dst_path])
+ except OSError as e:
+ if e.errno == errno.ENOENT:
+ raise Exception('Executable "convert" (from the imagemagick package) not '
+ 'found. Run build/install-build-deps-android.sh and ensure '
+ 'that "convert" is on your path.')
+ raise
+
+
+def parse_args(args=None):
+ parser = optparse.OptionParser()
+ parser.add_option('--config-file', help='Configuration file specifying which '
+ 'images should be mirrored')
+ parser.add_option('--input-res-dir', help='The res folder containing the '
+ 'source images.')
+ parser.add_option('--output-res-dir', help='The res folder into which '
+ 'mirrored images will be saved.')
+
+ if args is None:
+ args = sys.argv[1:]
+ options, args = parser.parse_args(args)
+
+ # Check that required options have been provided.
+ required_options = ('config_file', 'input_res_dir', 'output_res_dir')
+ build_utils.CheckOptions(options, parser, required=required_options)
+
+ return options
+
+
+def main(args=None):
+ options = parse_args(args)
+ project = Project(options.config_file, options.input_res_dir,
+ options.output_res_dir)
+ try:
+ project.mirror_images()
+ except ConfigError as e:
+ sys.stderr.write('\nFailed to mirror images for RTL layouts.\n')
+ sys.stderr.write('{0} error(s) in config file {1}:\n'.format(
+ len(e.error_messages),
+ os.path.relpath(options.config_file, constants.DIR_SOURCE_ROOT)))
+ for error_message in e.error_messages:
+ sys.stderr.write(' - {0}\n'.format(error_message))
+ sys.stderr.write('For more information on mirroring images, read the '
+ 'header comment in build/android/gyp/mirror_images.py.\n')
+ sys.exit(1)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/build/android/gyp/process_resources.py b/build/android/gyp/process_resources.py
index bf13175..48e4b49 100755
--- a/build/android/gyp/process_resources.py
+++ b/build/android/gyp/process_resources.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
-# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Copyright 2012 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.
@@ -10,8 +10,10 @@ import optparse
import os
import shlex
import shutil
+import tempfile
from util import build_utils
+import mirror_images
def ParseArgs():
"""Parses command line options.
@@ -26,10 +28,15 @@ def ParseArgs():
parser.add_option('--R-dir', help='directory to hold generated R.java')
parser.add_option('--res-dirs',
help='directories containing resources to be packaged')
- parser.add_option('--crunch-input-dir',
- help='directory containing images to be crunched')
+ parser.add_option('--image-input-dir', help='directory containing images to '
+ 'be crunched and possibly mirrored.')
parser.add_option('--crunch-output-dir',
help='directory to hold crunched resources')
+ parser.add_option('--mirror-config', help='config file specifying which '
+ 'images should be mirrored. Images will be mirrored only '
+ 'if this is provided.')
+ parser.add_option('--mirror-output-dir',
+ help='directory where mirrored images should be saved')
parser.add_option('--non-constant-id', action='store_true')
parser.add_option('--custom-package', help='Java package for R.java')
parser.add_option('--android-manifest', help='AndroidManifest.xml path')
@@ -43,9 +50,13 @@ def ParseArgs():
if args:
parser.error('No positional arguments should be given.')
+ if (options.mirror_config is None) != (options.mirror_output_dir is None):
+ parser.error('--mirror-config and --mirror-output-dir must both be present '
+ 'or neither present.')
+
# Check that required options have been provided.
required_options = ('android_sdk', 'android_sdk_tools', 'R_dir',
- 'res_dirs', 'crunch_input_dir', 'crunch_output_dir')
+ 'res_dirs', 'image_input_dir', 'crunch_output_dir')
build_utils.CheckOptions(options, parser, required=required_options)
return options
@@ -77,6 +88,15 @@ def MoveImagesToNonMdpiFolders(res_root):
shutil.move(src_file, dst_file)
+def CrunchImages(aapt, input_res_dir, output_res_dir):
+ build_utils.MakeDirectory(output_res_dir)
+ aapt_cmd = [aapt,
+ 'crunch',
+ '-S', input_res_dir,
+ '-C', output_res_dir]
+ build_utils.CheckOutput(aapt_cmd, fail_if_stderr=True)
+
+
def main():
options = ParseArgs()
android_jar = os.path.join(options.android_sdk, 'android.jar')
@@ -106,16 +126,22 @@ def main():
package_command += ['--custom-package', options.custom_package]
build_utils.CheckOutput(package_command)
+ # Mirror images if requested.
+ if options.mirror_config:
+ # Mirrored images are generated into a temp dir. Once these images are
+ # crunched, they'll go in the desired mirror output dir.
+ mirrored_uncrunched_dir = tempfile.mkdtemp()
+ try:
+ mirror_images.main(['--config', options.mirror_config,
+ '--input-res-dir', options.image_input_dir,
+ '--output-res-dir', mirrored_uncrunched_dir])
+ CrunchImages(aapt, mirrored_uncrunched_dir, options.mirror_output_dir)
+ finally:
+ shutil.rmtree(mirrored_uncrunched_dir)
+
# Crunch image resources. This shrinks png files and is necessary for 9-patch
# images to display correctly.
- build_utils.MakeDirectory(options.crunch_output_dir)
- aapt_cmd = [aapt,
- 'crunch',
- '-S', options.crunch_input_dir,
- '-C', options.crunch_output_dir]
- build_utils.CheckOutput(aapt_cmd, fail_if_stderr=True)
-
- MoveImagesToNonMdpiFolders(options.crunch_output_dir)
+ CrunchImages(aapt, options.image_input_dir, options.crunch_output_dir)
if options.stamp:
build_utils.Touch(options.stamp)
diff --git a/build/install-build-deps-android.sh b/build/install-build-deps-android.sh
index 73e534a..6535b62 100755
--- a/build/install-build-deps-android.sh
+++ b/build/install-build-deps-android.sh
@@ -43,8 +43,15 @@ sudo apt-get -f install
# on since the package names are different, and Sun's Java must
# be installed manually on late-model versions.
-# common
-sudo apt-get -y install checkstyle lighttpd python-pexpect xvfb x11-utils
+# Common to all Ubuntu versions:
+# - checkstyle: Used to check Java coding style during presubmit.
+# - imagemagick: Used to mirror image assets for RTL at build time.
+# - lighttpd
+# - python-pexpect
+# - xvfb
+# - x11-utils
+sudo apt-get -y install checkstyle imagemagick lighttpd python-pexpect xvfb \
+ x11-utils
# Few binaries in the Android SDK require 32-bit libraries on the host.
sudo apt-get -y install lib32z1 g++-multilib
diff --git a/build/java.gypi b/build/java.gypi
index b3daebb..a87cace 100644
--- a/build/java.gypi
+++ b/build/java.gypi
@@ -71,6 +71,8 @@
'res_extra_files': [],
'res_v14_verify_only%': 0,
'resource_input_paths': ['>@(res_extra_files)'],
+ 'mirror_images%': 0,
+ 'mirror_images_args': [],
'intermediate_dir': '<(SHARED_INTERMEDIATE_DIR)/<(_target_name)',
'classes_dir': '<(intermediate_dir)/classes',
'compile_stamp': '<(intermediate_dir)/compile.stamp',
@@ -171,9 +173,24 @@
},
],
}],
+ ['mirror_images == 1', {
+ 'variables': {
+ 'res_mirrored_dir': '<(intermediate_dir)/res_mirrored',
+ 'mirror_images_config': '<(java_in_dir)/mirror_images_config',
+ 'mirror_images_args': ['--mirror-config', '<(mirror_images_config)',
+ '--mirror-output-dir', '<(res_mirrored_dir)'],
+ 'resource_input_paths': ['<(mirror_images_config)',
+ '<(DEPTH)/build/android/gyp/mirror_images.py'],
+ },
+ 'all_dependent_settings': {
+ 'variables': {
+ 'additional_res_dirs': ['<(res_mirrored_dir)'],
+ },
+ },
+ }],
],
'actions': [
- # Generate R.java and crunch image resources.
+ # Generate R.java; mirror and crunch image resources.
{
'action_name': 'process_resources',
'message': 'processing resources for <(_target_name)',
@@ -203,12 +220,13 @@
'--android-sdk-tools', '<(android_sdk_tools)',
'--R-dir', '<(R_dir)',
'--res-dirs', '>(all_res_dirs)',
- '--crunch-input-dir', '>(res_dir)',
+ '--image-input-dir', '>(res_dir)',
'--crunch-output-dir', '<(res_crunched_dir)',
'--android-manifest', '<(android_manifest)',
'--non-constant-id',
'--custom-package', '<(R_package)',
'--stamp', '<(R_stamp)',
+ '<@(mirror_images_args)',
# Add hash of inputs to the command line, so if inputs change
# (e.g. if a resource if removed), the command will be re-run.