summaryrefslogtreecommitdiffstats
path: root/build
diff options
context:
space:
mode:
authoragrieve <agrieve@chromium.org>2015-06-19 14:20:03 -0700
committerCommit bot <commit-bot@chromium.org>2015-06-19 21:21:24 +0000
commitf30e04470453ccd7e6a9abf586185b991d174902 (patch)
tree6294dd378c05275f7f142c8665d3f83b455c4e84 /build
parente33731cd7108536b56e18e722af5c9d425c47d55 (diff)
downloadchromium_src-f30e04470453ccd7e6a9abf586185b991d174902.zip
chromium_src-f30e04470453ccd7e6a9abf586185b991d174902.tar.gz
chromium_src-f30e04470453ccd7e6a9abf586185b991d174902.tar.bz2
Android: Store language .pak files in res/raw rather than assets
This is a prerequisite for having them live within APK splits. Although pak files need to be renamed to be stored under res/raw, ResourceExtractor restores their original name during extraction (so no change to native code). All sub-locales are stored and extracted together, just as before. BUG=371610 Review URL: https://codereview.chromium.org/1158053005 Cr-Commit-Position: refs/heads/master@{#335350}
Diffstat (limited to 'build')
-rwxr-xr-xbuild/android/gyp/locale_pak_resources.py97
-rw-r--r--build/android/locale_pak_resources.gypi52
-rw-r--r--build/config/android/rules.gni57
3 files changed, 206 insertions, 0 deletions
diff --git a/build/android/gyp/locale_pak_resources.py b/build/android/gyp/locale_pak_resources.py
new file mode 100755
index 0000000..8bf9218
--- /dev/null
+++ b/build/android/gyp/locale_pak_resources.py
@@ -0,0 +1,97 @@
+#!/usr/bin/env python
+#
+# Copyright 2015 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.
+"""Creates a resources.zip for locale .pak files.
+
+Places the locale.pak files into appropriate resource configs
+(e.g. en-GB.pak -> res/raw-en/en_gb.pak). Also generates a locale_paks
+TypedArray so that resource files can be enumerated at runtime.
+"""
+
+import collections
+import optparse
+import os
+import sys
+import zipfile
+
+from util import build_utils
+
+
+# This should stay in sync with:
+# base/android/java/src/org/chromium/base/LocaleUtils.java
+_CHROME_TO_ANDROID_LOCALE_MAP = {
+ 'he': 'iw',
+ 'id': 'in',
+ 'fil': 'tl',
+}
+
+
+def CreateLocalePaksXml(names):
+ """Creates the contents for the locale-paks.xml files."""
+ VALUES_FILE_TEMPLATE = '''<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <array name="locale_paks">%s
+ </array>
+</resources>
+'''
+ VALUES_ITEM_TEMPLATE = '''
+ <item>@raw/%s</item>'''
+
+ res_names = (os.path.splitext(name)[0] for name in names)
+ items = ''.join((VALUES_ITEM_TEMPLATE % name for name in res_names))
+ return VALUES_FILE_TEMPLATE % items
+
+
+def main():
+ parser = optparse.OptionParser()
+ build_utils.AddDepfileOption(parser)
+ parser.add_option('--locale-paks', help='List of files for res/raw-LOCALE')
+ parser.add_option('--resources-zip', help='Path to output resources.zip')
+
+ options, _ = parser.parse_args()
+ build_utils.CheckOptions(options, parser,
+ required=['locale_paks', 'resources_zip'])
+
+ sources = build_utils.ParseGypList(options.locale_paks)
+
+ if options.depfile:
+ deps = sources + build_utils.GetPythonDependencies()
+ build_utils.WriteDepfile(options.depfile, deps)
+
+ with zipfile.ZipFile(options.resources_zip, 'w', zipfile.ZIP_STORED) as out:
+ # e.g. "en" -> ["en_gb.pak"]
+ lang_to_locale_map = collections.defaultdict(list)
+ for src_path in sources:
+ basename = os.path.basename(src_path)
+ name = os.path.splitext(basename)[0]
+ # Resources file names must consist of [a-z0-9_.].
+ res_compatible_name = basename.replace('-', '_').lower()
+ if name == 'en-US':
+ dest_dir = 'raw'
+ else:
+ # Chrome uses different region mapping logic from Android, so include
+ # all regions for each language.
+ android_locale = _CHROME_TO_ANDROID_LOCALE_MAP.get(name, name)
+ lang = android_locale[0:2]
+ dest_dir = 'raw-' + lang
+ lang_to_locale_map[lang].append(res_compatible_name)
+ out.write(src_path, os.path.join(dest_dir, res_compatible_name))
+
+ # Create a String Arrays resource so ResourceExtractor can enumerate files.
+ def WriteValuesFile(lang, names):
+ dest_dir = 'values'
+ if lang:
+ dest_dir += '-' + lang
+ # Always extract en-US.pak since it's the fallback.
+ xml = CreateLocalePaksXml(names + ['en_us.pak'])
+ out.writestr(os.path.join(dest_dir, 'locale-paks.xml'), xml)
+
+ for lang, names in lang_to_locale_map.iteritems():
+ WriteValuesFile(lang, names)
+ WriteValuesFile(None, [])
+
+
+if __name__ == '__main__':
+ sys.exit(main())
diff --git a/build/android/locale_pak_resources.gypi b/build/android/locale_pak_resources.gypi
new file mode 100644
index 0000000..6f8e56f8
--- /dev/null
+++ b/build/android/locale_pak_resources.gypi
@@ -0,0 +1,52 @@
+# Copyright 2015 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.
+
+# Creates a resources.zip with locale.pak files placed into appropriate
+# resource configs (e.g. en-GB.pak -> res/raw-en/en_gb.pak). Also generates
+# a locale_paks TypedArray so that resource files can be enumerated at runtime.
+#
+# If this target is included in the deps of an android resources/library/apk,
+# the resources will be included with that target.
+#
+# Variables:
+# locale_pak_files - List of .pak files to process.
+# Names must be of the form "en.pak" or "en-US.pak".
+#
+# Example
+# {
+# 'target_name': 'my_locale_resources',
+# 'type': 'none',
+# 'variables': {
+# 'locale_paks_files': ['path1/fr.pak'],
+# },
+# 'includes': [ '../build/android/locale_pak_resources.gypi' ],
+# },
+#
+{
+ 'variables': {
+ 'resources_zip_path': '<(PRODUCT_DIR)/res.java/<(_target_name).zip',
+ },
+ 'all_dependent_settings': {
+ 'variables': {
+ 'additional_input_paths': ['<(resources_zip_path)'],
+ 'dependencies_res_zip_paths': ['<(resources_zip_path)'],
+ },
+ },
+ 'actions': [{
+ 'action_name': '<(_target_name)_locale_pak_resources',
+ 'inputs': [
+ '<(DEPTH)/build/android/gyp/util/build_utils.py',
+ '<(DEPTH)/build/android/gyp/locale_pak_resources.py',
+ '<@(locale_pak_files)',
+ ],
+ 'outputs': [
+ '<(resources_zip_path)',
+ ],
+ 'action': [
+ 'python', '<(DEPTH)/build/android/gyp/locale_pak_resources.py',
+ '--locale-paks', '<(locale_pak_files)',
+ '--resources-zip', '<(resources_zip_path)',
+ ],
+ }],
+}
diff --git a/build/config/android/rules.gni b/build/config/android/rules.gni
index 4c22fac..96cbe44 100644
--- a/build/config/android/rules.gni
+++ b/build/config/android/rules.gni
@@ -544,6 +544,63 @@ template("jinja_template_resources") {
}
}
+# Creates a resources.zip with locale.pak files placed into appropriate
+# resource configs (e.g. en-GB.pak -> res/raw-en/en_gb.pak). Also generates
+# a locale_paks TypedArray so that resource files can be enumerated at runtime.
+#
+# If this target is included in the deps of an android resources/library/apk,
+# the resources will be included with that target.
+#
+# Variables:
+# sources: List of .pak files. Names must be of the form "en.pak" or
+# "en-US.pak".
+#
+# Example
+# locale_pak_resources("locale_paks") {
+# sources = [ "path/en-US.pak", "path/fr.pak", ... ]
+# }
+template("locale_pak_resources") {
+ set_sources_assignment_filter([])
+ assert(defined(invoker.sources))
+
+ _base_path = "$target_gen_dir/$target_name"
+ _resources_zip = _base_path + ".resources.zip"
+ _build_config = _base_path + ".build_config"
+
+ write_build_config("${target_name}__build_config") {
+ build_config = _build_config
+ resources_zip = _resources_zip
+ type = "android_resources"
+ }
+
+ action("${target_name}__create_resources_zip") {
+ sources = invoker.sources
+ script = "//build/android/gyp/locale_pak_resources.py"
+ depfile = "$target_gen_dir/$target_name.d"
+
+ outputs = [
+ depfile,
+ _resources_zip,
+ ]
+
+ _rebased_sources = rebase_path(invoker.sources, root_build_dir)
+ args = [
+ "--locale-paks=${_rebased_sources}",
+ "--resources-zip",
+ rebase_path(_resources_zip, root_build_dir),
+ "--depfile",
+ rebase_path(depfile, root_build_dir),
+ ]
+ }
+
+ group(target_name) {
+ deps = [
+ ":${target_name}__build_config",
+ ":${target_name}__create_resources_zip",
+ ]
+ }
+}
+
# Declare an Android resources target
#
# This creates a resources zip file that will be used when building an Android