blob: f128c4a94386acb968d3c744dacbf9b37c765e94 [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 }
135 outputs = [
136 "$proto_gen_dir/protos.js",
137 ]
138 node_cmd = "pbjs"
139 args = [
140 "-t",
141 "static-module",
142 "-w",
143 "commonjs",
144 "-p",
145 rebase_path("../protos", root_build_dir),
146 "-o",
147 rebase_path(outputs[0], root_build_dir),
148 ] + rebase_path(inputs, root_build_dir)
149}
150
151# Protobuf.js requires to first generate .js files from the .proto and then
152# create .ts definitions for them.
153node_bin("protos_to_ts") {
154 deps = [
155 ":protos_to_js",
156 ]
157 inputs = [
158 "$proto_gen_dir/protos.js",
159 ]
160 outputs = [
161 "$proto_gen_dir/protos.d.ts",
162 ]
163 node_cmd = "pbts"
164 args = [
165 "-p",
166 rebase_path("../protos", root_build_dir),
167 "-o",
168 rebase_path(outputs[0], root_build_dir),
169 rebase_path(inputs[0], root_build_dir),
170 ]
171}
172
173# +----------------------------------------------------------------------------+
174# | TypeScript: transpiles all *.ts into .js |
175# +----------------------------------------------------------------------------+
176
177# Builds all .ts sources in the repo that are reachable from |sources|.
178node_bin("transpile_all_ts") {
179 sources = [
Hector Dearman3d858cc2018-05-24 17:09:48 +0100180 "src/main.ts",
Hector Dearman5f11e522018-05-31 16:54:28 +0100181 "src/worker.ts",
Primiano Tucci3faad742018-05-16 19:30:48 +0100182 ]
183 deps = [
184 ":dist_symlink",
185 ":protos_to_ts",
186 ]
187 inputs = sources + [ "tsconfig.json" ]
188 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100189 "$target_out_dir/main.js",
Hector Dearman5f11e522018-05-31 16:54:28 +0100190 "$target_out_dir/worker.js",
Primiano Tucci3faad742018-05-16 19:30:48 +0100191 ]
192
193 # Find all *.ts files and pass them as input triggers. This does NOT affect
194 # which files will get built. This defines only the set of files that, when
195 # changes, will re-trigger this rule in ninja. The files that get transpiled
196 # are the transitive dependencies of |sources|.
197 all_ts_files = exec_script("../gn/standalone/glob.py",
198 [
199 "--root=" + rebase_path(".", root_build_dir),
200 "--filter=*.ts",
201 "--exclude=node_modules",
202 "--exclude=dist",
203 ],
204 "list lines",
205 [ "." ])
206 inputs += all_ts_files
Primiano Tucci3faad742018-05-16 19:30:48 +0100207 node_cmd = "tsc"
208 args = [
209 "--project",
210 rebase_path(".", root_build_dir),
211 "--outDir",
Hector Dearman262cbe22018-05-31 13:28:21 +0100212 rebase_path(target_out_dir, root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100213 ]
214}
215
216# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100217# | Copy rules: create the final output directory. |
Primiano Tucci3faad742018-05-16 19:30:48 +0100218# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100219copy("index_release") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100220 sources = [
221 "index.html",
222 ]
223 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100224 "$ui_dir/index.html",
Primiano Tucci3faad742018-05-16 19:30:48 +0100225 ]
226}
227
Hector Dearman9bd55832018-05-31 17:33:52 +0100228node_bin("main_bundle_release") {
Hector Dearman262cbe22018-05-31 13:28:21 +0100229 deps = [
230 ":main_bundle",
231 ]
Hector Dearman9bd55832018-05-31 17:33:52 +0100232 inputs = [
Hector Dearman5f11e522018-05-31 16:54:28 +0100233 "$target_out_dir/main_bundle.js",
Hector Dearman262cbe22018-05-31 13:28:21 +0100234 ]
235 outputs = [
Hector Dearman5f11e522018-05-31 16:54:28 +0100236 "$ui_dir/main_bundle.js",
237 ]
Hector Dearman9bd55832018-05-31 17:33:52 +0100238 node_cmd = "sorcery"
239 args = [
240 "-i",
241 rebase_path(inputs[0], root_build_dir),
242 "-o",
243 rebase_path(outputs[0], root_build_dir),
244 ]
Hector Dearman5f11e522018-05-31 16:54:28 +0100245}
246
Hector Dearman9bd55832018-05-31 17:33:52 +0100247node_bin("worker_bundle_release") {
Hector Dearman5f11e522018-05-31 16:54:28 +0100248 deps = [
249 ":worker_bundle",
250 ]
Hector Dearman9bd55832018-05-31 17:33:52 +0100251 inputs = [
Hector Dearman5f11e522018-05-31 16:54:28 +0100252 "$target_out_dir/worker_bundle.js",
253 ]
254 outputs = [
255 "$ui_dir/worker_bundle.js",
Hector Dearman262cbe22018-05-31 13:28:21 +0100256 ]
Hector Dearman9bd55832018-05-31 17:33:52 +0100257 node_cmd = "sorcery"
258 args = [
259 "-i",
260 rebase_path(inputs[0], root_build_dir),
261 "-o",
262 rebase_path(outputs[0], root_build_dir),
263 ]
Hector Dearman262cbe22018-05-31 13:28:21 +0100264}
265
266copy("wasm_release") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100267 deps = [
268 "//src/trace_processor:trace_processor.js($wasm_toolchain)",
269 "//src/trace_processor:trace_processor.wasm($wasm_toolchain)",
270 ]
271 sources = [
272 "$root_build_dir/wasm/trace_processor.js",
273 "$root_build_dir/wasm/trace_processor.wasm",
274 ]
275 outputs = [
Hector Dearmana817a062018-06-01 11:01:07 +0100276 "$ui_dir/wasm/{{source_file_part}}",
Primiano Tucci3faad742018-05-16 19:30:48 +0100277 ]
278}
279
280# +----------------------------------------------------------------------------+
281# | Node JS: run npm install and creates a symlink in the out directory. |
282# +----------------------------------------------------------------------------+
283
284action("check_node_exists") {
285 script = "../gn/standalone/check_buildtool_exists.py"
Hector Dearman48783b12018-05-22 08:26:06 +0100286 args = [
287 nodejs_bin,
288 "--touch",
289 rebase_path("$target_out_dir/node_exists", ""),
290 ]
Primiano Tucci3faad742018-05-16 19:30:48 +0100291 inputs = []
292 outputs = [
Hector Dearman48783b12018-05-22 08:26:06 +0100293 "$target_out_dir/node_exists",
Primiano Tucci3faad742018-05-16 19:30:48 +0100294 ]
295}
296
297# Creates a symlink from out/xxx/ui/node_modules -> ../../../ui/node_modules.
Hector Dearmanb06d4152018-05-31 16:55:04 +0100298# This allows to run rollup and other node tools from the out/xxx directory.
Primiano Tucci3faad742018-05-16 19:30:48 +0100299action("node_modules_symlink") {
300 deps = [
301 ":check_node_exists",
302 ]
Hector Dearman6186c872018-05-31 13:28:48 +0100303
Primiano Tucci3faad742018-05-16 19:30:48 +0100304 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman6186c872018-05-31 13:28:48 +0100305 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100306 args = [
Hector Dearman6186c872018-05-31 13:28:48 +0100307 "--stamp",
308 rebase_path(stamp_file, root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100309 "/bin/ln",
310 "-fns",
Hector Dearmana817a062018-06-01 11:01:07 +0100311 rebase_path("node_modules", target_out_dir),
Hector Dearman6186c872018-05-31 13:28:48 +0100312 rebase_path("$target_out_dir/node_modules", root_build_dir),
313 ]
314 outputs = [
315 stamp_file,
Primiano Tucci3faad742018-05-16 19:30:48 +0100316 ]
317}
318
319# Runs npm install.
Hector Dearman6186c872018-05-31 13:28:48 +0100320action("node_modules_install") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100321 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100322 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100323 args = [
324 "--chdir",
325 rebase_path(".", root_build_dir),
326 "--stamp",
327 rebase_path(stamp_file, root_build_dir),
328 ]
329 args += [
330 "--path=$nodejs_bin",
331 "node",
332 rebase_path("$nodejs_root/bin/npm", "."),
333 "install",
334 "--no-save",
335 "--silent",
336 ]
337 inputs = [
338 "package.json",
339 "package-lock.json",
340 ]
341 outputs = [
342 stamp_file,
343 ]
Hector Dearman6186c872018-05-31 13:28:48 +0100344}
345
346group("node_modules") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100347 deps = [
Hector Dearman6186c872018-05-31 13:28:48 +0100348 ":node_modules_install",
Primiano Tucci3faad742018-05-16 19:30:48 +0100349 ":node_modules_symlink",
350 ]
351}
352
353# Creates a symlink from //ui/dist -> ../../out/xxx/ui. Used only for
354# autocompletion in IDEs. The problem this is solving is that in tsconfig.json
355# we can't possibly know the path to ../../out/xxx for outDir. Instead, we set
356# outDir to "./dist" and create a symlink on the first build.
357action("dist_symlink") {
358 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100359 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100360 args = [
361 "--stamp",
362 rebase_path(stamp_file, root_build_dir),
363 "/bin/ln",
364 "-fns",
Hector Dearman262cbe22018-05-31 13:28:21 +0100365 rebase_path(target_out_dir, "."),
Primiano Tucci3faad742018-05-16 19:30:48 +0100366 rebase_path("dist", root_build_dir),
367 ]
368 inputs = [
369 "$root_build_dir",
370 ]
371 outputs = [
372 stamp_file,
373 ]
374}