blob: 221b0387ec56ab42aca9d61384ad2f81be24a5bc [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# +----------------------------------------------------------------------------+
Hector Dearman92aec392018-06-19 14:33:38 +010069# | Template for "sorcery" the source map resolver. |
70# +----------------------------------------------------------------------------+
71template("sorcery") {
72 node_bin(target_name) {
73 assert(defined(invoker.input))
74 assert(defined(invoker.output))
75 forward_variables_from(invoker, [ "deps" ])
76 inputs = [
77 invoker.input,
78 ]
79 outputs = [
80 invoker.output,
81 invoker.output + ".map",
82 ]
83 node_cmd = "sorcery"
84 args = [
85 "-i",
86 rebase_path(invoker.input, root_build_dir),
87 "-o",
88 rebase_path(invoker.output, root_build_dir),
89 ]
90 }
91}
92
93# +----------------------------------------------------------------------------+
Primiano Tucci3faad742018-05-16 19:30:48 +010094# | Bundles all *.js files together resolving CommonJS require() deps. |
95# +----------------------------------------------------------------------------+
96
97# Bundle together all js sources into a bundle.js file, that will ultimately be
98# included by the .html files.
Hector Dearman5f11e522018-05-31 16:54:28 +010099
Hector Dearman262cbe22018-05-31 13:28:21 +0100100node_bin("main_bundle") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100101 deps = [
102 ":transpile_all_ts",
103 ]
Hector Dearmanb06d4152018-05-31 16:55:04 +0100104 node_cmd = "rollup"
Primiano Tucci3faad742018-05-16 19:30:48 +0100105 inputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100106 "$target_out_dir/main.js",
Hector Dearmanb06d4152018-05-31 16:55:04 +0100107 "rollup.config.js",
Primiano Tucci3faad742018-05-16 19:30:48 +0100108 ]
109 outputs = [
Hector Dearman5f11e522018-05-31 16:54:28 +0100110 "$target_out_dir/main_bundle.js",
111 ]
112 args = [
Hector Dearmanb06d4152018-05-31 16:55:04 +0100113 "-c",
114 rebase_path("rollup.config.js", root_build_dir),
Hector Dearman5f11e522018-05-31 16:54:28 +0100115 rebase_path(inputs[0], root_build_dir),
Hector Dearman5f11e522018-05-31 16:54:28 +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",
Hector Dearman5f11e522018-05-31 16:54:28 +0100122 ]
123}
124
125node_bin("worker_bundle") {
126 deps = [
127 ":transpile_all_ts",
128 ]
Hector Dearmanb06d4152018-05-31 16:55:04 +0100129 node_cmd = "rollup"
Hector Dearman5f11e522018-05-31 16:54:28 +0100130 inputs = [
131 "$target_out_dir/worker.js",
Hector Dearmanb06d4152018-05-31 16:55:04 +0100132 "rollup.config.js",
Hector Dearman5f11e522018-05-31 16:54:28 +0100133 ]
134 outputs = [
135 "$target_out_dir/worker_bundle.js",
Primiano Tucci3faad742018-05-16 19:30:48 +0100136 ]
137 args = [
Hector Dearmanb06d4152018-05-31 16:55:04 +0100138 "-c",
139 rebase_path("rollup.config.js", root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100140 rebase_path(inputs[0], root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100141 "-o",
142 rebase_path(outputs[0], root_build_dir),
Hector Dearmanb06d4152018-05-31 16:55:04 +0100143 "-f",
144 "iife",
145 "-m",
146 "--silent",
Primiano Tucci3faad742018-05-16 19:30:48 +0100147 ]
148}
149
150# +----------------------------------------------------------------------------+
151# | Protobuf: gen rules to create .js and .d.ts files from protos. |
152# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100153proto_gen_dir = "$target_out_dir/gen"
Primiano Tucci3faad742018-05-16 19:30:48 +0100154
155node_bin("protos_to_js") {
156 inputs = []
157 foreach(proto, trace_processor_protos) {
158 inputs += [ "../protos/perfetto/trace_processor/$proto.proto" ]
159 }
Hector Dearmanf5fd1e12018-06-12 18:23:49 +0100160 inputs += [ "../protos/perfetto/config/perfetto_config.proto" ]
Primiano Tucci3faad742018-05-16 19:30:48 +0100161 outputs = [
162 "$proto_gen_dir/protos.js",
163 ]
164 node_cmd = "pbjs"
165 args = [
166 "-t",
167 "static-module",
168 "-w",
169 "commonjs",
170 "-p",
171 rebase_path("../protos", root_build_dir),
172 "-o",
173 rebase_path(outputs[0], root_build_dir),
174 ] + rebase_path(inputs, root_build_dir)
175}
176
177# Protobuf.js requires to first generate .js files from the .proto and then
178# create .ts definitions for them.
179node_bin("protos_to_ts") {
180 deps = [
181 ":protos_to_js",
182 ]
183 inputs = [
184 "$proto_gen_dir/protos.js",
185 ]
186 outputs = [
187 "$proto_gen_dir/protos.d.ts",
188 ]
189 node_cmd = "pbts"
190 args = [
191 "-p",
192 rebase_path("../protos", root_build_dir),
193 "-o",
194 rebase_path(outputs[0], root_build_dir),
195 rebase_path(inputs[0], root_build_dir),
196 ]
197}
198
199# +----------------------------------------------------------------------------+
200# | TypeScript: transpiles all *.ts into .js |
201# +----------------------------------------------------------------------------+
202
203# Builds all .ts sources in the repo that are reachable from |sources|.
204node_bin("transpile_all_ts") {
205 sources = [
Hector Dearman3d858cc2018-05-24 17:09:48 +0100206 "src/main.ts",
Hector Dearman5f11e522018-05-31 16:54:28 +0100207 "src/worker.ts",
Primiano Tucci3faad742018-05-16 19:30:48 +0100208 ]
209 deps = [
210 ":dist_symlink",
211 ":protos_to_ts",
212 ]
213 inputs = sources + [ "tsconfig.json" ]
214 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100215 "$target_out_dir/main.js",
Hector Dearman5f11e522018-05-31 16:54:28 +0100216 "$target_out_dir/worker.js",
Primiano Tucci3faad742018-05-16 19:30:48 +0100217 ]
218
219 # Find all *.ts files and pass them as input triggers. This does NOT affect
220 # which files will get built. This defines only the set of files that, when
221 # changes, will re-trigger this rule in ninja. The files that get transpiled
222 # are the transitive dependencies of |sources|.
223 all_ts_files = exec_script("../gn/standalone/glob.py",
224 [
225 "--root=" + rebase_path(".", root_build_dir),
226 "--filter=*.ts",
227 "--exclude=node_modules",
228 "--exclude=dist",
229 ],
230 "list lines",
231 [ "." ])
232 inputs += all_ts_files
Primiano Tucci3faad742018-05-16 19:30:48 +0100233 node_cmd = "tsc"
234 args = [
235 "--project",
236 rebase_path(".", root_build_dir),
237 "--outDir",
Hector Dearman262cbe22018-05-31 13:28:21 +0100238 rebase_path(target_out_dir, root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100239 ]
240}
241
242# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100243# | Copy rules: create the final output directory. |
Primiano Tucci3faad742018-05-16 19:30:48 +0100244# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100245copy("index_release") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100246 sources = [
247 "index.html",
248 ]
249 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100250 "$ui_dir/index.html",
Primiano Tucci3faad742018-05-16 19:30:48 +0100251 ]
252}
253
Hector Dearman92aec392018-06-19 14:33:38 +0100254sorcery("main_bundle_release") {
Hector Dearman262cbe22018-05-31 13:28:21 +0100255 deps = [
256 ":main_bundle",
257 ]
Hector Dearman92aec392018-06-19 14:33:38 +0100258 input = "$target_out_dir/main_bundle.js"
259 output = "$ui_dir/main_bundle.js"
Hector Dearman5f11e522018-05-31 16:54:28 +0100260}
261
Hector Dearman92aec392018-06-19 14:33:38 +0100262sorcery("worker_bundle_release") {
Hector Dearman5f11e522018-05-31 16:54:28 +0100263 deps = [
264 ":worker_bundle",
265 ]
Hector Dearman92aec392018-06-19 14:33:38 +0100266 input = "$target_out_dir/worker_bundle.js"
267 output = "$ui_dir/worker_bundle.js"
Hector Dearman262cbe22018-05-31 13:28:21 +0100268}
269
270copy("wasm_release") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100271 deps = [
272 "//src/trace_processor:trace_processor.js($wasm_toolchain)",
273 "//src/trace_processor:trace_processor.wasm($wasm_toolchain)",
274 ]
275 sources = [
276 "$root_build_dir/wasm/trace_processor.js",
277 "$root_build_dir/wasm/trace_processor.wasm",
278 ]
279 outputs = [
Hector Dearmana817a062018-06-01 11:01:07 +0100280 "$ui_dir/wasm/{{source_file_part}}",
Primiano Tucci3faad742018-05-16 19:30:48 +0100281 ]
282}
283
284# +----------------------------------------------------------------------------+
285# | Node JS: run npm install and creates a symlink in the out directory. |
286# +----------------------------------------------------------------------------+
287
288action("check_node_exists") {
289 script = "../gn/standalone/check_buildtool_exists.py"
Hector Dearman48783b12018-05-22 08:26:06 +0100290 args = [
291 nodejs_bin,
292 "--touch",
293 rebase_path("$target_out_dir/node_exists", ""),
294 ]
Primiano Tucci3faad742018-05-16 19:30:48 +0100295 inputs = []
296 outputs = [
Hector Dearman48783b12018-05-22 08:26:06 +0100297 "$target_out_dir/node_exists",
Primiano Tucci3faad742018-05-16 19:30:48 +0100298 ]
299}
300
301# Creates a symlink from out/xxx/ui/node_modules -> ../../../ui/node_modules.
Hector Dearmanb06d4152018-05-31 16:55:04 +0100302# This allows to run rollup and other node tools from the out/xxx directory.
Primiano Tucci3faad742018-05-16 19:30:48 +0100303action("node_modules_symlink") {
304 deps = [
305 ":check_node_exists",
306 ]
Hector Dearman6186c872018-05-31 13:28:48 +0100307
Primiano Tucci3faad742018-05-16 19:30:48 +0100308 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman6186c872018-05-31 13:28:48 +0100309 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100310 args = [
Hector Dearman6186c872018-05-31 13:28:48 +0100311 "--stamp",
312 rebase_path(stamp_file, root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100313 "/bin/ln",
314 "-fns",
Hector Dearmana817a062018-06-01 11:01:07 +0100315 rebase_path("node_modules", target_out_dir),
Hector Dearman6186c872018-05-31 13:28:48 +0100316 rebase_path("$target_out_dir/node_modules", root_build_dir),
317 ]
318 outputs = [
319 stamp_file,
Primiano Tucci3faad742018-05-16 19:30:48 +0100320 ]
321}
322
323# Runs npm install.
Hector Dearman6186c872018-05-31 13:28:48 +0100324action("node_modules_install") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100325 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100326 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100327 args = [
328 "--chdir",
329 rebase_path(".", root_build_dir),
330 "--stamp",
331 rebase_path(stamp_file, root_build_dir),
332 ]
333 args += [
334 "--path=$nodejs_bin",
335 "node",
336 rebase_path("$nodejs_root/bin/npm", "."),
337 "install",
338 "--no-save",
339 "--silent",
340 ]
341 inputs = [
342 "package.json",
343 "package-lock.json",
344 ]
345 outputs = [
346 stamp_file,
347 ]
Hector Dearman6186c872018-05-31 13:28:48 +0100348}
349
350group("node_modules") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100351 deps = [
Hector Dearman6186c872018-05-31 13:28:48 +0100352 ":node_modules_install",
Primiano Tucci3faad742018-05-16 19:30:48 +0100353 ":node_modules_symlink",
354 ]
355}
356
357# Creates a symlink from //ui/dist -> ../../out/xxx/ui. Used only for
358# autocompletion in IDEs. The problem this is solving is that in tsconfig.json
359# we can't possibly know the path to ../../out/xxx for outDir. Instead, we set
360# outDir to "./dist" and create a symlink on the first build.
361action("dist_symlink") {
362 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100363 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100364 args = [
365 "--stamp",
366 rebase_path(stamp_file, root_build_dir),
367 "/bin/ln",
368 "-fns",
Hector Dearman262cbe22018-05-31 13:28:21 +0100369 rebase_path(target_out_dir, "."),
Primiano Tucci3faad742018-05-16 19:30:48 +0100370 rebase_path("dist", root_build_dir),
371 ]
372 inputs = [
373 "$root_build_dir",
374 ]
375 outputs = [
376 stamp_file,
377 ]
378}