blob: 15e5acca436844abe9d5be2fab1d421f2ad82696 [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"
Hector Dearman21fa9162018-06-22 14:50:29 +010020ui_gen_dir = "$target_out_dir/gen"
Primiano Tucci3faad742018-05-16 19:30:48 +010021nodejs_root = "../buildtools/nodejs"
22nodejs_bin = rebase_path("$nodejs_root/bin", root_build_dir)
23
24# +----------------------------------------------------------------------------+
25# | The outer "ui" target to just ninja -C out/xxx ui |
26# +----------------------------------------------------------------------------+
27group("ui") {
28 deps = [
Hector Dearman10641512018-07-04 10:38:53 +010029 ":index_dist",
30 ":main_bundle_dist",
Hector Dearmanbf384922018-06-25 10:42:01 +010031 ":test_scripts",
Hector Dearman10641512018-07-04 10:38:53 +010032 ":wasm_bundle_dist",
33 ":wasm_dist",
34 ":worker_bundle_dist",
Primiano Tucci3faad742018-05-16 19:30:48 +010035 ]
36}
37
38# +----------------------------------------------------------------------------+
39# | Template used to run node binaries using the hermetic node toolchain. |
40# +----------------------------------------------------------------------------+
41template("node_bin") {
42 action(target_name) {
43 forward_variables_from(invoker,
44 [
45 "inputs",
46 "outputs",
47 ])
48 deps = [
49 ":node_modules",
50 ]
51 if (defined(invoker.deps)) {
52 deps += invoker.deps
53 }
54 script = "../gn/standalone/build_tool_wrapper.py"
55 _node_cmd = invoker.node_cmd
Hector Dearmanb06d4152018-05-31 16:55:04 +010056 args = []
57 if (defined(invoker.suppress_stdout) && invoker.suppress_stdout) {
58 args += [ "--suppress_stdout" ]
59 }
60 if (defined(invoker.suppress_stderr) && invoker.suppress_stderr) {
61 args += [ "--suppress_stderr" ]
62 }
63 args += [
64 "--path=$nodejs_bin",
65 "node",
66 rebase_path("node_modules/.bin/$_node_cmd", root_build_dir),
67 ] + invoker.args
Primiano Tucci3faad742018-05-16 19:30:48 +010068 }
69}
70
71# +----------------------------------------------------------------------------+
Hector Dearman92aec392018-06-19 14:33:38 +010072# | Template for "sorcery" the source map resolver. |
73# +----------------------------------------------------------------------------+
74template("sorcery") {
75 node_bin(target_name) {
76 assert(defined(invoker.input))
77 assert(defined(invoker.output))
78 forward_variables_from(invoker, [ "deps" ])
79 inputs = [
80 invoker.input,
81 ]
82 outputs = [
83 invoker.output,
84 invoker.output + ".map",
85 ]
86 node_cmd = "sorcery"
87 args = [
88 "-i",
89 rebase_path(invoker.input, root_build_dir),
90 "-o",
91 rebase_path(invoker.output, root_build_dir),
92 ]
93 }
94}
95
96# +----------------------------------------------------------------------------+
Hector Dearman6999adc2018-06-20 12:07:14 +010097# | Template for bundling js |
98# +----------------------------------------------------------------------------+
99template("bundle") {
100 node_bin(target_name) {
101 assert(defined(invoker.input))
102 assert(defined(invoker.output))
103 forward_variables_from(invoker, [ "deps" ])
104 inputs = [
105 invoker.input,
106 "rollup.config.js",
107 ]
108 outputs = [
109 invoker.output,
110 invoker.output + ".map",
111 ]
112 node_cmd = "rollup"
113 args = [
114 "-c",
115 rebase_path("rollup.config.js", root_build_dir),
116 rebase_path(invoker.input, root_build_dir),
117 "-o",
118 rebase_path(invoker.output, root_build_dir),
119 "-f",
120 "iife",
121 "-m",
122 "--silent",
123 ]
124 }
125}
126
127# +----------------------------------------------------------------------------+
Primiano Tucci3faad742018-05-16 19:30:48 +0100128# | Bundles all *.js files together resolving CommonJS require() deps. |
129# +----------------------------------------------------------------------------+
130
131# Bundle together all js sources into a bundle.js file, that will ultimately be
132# included by the .html files.
Hector Dearman5f11e522018-05-31 16:54:28 +0100133
Hector Dearman6999adc2018-06-20 12:07:14 +0100134bundle("main_bundle") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100135 deps = [
136 ":transpile_all_ts",
137 ]
Hector Dearman6999adc2018-06-20 12:07:14 +0100138 input = "$target_out_dir/main.js"
139 output = "$target_out_dir/main_bundle.js"
Hector Dearman5f11e522018-05-31 16:54:28 +0100140}
141
Hector Dearman6999adc2018-06-20 12:07:14 +0100142bundle("worker_bundle") {
Hector Dearman5f11e522018-05-31 16:54:28 +0100143 deps = [
144 ":transpile_all_ts",
145 ]
Hector Dearman6999adc2018-06-20 12:07:14 +0100146 input = "$target_out_dir/worker.js"
147 output = "$target_out_dir/worker_bundle.js"
Primiano Tucci3faad742018-05-16 19:30:48 +0100148}
149
Hector Dearman21fa9162018-06-22 14:50:29 +0100150bundle("wasm_bundle") {
151 deps = [
152 ":transpile_all_ts",
153 ]
154 input = "$target_out_dir/wasm.js"
155 output = "$target_out_dir/wasm_bundle.js"
156}
157
Primiano Tucci3faad742018-05-16 19:30:48 +0100158# +----------------------------------------------------------------------------+
159# | Protobuf: gen rules to create .js and .d.ts files from protos. |
160# +----------------------------------------------------------------------------+
Primiano Tucci3faad742018-05-16 19:30:48 +0100161node_bin("protos_to_js") {
162 inputs = []
163 foreach(proto, trace_processor_protos) {
164 inputs += [ "../protos/perfetto/trace_processor/$proto.proto" ]
165 }
Hector Dearmanf5fd1e12018-06-12 18:23:49 +0100166 inputs += [ "../protos/perfetto/config/perfetto_config.proto" ]
Primiano Tucci3faad742018-05-16 19:30:48 +0100167 outputs = [
Hector Dearman21fa9162018-06-22 14:50:29 +0100168 "$ui_gen_dir/protos.js",
Primiano Tucci3faad742018-05-16 19:30:48 +0100169 ]
170 node_cmd = "pbjs"
171 args = [
172 "-t",
173 "static-module",
174 "-w",
175 "commonjs",
176 "-p",
177 rebase_path("../protos", root_build_dir),
178 "-o",
179 rebase_path(outputs[0], root_build_dir),
180 ] + rebase_path(inputs, root_build_dir)
181}
182
183# Protobuf.js requires to first generate .js files from the .proto and then
184# create .ts definitions for them.
185node_bin("protos_to_ts") {
186 deps = [
187 ":protos_to_js",
188 ]
189 inputs = [
Hector Dearman21fa9162018-06-22 14:50:29 +0100190 "$ui_gen_dir/protos.js",
Primiano Tucci3faad742018-05-16 19:30:48 +0100191 ]
192 outputs = [
Hector Dearman21fa9162018-06-22 14:50:29 +0100193 "$ui_gen_dir/protos.d.ts",
Primiano Tucci3faad742018-05-16 19:30:48 +0100194 ]
195 node_cmd = "pbts"
196 args = [
197 "-p",
198 rebase_path("../protos", root_build_dir),
199 "-o",
200 rebase_path(outputs[0], root_build_dir),
201 rebase_path(inputs[0], root_build_dir),
202 ]
203}
204
205# +----------------------------------------------------------------------------+
206# | TypeScript: transpiles all *.ts into .js |
207# +----------------------------------------------------------------------------+
208
209# Builds all .ts sources in the repo that are reachable from |sources|.
210node_bin("transpile_all_ts") {
211 sources = [
Hector Dearman3d858cc2018-05-24 17:09:48 +0100212 "src/main.ts",
Hector Dearman21fa9162018-06-22 14:50:29 +0100213 "src/wasm.ts",
Hector Dearman5f11e522018-05-31 16:54:28 +0100214 "src/worker.ts",
Primiano Tucci3faad742018-05-16 19:30:48 +0100215 ]
216 deps = [
217 ":dist_symlink",
218 ":protos_to_ts",
Hector Dearman21fa9162018-06-22 14:50:29 +0100219 ":wasm_gen",
Primiano Tucci3faad742018-05-16 19:30:48 +0100220 ]
221 inputs = sources + [ "tsconfig.json" ]
222 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100223 "$target_out_dir/main.js",
Hector Dearman5f11e522018-05-31 16:54:28 +0100224 "$target_out_dir/worker.js",
Hector Dearman21fa9162018-06-22 14:50:29 +0100225 "$target_out_dir/wasm.js",
Primiano Tucci3faad742018-05-16 19:30:48 +0100226 ]
227
228 # Find all *.ts files and pass them as input triggers. This does NOT affect
229 # which files will get built. This defines only the set of files that, when
230 # changes, will re-trigger this rule in ninja. The files that get transpiled
231 # are the transitive dependencies of |sources|.
232 all_ts_files = exec_script("../gn/standalone/glob.py",
233 [
234 "--root=" + rebase_path(".", root_build_dir),
235 "--filter=*.ts",
236 "--exclude=node_modules",
237 "--exclude=dist",
238 ],
239 "list lines",
240 [ "." ])
241 inputs += all_ts_files
Primiano Tucci3faad742018-05-16 19:30:48 +0100242 node_cmd = "tsc"
243 args = [
244 "--project",
245 rebase_path(".", root_build_dir),
246 "--outDir",
Hector Dearman262cbe22018-05-31 13:28:21 +0100247 rebase_path(target_out_dir, root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100248 ]
249}
250
251# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100252# | Copy rules: create the final output directory. |
Primiano Tucci3faad742018-05-16 19:30:48 +0100253# +----------------------------------------------------------------------------+
Hector Dearman10641512018-07-04 10:38:53 +0100254copy("index_dist") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100255 sources = [
256 "index.html",
257 ]
258 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100259 "$ui_dir/index.html",
Primiano Tucci3faad742018-05-16 19:30:48 +0100260 ]
261}
262
Hector Dearman10641512018-07-04 10:38:53 +0100263sorcery("main_bundle_dist") {
Hector Dearman262cbe22018-05-31 13:28:21 +0100264 deps = [
265 ":main_bundle",
266 ]
Hector Dearman92aec392018-06-19 14:33:38 +0100267 input = "$target_out_dir/main_bundle.js"
268 output = "$ui_dir/main_bundle.js"
Hector Dearman5f11e522018-05-31 16:54:28 +0100269}
270
Hector Dearman10641512018-07-04 10:38:53 +0100271sorcery("worker_bundle_dist") {
Hector Dearman5f11e522018-05-31 16:54:28 +0100272 deps = [
273 ":worker_bundle",
274 ]
Hector Dearman92aec392018-06-19 14:33:38 +0100275 input = "$target_out_dir/worker_bundle.js"
276 output = "$ui_dir/worker_bundle.js"
Hector Dearman262cbe22018-05-31 13:28:21 +0100277}
278
Hector Dearman10641512018-07-04 10:38:53 +0100279sorcery("wasm_bundle_dist") {
Hector Dearman21fa9162018-06-22 14:50:29 +0100280 deps = [
281 ":wasm_bundle",
282 ]
283 input = "$target_out_dir/wasm_bundle.js"
284 output = "$ui_dir/wasm_bundle.js"
285}
286
Hector Dearman10641512018-07-04 10:38:53 +0100287copy("wasm_dist") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100288 deps = [
Hector Dearman21fa9162018-06-22 14:50:29 +0100289 "//src/trace_processor:trace_processor.wasm($wasm_toolchain)",
290 ]
291 sources = [
292 "$root_build_dir/wasm/trace_processor.wasm",
293 ]
294 outputs = [
295 "$ui_dir/{{source_file_part}}",
296 ]
297}
298
299copy("wasm_gen") {
300 deps = [
301 ":dist_symlink",
302 "//src/trace_processor:trace_processor.d.ts($wasm_toolchain)",
Primiano Tucci3faad742018-05-16 19:30:48 +0100303 "//src/trace_processor:trace_processor.js($wasm_toolchain)",
304 "//src/trace_processor:trace_processor.wasm($wasm_toolchain)",
305 ]
306 sources = [
Hector Dearman21fa9162018-06-22 14:50:29 +0100307 "$root_build_dir/wasm/trace_processor.d.ts",
Primiano Tucci3faad742018-05-16 19:30:48 +0100308 "$root_build_dir/wasm/trace_processor.js",
309 "$root_build_dir/wasm/trace_processor.wasm",
310 ]
311 outputs = [
Hector Dearman21fa9162018-06-22 14:50:29 +0100312 "$ui_gen_dir/{{source_file_part}}",
Primiano Tucci3faad742018-05-16 19:30:48 +0100313 ]
314}
315
316# +----------------------------------------------------------------------------+
317# | Node JS: run npm install and creates a symlink in the out directory. |
318# +----------------------------------------------------------------------------+
319
320action("check_node_exists") {
321 script = "../gn/standalone/check_buildtool_exists.py"
Hector Dearman48783b12018-05-22 08:26:06 +0100322 args = [
323 nodejs_bin,
324 "--touch",
325 rebase_path("$target_out_dir/node_exists", ""),
326 ]
Primiano Tucci3faad742018-05-16 19:30:48 +0100327 inputs = []
328 outputs = [
Hector Dearman48783b12018-05-22 08:26:06 +0100329 "$target_out_dir/node_exists",
Primiano Tucci3faad742018-05-16 19:30:48 +0100330 ]
331}
332
333# Creates a symlink from out/xxx/ui/node_modules -> ../../../ui/node_modules.
Hector Dearmanb06d4152018-05-31 16:55:04 +0100334# This allows to run rollup and other node tools from the out/xxx directory.
Primiano Tucci3faad742018-05-16 19:30:48 +0100335action("node_modules_symlink") {
336 deps = [
337 ":check_node_exists",
338 ]
Hector Dearman6186c872018-05-31 13:28:48 +0100339
Primiano Tucci3faad742018-05-16 19:30:48 +0100340 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman6186c872018-05-31 13:28:48 +0100341 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100342 args = [
Hector Dearman6186c872018-05-31 13:28:48 +0100343 "--stamp",
344 rebase_path(stamp_file, root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100345 "/bin/ln",
346 "-fns",
Hector Dearmana817a062018-06-01 11:01:07 +0100347 rebase_path("node_modules", target_out_dir),
Hector Dearman6186c872018-05-31 13:28:48 +0100348 rebase_path("$target_out_dir/node_modules", root_build_dir),
349 ]
350 outputs = [
351 stamp_file,
Primiano Tucci3faad742018-05-16 19:30:48 +0100352 ]
353}
354
355# Runs npm install.
Hector Dearman6186c872018-05-31 13:28:48 +0100356action("node_modules_install") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100357 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100358 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100359 args = [
360 "--chdir",
361 rebase_path(".", root_build_dir),
362 "--stamp",
363 rebase_path(stamp_file, root_build_dir),
364 ]
365 args += [
366 "--path=$nodejs_bin",
367 "node",
368 rebase_path("$nodejs_root/bin/npm", "."),
369 "install",
370 "--no-save",
371 "--silent",
372 ]
373 inputs = [
374 "package.json",
375 "package-lock.json",
376 ]
377 outputs = [
378 stamp_file,
379 ]
Hector Dearman6186c872018-05-31 13:28:48 +0100380}
381
382group("node_modules") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100383 deps = [
Hector Dearman6186c872018-05-31 13:28:48 +0100384 ":node_modules_install",
Primiano Tucci3faad742018-05-16 19:30:48 +0100385 ":node_modules_symlink",
386 ]
387}
388
389# Creates a symlink from //ui/dist -> ../../out/xxx/ui. Used only for
390# autocompletion in IDEs. The problem this is solving is that in tsconfig.json
391# we can't possibly know the path to ../../out/xxx for outDir. Instead, we set
392# outDir to "./dist" and create a symlink on the first build.
393action("dist_symlink") {
394 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100395 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100396 args = [
397 "--stamp",
398 rebase_path(stamp_file, root_build_dir),
399 "/bin/ln",
400 "-fns",
Hector Dearman262cbe22018-05-31 13:28:21 +0100401 rebase_path(target_out_dir, "."),
Primiano Tucci3faad742018-05-16 19:30:48 +0100402 rebase_path("dist", root_build_dir),
403 ]
404 inputs = [
405 "$root_build_dir",
406 ]
407 outputs = [
408 stamp_file,
409 ]
410}
Hector Dearmanbf384922018-06-25 10:42:01 +0100411
412group("test_scripts") {
413 deps = [
414 ":copy_tests_script",
415 ":copy_unittests_script",
416 ]
417}
418
419copy("copy_unittests_script") {
420 sources = [
421 "config/ui_unittests_template",
422 ]
423 outputs = [
424 "$root_build_dir/ui_unittests",
425 ]
426}
427
428copy("copy_tests_script") {
429 sources = [
430 "config/ui_tests_template",
431 ]
432 outputs = [
433 "$root_build_dir/ui_tests",
434 ]
435}