blob: 960f4265daca647b525d6bd3b944808a0a350a47 [file] [log] [blame]
Primiano Tucci3faad742018-05-16 19:30:48 +01001# Copyright (C) 2018 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import("../gn/perfetto.gni")
16import("../gn/wasm.gni")
17import("../protos/perfetto/trace_processor/proto_files.gni")
18
Hector Dearman262cbe22018-05-31 13:28:21 +010019ui_dir = "$root_build_dir/ui"
Primiano Tucci3faad742018-05-16 19:30:48 +010020nodejs_root = "../buildtools/nodejs"
21nodejs_bin = rebase_path("$nodejs_root/bin", root_build_dir)
22
23# +----------------------------------------------------------------------------+
24# | The outer "ui" target to just ninja -C out/xxx ui |
25# +----------------------------------------------------------------------------+
26group("ui") {
27 deps = [
Hector Dearman262cbe22018-05-31 13:28:21 +010028 ":index_release",
29 ":main_bundle_release",
30 ":wasm_release",
Primiano Tucci3faad742018-05-16 19:30:48 +010031 ]
32}
33
34# +----------------------------------------------------------------------------+
35# | Template used to run node binaries using the hermetic node toolchain. |
36# +----------------------------------------------------------------------------+
37template("node_bin") {
38 action(target_name) {
39 forward_variables_from(invoker,
40 [
41 "inputs",
42 "outputs",
43 ])
44 deps = [
45 ":node_modules",
46 ]
47 if (defined(invoker.deps)) {
48 deps += invoker.deps
49 }
50 script = "../gn/standalone/build_tool_wrapper.py"
51 _node_cmd = invoker.node_cmd
52 args = [
53 "--path=$nodejs_bin",
54 "node",
55 rebase_path("node_modules/.bin/$_node_cmd", root_build_dir),
56 ] + invoker.args
57 }
58}
59
60# +----------------------------------------------------------------------------+
61# | Bundles all *.js files together resolving CommonJS require() deps. |
62# +----------------------------------------------------------------------------+
63
64# Bundle together all js sources into a bundle.js file, that will ultimately be
65# included by the .html files.
Hector Dearman262cbe22018-05-31 13:28:21 +010066node_bin("main_bundle") {
Primiano Tucci3faad742018-05-16 19:30:48 +010067 deps = [
68 ":transpile_all_ts",
69 ]
70 node_cmd = "browserify"
71 inputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +010072 "$target_out_dir/main.js",
Primiano Tucci3faad742018-05-16 19:30:48 +010073 ]
74 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +010075 "$target_out_dir/bundle.js",
Primiano Tucci3faad742018-05-16 19:30:48 +010076 ]
77 args = [
78 rebase_path(inputs[0], root_build_dir),
79 "-d",
80 "-o",
81 rebase_path(outputs[0], root_build_dir),
82 ]
83}
84
85# +----------------------------------------------------------------------------+
86# | Protobuf: gen rules to create .js and .d.ts files from protos. |
87# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +010088proto_gen_dir = "$target_out_dir/gen"
Primiano Tucci3faad742018-05-16 19:30:48 +010089
90node_bin("protos_to_js") {
91 inputs = []
92 foreach(proto, trace_processor_protos) {
93 inputs += [ "../protos/perfetto/trace_processor/$proto.proto" ]
94 }
95 outputs = [
96 "$proto_gen_dir/protos.js",
97 ]
98 node_cmd = "pbjs"
99 args = [
100 "-t",
101 "static-module",
102 "-w",
103 "commonjs",
104 "-p",
105 rebase_path("../protos", root_build_dir),
106 "-o",
107 rebase_path(outputs[0], root_build_dir),
108 ] + rebase_path(inputs, root_build_dir)
109}
110
111# Protobuf.js requires to first generate .js files from the .proto and then
112# create .ts definitions for them.
113node_bin("protos_to_ts") {
114 deps = [
115 ":protos_to_js",
116 ]
117 inputs = [
118 "$proto_gen_dir/protos.js",
119 ]
120 outputs = [
121 "$proto_gen_dir/protos.d.ts",
122 ]
123 node_cmd = "pbts"
124 args = [
125 "-p",
126 rebase_path("../protos", root_build_dir),
127 "-o",
128 rebase_path(outputs[0], root_build_dir),
129 rebase_path(inputs[0], root_build_dir),
130 ]
131}
132
133# +----------------------------------------------------------------------------+
134# | TypeScript: transpiles all *.ts into .js |
135# +----------------------------------------------------------------------------+
136
137# Builds all .ts sources in the repo that are reachable from |sources|.
138node_bin("transpile_all_ts") {
139 sources = [
Hector Dearman3d858cc2018-05-24 17:09:48 +0100140 "src/main.ts",
Primiano Tucci3faad742018-05-16 19:30:48 +0100141 ]
142 deps = [
143 ":dist_symlink",
144 ":protos_to_ts",
145 ]
146 inputs = sources + [ "tsconfig.json" ]
147 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100148 "$target_out_dir/main.js",
Primiano Tucci3faad742018-05-16 19:30:48 +0100149 ]
150
151 # Find all *.ts files and pass them as input triggers. This does NOT affect
152 # which files will get built. This defines only the set of files that, when
153 # changes, will re-trigger this rule in ninja. The files that get transpiled
154 # are the transitive dependencies of |sources|.
155 all_ts_files = exec_script("../gn/standalone/glob.py",
156 [
157 "--root=" + rebase_path(".", root_build_dir),
158 "--filter=*.ts",
159 "--exclude=node_modules",
160 "--exclude=dist",
161 ],
162 "list lines",
163 [ "." ])
164 inputs += all_ts_files
Primiano Tucci3faad742018-05-16 19:30:48 +0100165 node_cmd = "tsc"
166 args = [
167 "--project",
168 rebase_path(".", root_build_dir),
169 "--outDir",
Hector Dearman262cbe22018-05-31 13:28:21 +0100170 rebase_path(target_out_dir, root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100171 ]
172}
173
174# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100175# | Copy rules: create the final output directory. |
Primiano Tucci3faad742018-05-16 19:30:48 +0100176# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100177copy("index_release") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100178 sources = [
179 "index.html",
180 ]
181 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100182 "$ui_dir/index.html",
Primiano Tucci3faad742018-05-16 19:30:48 +0100183 ]
184}
185
Hector Dearman262cbe22018-05-31 13:28:21 +0100186copy("main_bundle_release") {
187 deps = [
188 ":main_bundle",
189 ]
190 sources = [
191 "$target_out_dir/bundle.js",
192 ]
193 outputs = [
194 "$ui_dir/bundle.js",
195 ]
196}
197
198copy("wasm_release") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100199 deps = [
200 "//src/trace_processor:trace_processor.js($wasm_toolchain)",
201 "//src/trace_processor:trace_processor.wasm($wasm_toolchain)",
202 ]
203 sources = [
204 "$root_build_dir/wasm/trace_processor.js",
205 "$root_build_dir/wasm/trace_processor.wasm",
206 ]
207 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100208 "$target_out_dir/wasm/{{source_file_part}}",
Primiano Tucci3faad742018-05-16 19:30:48 +0100209 ]
210}
211
212# +----------------------------------------------------------------------------+
213# | Node JS: run npm install and creates a symlink in the out directory. |
214# +----------------------------------------------------------------------------+
215
216action("check_node_exists") {
217 script = "../gn/standalone/check_buildtool_exists.py"
Hector Dearman48783b12018-05-22 08:26:06 +0100218 args = [
219 nodejs_bin,
220 "--touch",
221 rebase_path("$target_out_dir/node_exists", ""),
222 ]
Primiano Tucci3faad742018-05-16 19:30:48 +0100223 inputs = []
224 outputs = [
Hector Dearman48783b12018-05-22 08:26:06 +0100225 "$target_out_dir/node_exists",
Primiano Tucci3faad742018-05-16 19:30:48 +0100226 ]
227}
228
229# Creates a symlink from out/xxx/ui/node_modules -> ../../../ui/node_modules.
230# This allows to run browserify and other node tools from the out/xxx directory.
231action("node_modules_symlink") {
232 deps = [
233 ":check_node_exists",
234 ]
235 inputs = [
236 "node_modules",
237 ]
238 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100239 "$target_out_dir/node_modules",
Primiano Tucci3faad742018-05-16 19:30:48 +0100240 ]
241 script = "../gn/standalone/build_tool_wrapper.py"
242 args = [
243 "/bin/ln",
244 "-fns",
Hector Dearman262cbe22018-05-31 13:28:21 +0100245 rebase_path(inputs[0], target_out_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100246 rebase_path(outputs[0], root_build_dir),
247 ]
248}
249
250# Runs npm install.
251action("node_modules") {
252 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100253 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100254 args = [
255 "--chdir",
256 rebase_path(".", root_build_dir),
257 "--stamp",
258 rebase_path(stamp_file, root_build_dir),
259 ]
260 args += [
261 "--path=$nodejs_bin",
262 "node",
263 rebase_path("$nodejs_root/bin/npm", "."),
264 "install",
265 "--no-save",
266 "--silent",
267 ]
268 inputs = [
269 "package.json",
270 "package-lock.json",
271 ]
272 outputs = [
273 stamp_file,
274 ]
275 deps = [
276 ":node_modules_symlink",
277 ]
278}
279
280# Creates a symlink from //ui/dist -> ../../out/xxx/ui. Used only for
281# autocompletion in IDEs. The problem this is solving is that in tsconfig.json
282# we can't possibly know the path to ../../out/xxx for outDir. Instead, we set
283# outDir to "./dist" and create a symlink on the first build.
284action("dist_symlink") {
285 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100286 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100287 args = [
288 "--stamp",
289 rebase_path(stamp_file, root_build_dir),
290 "/bin/ln",
291 "-fns",
Hector Dearman262cbe22018-05-31 13:28:21 +0100292 rebase_path(target_out_dir, "."),
Primiano Tucci3faad742018-05-16 19:30:48 +0100293 rebase_path("dist", root_build_dir),
294 ]
295 inputs = [
296 "$root_build_dir",
297 ]
298 outputs = [
299 stamp_file,
300 ]
301}