Bazel Bootcamp
Danna Kelmer (dannark@)
Before we get started
Bazel
bazel.build
Bazel
bazel.build
Bazel
bazel.build
Intro + Bazel Project Structure
What is Bazel?
.java
.h
.cpp
.h
.plist
…
(etc.)
.scala
javac
gcc
.m
tsc
…
(etc.)
packages
executables
<test results>
transformed source
Bazel
bazel.build
Features of Bazel?
Bazel
bazel.build
Where did Bazel Come From?
Bazel
bazel.build
WORKSPACE files
BUILD files
Bazel
bazel.build
WORKSPACE
Bazel
bazel.build
Multiple Bazel Workspaces
$ tree
.
├── churros_project
│ └── WORKSPACE
├── donut_project
│ └── WORKSPACE
└── ice_cream_project
└── WORKSPACE
Bazel
bazel.build
BUILD files
Bazel
bazel.build
├── WORKSPACE�├── BUILD.bazel
├── README.md
├── animations
│ ├── BUILD.bazel
│ ├── PACKAGE.md
│ ├── browser
│ │ ├── BUILD.bazel
│ │ ├── index.ts
│ │ ├── package.json
│ │ ├── public_api.ts
│ │ └── tsconfig-build.json
│ ├── index.ts
│ ├── rollup.config.js
│ ├── src
│ │ ├── animation_player_spec.ts
│ │ ├── players
│ │ ├── private_export.ts
│ │ ├── util.ts
│ │ ├── util_spec.ts
│ │ └── version.ts
│ ├── utils
│ │ ├── BUILD.bazel
│ │ └── util.ts
│ └── tsconfig-build.json
...
13
# How many packages do we have�# in this workspace?
├── WORKSPACE�├── BUILD.bazel
├── README.md
├── animations
│ ├── BUILD.bazel
│ ├── PACKAGE.md
│ ├── browser
│ │ ├── BUILD.bazel
│ │ ├── index.ts
│ │ ├── package.json
│ │ ├── public_api.ts
│ │ └── tsconfig-build.json
│ ├── index.ts
│ ├── rollup.config.js
│ ├── src
│ │ ├── animation_player_spec.ts
│ │ ├── players
│ │ ├── private_export.ts
│ │ ├── util.ts
│ │ ├── util_spec.ts
│ │ └── version.ts
│ ├── utils
│ │ ├── BUILD.bazel
│ │ └── util.ts
│ └── tsconfig-build.json
...
14
# How many packages do we have�# in this workspace?
├── WORKSPACE�├── BUILD.bazel
├── README.md
├── animations
│ ├── BUILD.bazel
│ ├── PACKAGE.md
│ ├── browser
│ │ ├── BUILD.bazel
│ │ ├── index.ts
│ │ ├── package.json
│ │ ├── public_api.ts
│ │ └── tsconfig-build.json
│ ├── index.ts
│ ├── rollup.config.js
│ ├── src
│ │ ├── animation_player_spec.ts
│ │ ├── players
│ │ ├── private_export.ts
│ │ ├── util.ts
│ │ ├── util_spec.ts
│ │ └── version.ts
│ ├── utils
│ │ ├── BUILD.bazel
│ │ └── util.ts
│ └── tsconfig-build.json
...
15
# How many packages do we have�# in this workspace?
├── WORKSPACE�├── BUILD.bazel
├── README.md
├── animations
│ ├── BUILD.bazel
│ ├── PACKAGE.md
│ ├── browser
│ │ ├── BUILD.bazel
│ │ ├── index.ts
│ │ ├── package.json
│ │ ├── public_api.ts
│ │ └── tsconfig-build.json
│ ├── index.ts
│ ├── rollup.config.js
│ ├── src
│ │ ├── animation_player_spec.ts
│ │ ├── players
│ │ ├── private_export.ts
│ │ ├── util.ts
│ │ ├── util_spec.ts
│ │ └── version.ts
│ ├── utils
│ │ ├── BUILD.bazel
│ │ └── util.ts
│ └── tsconfig-build.json
...
16
# How many packages do we have�# in this workspace?
├── WORKSPACE�├── BUILD.bazel
├── README.md
├── animations
│ ├── BUILD.bazel
│ ├── PACKAGE.md
│ ├── browser
│ │ ├── BUILD.bazel
│ │ ├── index.ts
│ │ ├── package.json
│ │ ├── public_api.ts
│ │ └── tsconfig-build.json
│ ├── index.ts
│ ├── rollup.config.js
│ ├── src
│ │ ├── animation_player_spec.ts
│ │ ├── players
│ │ ├── private_export.ts
│ │ ├── util.ts
│ │ ├── util_spec.ts
│ │ └── version.ts
│ ├── utils
│ │ ├── BUILD.bazel
│ │ └── util.ts
│ └── tsconfig-build.json
...
17
# How many packages do we have�# in this workspace?
Anatomy of a Label
@angular//animations/utils:draw_circle
optional repository name
root of repository
package
target
Bazel
bazel.build
Anatomy of a Label - Shorthand
//animations/util:draw_circle
package
target
root of workspace
containing $PWD
Bazel
bazel.build
Anatomy of a Label - Shorthand
animations/util
default target :util in the util package
Bazel
bazel.build
Anatomy of a Label - Shorthand
:draw_circle
target inside of $PWD
Bazel
bazel.build
Anatomy of a BUILD file
22
package(default_visibility = ["//visibility:private"])
�load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
ts_library(
name = "lib",
srcs = glob(["*.ts"]),
tsconfig = ":tsconfig.json",
deps = [
"@ngdeps//@types/node",
],
)
Package visibility
23
package(default_visibility = ["//visibility:private"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
ts_library(
name = "lib",
srcs = glob(["*.ts"]),
tsconfig = ":tsconfig.json",
deps = [
"@ngdeps//@types/node",
],
)
Imports
24
package(default_visibility = ["//visibility:private"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
ts_library(
name = "lib",
srcs = glob(["*.ts"]),
tsconfig = ":tsconfig.json",
deps = [
"@ngdeps//@types/node",
],
)
Definition of build targets
25
package(default_visibility = ["//visibility:private"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
ts_library(
name = "lib",
srcs = glob(["*.ts"]),
tsconfig = ":tsconfig.json",
deps = [
"@ngdeps//@types/node",
],
)
Attributes
26
package(default_visibility = ["//visibility:private"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
ts_library(
name = "lib",
srcs = glob(["*.ts"]),
tsconfig = ":tsconfig.json",
deps = [
"@ngdeps//@types/node",
],
)
Target dependencies
27
package(default_visibility = ["//visibility:private"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
ts_library(
name = "lib",
srcs = glob(["*.ts"]),
tsconfig = ":tsconfig.json",
deps = [
"@ngdeps//types/node",
],
)
j
Workspace
Package
Target
Bazel
bazel.build
Anatomy of an Invocation
bazel --max_idle_secs build --c opt //animations/util:draw_circle
startup options
label
command options
command
bazel
Bazel
bazel.build
.bazelrc
Bazel
bazel.build
Exercise #1: Backend
Tests
Testing With Bazel
Bazel
bazel.build
Testing With Bazel
Bazel
bazel.build
Data dependencies
Bazel
bazel.build
Testing With Bazel
Bazel
bazel.build
Testing With Bazel
Bazel
bazel.build
Exercise #2: Java Client and Unit Tests
Testing Demo
Rules Deep Dive
Rules
Bazel
bazel.build
Attributes
Bazel
bazel.build
Outputs
Bazel
bazel.build
Rule Implementation Function
Bazel
bazel.build
Target Graph
//packages/core:core
//packages/bazel/src/ngc-wrapped:xi18n
//packages/bazel/src/ngc-wrapped:xi18n_bin
//packages/bazel/src/ngc-wrapped:ngc_lib
//packages/compiler-cli:compiler-cli
//packages/compiler:compiler
Bazel
bazel.build
Action Graph
core.umd.js
rollup
di.js, linker.js, render.js, ...
tsc
di.ts, linker.ts, render.ts, ...
uglify-js
Bazel
bazel.build
Look at bootcamp WORKSPACE file
Bazel
bazel.build
Exercise #3 + #4: Frontend + Integration Tests
Exploring Bazel
Exploring Bazel's output tree
bazel info
INFO: Invocation ID: 80e22a8f-43c4-40f8-8c61-a75ce680b7cd
bazel-bin: /private/.../execroot/angular/bazel-out/darwin-fastbuild/bin
output_base: /private/var/tmp/_bazel_$USR/7828fda17547c0ca9f5e48bf8194e356
50
What is Query?
Bazel
bazel.build
List all targets in //proto/logger
$ bazel query //proto/logger:all
$ bazel query //proto/logger:*
Bazel
bazel.build
List all targets in the project
$ bazel query //...
Bazel
bazel.build
Find all dependencies of //go/cmd/server:go-server
$ bazel query “deps(//go/cmd/server:go-server)”
Bazel
bazel.build
Why does �//java/src/main/java/bazel/bootcamp:JavaLoggingClient depend on�//proto/logger:logger_proto
$ bazel query ‘somepath(//java/src/main/java/bazel/bootcamp:JavaLoggingClient, //proto/logger:logger_proto)’
$ bazel query ‘allpaths(//java/src/main/java/bazel/bootcamp:JavaLoggingClient, //proto/logger:logger_proto)’
Bazel
bazel.build
Why does the //foo tree depend on //bar/baz?
$ bazel query ‘somepath(foo/…, //bar/baz:all)’
Bazel
bazel.build
What C++ libraries do all the foo tests depend on that the foo_bin target does not?
$ bazel query ‘kind("cc_library", deps(kind(".*test rule", foo/...)) except deps(//foo:foo_bin))’
Bazel
bazel.build
Bazel
bazel.build
Genquery
chocolate/BUILD
genquery(
name = “chocolate-deps”,
expression = “deps(//chocolate:choclate_lib”,
scope = [“//chocolate:chocolate_lib”],
)
java_binary(
name = “chocolate”,
deps = [“:chocolate_lib”],
main_class = “chocolate.chocolateMain”,
)
Bazel
bazel.build
Thank you!