blob: 82972c17262a38b4d9a211fde8dc90040adeafbe [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
53 args = [
54 "--path=$nodejs_bin",
55 "node",
56 rebase_path("node_modules/.bin/$_node_cmd", root_build_dir),
57 ] + invoker.args
58 }
59}
60
61# +----------------------------------------------------------------------------+
62# | Bundles all *.js files together resolving CommonJS require() deps. |
63# +----------------------------------------------------------------------------+
64
65# Bundle together all js sources into a bundle.js file, that will ultimately be
66# included by the .html files.
Hector Dearman5f11e522018-05-31 16:54:28 +010067
Hector Dearman262cbe22018-05-31 13:28:21 +010068node_bin("main_bundle") {
Primiano Tucci3faad742018-05-16 19:30:48 +010069 deps = [
70 ":transpile_all_ts",
71 ]
72 node_cmd = "browserify"
73 inputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +010074 "$target_out_dir/main.js",
Primiano Tucci3faad742018-05-16 19:30:48 +010075 ]
76 outputs = [
Hector Dearman5f11e522018-05-31 16:54:28 +010077 "$target_out_dir/main_bundle.js",
78 ]
79 args = [
80 rebase_path(inputs[0], root_build_dir),
81 "-d",
82 "-o",
83 rebase_path(outputs[0], root_build_dir),
84 ]
85}
86
87node_bin("worker_bundle") {
88 deps = [
89 ":transpile_all_ts",
90 ]
91 node_cmd = "browserify"
92 inputs = [
93 "$target_out_dir/worker.js",
94 ]
95 outputs = [
96 "$target_out_dir/worker_bundle.js",
Primiano Tucci3faad742018-05-16 19:30:48 +010097 ]
98 args = [
99 rebase_path(inputs[0], root_build_dir),
100 "-d",
101 "-o",
102 rebase_path(outputs[0], root_build_dir),
103 ]
104}
105
106# +----------------------------------------------------------------------------+
107# | Protobuf: gen rules to create .js and .d.ts files from protos. |
108# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100109proto_gen_dir = "$target_out_dir/gen"
Primiano Tucci3faad742018-05-16 19:30:48 +0100110
111node_bin("protos_to_js") {
112 inputs = []
113 foreach(proto, trace_processor_protos) {
114 inputs += [ "../protos/perfetto/trace_processor/$proto.proto" ]
115 }
116 outputs = [
117 "$proto_gen_dir/protos.js",
118 ]
119 node_cmd = "pbjs"
120 args = [
121 "-t",
122 "static-module",
123 "-w",
124 "commonjs",
125 "-p",
126 rebase_path("../protos", root_build_dir),
127 "-o",
128 rebase_path(outputs[0], root_build_dir),
129 ] + rebase_path(inputs, root_build_dir)
130}
131
132# Protobuf.js requires to first generate .js files from the .proto and then
133# create .ts definitions for them.
134node_bin("protos_to_ts") {
135 deps = [
136 ":protos_to_js",
137 ]
138 inputs = [
139 "$proto_gen_dir/protos.js",
140 ]
141 outputs = [
142 "$proto_gen_dir/protos.d.ts",
143 ]
144 node_cmd = "pbts"
145 args = [
146 "-p",
147 rebase_path("../protos", root_build_dir),
148 "-o",
149 rebase_path(outputs[0], root_build_dir),
150 rebase_path(inputs[0], root_build_dir),
151 ]
152}
153
154# +----------------------------------------------------------------------------+
155# | TypeScript: transpiles all *.ts into .js |
156# +----------------------------------------------------------------------------+
157
158# Builds all .ts sources in the repo that are reachable from |sources|.
159node_bin("transpile_all_ts") {
160 sources = [
Hector Dearman3d858cc2018-05-24 17:09:48 +0100161 "src/main.ts",
Hector Dearman5f11e522018-05-31 16:54:28 +0100162 "src/worker.ts",
Primiano Tucci3faad742018-05-16 19:30:48 +0100163 ]
164 deps = [
165 ":dist_symlink",
166 ":protos_to_ts",
167 ]
168 inputs = sources + [ "tsconfig.json" ]
169 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100170 "$target_out_dir/main.js",
Hector Dearman5f11e522018-05-31 16:54:28 +0100171 "$target_out_dir/worker.js",
Primiano Tucci3faad742018-05-16 19:30:48 +0100172 ]
173
174 # Find all *.ts files and pass them as input triggers. This does NOT affect
175 # which files will get built. This defines only the set of files that, when
176 # changes, will re-trigger this rule in ninja. The files that get transpiled
177 # are the transitive dependencies of |sources|.
178 all_ts_files = exec_script("../gn/standalone/glob.py",
179 [
180 "--root=" + rebase_path(".", root_build_dir),
181 "--filter=*.ts",
182 "--exclude=node_modules",
183 "--exclude=dist",
184 ],
185 "list lines",
186 [ "." ])
187 inputs += all_ts_files
Primiano Tucci3faad742018-05-16 19:30:48 +0100188 node_cmd = "tsc"
189 args = [
190 "--project",
191 rebase_path(".", root_build_dir),
192 "--outDir",
Hector Dearman262cbe22018-05-31 13:28:21 +0100193 rebase_path(target_out_dir, root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100194 ]
195}
196
197# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100198# | Copy rules: create the final output directory. |
Primiano Tucci3faad742018-05-16 19:30:48 +0100199# +----------------------------------------------------------------------------+
Hector Dearman262cbe22018-05-31 13:28:21 +0100200copy("index_release") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100201 sources = [
202 "index.html",
203 ]
204 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100205 "$ui_dir/index.html",
Primiano Tucci3faad742018-05-16 19:30:48 +0100206 ]
207}
208
Hector Dearman262cbe22018-05-31 13:28:21 +0100209copy("main_bundle_release") {
210 deps = [
211 ":main_bundle",
212 ]
213 sources = [
Hector Dearman5f11e522018-05-31 16:54:28 +0100214 "$target_out_dir/main_bundle.js",
Hector Dearman262cbe22018-05-31 13:28:21 +0100215 ]
216 outputs = [
Hector Dearman5f11e522018-05-31 16:54:28 +0100217 "$ui_dir/main_bundle.js",
218 ]
219}
220
221copy("worker_bundle_release") {
222 deps = [
223 ":worker_bundle",
224 ]
225 sources = [
226 "$target_out_dir/worker_bundle.js",
227 ]
228 outputs = [
229 "$ui_dir/worker_bundle.js",
Hector Dearman262cbe22018-05-31 13:28:21 +0100230 ]
231}
232
233copy("wasm_release") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100234 deps = [
235 "//src/trace_processor:trace_processor.js($wasm_toolchain)",
236 "//src/trace_processor:trace_processor.wasm($wasm_toolchain)",
237 ]
238 sources = [
239 "$root_build_dir/wasm/trace_processor.js",
240 "$root_build_dir/wasm/trace_processor.wasm",
241 ]
242 outputs = [
Hector Dearman262cbe22018-05-31 13:28:21 +0100243 "$target_out_dir/wasm/{{source_file_part}}",
Primiano Tucci3faad742018-05-16 19:30:48 +0100244 ]
245}
246
247# +----------------------------------------------------------------------------+
248# | Node JS: run npm install and creates a symlink in the out directory. |
249# +----------------------------------------------------------------------------+
250
251action("check_node_exists") {
252 script = "../gn/standalone/check_buildtool_exists.py"
Hector Dearman48783b12018-05-22 08:26:06 +0100253 args = [
254 nodejs_bin,
255 "--touch",
256 rebase_path("$target_out_dir/node_exists", ""),
257 ]
Primiano Tucci3faad742018-05-16 19:30:48 +0100258 inputs = []
259 outputs = [
Hector Dearman48783b12018-05-22 08:26:06 +0100260 "$target_out_dir/node_exists",
Primiano Tucci3faad742018-05-16 19:30:48 +0100261 ]
262}
263
264# Creates a symlink from out/xxx/ui/node_modules -> ../../../ui/node_modules.
265# This allows to run browserify and other node tools from the out/xxx directory.
266action("node_modules_symlink") {
267 deps = [
268 ":check_node_exists",
269 ]
Hector Dearman6186c872018-05-31 13:28:48 +0100270
Primiano Tucci3faad742018-05-16 19:30:48 +0100271 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman6186c872018-05-31 13:28:48 +0100272 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100273 args = [
Hector Dearman6186c872018-05-31 13:28:48 +0100274 "--stamp",
275 rebase_path(stamp_file, root_build_dir),
Primiano Tucci3faad742018-05-16 19:30:48 +0100276 "/bin/ln",
277 "-fns",
Hector Dearman6186c872018-05-31 13:28:48 +0100278 rebase_path("node_modules", root_build_dir),
279 rebase_path("$target_out_dir/node_modules", root_build_dir),
280 ]
281 outputs = [
282 stamp_file,
Primiano Tucci3faad742018-05-16 19:30:48 +0100283 ]
284}
285
286# Runs npm install.
Hector Dearman6186c872018-05-31 13:28:48 +0100287action("node_modules_install") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100288 script = "../gn/standalone/build_tool_wrapper.py"
Hector Dearman262cbe22018-05-31 13:28:21 +0100289 stamp_file = "$target_out_dir/.$target_name.stamp"
Primiano Tucci3faad742018-05-16 19:30:48 +0100290 args = [
291 "--chdir",
292 rebase_path(".", root_build_dir),
293 "--stamp",
294 rebase_path(stamp_file, root_build_dir),
295 ]
296 args += [
297 "--path=$nodejs_bin",
298 "node",
299 rebase_path("$nodejs_root/bin/npm", "."),
300 "install",
301 "--no-save",
302 "--silent",
303 ]
304 inputs = [
305 "package.json",
306 "package-lock.json",
307 ]
308 outputs = [
309 stamp_file,
310 ]
Hector Dearman6186c872018-05-31 13:28:48 +0100311}
312
313group("node_modules") {
Primiano Tucci3faad742018-05-16 19:30:48 +0100314 deps = [
Hector Dearman6186c872018-05-31 13:28:48 +0100315 ":node_modules_install",
Primiano Tucci3faad742018-05-16 19:30:48 +0100316 ":node_modules_symlink",
317 ]
318}
319
320# Creates a symlink from //ui/dist -> ../../out/xxx/ui. Used only for
321# autocompletion in IDEs. The problem this is solving is that in tsconfig.json
322# we can't possibly know the path to ../../out/xxx for outDir. Instead, we set
323# outDir to "./dist" and create a symlink on the first build.
324action("dist_symlink") {
325 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 "--stamp",
329 rebase_path(stamp_file, root_build_dir),
330 "/bin/ln",
331 "-fns",
Hector Dearman262cbe22018-05-31 13:28:21 +0100332 rebase_path(target_out_dir, "."),
Primiano Tucci3faad742018-05-16 19:30:48 +0100333 rebase_path("dist", root_build_dir),
334 ]
335 inputs = [
336 "$root_build_dir",
337 ]
338 outputs = [
339 stamp_file,
340 ]
341}