Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / build / secondary / tools / grit / grit_rule.gni
blob13bb8e0ec9c59dbd98e28be52de925376b4f2885
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. Grit will
15 #       verify at build time that this list is correct and will fail if there
16 #       is a mismatch between the outputs specified by the .grd file and the
17 #       outputs list here.
19 #       To get this list, you can look in the .grd file for
20 #       <output filename="..." and put those filename here. The base directory
21 #       of the list in Grit and the output list specified in the GN grit target
22 #       are the same (the target_gen_dir) so you can generally copy the names
23 #       exactly.
25 #       To get the list of outputs programatically, run:
26 #           python tools/grit/grit_info.py --outputs . path/to/your.grd
27 #       And strip the leading "./" from the output files.
29 #   defines (optional)
30 #       Extra defines to pass to grit (on top of the global grit_defines list).
32 #   grit_flags (optional)
33 #       List of strings containing extra command-line flags to pass to Grit.
35 #   resource_ids (optional)
36 #       Path to a grit "firstidsfile". Default is
37 #       //tools/gritsettings/resource_ids. Set to "" to use the value specified
38 #       in the <grit> nodes of the processed files.
40 #   output_dir (optional)
41 #       Directory for generated files. If you specify this, you will often
42 #       want to specify output_name if the target name is not particularly
43 #       unique, since this can cause files from multiple grit targets to
44 #       overwrite each other.
46 #   output_name (optiona)
47 #       Provide an alternate base name for the generated files, like the .d
48 #       files. Normally these are based on the target name and go in the
49 #       output_dir, but if multiple targets with the same name end up in
50 #       the same output_dir, they can collide.
52 #   depfile_dir (optional)
53 #       If set, used to store the depfile and corresponding stamp file.
54 #       Defaults to output_dir
56 #   use_qualified_include (optional)
57 #       If set, output_dir is not added to include_dirs.
59 #   configs (optional)
60 #       List of additional configs to be applied to the generated target.
61 #   deps  (optional)
62 #   inputs  (optional)
63 #       List of additional files, required for grit to process source file.
64 #   visibility  (optional)
65 #       Normal meaning.
67 # Example
69 #   grit("my_resources") {
70 #     # Source and outputs are required.
71 #     source = "myfile.grd"
72 #     outputs = [
73 #       "foo_strings.h",
74 #       "foo_strings.pak",
75 #     ]
77 #     grit_flags = [ "-E", "foo=bar" ]  # Optional extra flags.
78 #     # You can also put deps here if the grit source depends on generated
79 #     # files.
80 #   }
81 import("//build/config/chrome_build.gni")
82 import("//third_party/WebKit/Source/config.gni")
83 import("//build/config/crypto.gni")
84 import("//build/config/features.gni")
85 import("//build/config/ui.gni")
87 grit_defines = []
89 # Mac and iOS want Title Case strings.
90 use_titlecase_in_grd_files = is_mac || is_ios
91 if (use_titlecase_in_grd_files) {
92   grit_defines += [
93     "-D",
94     "use_titlecase",
95   ]
98 if (is_chrome_branded) {
99   grit_defines += [
100     "-D",
101     "_google_chrome",
102     "-E",
103     "CHROMIUM_BUILD=google_chrome",
104   ]
105 } else {
106   grit_defines += [
107     "-D",
108     "_chromium",
109     "-E",
110     "CHROMIUM_BUILD=chromium",
111   ]
114 if (is_chromeos) {
115   grit_defines += [
116     "-D",
117     "chromeos",
118     "-D",
119     "scale_factors=2x",
120   ]
123 if (is_desktop_linux) {
124   grit_defines += [
125     "-D",
126     "desktop_linux",
127   ]
130 if (toolkit_views) {
131   grit_defines += [
132     "-D",
133     "toolkit_views",
134   ]
137 if (use_aura) {
138   grit_defines += [
139     "-D",
140     "use_aura",
141   ]
144 if (use_ash) {
145   grit_defines += [
146     "-D",
147     "use_ash",
148   ]
151 if (use_nss_certs) {
152   grit_defines += [
153     "-D",
154     "use_nss_certs",
155   ]
158 if (use_ozone) {
159   grit_defines += [
160     "-D",
161     "use_ozone",
162   ]
165 if (enable_image_loader_extension) {
166   grit_defines += [
167     "-D",
168     "image_loader_extension",
169   ]
172 if (is_android) {
173   grit_defines += [
174     "-t",
175     "android",
176     "-E",
177     "ANDROID_JAVA_TAGGED_ONLY=true",
178   ]
181 if (is_mac || is_ios) {
182   grit_defines += [
183     "-D",
184     "scale_factors=2x",
185   ]
188 if (is_ios) {
189   grit_defines += [
190     "-t",
191     "ios",
193     # iOS uses a whitelist to filter resources.
194     "-w",
195     rebase_path("//build/ios/grit_whitelist.txt", root_build_dir),
196   ]
199 if (enable_extensions) {
200   grit_defines += [
201     "-D",
202     "enable_extensions",
203   ]
205 if (enable_media_router) {
206   grit_defines += [
207     "-D",
208     "enable_media_router",
209   ]
211 if (enable_plugins) {
212   grit_defines += [
213     "-D",
214     "enable_plugins",
215   ]
217 if (enable_basic_printing || enable_print_preview) {
218   grit_defines += [
219     "-D",
220     "enable_printing",
221   ]
222   if (enable_print_preview) {
223     grit_defines += [
224       "-D",
225       "enable_print_preview",
226     ]
227   }
229 if (enable_themes) {
230   grit_defines += [
231     "-D",
232     "enable_themes",
233   ]
235 if (enable_app_list) {
236   grit_defines += [
237     "-D",
238     "enable_app_list",
239   ]
241 if (enable_settings_app) {
242   grit_defines += [
243     "-D",
244     "enable_settings_app",
245   ]
247 if (enable_google_now) {
248   grit_defines += [
249     "-D",
250     "enable_google_now",
251   ]
254 if (use_concatenated_impulse_responses) {
255   grit_defines += [
256     "-D",
257     "use_concatenated_impulse_responses",
258   ]
261 if (enable_webrtc) {
262   grit_defines += [
263     "-D",
264     "enable_webrtc",
265   ]
267 if (enable_hangout_services_extension) {
268   grit_defines += [
269     "-D",
270     "enable_hangout_services_extension",
271   ]
273 if (enable_task_manager) {
274   grit_defines += [
275     "-D",
276     "enable_task_manager",
277   ]
279 if (enable_notifications) {
280   grit_defines += [
281     "-D",
282     "enable_notifications",
283   ]
285 if (enable_wifi_bootstrapping) {
286   grit_defines += [
287     "-D",
288     "enable_wifi_bootstrapping",
289   ]
291 if (enable_service_discovery) {
292   grit_defines += [
293     "-D",
294     "enable_service_discovery",
295   ]
297 if (mac_views_browser) {
298   grit_defines += [
299     "-D",
300     "mac_views_browser",
301   ]
303 if (enable_topchrome_md) {
304   grit_defines += [
305     "-D",
306     "enable_topchrome_md",
307   ]
310 grit_resource_id_file = "//tools/gritsettings/resource_ids"
311 grit_info_script = "//tools/grit/grit_info.py"
313 template("grit") {
314   assert(defined(invoker.source),
315          "\"source\" must be defined for the grit template $target_name")
317   grit_inputs = [ invoker.source ]
319   if (defined(invoker.resource_ids)) {
320     resource_ids = invoker.resource_ids
321   } else {
322     resource_ids = grit_resource_id_file
323   }
324   if (resource_ids != "") {
325     # The script depends on the ID file. Only add this dependency if the ID
326     # file is specified.
327     grit_inputs += [ resource_ids ]
328   }
330   if (defined(invoker.output_dir)) {
331     output_dir = invoker.output_dir
332   } else {
333     output_dir = target_gen_dir
334   }
336   if (defined(invoker.output_name)) {
337     grit_output_name = invoker.output_name
338   } else {
339     grit_output_name = target_name
340   }
342   if (defined(invoker.depfile_dir)) {
343     depfile_dir = invoker.depfile_dir
344   } else {
345     depfile_dir = output_dir
346   }
348   # These are all passed as arguments to the script so have to be relative to
349   # the build directory.
350   if (resource_ids != "") {
351     resource_ids = rebase_path(resource_ids, root_build_dir)
352   }
353   rebased_output_dir = rebase_path(output_dir, root_build_dir)
354   source_path = rebase_path(invoker.source, root_build_dir)
356   if (defined(invoker.grit_flags)) {
357     grit_flags = invoker.grit_flags
358   } else {
359     grit_flags = []  # These are optional so default to empty list.
360   }
362   assert_files_flags = []
364   # We want to make sure the declared outputs actually match what Grit is
365   # writing. We write the list to a file (some of the output lists are long
366   # enough to not fit on a Windows command line) and ask Grit to verify those
367   # are the actual outputs at runtime.
368   asserted_list_file =
369       "$target_out_dir/${grit_output_name}_expected_outputs.txt"
370   write_file(asserted_list_file,
371              rebase_path(invoker.outputs, root_build_dir, output_dir))
372   assert_files_flags += [ "--assert-file-list=" +
373                           rebase_path(asserted_list_file, root_build_dir) ]
374   grit_outputs =
375       get_path_info(rebase_path(invoker.outputs, ".", output_dir), "abspath")
377   # The config and the action below get this visibility son only the generated
378   # source set can depend on them. The variable "target_name" will get
379   # overwritten inside the inner classes so we need to compute it here.
380   target_visibility = [ ":$target_name" ]
382   # The current grit setup makes an file in $output_dir/grit/foo.h that
383   # the source code expects to include via "grit/foo.h". It would be nice to
384   # change this to including absolute paths relative to the root gen directory
385   # (like "mycomponent/foo.h"). This config sets up the include path.
386   grit_config = target_name + "_grit_config"
387   config(grit_config) {
388     if (!defined(invoker.use_qualified_include) ||
389         !invoker.use_qualified_include) {
390       include_dirs = [ output_dir ]
391     }
392     visibility = target_visibility
393   }
395   grit_custom_target = target_name + "_grit"
396   action(grit_custom_target) {
397     script = "//tools/grit/grit.py"
398     inputs = grit_inputs
400     depfile = "$depfile_dir/${grit_output_name}_stamp.d"
401     outputs = [ "${depfile}.stamp" ] + grit_outputs
403     args = [
404       "-i",
405       source_path,
406       "build",
407     ]
408     if (resource_ids != "") {
409       args += [
410         "-f",
411         resource_ids,
412       ]
413     }
414     args += [
415               "-o",
416               rebased_output_dir,
417               "--depdir",
418               ".",
419               "--depfile",
420               rebase_path(depfile, root_build_dir),
421               "--write-only-new=1",
422               "--depend-on-stamp",
423             ] + grit_defines
425     # Add extra defines with -D flags.
426     if (defined(invoker.defines)) {
427       foreach(i, invoker.defines) {
428         args += [
429           "-D",
430           i,
431         ]
432       }
433     }
435     args += grit_flags + assert_files_flags
437     if (defined(invoker.visibility)) {
438       # This needs to include both what the invoker specified (since they
439       # probably include generated headers from this target), as well as the
440       # generated source set (since there's no guarantee that the visibility
441       # specified by the invoker includes our target).
442       #
443       # Only define visibility at all if the invoker specified it. Otherwise,
444       # we want to keep the public "no visibility specified" default.
445       visibility = target_visibility + invoker.visibility
446     }
448     deps = [
449       "//tools/grit:grit_sources",
450     ]
451     if (defined(invoker.deps)) {
452       deps += invoker.deps
453     }
454     if (defined(invoker.inputs)) {
455       inputs += invoker.inputs
456     }
457   }
459   # This is the thing that people actually link with, it must be named the
460   # same as the argument the template was invoked with.
461   source_set(target_name) {
462     # Since we generate a file, we need to be run before the targets that
463     # depend on us.
464     sources = grit_outputs
466     # Deps set on the template invocation will go on the action that runs
467     # grit above rather than this library. This target needs to depend on the
468     # action publicly so other scripts can take the outputs from the grit
469     # script as inputs.
470     public_deps = [
471       ":$grit_custom_target",
472     ]
473     public_configs = [ ":$grit_config" ]
475     if (defined(invoker.public_configs)) {
476       public_configs += invoker.public_configs
477     }
479     if (defined(invoker.configs)) {
480       configs += invoker.configs
481     }
483     if (defined(invoker.visibility)) {
484       visibility = invoker.visibility
485     }
486     output_name = grit_output_name
487   }