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", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 31 | ] |
| 32 | } |
| 33 | |
| 34 | # +----------------------------------------------------------------------------+ |
| 35 | # | Template used to run node binaries using the hermetic node toolchain. | |
| 36 | # +----------------------------------------------------------------------------+ |
| 37 | template("node_bin") { |
| 38 | action(target_name) { |
| 39 | forward_variables_from(invoker, |
| 40 | [ |
| 41 | "inputs", |
| 42 | "outputs", |
| 43 | ]) |
| 44 | deps = [ |
| 45 | ":node_modules", |
| 46 | ] |
| 47 | if (defined(invoker.deps)) { |
| 48 | deps += invoker.deps |
| 49 | } |
| 50 | script = "../gn/standalone/build_tool_wrapper.py" |
| 51 | _node_cmd = invoker.node_cmd |
| 52 | args = [ |
| 53 | "--path=$nodejs_bin", |
| 54 | "node", |
| 55 | rebase_path("node_modules/.bin/$_node_cmd", root_build_dir), |
| 56 | ] + invoker.args |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | # +----------------------------------------------------------------------------+ |
| 61 | # | Bundles all *.js files together resolving CommonJS require() deps. | |
| 62 | # +----------------------------------------------------------------------------+ |
| 63 | |
| 64 | # Bundle together all js sources into a bundle.js file, that will ultimately be |
| 65 | # included by the .html files. |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 66 | node_bin("main_bundle") { |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 67 | deps = [ |
| 68 | ":transpile_all_ts", |
| 69 | ] |
| 70 | node_cmd = "browserify" |
| 71 | inputs = [ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 72 | "$target_out_dir/main.js", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 73 | ] |
| 74 | outputs = [ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 75 | "$target_out_dir/bundle.js", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 76 | ] |
| 77 | args = [ |
| 78 | rebase_path(inputs[0], root_build_dir), |
| 79 | "-d", |
| 80 | "-o", |
| 81 | rebase_path(outputs[0], root_build_dir), |
| 82 | ] |
| 83 | } |
| 84 | |
| 85 | # +----------------------------------------------------------------------------+ |
| 86 | # | Protobuf: gen rules to create .js and .d.ts files from protos. | |
| 87 | # +----------------------------------------------------------------------------+ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 88 | proto_gen_dir = "$target_out_dir/gen" |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 89 | |
| 90 | node_bin("protos_to_js") { |
| 91 | inputs = [] |
| 92 | foreach(proto, trace_processor_protos) { |
| 93 | inputs += [ "../protos/perfetto/trace_processor/$proto.proto" ] |
| 94 | } |
| 95 | outputs = [ |
| 96 | "$proto_gen_dir/protos.js", |
| 97 | ] |
| 98 | node_cmd = "pbjs" |
| 99 | args = [ |
| 100 | "-t", |
| 101 | "static-module", |
| 102 | "-w", |
| 103 | "commonjs", |
| 104 | "-p", |
| 105 | rebase_path("../protos", root_build_dir), |
| 106 | "-o", |
| 107 | rebase_path(outputs[0], root_build_dir), |
| 108 | ] + rebase_path(inputs, root_build_dir) |
| 109 | } |
| 110 | |
| 111 | # Protobuf.js requires to first generate .js files from the .proto and then |
| 112 | # create .ts definitions for them. |
| 113 | node_bin("protos_to_ts") { |
| 114 | deps = [ |
| 115 | ":protos_to_js", |
| 116 | ] |
| 117 | inputs = [ |
| 118 | "$proto_gen_dir/protos.js", |
| 119 | ] |
| 120 | outputs = [ |
| 121 | "$proto_gen_dir/protos.d.ts", |
| 122 | ] |
| 123 | node_cmd = "pbts" |
| 124 | args = [ |
| 125 | "-p", |
| 126 | rebase_path("../protos", root_build_dir), |
| 127 | "-o", |
| 128 | rebase_path(outputs[0], root_build_dir), |
| 129 | rebase_path(inputs[0], root_build_dir), |
| 130 | ] |
| 131 | } |
| 132 | |
| 133 | # +----------------------------------------------------------------------------+ |
| 134 | # | TypeScript: transpiles all *.ts into .js | |
| 135 | # +----------------------------------------------------------------------------+ |
| 136 | |
| 137 | # Builds all .ts sources in the repo that are reachable from |sources|. |
| 138 | node_bin("transpile_all_ts") { |
| 139 | sources = [ |
Hector Dearman | 3d858cc | 2018-05-24 17:09:48 +0100 | [diff] [blame] | 140 | "src/main.ts", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 141 | ] |
| 142 | deps = [ |
| 143 | ":dist_symlink", |
| 144 | ":protos_to_ts", |
| 145 | ] |
| 146 | inputs = sources + [ "tsconfig.json" ] |
| 147 | outputs = [ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 148 | "$target_out_dir/main.js", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 149 | ] |
| 150 | |
| 151 | # Find all *.ts files and pass them as input triggers. This does NOT affect |
| 152 | # which files will get built. This defines only the set of files that, when |
| 153 | # changes, will re-trigger this rule in ninja. The files that get transpiled |
| 154 | # are the transitive dependencies of |sources|. |
| 155 | all_ts_files = exec_script("../gn/standalone/glob.py", |
| 156 | [ |
| 157 | "--root=" + rebase_path(".", root_build_dir), |
| 158 | "--filter=*.ts", |
| 159 | "--exclude=node_modules", |
| 160 | "--exclude=dist", |
| 161 | ], |
| 162 | "list lines", |
| 163 | [ "." ]) |
| 164 | inputs += all_ts_files |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 165 | node_cmd = "tsc" |
| 166 | args = [ |
| 167 | "--project", |
| 168 | rebase_path(".", root_build_dir), |
| 169 | "--outDir", |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 170 | rebase_path(target_out_dir, root_build_dir), |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 171 | ] |
| 172 | } |
| 173 | |
| 174 | # +----------------------------------------------------------------------------+ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 175 | # | Copy rules: create the final output directory. | |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 176 | # +----------------------------------------------------------------------------+ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 177 | copy("index_release") { |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 178 | sources = [ |
| 179 | "index.html", |
| 180 | ] |
| 181 | outputs = [ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 182 | "$ui_dir/index.html", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 183 | ] |
| 184 | } |
| 185 | |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 186 | copy("main_bundle_release") { |
| 187 | deps = [ |
| 188 | ":main_bundle", |
| 189 | ] |
| 190 | sources = [ |
| 191 | "$target_out_dir/bundle.js", |
| 192 | ] |
| 193 | outputs = [ |
| 194 | "$ui_dir/bundle.js", |
| 195 | ] |
| 196 | } |
| 197 | |
| 198 | copy("wasm_release") { |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 199 | deps = [ |
| 200 | "//src/trace_processor:trace_processor.js($wasm_toolchain)", |
| 201 | "//src/trace_processor:trace_processor.wasm($wasm_toolchain)", |
| 202 | ] |
| 203 | sources = [ |
| 204 | "$root_build_dir/wasm/trace_processor.js", |
| 205 | "$root_build_dir/wasm/trace_processor.wasm", |
| 206 | ] |
| 207 | outputs = [ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 208 | "$target_out_dir/wasm/{{source_file_part}}", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 209 | ] |
| 210 | } |
| 211 | |
| 212 | # +----------------------------------------------------------------------------+ |
| 213 | # | Node JS: run npm install and creates a symlink in the out directory. | |
| 214 | # +----------------------------------------------------------------------------+ |
| 215 | |
| 216 | action("check_node_exists") { |
| 217 | script = "../gn/standalone/check_buildtool_exists.py" |
Hector Dearman | 48783b1 | 2018-05-22 08:26:06 +0100 | [diff] [blame] | 218 | args = [ |
| 219 | nodejs_bin, |
| 220 | "--touch", |
| 221 | rebase_path("$target_out_dir/node_exists", ""), |
| 222 | ] |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 223 | inputs = [] |
| 224 | outputs = [ |
Hector Dearman | 48783b1 | 2018-05-22 08:26:06 +0100 | [diff] [blame] | 225 | "$target_out_dir/node_exists", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 226 | ] |
| 227 | } |
| 228 | |
| 229 | # Creates a symlink from out/xxx/ui/node_modules -> ../../../ui/node_modules. |
| 230 | # This allows to run browserify and other node tools from the out/xxx directory. |
| 231 | action("node_modules_symlink") { |
| 232 | deps = [ |
| 233 | ":check_node_exists", |
| 234 | ] |
| 235 | inputs = [ |
| 236 | "node_modules", |
| 237 | ] |
| 238 | outputs = [ |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 239 | "$target_out_dir/node_modules", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 240 | ] |
| 241 | script = "../gn/standalone/build_tool_wrapper.py" |
| 242 | args = [ |
| 243 | "/bin/ln", |
| 244 | "-fns", |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 245 | rebase_path(inputs[0], target_out_dir), |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 246 | rebase_path(outputs[0], root_build_dir), |
| 247 | ] |
| 248 | } |
| 249 | |
| 250 | # Runs npm install. |
| 251 | action("node_modules") { |
| 252 | script = "../gn/standalone/build_tool_wrapper.py" |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 253 | stamp_file = "$target_out_dir/.$target_name.stamp" |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 254 | args = [ |
| 255 | "--chdir", |
| 256 | rebase_path(".", root_build_dir), |
| 257 | "--stamp", |
| 258 | rebase_path(stamp_file, root_build_dir), |
| 259 | ] |
| 260 | args += [ |
| 261 | "--path=$nodejs_bin", |
| 262 | "node", |
| 263 | rebase_path("$nodejs_root/bin/npm", "."), |
| 264 | "install", |
| 265 | "--no-save", |
| 266 | "--silent", |
| 267 | ] |
| 268 | inputs = [ |
| 269 | "package.json", |
| 270 | "package-lock.json", |
| 271 | ] |
| 272 | outputs = [ |
| 273 | stamp_file, |
| 274 | ] |
| 275 | deps = [ |
| 276 | ":node_modules_symlink", |
| 277 | ] |
| 278 | } |
| 279 | |
| 280 | # Creates a symlink from //ui/dist -> ../../out/xxx/ui. Used only for |
| 281 | # autocompletion in IDEs. The problem this is solving is that in tsconfig.json |
| 282 | # we can't possibly know the path to ../../out/xxx for outDir. Instead, we set |
| 283 | # outDir to "./dist" and create a symlink on the first build. |
| 284 | action("dist_symlink") { |
| 285 | script = "../gn/standalone/build_tool_wrapper.py" |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 286 | stamp_file = "$target_out_dir/.$target_name.stamp" |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 287 | args = [ |
| 288 | "--stamp", |
| 289 | rebase_path(stamp_file, root_build_dir), |
| 290 | "/bin/ln", |
| 291 | "-fns", |
Hector Dearman | 262cbe2 | 2018-05-31 13:28:21 +0100 | [diff] [blame^] | 292 | rebase_path(target_out_dir, "."), |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 293 | rebase_path("dist", root_build_dir), |
| 294 | ] |
| 295 | inputs = [ |
| 296 | "$root_build_dir", |
| 297 | ] |
| 298 | outputs = [ |
| 299 | stamp_file, |
| 300 | ] |
| 301 | } |