blob: e50fa602a35f0ae4460dc094e37d540182ea1c7b [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",
Hector Dearman5f11e522018-05-31 16:54:28 +010031 ":worker_bundle_release",
Primiano Tucci3faad742018-05-16 19:30:48 +010032 ]
33}
34
35# +----------------------------------------------------------------------------+
36# | Template used to run node binaries using the hermetic node toolchain. |
37# +----------------------------------------------------------------------------+
38template("node_bin") {
39 action(target_name) {
40 forward_variables_from(invoker,
41 [
42 "inputs",
43 "outputs",
44 ])
45 deps = [
46 ":node_modules",
47 ]
48 if (defined(invoker.deps)) {
49 deps += invoker.deps
50 }
51 script = "../gn/standalone/build_tool_wrapper.py"
52 _node_cmd = invoker.node_cmd
Hector Dearmanb06d4152018-05-31 16:55:04 +010053 args = []
54 if (defined(invoker.suppress_stdout) && invoker.suppress_stdout) {
55 args += [ "--suppress_stdout" ]
56 }
57 if (defined(invoker.suppress_stderr) && invoker.suppress_stderr) {
58 args += [ "--suppress_stderr" ]
59 }
60 args += [
61 "--path=$nodejs_bin",
62 "node",
63 rebase_path("node_modules/.bin/$_node_cmd", root_build_dir),
64 ] + invoker.args
Primiano Tucci3faad742018-05-16 19:30:48 +010065 }
66}
67
68# +----------------------------------------------------------------------------+
69# | Bundles all *.js files together resolving CommonJS require() deps. |
70# +----------------------------------------------------------------------------+
71
72# Bundle together all js sources into a bundle.js file, that will ultimately be
73# included by the .html files.
Hector Dearman5f11e522018-05-31 16:54:28 +010074
Hector Dearman262cbe22018-05-31 13:28:21 +010075node_bin("main_bundle") {
Primiano Tucci3faad742018-05-16 19:30:48 +010076 deps = [
77 ":transpile_all_ts",
78 ]
Hector Dearmanb06d4152018-05-31 16:55:04 +010079 node_cmd = "rollup"
Primiano Tucci3faad742018-05-16 19:30:48 +010080 inputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +010081 "$target_out_dir/main.js",
Hector Dearmanb06d4152018-05-31 16:55:04 +010082 "rollup.config.js",
Primiano Tucci3faad742018-05-16 19:30:48 +010083 ]
84 outputs = [
Hector Dearman5f11e522018-05-31 16:54:28 +010085 "$target_out_dir/main_bundle.js",
86 ]
87 args = [
Hector Dearmanb06d4152018-05-31 16:55:04 +010088 "-c",
89 rebase_path("rollup.config.js", root_build_dir),
Hector Dearman5f11e522018-05-31 16:54:28 +010090 rebase_path(inputs[0], root_build_dir),
Hector Dearman5f11e522018-05-31 16:54:28 +010091 "-o",
92 rebase_path(outputs[0], root_build_dir),
Hector Dearmanb06d4152018-05-31 16:55:04 +010093 "-f",
94 "iife",
95 "-m",
96 "--silent",
Hector Dearman5f11e522018-05-31 16:54:28 +010097 ]
98}
99
100node_bin("worker_bundle") {
101 deps = [
102 ":transpile_all_ts",
103 ]
Hector Dearmanb06d4152018-05-31 16:55:04 +0100104 node_cmd = "rollup"
Hector Dearman5f11e522018-05-31 16:54:28 +0100105 inputs = [
106 "$target_out_dir/worker.js",
Hector Dearmanb06d4152018-05-31 16:55:04 +0100107 "rollup.config.js",
Hector Dearman5f11e522018-05-31 16:54:28 +0100108 ]
109 outputs = [
110 "$target_out_dir/worker_bundle.js",
Primiano Tucci3faad742018-05-16 19:30:48 +0100111 ]
112 args = [
Hector Dearmanb06d4152018-05-31 16:55:04 +0100113 "-c",
114 rebase_path("rollup.config.js", root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100115 rebase_path(inputs[0], root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100116 "-o",
117 rebase_path(outputs[0], root_build_dir),
Hector Dearmanb06d4152018-05-31 16:55:04 +0100118 "-f",
119 "iife",
120 "-m",
121 "--silent",
Primiano Tucci3faad742018-05-16 19:30:48 +0100122 ]
123}
124
125# +----------------------------------------------------------------------------+
126# | Protobuf: gen rules to create .js and .d.ts files from protos. |
127# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100128proto_gen_dir = "$target_out_dir/gen"
Primiano Tucci3faad742018-05-16 19:30:48 +0100129
130node_bin("protos_to_js") {
131 inputs = []
132 foreach(proto, trace_processor_protos) {
133 inputs += [ "../protos/perfetto/trace_processor/$proto.proto" ]
134 }
Hector Dearmanf5fd1e12018-06-12 18:23:49 +0100135 inputs += [ "../protos/perfetto/config/perfetto_config.proto" ]
Primiano Tucci3faad742018-05-16 19:30:48 +0100136 outputs = [
137 "$proto_gen_dir/protos.js",
138 ]
139 node_cmd = "pbjs"
140 args = [
141 "-t",
142 "static-module",
143 "-w",
144 "commonjs",
145 "-p",
146 rebase_path("../protos", root_build_dir),
147 "-o",
148 rebase_path(outputs[0], root_build_dir),
149 ] + rebase_path(inputs, root_build_dir)
150}
151
152# Protobuf.js requires to first generate .js files from the .proto and then
153# create .ts definitions for them.
154node_bin("protos_to_ts") {
155 deps = [
156 ":protos_to_js",
157 ]
158 inputs = [
159 "$proto_gen_dir/protos.js",
160 ]
161 outputs = [
162 "$proto_gen_dir/protos.d.ts",
163 ]
164 node_cmd = "pbts"
165 args = [
166 "-p",
167 rebase_path("../protos", root_build_dir),
168 "-o",
169 rebase_path(outputs[0], root_build_dir),
170 rebase_path(inputs[0], root_build_dir),
171 ]
172}
173
174# +----------------------------------------------------------------------------+
175# | TypeScript: transpiles all *.ts into .js |
176# +----------------------------------------------------------------------------+
177
178# Builds all .ts sources in the repo that are reachable from |sources|.
179node_bin("transpile_all_ts") {
180 sources = [
Hector Dearman3d858cc2018-05-24 17:09:48 +0100181 "src/main.ts",
Hector Dearman5f11e522018-05-31 16:54:28 +0100182 "src/worker.ts",
Primiano Tucci3faad742018-05-16 19:30:48 +0100183 ]
184 deps = [
185 ":dist_symlink",
186 ":protos_to_ts",
187 ]
188 inputs = sources + [ "tsconfig.json" ]
189 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100190 "$target_out_dir/main.js",
Hector Dearman5f11e522018-05-31 16:54:28 +0100191 "$target_out_dir/worker.js",
Primiano Tucci3faad742018-05-16 19:30:48 +0100192 ]
193
194 # Find all *.ts files and pass them as input triggers. This does NOT affect
195 # which files will get built. This defines only the set of files that, when
196 # changes, will re-trigger this rule in ninja. The files that get transpiled
197 # are the transitive dependencies of |sources|.
198 all_ts_files = exec_script("../gn/standalone/glob.py",
199 [
200 "--root=" + rebase_path(".", root_build_dir),
201 "--filter=*.ts",
202 "--exclude=node_modules",
203 "--exclude=dist",
204 ],
205 "list lines",
206 [ "." ])
207 inputs += all_ts_files
Primiano Tucci3faad742018-05-16 19:30:48 +0100208 node_cmd = "tsc"
209 args = [
210 "--project",
211 rebase_path(".", root_build_dir),
212 "--outDir",
Hector Dearman262cbe22018-05-31 13:28:21 +0100213 rebase_path(target_out_dir, root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100214 ]
215}
216
217# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100218# | Copy rules: create the final output directory. |
Primiano Tucci3faad742018-05-16 19:30:48 +0100219# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100220copy("index_release") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100221 sources = [
222 "index.html",
223 ]
224 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100225 "$ui_dir/index.html",
Primiano Tucci3faad742018-05-16 19:30:48 +0100226 ]
227}
228
Hector Dearman9bd55832018-05-31 17:33:52 +0100229node_bin("main_bundle_release") {
Hector Dearman262cbe22018-05-31 13:28:21 +0100230 deps = [
231 ":main_bundle",
232 ]
Hector Dearman9bd55832018-05-31 17:33:52 +0100233 inputs = [
Hector Dearman5f11e522018-05-31 16:54:28 +0100234 "$target_out_dir/main_bundle.js",
Hector Dearman262cbe22018-05-31 13:28:21 +0100235 ]
236 outputs = [
Hector Dearman5f11e522018-05-31 16:54:28 +0100237 "$ui_dir/main_bundle.js",
238 ]
Hector Dearman9bd55832018-05-31 17:33:52 +0100239 node_cmd = "sorcery"
240 args = [
241 "-i",
242 rebase_path(inputs[0], root_build_dir),
243 "-o",
244 rebase_path(outputs[0], root_build_dir),
245 ]
Hector Dearman5f11e522018-05-31 16:54:28 +0100246}
247
Hector Dearman9bd55832018-05-31 17:33:52 +0100248node_bin("worker_bundle_release") {
Hector Dearman5f11e522018-05-31 16:54:28 +0100249 deps = [
250 ":worker_bundle",
251 ]
Hector Dearman9bd55832018-05-31 17:33:52 +0100252 inputs = [
Hector Dearman5f11e522018-05-31 16:54:28 +0100253 "$target_out_dir/worker_bundle.js",
254 ]
255 outputs = [
256 "$ui_dir/worker_bundle.js",
Hector Dearman262cbe22018-05-31 13:28:21 +0100257 ]
Hector Dearman9bd55832018-05-31 17:33:52 +0100258 node_cmd = "sorcery"
259 args = [
260 "-i",
261 rebase_path(inputs[0], root_build_dir),
262 "-o",
263 rebase_path(outputs[0], root_build_dir),
264 ]
Hector Dearman262cbe22018-05-31 13:28:21 +0100265}
266
267copy("wasm_release") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100268 deps = [
269 "//src/trace_processor:trace_processor.js($wasm_toolchain)",
270 "//src/trace_processor:trace_processor.wasm($wasm_toolchain)",
271 ]
272 sources = [
273 "$root_build_dir/wasm/trace_processor.js",
274 "$root_build_dir/wasm/trace_processor.wasm",
275 ]
276 outputs = [
Hector Dearmana817a062018-06-01 11:01:07 +0100277 "$ui_dir/wasm/{{source_file_part}}",
Primiano Tucci3faad742018-05-16 19:30:48 +0100278 ]
279}
280
281# +----------------------------------------------------------------------------+
282# | Node JS: run npm install and creates a symlink in the out directory. |
283# +----------------------------------------------------------------------------+
284
285action("check_node_exists") {
286 script = "../gn/standalone/check_buildtool_exists.py"
Hector Dearman48783b12018-05-22 08:26:06 +0100287 args = [
288 nodejs_bin,
289 "--touch",
290 rebase_path("$target_out_dir/node_exists", ""),
291 ]
Primiano Tucci3faad742018-05-16 19:30:48 +0100292 inputs = []
293 outputs = [
Hector Dearman48783b12018-05-22 08:26:06 +0100294 "$target_out_dir/node_exists",
Primiano Tucci3faad742018-05-16 19:30:48 +0100295 ]
296}
297
298# Creates a symlink from out/xxx/ui/node_modules -> ../../../ui/node_modules.
Hector Dearmanb06d4152018-05-31 16:55:04 +0100299# This allows to run rollup and other node tools from the out/xxx directory.
Primiano Tucci3faad742018-05-16 19:30:48 +0100300action("node_modules_symlink") {
301 deps = [
302 ":check_node_exists",
303 ]
Hector Dearman6186c872018-05-31 13:28:48 +0100304
Primiano Tucci3faad742018-05-16 19:30:48 +0100305 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman6186c872018-05-31 13:28:48 +0100306 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100307 args = [
Hector Dearman6186c872018-05-31 13:28:48 +0100308 "--stamp",
309 rebase_path(stamp_file, root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100310 "/bin/ln",
311 "-fns",
Hector Dearmana817a062018-06-01 11:01:07 +0100312 rebase_path("node_modules", target_out_dir),
Hector Dearman6186c872018-05-31 13:28:48 +0100313 rebase_path("$target_out_dir/node_modules", root_build_dir),
314 ]
315 outputs = [
316 stamp_file,
Primiano Tucci3faad742018-05-16 19:30:48 +0100317 ]
318}
319
320# Runs npm install.
Hector Dearman6186c872018-05-31 13:28:48 +0100321action("node_modules_install") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100322 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100323 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100324 args = [
325 "--chdir",
326 rebase_path(".", root_build_dir),
327 "--stamp",
328 rebase_path(stamp_file, root_build_dir),
329 ]
330 args += [
331 "--path=$nodejs_bin",
332 "node",
333 rebase_path("$nodejs_root/bin/npm", "."),
334 "install",
335 "--no-save",
336 "--silent",
337 ]
338 inputs = [
339 "package.json",
340 "package-lock.json",
341 ]
342 outputs = [
343 stamp_file,
344 ]
Hector Dearman6186c872018-05-31 13:28:48 +0100345}
346
347group("node_modules") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100348 deps = [
Hector Dearman6186c872018-05-31 13:28:48 +0100349 ":node_modules_install",
Primiano Tucci3faad742018-05-16 19:30:48 +0100350 ":node_modules_symlink",
351 ]
352}
353
354# Creates a symlink from //ui/dist -> ../../out/xxx/ui. Used only for
355# autocompletion in IDEs. The problem this is solving is that in tsconfig.json
356# we can't possibly know the path to ../../out/xxx for outDir. Instead, we set
357# outDir to "./dist" and create a symlink on the first build.
358action("dist_symlink") {
359 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100360 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100361 args = [
362 "--stamp",
363 rebase_path(stamp_file, root_build_dir),
364 "/bin/ln",
365 "-fns",
Hector Dearman262cbe22018-05-31 13:28:21 +0100366 rebase_path(target_out_dir, "."),
Primiano Tucci3faad742018-05-16 19:30:48 +0100367 rebase_path("dist", root_build_dir),
368 ]
369 inputs = [
370 "$root_build_dir",
371 ]
372 outputs = [
373 stamp_file,
374 ]
375}