blob: 66063bcb38ed29a33ea22e851193b996979622aa [file] [log] [blame]
damargulis5c5337d2017-04-21 22:36:081# Copyright 2017 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
5import("//third_party/closure_compiler/closure_args.gni")
Christopher Lamef75ebfe2018-03-28 04:20:276import("//ui/webui/webui_features.gni")
damargulis5c5337d2017-04-21 22:36:087
8script_path = "//third_party/closure_compiler"
9compiler_path = "$script_path/compiler/compiler.jar"
Christopher Lam997fd812017-11-03 03:42:4010externs_path = "$script_path/externs"
Christopher Lam739a90d2018-03-07 03:54:3711interfaces_path = "$script_path/interfaces"
dpapad9d788442018-04-17 20:04:2712chrome_externs = "$externs_path/chrome.js"
Christopher Lam997fd812017-11-03 03:42:4013polymer_externs = "$externs_path/polymer-1.0.js"
damargulis5c5337d2017-04-21 22:36:0814
15# Defines a target that creates an ordering for .js files to be used by
16# js_binary to compile.
17#
18# Variables:
19# sources:
Christopher Lam997fd812017-11-03 03:42:4020# List of Javascript files to include in the library. Uses target_name.js by
21# default.
damargulis5c5337d2017-04-21 22:36:0822#
23# deps:
24# List of js_library targets to depend on
25#
alexclarke1a69b3b22017-06-09 11:52:4126# extra_deps:
27# List of any other rules to depend on. E.g. a rule that generates js source
28# files
29#
Christopher Lam739a90d2018-03-07 03:54:3730# externs_list:
31# A list of .js files to pass to the compiler as externs
32#
33# extra_sources:
34# A list of .js files to pass to the compiler as interfaces
35#
damargulis5c5337d2017-04-21 22:36:0836# Example:
37# js_library("apple_tree") {
38# sources = ["tree_main.js"]
39# deps = [
40# ":branch",
41# ":trunk",
42# ":root",
43# ]
44# }
45
46template("js_library") {
damargulis5c5337d2017-04-21 22:36:0847 action(target_name) {
48 script = "$script_path/js_library.py"
49 forward_variables_from(invoker,
50 [
51 "sources",
52 "deps",
Christopher Lam997fd812017-11-03 03:42:4053 "externs_list",
Christopher Lam739a90d2018-03-07 03:54:3754 "extra_sources",
alexclarke1a69b3b22017-06-09 11:52:4155 "extra_deps",
damargulis5c5337d2017-04-21 22:36:0856 ])
57 output_file = "$target_gen_dir/$target_name.js_library"
58 outputs = [
59 output_file,
60 ]
61 args = [ "--output" ] + [ rebase_path(output_file, root_build_dir) ]
Christopher Lam997fd812017-11-03 03:42:4062
63 if (!defined(sources)) {
64 sources = [
65 target_name + ".js",
66 ]
damargulis5c5337d2017-04-21 22:36:0867 }
Christopher Lam997fd812017-11-03 03:42:4068
69 args += [ "--sources" ] + rebase_path(sources, root_build_dir)
Christopher Lam739a90d2018-03-07 03:54:3770 if (defined(extra_sources)) {
71 args += rebase_path(extra_sources, root_build_dir)
Christopher Lam992239042018-06-13 00:36:2672 sources += extra_sources
Christopher Lam739a90d2018-03-07 03:54:3773 }
Christopher Lam997fd812017-11-03 03:42:4074
damargulis5c5337d2017-04-21 22:36:0875 if (defined(deps)) {
76 args += [ "--deps" ]
77 foreach(dep, deps) {
78 # Get the output path for each dep
79 dep_gen_dir = get_label_info(dep, "target_gen_dir")
80 dep_name = get_label_info(dep, "name")
81 dep_output_path = "$dep_gen_dir/$dep_name.js_library"
82 args += [ rebase_path(dep_output_path, root_build_dir) ]
83 }
84 }
Christopher Lam997fd812017-11-03 03:42:4085 if (defined(externs_list)) {
86 args += [ "--externs" ] + rebase_path(externs_list, root_build_dir)
Christopher Lam992239042018-06-13 00:36:2687 sources += externs_list
Christopher Lam997fd812017-11-03 03:42:4088 }
alexclarke1a69b3b22017-06-09 11:52:4189 if (defined(extra_deps)) {
90 if (!defined(deps)) {
91 deps = []
92 }
93 deps += extra_deps
94 }
damargulis5c5337d2017-04-21 22:36:0895 }
96}
97
98# Defines a target that compiles javascript files using the Closure compiler.
99# This will produce a minified javascript output file. Additional checks and
100# optimizations can be configured using the closure_flags attribute.
101#
102# Variables:
103# sources:
Christopher Lam997fd812017-11-03 03:42:40104# List of .js files to compile. Will be target_name.js by default. Use
105# sources = [] to specify no input files (when making a target that just
106# group compiles other targets, for example).
damargulis5c5337d2017-04-21 22:36:08107#
108# deps:
109# List of js_library rules to depend on
110#
111# outputs:
112# A file to write the compiled .js to.
113# Only takes in a single file, but must be placed in a list
114#
115# bootstrap_file:
116# A .js files to include before all others
117#
118# config_files:
119# A list of .js files to include after the bootstrap_file but before all
120# others
121#
122# closure_flags:
123# A list of custom flags to pass to the Closure compiler. Do not include
124# the leading dashes
125#
126# externs_list:
127# A list of .js files to pass to the compiler as externs
128#
129# Example:
130# js_binary("tree") {
131# sources = ["tree_main.js"]
132# deps = [":apple_tree"]
133# outputs = [ "$target_gen_dir/tree.js" ]
134# bootstrap_file = "bootstrap.js"
135# config_files = [
136# "config1.js",
137# "config2.js",
138# ]
139# closure_flags = ["jscomp_error=undefinedVars"]
140# externs_list = [ "externs.js" ]
141# }
142
143template("js_binary") {
damargulis5c5337d2017-04-21 22:36:08144 action(target_name) {
145 script = "$script_path/js_binary.py"
146 forward_variables_from(invoker,
147 [
148 "sources",
149 "deps",
150 "outputs",
151 "bootstrap_file",
152 "config_files",
153 "closure_flags",
154 "externs_list",
Christopher Lam770bd962018-03-07 11:22:19155 "extra_deps",
damargulis5c5337d2017-04-21 22:36:08156 ])
157 args = [
158 "--compiler",
159 rebase_path(compiler_path, root_build_dir),
160 ]
Christopher Lam997fd812017-11-03 03:42:40161
162 if (!defined(outputs)) {
163 outputs = [
164 "$target_gen_dir/$target_name.js",
165 ]
damargulis5c5337d2017-04-21 22:36:08166 }
Christopher Lam997fd812017-11-03 03:42:40167 args += [ "--output" ] + rebase_path(outputs, root_build_dir)
168
169 if (!defined(sources)) {
170 sources = [
171 "$target_name.js",
172 ]
173 }
174
damargulis5c5337d2017-04-21 22:36:08175 if (defined(deps)) {
176 args += [ "--deps" ]
177 foreach(dep, deps) {
178 # Get the output path for each dep
179 dep_gen_dir = get_label_info(dep, "target_gen_dir")
180 dep_name = get_label_info(dep, "name")
181 dep_output_path = "$dep_gen_dir/$dep_name.js_library"
182 args += [ rebase_path(dep_output_path, root_build_dir) ]
183 }
184 }
Christopher Lam997fd812017-11-03 03:42:40185
186 args += [ "--sources" ] + rebase_path(sources, root_build_dir)
187
damargulis5c5337d2017-04-21 22:36:08188 if (defined(bootstrap_file)) {
189 args += [
190 "--bootstrap",
191 rebase_path(bootstrap_file, root_build_dir),
192 ]
193 sources += [ bootstrap_file ]
194 }
195 if (defined(config_files)) {
196 args += [ "--config" ] + rebase_path(config_files, root_build_dir)
197 sources += config_files
198 }
199
200 # |minifying_closure_args| from
201 # //third_party/closure_compiler/closure_args.gni
Christopher Lam997fd812017-11-03 03:42:40202 if (!defined(closure_flags)) {
203 closure_flags = default_closure_args
damargulis5c5337d2017-04-21 22:36:08204 }
Christopher Lam997fd812017-11-03 03:42:40205 args += [ "--flags" ] + closure_flags
206 args += [
207 "--externs",
dpapad9d788442018-04-17 20:04:27208 rebase_path("$chrome_externs", root_build_dir),
Christopher Lam997fd812017-11-03 03:42:40209 rebase_path("$polymer_externs", root_build_dir),
210 ]
damargulis5c5337d2017-04-21 22:36:08211 if (defined(externs_list)) {
damargulis5c5337d2017-04-21 22:36:08212 args += rebase_path(externs_list, root_build_dir)
213 sources += externs_list
214 }
Christopher Lam770bd962018-03-07 11:22:19215
216 if (defined(extra_deps)) {
217 if (!defined(deps)) {
218 deps = []
219 }
220 deps += extra_deps
221 }
damargulis5c5337d2017-04-21 22:36:08222 }
223}
Christopher Lam739a90d2018-03-07 03:54:37224
225# Defines a target that compiles a group of js_library targets.
226template("js_type_check") {
Christopher Lamef75ebfe2018-03-28 04:20:27227 if (closure_compile) {
228 js_binary(target_name) {
229 sources = []
230 forward_variables_from(invoker, [ "deps" ])
231 }
232 } else {
233 not_needed(invoker, "*")
234 group(target_name) {
235 }
Christopher Lam739a90d2018-03-07 03:54:37236 }
237}