2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Helper script to repack paks for a list of locales.
8 Gyp doesn't have any built-in looping capability, so this just provides a way to
9 loop over a list of locales when repacking pak files, thus avoiding a
10 proliferation of mostly duplicate, cut-n-paste gyp actions.
17 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..',
19 from grit.format import data_pack
21 # The gyp "branding" variable.
24 # Some build paths defined by gyp.
29 # The target platform. If it is not defined, sys.platform will be used.
37 EXTRA_INPUT_FILES = []
39 class Usage(Exception):
40 def __init__(self, msg):
44 def calc_output(locale):
45 """Determine the file that will be generated for the given locale."""
46 #e.g. '<(INTERMEDIATE_DIR)/repack/da.pak',
47 # For Fake Bidi, generate it at a fixed path so that tests can safely
49 if locale == 'fake-bidi':
50 return '%s/%s.pak' % (INT_DIR, locale)
51 if OS == 'mac' or OS == 'ios':
52 # For Cocoa to find the locale at runtime, it needs to use '_' instead
53 # of '-' (http://crbug.com/20441). Also, 'en-US' should be represented
54 # simply as 'en' (http://crbug.com/19165, http://crbug.com/25578).
57 return '%s/repack/%s.lproj/locale.pak' % (INT_DIR, locale.replace('-', '_'))
59 return os.path.join(INT_DIR, 'repack', locale + '.pak')
62 def calc_inputs(locale):
63 """Determine the files that need processing for the given locale."""
66 #e.g. '<(grit_out_dir)/generated_resources_da.pak'
67 inputs.append(os.path.join(GRIT_DIR, 'generated_resources_%s.pak' % locale))
69 #e.g. '<(grit_out_dir)/locale_settings_da.pak'
70 inputs.append(os.path.join(GRIT_DIR, 'locale_settings_%s.pak' % locale))
72 #e.g. '<(grit_out_dir)/platform_locale_settings_da.pak'
73 inputs.append(os.path.join(GRIT_DIR,
74 'platform_locale_settings_%s.pak' % locale))
76 #e.g. '<(SHARED_INTERMEDIATE_DIR)/components/strings/
77 # component_strings_da.pak',
78 inputs.append(os.path.join(SHARE_INT_DIR, 'components', 'strings',
79 'component_strings_%s.pak' % locale))
82 #e.g. '<(SHARED_INTERMEDIATE_DIR)/ash_strings/ash_strings_da.pak',
83 inputs.append(os.path.join(SHARE_INT_DIR, 'ash_strings',
84 'ash_strings_%s.pak' % locale))
87 #e.g. '<(SHARED_INTERMEDIATE_DIR)/webkit/webkit_strings_da.pak'
88 inputs.append(os.path.join(SHARE_INT_DIR, 'webkit',
89 'webkit_strings_%s.pak' % locale))
91 #e.g. '<(SHARED_INTERMEDIATE_DIR)/ui/ui_strings_da.pak',
92 inputs.append(os.path.join(SHARE_INT_DIR, 'ui', 'ui_strings',
93 'ui_strings_%s.pak' % locale))
95 #e.g. '<(SHARED_INTERMEDIATE_DIR)/device/bluetooth/strings/
96 # device_bluetooth_strings_da.pak',
97 inputs.append(os.path.join(SHARE_INT_DIR, 'device', 'bluetooth', 'strings',
98 'device_bluetooth_strings_%s.pak' % locale))
100 #e.g. '<(SHARED_INTERMEDIATE_DIR)/ui/app_locale_settings_da.pak',
101 inputs.append(os.path.join(SHARE_INT_DIR, 'ui', 'app_locale_settings',
102 'app_locale_settings_%s.pak' % locale))
105 # '<(SHARED_INTERMEDIATE_DIR)/extensions/strings/extensions_strings_da.pak
106 # TODO(jamescook): When Android stops building extensions code move this
107 # to the OS != 'ios' and OS != 'android' section below.
108 inputs.append(os.path.join(SHARE_INT_DIR, 'extensions', 'strings',
109 'extensions_strings_%s.pak' % locale))
111 if OS != 'ios' and OS != 'android':
112 #e.g. '<(SHARED_INTERMEDIATE_DIR)/third_party/libaddressinput/
113 # libaddressinput_strings_da.pak',
114 inputs.append(os.path.join(SHARE_INT_DIR, 'third_party', 'libaddressinput',
115 'libaddressinput_strings_%s.pak' % locale))
117 #e.g. '<(grit_out_dir)/google_chrome_strings_da.pak'
119 # '<(grit_out_dir)/chromium_strings_da.pak'
120 inputs.append(os.path.join(
121 GRIT_DIR, '%s_strings_%s.pak' % (BRANDING, locale)))
123 # Add any extra input files.
124 for extra_file in EXTRA_INPUT_FILES:
125 inputs.append('%s_%s.pak' % (extra_file, locale))
130 def list_outputs(locales):
131 """Returns the names of files that will be generated for the given locales.
133 This is to provide gyp the list of output files, so build targets can
134 properly track what needs to be built.
137 for locale in locales:
138 outputs.append(calc_output(locale))
139 # Quote each element so filename spaces don't mess up gyp's attempt to parse
141 return " ".join(['"%s"' % x for x in outputs])
144 def list_inputs(locales):
145 """Returns the names of files that will be processed for the given locales.
147 This is to provide gyp the list of input files, so build targets can properly
148 track their prerequisites.
151 for locale in locales:
152 inputs += calc_inputs(locale)
153 # Quote each element so filename spaces don't mess up gyp's attempt to parse
155 return " ".join(['"%s"' % x for x in inputs])
158 def repack_locales(locales):
159 """ Loop over and repack the given locales."""
160 for locale in locales:
162 inputs += calc_inputs(locale)
163 output = calc_output(locale)
164 data_pack.DataPack.RePack(output, inputs, whitelist_file=WHITELIST)
175 global EXTRA_INPUT_FILES
177 parser = optparse.OptionParser("usage: %prog [options] locales")
178 parser.add_option("-i", action="store_true", dest="inputs", default=False,
179 help="Print the expected input file list, then exit.")
180 parser.add_option("-o", action="store_true", dest="outputs", default=False,
181 help="Print the expected output file list, then exit.")
182 parser.add_option("-g", action="store", dest="grit_dir",
183 help="GRIT build files output directory.")
184 parser.add_option("-x", action="store", dest="int_dir",
185 help="Intermediate build files output directory.")
186 parser.add_option("-s", action="store", dest="share_int_dir",
187 help="Shared intermediate build files output directory.")
188 parser.add_option("-b", action="store", dest="branding",
189 help="Branding type of this build.")
190 parser.add_option("-e", action="append", dest="extra_input", default=[],
191 help="Full path to an extra input pak file without the\
192 locale suffix and \".pak\" extension.")
193 parser.add_option("-p", action="store", dest="os",
194 help="The target OS. (e.g. mac, linux, win, etc.)")
195 parser.add_option("--use-ash", action="store", dest="use_ash",
196 help="Whether to include ash strings")
197 parser.add_option("--whitelist", action="store", help="Full path to the "
198 "whitelist used to filter output pak file resource IDs")
199 options, locales = parser.parse_args(argv)
202 parser.error('Please specificy at least one locale to process.\n')
204 print_inputs = options.inputs
205 print_outputs = options.outputs
206 GRIT_DIR = options.grit_dir
207 INT_DIR = options.int_dir
208 SHARE_INT_DIR = options.share_int_dir
209 BRANDING = options.branding
210 EXTRA_INPUT_FILES = options.extra_input
212 USE_ASH = options.use_ash == '1'
213 WHITELIST = options.whitelist
216 if sys.platform == 'darwin':
218 elif sys.platform.startswith('linux'):
220 elif sys.platform in ('cygwin', 'win32'):
225 if not (GRIT_DIR and INT_DIR and SHARE_INT_DIR):
226 parser.error('Please specify all of "-g" and "-x" and "-s".\n')
227 if print_inputs and print_outputs:
228 parser.error('Please specify only one of "-i" or "-o".\n')
229 # Need to know the branding, unless we're just listing the outputs.
230 if not print_outputs and not BRANDING:
231 parser.error('Please specify "-b" to determine the input files.\n')
234 return list_inputs(locales)
237 return list_outputs(locales)
239 return repack_locales(locales)
241 if __name__ == '__main__':
242 results = DoMain(sys.argv[1:])