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. 4 5# Instantiate grit. This will produce a script target to run grit, and a 6# static library that compiles the .cc files. 7# 8# Parameters 9# 10# source 11# Path to .grd file. 12# 13# grit_flags (optional) 14# List of strings containing extra command-line flags to pass to Grit. 15# 16# deps (optional) 17# visibility (optional) 18# Normal meaning. 19# 20# Example 21# 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 26# # files. 27# } 28import ("//build/config/features.gni") 29import ("//build/config/ui.gni") 30 31grit_defines = [] 32 33if (is_chromeos) { 34 grit_defines += [ 35 "-D", "chromeos", 36 "-D", "scale_factors=2x" 37 ] 38} 39 40if (is_desktop_linux) { 41 grit_defines += [ "-D", "desktop_linux" ] 42} 43 44if (is_android) { 45 grit_defines += [ 46 "-t", "android", 47 "-E", "ANDROID_JAVA_TAGGED_ONLY=true", 48 ] 49} 50 51if (enable_extensions) { 52 grit_defines += [ "-D", "enable_extensions" ] 53} 54if (enable_plugins) { 55 grit_defines += [ "-D", "enable_plugins" ] 56} 57 58# TODO(GYP) the rest of the grit_defines from the gyp build. 59 60grit_resource_id_file = "//tools/gritsettings/resource_ids" 61grit_info_script = "//tools/grit/grit_info.py" 62 63template("grit") { 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") 69 70 # These are all passed as arguments to the script so have to be relative to 71 # the build directory. 72 resource_ids = 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) 76 77 if (defined(invoker.grit_flags)) { 78 grit_flags = invoker.grit_flags 79 } else { 80 grit_flags = [] # These are optional so default to empty list. 81 } 82 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 86 # the current one. 87 grit_inputs = rebase_path(grit_inputs_build_rel, ".", root_build_dir) + [ 88 grit_resource_id_file, 89 ] 90 91 grit_outputs_build_rel = exec_script(grit_info_script, 92 [ "--outputs", "$output_dir", source_path, "-f", resource_ids ] + 93 grit_flags, 94 "list lines") 95 96 # The inputs are relative to the current (build) directory, rebase to 97 # the current one. 98 grit_outputs = rebase_path(grit_outputs_build_rel, ".", root_build_dir) 99 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" 104 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 113 } 114 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 120 121 args = [ 122 "-i", source_path, "build", 123 "-f", resource_ids, 124 "-o", output_dir, 125 ] + grit_defines + grit_flags 126 127 visibility = target_visibility 128 129 if (defined(invoker.deps)) { 130 deps = invoker.deps 131 } 132 } 133 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 138 # depend on us. 139 sources = grit_outputs 140 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" ] 145 146 if (defined(invoker.visibility)) { 147 visibility = invoker.visibility 148 } 149 if (defined(invoker.output_name)) { 150 output_name = invoker.output_name 151 } 152 } 153} 154