Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 1 | # 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 | |
| 15 | import("../gn/perfetto.gni") |
| 16 | import("../gn/wasm.gni") |
| 17 | import("../protos/perfetto/trace_processor/proto_files.gni") |
| 18 | |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 19 | ui_dir = "$root_build_dir/ui" |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 20 | nodejs_root = "../buildtools/nodejs" |
| 21 | nodejs_bin = rebase_path("$nodejs_root/bin", root_build_dir) |
| 22 | |
| 23 | # +----------------------------------------------------------------------------+ |
| 24 | # | The outer "ui" target to just ninja -C out/xxx ui | |
| 25 | # +----------------------------------------------------------------------------+ |
| 26 | group("ui") { |
| 27 | deps = [ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 28 | ":index_release", |
| 29 | ":main_bundle_release", |
| 30 | ":wasm_release", |
Hector Dearman | 5f11e52 | 2018-05-31 16:54:28 +0100 | [diff] [blame^] | 31 | ":worker_bundle_release", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 32 | ] |
| 33 | } |
| 34 | |
| 35 | # +----------------------------------------------------------------------------+ |
| 36 | # | Template used to run node binaries using the hermetic node toolchain. | |
| 37 | # +----------------------------------------------------------------------------+ |
| 38 | template("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 Dearman | 5f11e52 | 2018-05-31 16:54:28 +0100 | [diff] [blame^] | 67 | |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 68 | node_bin("main_bundle") { |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 69 | deps = [ |
| 70 | ":transpile_all_ts", |
| 71 | ] |
| 72 | node_cmd = "browserify" |
| 73 | inputs = [ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 74 | "$target_out_dir/main.js", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 75 | ] |
| 76 | outputs = [ |
Hector Dearman | 5f11e52 | 2018-05-31 16:54:28 +0100 | [diff] [blame^] | 77 | "$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 | |
| 87 | node_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 Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 97 | ] |
| 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 Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 109 | proto_gen_dir = "$target_out_dir/gen" |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 110 | |
| 111 | node_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. |
| 134 | node_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|. |
| 159 | node_bin("transpile_all_ts") { |
| 160 | sources = [ |
Hector Dearman | 3d858cc | 2018-05-24 17:09:48 +0100 | [diff] [blame] | 161 | "src/main.ts", |
Hector Dearman | 5f11e52 | 2018-05-31 16:54:28 +0100 | [diff] [blame^] | 162 | "src/worker.ts", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 163 | ] |
| 164 | deps = [ |
| 165 | ":dist_symlink", |
| 166 | ":protos_to_ts", |
| 167 | ] |
| 168 | inputs = sources + [ "tsconfig.json" ] |
| 169 | outputs = [ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 170 | "$target_out_dir/main.js", |
Hector Dearman | 5f11e52 | 2018-05-31 16:54:28 +0100 | [diff] [blame^] | 171 | "$target_out_dir/worker.js", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 172 | ] |
| 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 Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 188 | node_cmd = "tsc" |
| 189 | args = [ |
| 190 | "--project", |
| 191 | rebase_path(".", root_build_dir), |
| 192 | "--outDir", |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 193 | rebase_path(target_out_dir, root_build_dir), |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 194 | ] |
| 195 | } |
| 196 | |
| 197 | # +----------------------------------------------------------------------------+ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 198 | # | Copy rules: create the final output directory. | |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 199 | # +----------------------------------------------------------------------------+ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 200 | copy("index_release") { |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 201 | sources = [ |
| 202 | "index.html", |
| 203 | ] |
| 204 | outputs = [ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 205 | "$ui_dir/index.html", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 206 | ] |
| 207 | } |
| 208 | |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 209 | copy("main_bundle_release") { |
| 210 | deps = [ |
| 211 | ":main_bundle", |
| 212 | ] |
| 213 | sources = [ |
Hector Dearman | 5f11e52 | 2018-05-31 16:54:28 +0100 | [diff] [blame^] | 214 | "$target_out_dir/main_bundle.js", |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 215 | ] |
| 216 | outputs = [ |
Hector Dearman | 5f11e52 | 2018-05-31 16:54:28 +0100 | [diff] [blame^] | 217 | "$ui_dir/main_bundle.js", |
| 218 | ] |
| 219 | } |
| 220 | |
| 221 | copy("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 Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 230 | ] |
| 231 | } |
| 232 | |
| 233 | copy("wasm_release") { |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 234 | 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 Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 243 | "$target_out_dir/wasm/{{source_file_part}}", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 244 | ] |
| 245 | } |
| 246 | |
| 247 | # +----------------------------------------------------------------------------+ |
| 248 | # | Node JS: run npm install and creates a symlink in the out directory. | |
| 249 | # +----------------------------------------------------------------------------+ |
| 250 | |
| 251 | action("check_node_exists") { |
| 252 | script = "../gn/standalone/check_buildtool_exists.py" |
Hector Dearman | 48783b1 | 2018-05-22 08:26:06 +0100 | [diff] [blame] | 253 | args = [ |
| 254 | nodejs_bin, |
| 255 | "--touch", |
| 256 | rebase_path("$target_out_dir/node_exists", ""), |
| 257 | ] |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 258 | inputs = [] |
| 259 | outputs = [ |
Hector Dearman | 48783b1 | 2018-05-22 08:26:06 +0100 | [diff] [blame] | 260 | "$target_out_dir/node_exists", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 261 | ] |
| 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. |
| 266 | action("node_modules_symlink") { |
| 267 | deps = [ |
| 268 | ":check_node_exists", |
| 269 | ] |
Hector Dearman | 6186c87 | 2018-05-31 13:28:48 +0100 | [diff] [blame] | 270 | |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 271 | script = "../gn/standalone/build_tool_wrapper.py" |
Hector Dearman | 6186c87 | 2018-05-31 13:28:48 +0100 | [diff] [blame] | 272 | stamp_file = "$target_out_dir/.$target_name.stamp" |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 273 | args = [ |
Hector Dearman | 6186c87 | 2018-05-31 13:28:48 +0100 | [diff] [blame] | 274 | "--stamp", |
| 275 | rebase_path(stamp_file, root_build_dir), |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 276 | "/bin/ln", |
| 277 | "-fns", |
Hector Dearman | 6186c87 | 2018-05-31 13:28:48 +0100 | [diff] [blame] | 278 | 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 Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 283 | ] |
| 284 | } |
| 285 | |
| 286 | # Runs npm install. |
Hector Dearman | 6186c87 | 2018-05-31 13:28:48 +0100 | [diff] [blame] | 287 | action("node_modules_install") { |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 288 | script = "../gn/standalone/build_tool_wrapper.py" |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 289 | stamp_file = "$target_out_dir/.$target_name.stamp" |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 290 | 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 Dearman | 6186c87 | 2018-05-31 13:28:48 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | group("node_modules") { |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 314 | deps = [ |
Hector Dearman | 6186c87 | 2018-05-31 13:28:48 +0100 | [diff] [blame] | 315 | ":node_modules_install", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 316 | ":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. |
| 324 | action("dist_symlink") { |
| 325 | script = "../gn/standalone/build_tool_wrapper.py" |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 326 | stamp_file = "$target_out_dir/.$target_name.stamp" |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 327 | args = [ |
| 328 | "--stamp", |
| 329 | rebase_path(stamp_file, root_build_dir), |
| 330 | "/bin/ln", |
| 331 | "-fns", |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame] | 332 | rebase_path(target_out_dir, "."), |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 333 | rebase_path("dist", root_build_dir), |
| 334 | ] |
| 335 | inputs = [ |
| 336 | "$root_build_dir", |
| 337 | ] |
| 338 | outputs = [ |
| 339 | stamp_file, |
| 340 | ] |
| 341 | } |