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.
13 # grit_flags (optional)
14 # List of strings containing extra command-line flags to pass to Grit.
17 # visibility (optional)
22 # grit("my_resources") {
23 # source = "myfile.grd" # source is required.
24 # grit_flags = [ "-E", "foo=bar" ] # Optional extra flags.
25 # # You can also put deps here if the grit source depends on generated
28 import ("//build/config/features.gni")
29 import ("//build/config/ui.gni")
36 "-D", "scale_factors=2x"
40 if (is_desktop_linux) {
41 grit_defines += [ "-D", "desktop_linux" ]
47 "-E", "ANDROID_JAVA_TAGGED_ONLY=true",
51 if (enable_extensions) {
52 grit_defines += [ "-D", "enable_extensions" ]
55 grit_defines += [ "-D", "enable_plugins" ]
58 # TODO(GYP) the rest of the grit_defines from the gyp build.
60 grit_resource_id_file = "//tools/gritsettings/resource_ids"
61 grit_info_script = "//tools/grit/grit_info.py"
64 assert(defined(invoker.source),
65 "\"source\" must be defined for the grit template $target_name")
66 assert(!defined(invoker.sources) && !defined(invoker.outputs),
67 "Neither \"sources\" nor \"outputs\" can be defined for the grit " +
68 "template $target_name")
70 # These are all passed as arguments to the script so have to be relative to
71 # the build directory.
73 rebase_path(grit_resource_id_file, root_build_dir)
74 output_dir = rebase_path(target_gen_dir, root_build_dir)
75 source_path = rebase_path(invoker.source, root_build_dir)
77 if (defined(invoker.grit_flags)) {
78 grit_flags = invoker.grit_flags
80 grit_flags = [] # These are optional so default to empty list.
83 grit_inputs_build_rel = exec_script(grit_info_script,
84 [ "--inputs", source_path, "-f", resource_ids] + grit_flags, "list lines")
85 # The inputs are relative to the current (build) directory, rebase to
87 grit_inputs = rebase_path(grit_inputs_build_rel, ".", root_build_dir) + [
88 grit_resource_id_file,
91 grit_outputs_build_rel = exec_script(grit_info_script,
92 [ "--outputs", "$output_dir", source_path, "-f", resource_ids ] +
96 # The inputs are relative to the current (build) directory, rebase to
98 grit_outputs = rebase_path(grit_outputs_build_rel, ".", root_build_dir)
100 # The config and the action below get this visibility son only the generated
101 # source set can depend on them. The variable "target_name" will get
102 # overwritten inside the innter classes so we need to compute it here.
103 target_visibility = ":$target_name"
105 # The current grit setup makes an file in $target_gen_dir/grit/foo.h that
106 # the source code expects to include via "grit/foo.h". It would be nice to
107 # change this to including absolute paths relative to the root gen directory
108 # (like "mycomponent/foo.h"). This config sets up the include path.
109 grit_config = target_name + "_grit_config"
110 config(grit_config) {
111 include_dirs = [ target_gen_dir ]
112 visibility = target_visibility
115 grit_custom_target = target_name + "_grit"
116 action(grit_custom_target) {
117 script = "//tools/grit/grit.py"
118 source_prereqs = grit_inputs
119 outputs = grit_outputs
122 "-i", source_path, "build",
125 ] + grit_defines + grit_flags
127 visibility = target_visibility
129 if (defined(invoker.deps)) {
134 # This is the thing that people actually link with, it must be named the
135 # same as the argument the template was invoked with.
136 source_set(target_name) {
137 # Since we generate a file, we need to be run before the targets that
139 sources = grit_outputs
141 # Deps set on the template invocation will go on the grit script running
142 # target rather than this library.
143 deps = [ ":$grit_custom_target" ]
144 direct_dependent_configs = [ ":$grit_config" ]
146 if (defined(invoker.visibility)) {
147 visibility = invoker.visibility
149 if (defined(invoker.output_name)) {
150 output_name = invoker.output_name