Fix breakages in https://codereview.chromium.org/1155713003/
[chromium-blink-merge.git] / build / secondary / tools / grit / grit_rule.gni
blob95a5e2e1332705ae8ad358d69cd26ee60c595ad4
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 # Instantiate grit. This will produce a script target to run grit, and a
6 # static library that compiles the .cc files.
8 # Parameters
10 #   source (required)
11 #       Path to .grd file.
13 #   outputs (required)
14 #       List of outputs from grit, relative to the target_gen_dir. If supplied,
15 #       a call to Grit to compute the outputs can be skipped which will make
16 #       GN run faster. Grit will verify at build time that this list is correct
17 #       and will fail if there is a mismatch between the outputs specified by
18 #       the .grd file and the outputs list here.
20 #       To get this list, you can look in the .grd file for
21 #       <output filename="..." and put those filename here. The base directory
22 #       of the list in Grit and the output list specified in the GN grit target
23 #       are the same (the target_gen_dir) so you can generally copy the names
24 #       exactly.
26 #       To get the list of outputs programatically, run:
27 #           python tools/grit/grit_info.py --outputs . path/to/your.grd
28 #       And strip the leading "./" from the output files.
30 #   defines (optional)
31 #       Extra defines to pass to grit (on top of the global grit_defines list).
33 #   grit_flags (optional)
34 #       List of strings containing extra command-line flags to pass to Grit.
36 #   resource_ids (optional)
37 #       Path to a grit "firstidsfile". Default is
38 #       //tools/gritsettings/resource_ids. Set to "" to use the value specified
39 #       in the <grit> nodes of the processed files.
41 #   output_dir (optional)
42 #       Directory for generated files. If you specify this, you will often
43 #       want to specify output_name if the target name is not particularly
44 #       unique, since this can cause files from multiple grit targets to
45 #       overwrite each other.
47 #   output_name (optiona)
48 #       Provide an alternate base name for the generated files, like the .d
49 #       files. Normally these are based on the target name and go in the
50 #       output_dir, but if multiple targets with the same name end up in
51 #       the same output_dir, they can collide.
53 #   use_qualified_include (optional)
54 #       If set, output_dir is not added to include_dirs.
56 #   configs (optional)
57 #       List of additional configs to be applied to the generated target.
58 #   deps  (optional)
59 #   inputs  (optional)
60 #       List of additional files, required for grit to process source file.
61 #   visibility  (optional)
62 #       Normal meaning.
64 # Example
66 #   grit("my_resources") {
67 #     # Source and outputs are required.
68 #     source = "myfile.grd"
69 #     outputs = [
70 #       "foo_strings.h",
71 #       "foo_strings.pak",
72 #     ]
74 #     grit_flags = [ "-E", "foo=bar" ]  # Optional extra flags.
75 #     # You can also put deps here if the grit source depends on generated
76 #     # files.
77 #   }
78 import("//build/config/chrome_build.gni")
79 import("//build/config/crypto.gni")
80 import("//build/config/features.gni")
81 import("//build/config/ui.gni")
83 grit_defines = []
85 # Mac and iOS want Title Case strings.
86 use_titlecase_in_grd_files = is_mac || is_ios
87 if (use_titlecase_in_grd_files) {
88   grit_defines += [
89     "-D",
90     "use_titlecase",
91   ]
94 if (is_chrome_branded) {
95   grit_defines += [
96     "-D",
97     "_google_chrome",
98     "-E",
99     "CHROMIUM_BUILD=google_chrome",
100   ]
101 } else {
102   grit_defines += [
103     "-D",
104     "_chromium",
105     "-E",
106     "CHROMIUM_BUILD=chromium",
107   ]
110 if (is_chromeos) {
111   grit_defines += [
112     "-D",
113     "chromeos",
114     "-D",
115     "scale_factors=2x",
116   ]
119 if (is_desktop_linux) {
120   grit_defines += [
121     "-D",
122     "desktop_linux",
123   ]
126 if (toolkit_views) {
127   grit_defines += [
128     "-D",
129     "toolkit_views",
130   ]
133 if (use_aura) {
134   grit_defines += [
135     "-D",
136     "use_aura",
137   ]
140 if (use_ash) {
141   grit_defines += [
142     "-D",
143     "use_ash",
144   ]
147 if (use_nss_certs) {
148   grit_defines += [
149     "-D",
150     "use_nss_certs",
151   ]
154 if (use_ozone) {
155   grit_defines += [
156     "-D",
157     "use_ozone",
158   ]
161 if (enable_image_loader_extension) {
162   grit_defines += [
163     "-D",
164     "image_loader_extension",
165   ]
168 if (enable_remoting) {
169   grit_defines += [
170     "-D",
171     "remoting",
172   ]
175 if (is_android) {
176   grit_defines += [
177     "-t",
178     "android",
179     "-E",
180     "ANDROID_JAVA_TAGGED_ONLY=true",
181   ]
184 if (is_mac || is_ios) {
185   grit_defines += [
186     "-D",
187     "scale_factors=2x",
188   ]
191 if (is_ios) {
192   grit_defines += [
193     "-t",
194     "ios",
196     # iOS uses a whitelist to filter resources.
197     "-w",
198     rebase_path("//build/ios/grit_whitelist.txt", root_build_dir),
199   ]
202 if (enable_extensions) {
203   grit_defines += [
204     "-D",
205     "enable_extensions",
206   ]
208 if (enable_media_router) {
209   grit_defines += [
210     "-D",
211     "enable_media_router",
212   ]
214 if (enable_plugins) {
215   grit_defines += [
216     "-D",
217     "enable_plugins",
218   ]
220 if (enable_basic_printing || enable_print_preview) {
221   grit_defines += [
222     "-D",
223     "enable_printing",
224   ]
225   if (enable_print_preview) {
226     grit_defines += [
227       "-D",
228       "enable_print_preview",
229     ]
230   }
232 if (enable_themes) {
233   grit_defines += [
234     "-D",
235     "enable_themes",
236   ]
238 if (enable_app_list) {
239   grit_defines += [
240     "-D",
241     "enable_app_list",
242   ]
244 if (enable_settings_app) {
245   grit_defines += [
246     "-D",
247     "enable_settings_app",
248   ]
250 if (enable_google_now) {
251   grit_defines += [
252     "-D",
253     "enable_google_now",
254   ]
257 # Note: use_concatenated_impulse_responses is omitted. It is never used and
258 # should probably be removed from GYP build.
259 if (enable_webrtc) {
260   grit_defines += [
261     "-D",
262     "enable_webrtc",
263   ]
265 if (enable_hangout_services_extension) {
266   grit_defines += [
267     "-D",
268     "enable_hangout_services_extension",
269   ]
271 if (enable_task_manager) {
272   grit_defines += [
273     "-D",
274     "enable_task_manager",
275   ]
277 if (enable_notifications) {
278   grit_defines += [
279     "-D",
280     "enable_notifications",
281   ]
283 if (enable_wifi_bootstrapping) {
284   grit_defines += [
285     "-D",
286     "enable_wifi_bootstrapping",
287   ]
289 if (enable_service_discovery) {
290   grit_defines += [
291     "-D",
292     "enable_service_discovery",
293   ]
295 if (mac_views_browser) {
296   grit_defines += [
297     "-D",
298     "mac_views_browser",
299   ]
302 grit_resource_id_file = "//tools/gritsettings/resource_ids"
303 grit_info_script = "//tools/grit/grit_info.py"
305 template("grit") {
306   assert(defined(invoker.source),
307          "\"source\" must be defined for the grit template $target_name")
309   grit_inputs = [ invoker.source ]
311   if (defined(invoker.resource_ids)) {
312     resource_ids = invoker.resource_ids
313   } else {
314     resource_ids = grit_resource_id_file
315   }
316   if (resource_ids != "") {
317     # The script depends on the ID file. Only add this dependency if the ID
318     # file is specified.
319     grit_inputs += [ resource_ids ]
320   }
322   if (defined(invoker.output_dir)) {
323     output_dir = invoker.output_dir
324   } else {
325     output_dir = target_gen_dir
326   }
328   if (defined(invoker.output_name)) {
329     grit_output_name = invoker.output_name
330   } else {
331     grit_output_name = target_name
332   }
334   # These are all passed as arguments to the script so have to be relative to
335   # the build directory.
336   if (resource_ids != "") {
337     resource_ids = rebase_path(resource_ids, root_build_dir)
338   }
339   rebased_output_dir = rebase_path(output_dir, root_build_dir)
340   source_path = rebase_path(invoker.source, root_build_dir)
342   if (defined(invoker.grit_flags)) {
343     grit_flags = invoker.grit_flags
344   } else {
345     grit_flags = []  # These are optional so default to empty list.
346   }
348   assert_files_flags = []
350   # We want to make sure the declared outputs actually match what Grit is
351   # writing. We write the list to a file (some of the output lists are long
352   # enough to not fit on a Windows command line) and ask Grit to verify those
353   # are the actual outputs at runtime.
354   asserted_list_file =
355       "$target_out_dir/${grit_output_name}_expected_outputs.txt"
356   write_file(asserted_list_file,
357              rebase_path(invoker.outputs, root_build_dir, output_dir))
358   assert_files_flags += [ "--assert-file-list=" +
359                           rebase_path(asserted_list_file, root_build_dir) ]
360   grit_outputs =
361       get_path_info(rebase_path(invoker.outputs, ".", output_dir), "abspath")
363   # The config and the action below get this visibility son only the generated
364   # source set can depend on them. The variable "target_name" will get
365   # overwritten inside the inner classes so we need to compute it here.
366   target_visibility = [ ":$target_name" ]
368   # The current grit setup makes an file in $output_dir/grit/foo.h that
369   # the source code expects to include via "grit/foo.h". It would be nice to
370   # change this to including absolute paths relative to the root gen directory
371   # (like "mycomponent/foo.h"). This config sets up the include path.
372   grit_config = target_name + "_grit_config"
373   config(grit_config) {
374     if (!defined(invoker.use_qualified_include) ||
375         !invoker.use_qualified_include) {
376       include_dirs = [ output_dir ]
377     }
378     visibility = target_visibility
379   }
381   grit_custom_target = target_name + "_grit"
382   action(grit_custom_target) {
383     script = "//tools/grit/grit.py"
384     inputs = grit_inputs
386     depfile = "$output_dir/${grit_output_name}_stamp.d"
387     outputs = [ "${depfile}.stamp" ] + grit_outputs
389     args = [
390       "-i",
391       source_path,
392       "build",
393     ]
394     if (resource_ids != "") {
395       args += [
396         "-f",
397         resource_ids,
398       ]
399     }
400     args += [
401               "-o",
402               rebased_output_dir,
403               "--depdir",
404               ".",
405               "--depfile",
406               rebase_path(depfile, root_build_dir),
407               "--write-only-new=1",
408               "--depend-on-stamp",
409             ] + grit_defines
411     # Add extra defines with -D flags.
412     if (defined(invoker.defines)) {
413       foreach(i, invoker.defines) {
414         args += [
415           "-D",
416           i,
417         ]
418       }
419     }
421     args += grit_flags + assert_files_flags
423     if (defined(invoker.visibility)) {
424       # This needs to include both what the invoker specified (since they
425       # probably include generated headers from this target), as well as the
426       # generated source set (since there's no guarantee that the visibility
427       # specified by the invoker includes our target).
428       #
429       # Only define visibility at all if the invoker specified it. Otherwise,
430       # we want to keep the public "no visibility specified" default.
431       visibility = target_visibility + invoker.visibility
432     }
434     deps = [
435       "//tools/grit:grit_sources",
436     ]
437     if (defined(invoker.deps)) {
438       deps += invoker.deps
439     }
440     if (defined(invoker.inputs)) {
441       inputs += invoker.inputs
442     }
443   }
445   # This is the thing that people actually link with, it must be named the
446   # same as the argument the template was invoked with.
447   source_set(target_name) {
448     # Since we generate a file, we need to be run before the targets that
449     # depend on us.
450     sources = grit_outputs
452     # Deps set on the template invocation will go on the action that runs
453     # grit above rather than this library. This target needs to depend on the
454     # action publicly so other scripts can take the outputs from the grit
455     # script as inputs.
456     public_deps = [
457       ":$grit_custom_target",
458     ]
459     public_configs = [ ":$grit_config" ]
461     if (defined(invoker.public_configs)) {
462       public_configs += invoker.public_configs
463     }
465     if (defined(invoker.configs)) {
466       configs += invoker.configs
467     }
469     if (defined(invoker.visibility)) {
470       visibility = invoker.visibility
471     }
472     output_name = grit_output_name
473   }