blob: 168a70236460da4a19cacd0957edbe2e00b7f167 [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 Dearman262cbe22018-05-31 13:28:21 +0100228copy("main_bundle_release") {
229 deps = [
230 ":main_bundle",
231 ]
232 sources = [
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 ]
238}
239
240copy("worker_bundle_release") {
241 deps = [
242 ":worker_bundle",
243 ]
244 sources = [
245 "$target_out_dir/worker_bundle.js",
246 ]
247 outputs = [
248 "$ui_dir/worker_bundle.js",
Hector Dearman262cbe22018-05-31 13:28:21 +0100249 ]
250}
251
252copy("wasm_release") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100253 deps = [
254 "//src/trace_processor:trace_processor.js($wasm_toolchain)",
255 "//src/trace_processor:trace_processor.wasm($wasm_toolchain)",
256 ]
257 sources = [
258 "$root_build_dir/wasm/trace_processor.js",
259 "$root_build_dir/wasm/trace_processor.wasm",
260 ]
261 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100262 "$target_out_dir/wasm/{{source_file_part}}",
Primiano Tucci3faad742018-05-16 19:30:48 +0100263 ]
264}
265
266# +----------------------------------------------------------------------------+
267# | Node JS: run npm install and creates a symlink in the out directory. |
268# +----------------------------------------------------------------------------+
269
270action("check_node_exists") {
271 script = "../gn/standalone/check_buildtool_exists.py"
Hector Dearman48783b12018-05-22 08:26:06 +0100272 args = [
273 nodejs_bin,
274 "--touch",
275 rebase_path("$target_out_dir/node_exists", ""),
276 ]
Primiano Tucci3faad742018-05-16 19:30:48 +0100277 inputs = []
278 outputs = [
Hector Dearman48783b12018-05-22 08:26:06 +0100279 "$target_out_dir/node_exists",
Primiano Tucci3faad742018-05-16 19:30:48 +0100280 ]
281}
282
283# Creates a symlink from out/xxx/ui/node_modules -> ../../../ui/node_modules.
Hector Dearmanb06d4152018-05-31 16:55:04 +0100284# This allows to run rollup and other node tools from the out/xxx directory.
Primiano Tucci3faad742018-05-16 19:30:48 +0100285action("node_modules_symlink") {
286 deps = [
287 ":check_node_exists",
288 ]
Hector Dearman6186c872018-05-31 13:28:48 +0100289
Primiano Tucci3faad742018-05-16 19:30:48 +0100290 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman6186c872018-05-31 13:28:48 +0100291 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100292 args = [
Hector Dearman6186c872018-05-31 13:28:48 +0100293 "--stamp",
294 rebase_path(stamp_file, root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100295 "/bin/ln",
296 "-fns",
Hector Dearman6186c872018-05-31 13:28:48 +0100297 rebase_path("node_modules", root_build_dir),
298 rebase_path("$target_out_dir/node_modules", root_build_dir),
299 ]
300 outputs = [
301 stamp_file,
Primiano Tucci3faad742018-05-16 19:30:48 +0100302 ]
303}
304
305# Runs npm install.
Hector Dearman6186c872018-05-31 13:28:48 +0100306action("node_modules_install") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100307 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100308 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100309 args = [
310 "--chdir",
311 rebase_path(".", root_build_dir),
312 "--stamp",
313 rebase_path(stamp_file, root_build_dir),
314 ]
315 args += [
316 "--path=$nodejs_bin",
317 "node",
318 rebase_path("$nodejs_root/bin/npm", "."),
319 "install",
320 "--no-save",
321 "--silent",
322 ]
323 inputs = [
324 "package.json",
325 "package-lock.json",
326 ]
327 outputs = [
328 stamp_file,
329 ]
Hector Dearman6186c872018-05-31 13:28:48 +0100330}
331
332group("node_modules") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100333 deps = [
Hector Dearman6186c872018-05-31 13:28:48 +0100334 ":node_modules_install",
Primiano Tucci3faad742018-05-16 19:30:48 +0100335 ":node_modules_symlink",
336 ]
337}
338
339# Creates a symlink from //ui/dist -> ../../out/xxx/ui. Used only for
340# autocompletion in IDEs. The problem this is solving is that in tsconfig.json
341# we can't possibly know the path to ../../out/xxx for outDir. Instead, we set
342# outDir to "./dist" and create a symlink on the first build.
343action("dist_symlink") {
344 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100345 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100346 args = [
347 "--stamp",
348 rebase_path(stamp_file, root_build_dir),
349 "/bin/ln",
350 "-fns",
Hector Dearman262cbe22018-05-31 13:28:21 +0100351 rebase_path(target_out_dir, "."),
Primiano Tucci3faad742018-05-16 19:30:48 +0100352 rebase_path("dist", root_build_dir),
353 ]
354 inputs = [
355 "$root_build_dir",
356 ]
357 outputs = [
358 stamp_file,
359 ]
360}