Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / tools / build / repack_locales.py
blob3ed2c847dbc2f8adc039715d00601a898e86b8d2
1 #!/usr/bin/env python
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.
11 """
13 import optparse
14 import os
15 import sys
17 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..',
18 'tools', 'grit'))
19 from grit.format import data_pack
21 # The gyp "branding" variable.
22 BRANDING = None
24 # Some build paths defined by gyp.
25 GRIT_DIR = None
26 SHARE_INT_DIR = None
27 INT_DIR = None
29 # The target platform. If it is not defined, sys.platform will be used.
30 OS = None
32 # Note that OS is normally set to 'linux' when building for chromeos.
33 CHROMEOS = False
35 USE_ASH = False
36 ENABLE_AUTOFILL_DIALOG = False
37 ENABLE_EXTENSIONS = False
39 WHITELIST = None
41 # Extra input files.
42 EXTRA_INPUT_FILES = []
44 class Usage(Exception):
45 def __init__(self, msg):
46 self.msg = msg
49 def calc_output(locale):
50 """Determine the file that will be generated for the given locale."""
51 #e.g. '<(INTERMEDIATE_DIR)/repack/da.pak',
52 # For Fake Bidi, generate it at a fixed path so that tests can safely
53 # reference it.
54 if locale == 'fake-bidi':
55 return '%s/%s.pak' % (INT_DIR, locale)
56 if OS == 'mac' or OS == 'ios':
57 # For Cocoa to find the locale at runtime, it needs to use '_' instead
58 # of '-' (http://crbug.com/20441). Also, 'en-US' should be represented
59 # simply as 'en' (http://crbug.com/19165, http://crbug.com/25578).
60 if locale == 'en-US':
61 locale = 'en'
62 return '%s/repack/%s.lproj/locale.pak' % (INT_DIR, locale.replace('-', '_'))
63 else:
64 return os.path.join(INT_DIR, 'repack', locale + '.pak')
67 def calc_inputs(locale):
68 """Determine the files that need processing for the given locale."""
69 inputs = []
71 #e.g. '<(grit_out_dir)/generated_resources_da.pak'
72 inputs.append(os.path.join(GRIT_DIR, 'generated_resources_%s.pak' % locale))
74 #e.g. '<(grit_out_dir)/locale_settings_da.pak'
75 inputs.append(os.path.join(GRIT_DIR, 'locale_settings_%s.pak' % locale))
77 #e.g. '<(grit_out_dir)/platform_locale_settings_da.pak'
78 inputs.append(os.path.join(GRIT_DIR,
79 'platform_locale_settings_%s.pak' % locale))
81 #e.g. '<(SHARED_INTERMEDIATE_DIR)/components/strings/
82 # components_strings_da.pak',
83 inputs.append(os.path.join(SHARE_INT_DIR, 'components', 'strings',
84 'components_strings_%s.pak' % locale))
86 #e.g. '<(SHARED_INTERMEDIATE_DIR)/components/strings/
87 # components_chromium_strings_da.pak'
88 # or
89 # '<(SHARED_INTERMEDIATE_DIR)/components/strings/
90 # components_google_chrome_strings_da.pak',
91 inputs.append(os.path.join(SHARE_INT_DIR, 'components', 'strings',
92 'components_%s_strings_%s.pak' % (BRANDING, locale)))
94 if USE_ASH:
95 #e.g. '<(SHARED_INTERMEDIATE_DIR)/ash/strings/ash_strings_da.pak',
96 inputs.append(os.path.join(SHARE_INT_DIR, 'ash', 'strings',
97 'ash_strings_%s.pak' % locale))
99 if CHROMEOS:
100 inputs.append(os.path.join(SHARE_INT_DIR, 'ui', 'chromeos', 'strings',
101 'ui_chromeos_strings_%s.pak' % locale))
102 inputs.append(os.path.join(SHARE_INT_DIR, 'remoting', 'resources',
103 '%s.pak' % locale))
105 if OS != 'ios':
106 #e.g.
107 # '<(SHARED_INTERMEDIATE_DIR)/content/app/strings/content_strings_da.pak'
108 inputs.append(os.path.join(SHARE_INT_DIR, 'content', 'app', 'strings',
109 'content_strings_%s.pak' % locale))
111 #e.g. '<(SHARED_INTERMEDIATE_DIR)/device/bluetooth/strings/
112 # bluetooth_strings_da.pak',
113 inputs.append(os.path.join(SHARE_INT_DIR, 'device', 'bluetooth', 'strings',
114 'bluetooth_strings_%s.pak' % locale))
116 #e.g. '<(SHARED_INTERMEDIATE_DIR)/ui/strings/ui_strings_da.pak',
117 inputs.append(os.path.join(SHARE_INT_DIR, 'ui', 'strings',
118 'ui_strings_%s.pak' % locale))
120 #e.g. '<(SHARED_INTERMEDIATE_DIR)/ui/strings/app_locale_settings_da.pak',
121 inputs.append(os.path.join(SHARE_INT_DIR, 'ui', 'strings',
122 'app_locale_settings_%s.pak' % locale))
124 else:
125 #e.g. '<(SHARED_INTERMEDIATE_DIR)/ios/chrome/ios_strings_resources_da.pak'
126 inputs.append(os.path.join(SHARE_INT_DIR, 'ios', 'chrome',
127 'ios_strings_resources_%s.pak' % locale))
129 if ENABLE_AUTOFILL_DIALOG:
130 #e.g. '<(SHARED_INTERMEDIATE_DIR)/third_party/libaddressinput/
131 # address_input_strings_da.pak',
132 inputs.append(os.path.join(SHARE_INT_DIR, 'third_party', 'libaddressinput',
133 'address_input_strings_%s.pak' % locale))
135 if ENABLE_EXTENSIONS:
136 # For example:
137 # '<(SHARED_INTERMEDIATE_DIR)/extensions/strings/extensions_strings_da.pak
138 # TODO(jamescook): When Android stops building extensions code move this
139 # to the OS != 'ios' and OS != 'android' section below.
140 inputs.append(os.path.join(SHARE_INT_DIR, 'extensions', 'strings',
141 'extensions_strings_%s.pak' % locale))
143 #e.g. '<(grit_out_dir)/google_chrome_strings_da.pak'
144 # or
145 # '<(grit_out_dir)/chromium_strings_da.pak'
146 inputs.append(os.path.join(
147 GRIT_DIR, '%s_strings_%s.pak' % (BRANDING, locale)))
149 # Add any extra input files.
150 for extra_file in EXTRA_INPUT_FILES:
151 inputs.append('%s_%s.pak' % (extra_file, locale))
153 return inputs
156 def list_outputs(locales):
157 """Returns the names of files that will be generated for the given locales.
159 This is to provide gyp the list of output files, so build targets can
160 properly track what needs to be built.
162 outputs = []
163 for locale in locales:
164 outputs.append(calc_output(locale))
165 # Quote each element so filename spaces don't mess up gyp's attempt to parse
166 # it into a list.
167 return " ".join(['"%s"' % x for x in outputs])
170 def list_inputs(locales):
171 """Returns the names of files that will be processed for the given locales.
173 This is to provide gyp the list of input files, so build targets can properly
174 track their prerequisites.
176 inputs = []
177 for locale in locales:
178 inputs += calc_inputs(locale)
179 # Quote each element so filename spaces don't mess up gyp's attempt to parse
180 # it into a list.
181 return " ".join(['"%s"' % x for x in inputs])
184 def repack_locales(locales):
185 """ Loop over and repack the given locales."""
186 for locale in locales:
187 inputs = []
188 inputs += calc_inputs(locale)
189 output = calc_output(locale)
190 data_pack.DataPack.RePack(output, inputs, whitelist_file=WHITELIST)
193 def DoMain(argv):
194 global BRANDING
195 global GRIT_DIR
196 global SHARE_INT_DIR
197 global INT_DIR
198 global OS
199 global CHROMEOS
200 global USE_ASH
201 global WHITELIST
202 global ENABLE_AUTOFILL_DIALOG
203 global ENABLE_EXTENSIONS
204 global EXTRA_INPUT_FILES
206 parser = optparse.OptionParser("usage: %prog [options] locales")
207 parser.add_option("-i", action="store_true", dest="inputs", default=False,
208 help="Print the expected input file list, then exit.")
209 parser.add_option("-o", action="store_true", dest="outputs", default=False,
210 help="Print the expected output file list, then exit.")
211 parser.add_option("-g", action="store", dest="grit_dir",
212 help="GRIT build files output directory.")
213 parser.add_option("-x", action="store", dest="int_dir",
214 help="Intermediate build files output directory.")
215 parser.add_option("-s", action="store", dest="share_int_dir",
216 help="Shared intermediate build files output directory.")
217 parser.add_option("-b", action="store", dest="branding",
218 help="Branding type of this build.")
219 parser.add_option("-e", action="append", dest="extra_input", default=[],
220 help="Full path to an extra input pak file without the\
221 locale suffix and \".pak\" extension.")
222 parser.add_option("-p", action="store", dest="os",
223 help="The target OS. (e.g. mac, linux, win, etc.)")
224 parser.add_option("--use-ash", action="store", dest="use_ash",
225 help="Whether to include ash strings")
226 parser.add_option("--chromeos", action="store",
227 help="Whether building for Chrome OS")
228 parser.add_option("--whitelist", action="store", help="Full path to the "
229 "whitelist used to filter output pak file resource IDs")
230 parser.add_option("--enable-autofill-dialog", action="store",
231 dest="enable_autofill_dialog",
232 help="Whether to include strings for autofill dialog")
233 parser.add_option("--enable-extensions", action="store",
234 dest="enable_extensions",
235 help="Whether to include strings for extensions")
236 options, locales = parser.parse_args(argv)
238 if not locales:
239 parser.error('Please specificy at least one locale to process.\n')
241 print_inputs = options.inputs
242 print_outputs = options.outputs
243 GRIT_DIR = options.grit_dir
244 INT_DIR = options.int_dir
245 SHARE_INT_DIR = options.share_int_dir
246 BRANDING = options.branding
247 EXTRA_INPUT_FILES = options.extra_input
248 OS = options.os
249 CHROMEOS = options.chromeos == '1'
250 USE_ASH = options.use_ash == '1'
251 WHITELIST = options.whitelist
252 ENABLE_AUTOFILL_DIALOG = options.enable_autofill_dialog == '1'
253 ENABLE_EXTENSIONS = options.enable_extensions == '1'
255 if not OS:
256 if sys.platform == 'darwin':
257 OS = 'mac'
258 elif sys.platform.startswith('linux'):
259 OS = 'linux'
260 elif sys.platform in ('cygwin', 'win32'):
261 OS = 'win'
262 else:
263 OS = sys.platform
265 if not (GRIT_DIR and INT_DIR and SHARE_INT_DIR):
266 parser.error('Please specify all of "-g" and "-x" and "-s".\n')
267 if print_inputs and print_outputs:
268 parser.error('Please specify only one of "-i" or "-o".\n')
269 # Need to know the branding, unless we're just listing the outputs.
270 if not print_outputs and not BRANDING:
271 parser.error('Please specify "-b" to determine the input files.\n')
273 if print_inputs:
274 return list_inputs(locales)
276 if print_outputs:
277 return list_outputs(locales)
279 return repack_locales(locales)
281 if __name__ == '__main__':
282 results = DoMain(sys.argv[1:])
283 if results:
284 print results