xref: /aosp_15_r20/external/skia/site/docs/dev/contrib/bazel.md (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker
2*c8dee2aaSAndroid Build Coastguard Worker---
3*c8dee2aaSAndroid Build Coastguard Workertitle: "Using Bazel"
4*c8dee2aaSAndroid Build Coastguard WorkerlinkTitle: "Using Bazel"
5*c8dee2aaSAndroid Build Coastguard Worker
6*c8dee2aaSAndroid Build Coastguard Worker---
7*c8dee2aaSAndroid Build Coastguard Worker
8*c8dee2aaSAndroid Build Coastguard Worker## Overview
9*c8dee2aaSAndroid Build Coastguard Worker
10*c8dee2aaSAndroid Build Coastguard WorkerSkia is currently migrating towards using [Bazel](https://bazel.build/) as a build system, due to
11*c8dee2aaSAndroid Build Coastguard Workerthe ability to more tightly control what and how gets built.
12*c8dee2aaSAndroid Build Coastguard Worker
13*c8dee2aaSAndroid Build Coastguard WorkerWhen referring to a file in this doc, we use
14*c8dee2aaSAndroid Build Coastguard Worker[Bazel label notation](https://bazel.build/concepts/labels), so to refer the file located at
15*c8dee2aaSAndroid Build Coastguard Worker`$SKIA_ROOT/docs/examples/Arc.cpp`, we would say `//docs/examples/Arc.cpp`.
16*c8dee2aaSAndroid Build Coastguard Worker
17*c8dee2aaSAndroid Build Coastguard Worker## Learning more about Bazel
18*c8dee2aaSAndroid Build Coastguard WorkerThe Bazel docs are quite good. Suggested reading order if you are new to Bazel:
19*c8dee2aaSAndroid Build Coastguard Worker - [Getting Started with Bazel and C++](https://bazel.build/tutorials/cpp)
20*c8dee2aaSAndroid Build Coastguard Worker - [WORKSPACE.bazel and external deps](https://bazel.build/docs/external)
21*c8dee2aaSAndroid Build Coastguard Worker - [Targets and Labels](https://bazel.build/concepts/labels)
22*c8dee2aaSAndroid Build Coastguard Worker - [Understanding a build](https://bazel.build/docs/build)
23*c8dee2aaSAndroid Build Coastguard Worker - [Configuration with .bazelrc files](https://bazel.build/docs/bazelrc)
24*c8dee2aaSAndroid Build Coastguard Worker
25*c8dee2aaSAndroid Build Coastguard WorkerGooglers, check out [go/bazel-bites](http://go/bazel-bites) for more tips.
26*c8dee2aaSAndroid Build Coastguard Worker
27*c8dee2aaSAndroid Build Coastguard Worker## Building with Bazel
28*c8dee2aaSAndroid Build Coastguard Worker
29*c8dee2aaSAndroid Build Coastguard WorkerAll this assumes you have [downloaded Skia](/docs/user/download), especially having synced the
30*c8dee2aaSAndroid Build Coastguard Workerthird_party deps using `./tools/git-sync-deps`.
31*c8dee2aaSAndroid Build Coastguard Worker
32*c8dee2aaSAndroid Build Coastguard Worker### Linux Hosts (you are running Bazel on a Linux machine)
33*c8dee2aaSAndroid Build Coastguard WorkerYou can run a command like:
34*c8dee2aaSAndroid Build Coastguard Worker```
35*c8dee2aaSAndroid Build Coastguard Workerbazel build //example:hello_world_gl
36*c8dee2aaSAndroid Build Coastguard Worker```
37*c8dee2aaSAndroid Build Coastguard Worker
38*c8dee2aaSAndroid Build Coastguard WorkerThis uses a hermetic C++ toolchain we put together to compile Skia on a Linux host
39*c8dee2aaSAndroid Build Coastguard Worker(implementation is in `//toolchain`. It builds the _target_ defined in
40*c8dee2aaSAndroid Build Coastguard Worker`//examples/BUILD.bazel` named "hello_world_gl", which uses the `sk_app` framework
41*c8dee2aaSAndroid Build Coastguard Workerwe designed to make simple applications using Skia.
42*c8dee2aaSAndroid Build Coastguard Worker
43*c8dee2aaSAndroid Build Coastguard WorkerBazel will put this executable in `//bazel-bin/example/hello_world_gl` and tell you it did so in
44*c8dee2aaSAndroid Build Coastguard Workerthe logs. You can run this executable yourself, or have Bazel run it by modifying the command to
45*c8dee2aaSAndroid Build Coastguard Workerbe:
46*c8dee2aaSAndroid Build Coastguard Worker```
47*c8dee2aaSAndroid Build Coastguard Workerbazel run //example:hello_world_gl
48*c8dee2aaSAndroid Build Coastguard Worker```
49*c8dee2aaSAndroid Build Coastguard Worker
50*c8dee2aaSAndroid Build Coastguard WorkerIf you want to pass one or more flags to `bazel run`, add them on the end after a `--` like:
51*c8dee2aaSAndroid Build Coastguard Worker```
52*c8dee2aaSAndroid Build Coastguard Workerbazel run //example:hello_world_gl -- --flag_one=apple --flag_two=cherry
53*c8dee2aaSAndroid Build Coastguard Worker```
54*c8dee2aaSAndroid Build Coastguard Worker
55*c8dee2aaSAndroid Build Coastguard Worker### Mac Hosts (you are running Bazel on a Mac machine)
56*c8dee2aaSAndroid Build Coastguard WorkerYou can run a command like:
57*c8dee2aaSAndroid Build Coastguard Worker```
58*c8dee2aaSAndroid Build Coastguard Workerbazel build //example:bazel_test_exe
59*c8dee2aaSAndroid Build Coastguard Worker```
60*c8dee2aaSAndroid Build Coastguard Worker
61*c8dee2aaSAndroid Build Coastguard WorkerWhen building for Mac, we require the user to have Xcode installed on their device so that we can
62*c8dee2aaSAndroid Build Coastguard Workeruse system headers and Mac-specific includes when compiling. Googlers, as per usual, follow the
63*c8dee2aaSAndroid Build Coastguard Workerinstructions at [go/skia-corp-xcode](http://go/skia-corp-xcode) to install Xcode.
64*c8dee2aaSAndroid Build Coastguard Worker
65*c8dee2aaSAndroid Build Coastguard WorkerOur Bazel toolchain assumes you have `xcode-select` in your path so that we may symlink the
66*c8dee2aaSAndroid Build Coastguard Workeruser's current Xcode directory in the toolchain's cache. Make sure `xcode-select -p`
67*c8dee2aaSAndroid Build Coastguard Workerreturns a valid path.
68*c8dee2aaSAndroid Build Coastguard Worker
69*c8dee2aaSAndroid Build Coastguard Worker## .bazelrc Tips
70*c8dee2aaSAndroid Build Coastguard WorkerYou should make a [.bazelrc file](https://bazel.build/docs/bazelrc) in your home directory where
71*c8dee2aaSAndroid Build Coastguard Workeryou can specify settings that apply only to you. These can augment or replace the ones we define
72*c8dee2aaSAndroid Build Coastguard Workerin the `//.bazelrc` configuration file.
73*c8dee2aaSAndroid Build Coastguard Worker
74*c8dee2aaSAndroid Build Coastguard WorkerSkia defines some [configs](https://bazel.build/docs/bazelrc#config), that is, group of settings
75*c8dee2aaSAndroid Build Coastguard Workerand features in `//bazel/buildrc`. This file contains configs for builds that we use  regularly
76*c8dee2aaSAndroid Build Coastguard Worker(for example, in our continuous integration system).
77*c8dee2aaSAndroid Build Coastguard Worker
78*c8dee2aaSAndroid Build Coastguard WorkerIf you want to define Skia-specific configs (and options which do not conflict with other Bazel
79*c8dee2aaSAndroid Build Coastguard Workerprojects), you make a file in `//bazel/user/buildrc` which will automatically be read in. This
80*c8dee2aaSAndroid Build Coastguard Workerfile is covered by a `.gitignore` rule and should not be checked in.
81*c8dee2aaSAndroid Build Coastguard Worker
82*c8dee2aaSAndroid Build Coastguard WorkerYou may want some or all of the following entries in your `~/.bazelrc` or `//bazel/user/buildrc`
83*c8dee2aaSAndroid Build Coastguard Workerfile.
84*c8dee2aaSAndroid Build Coastguard Worker
85*c8dee2aaSAndroid Build Coastguard Worker### Build Skia faster locally
86*c8dee2aaSAndroid Build Coastguard WorkerMany Linux machines have a [RAM disk mounted at /dev/shm](https://www.cyberciti.biz/tips/what-is-devshm-and-its-practical-usage.html)
87*c8dee2aaSAndroid Build Coastguard Workerand using this as the location for the Bazel sandbox can dramatically improve compile times because
88*c8dee2aaSAndroid Build Coastguard Worker[sandboxing](https://bazel.build/docs/sandboxing) has been observed to be I/O intensive.
89*c8dee2aaSAndroid Build Coastguard Worker
90*c8dee2aaSAndroid Build Coastguard WorkerAdd the following to `~/.bazelrc` if you have a `/dev/shm` partition that is 4+ GB big.
91*c8dee2aaSAndroid Build Coastguard Worker```
92*c8dee2aaSAndroid Build Coastguard Workerbuild --sandbox_base=/dev/shm
93*c8dee2aaSAndroid Build Coastguard Worker```
94*c8dee2aaSAndroid Build Coastguard Worker
95*c8dee2aaSAndroid Build Coastguard WorkerMac users should probably bypass sandboxing as [it is known to be slow](https://github.com/bazelbuild/bazel/issues/8230).
96*c8dee2aaSAndroid Build Coastguard Worker```
97*c8dee2aaSAndroid Build Coastguard Workerbuild --spawn_strategy=local
98*c8dee2aaSAndroid Build Coastguard Worker```
99*c8dee2aaSAndroid Build Coastguard Worker
100*c8dee2aaSAndroid Build Coastguard Worker### Authenticate to RBE on a Linux VM
101*c8dee2aaSAndroid Build Coastguard WorkerWe are in the process of setting up Remote Build Execution (RBE) for Bazel. Some users have reported
102*c8dee2aaSAndroid Build Coastguard Workererrors when trying to use RBE (via `--config=linux_rbe`) on Linux VMs such as:
103*c8dee2aaSAndroid Build Coastguard Worker```
104*c8dee2aaSAndroid Build Coastguard WorkerERROR: Failed to query remote execution capabilities:
105*c8dee2aaSAndroid Build Coastguard WorkerError code 404 trying to get security access token from Compute Engine metadata for the default
106*c8dee2aaSAndroid Build Coastguard Workerservice account. This may be because the virtual machine instance does not have permission
107*c8dee2aaSAndroid Build Coastguard Workerscopes specified. It is possible to skip checking for Compute Engine metadata by specifying the
108*c8dee2aaSAndroid Build Coastguard Workerenvironment variable NO_GCE_CHECK=true.
109*c8dee2aaSAndroid Build Coastguard Worker```
110*c8dee2aaSAndroid Build Coastguard WorkerFor instances where it is not possible to set the `cloud-platform` scope
111*c8dee2aaSAndroid Build Coastguard Worker[on the VM](https://skia-review.googlesource.com/c/skia/+/525577), one can directly link to their
112*c8dee2aaSAndroid Build Coastguard WorkerGCP credentials by adding the following to `~/.bazelrc` (substituting their username for <user>)
113*c8dee2aaSAndroid Build Coastguard Workerafter logging in via `gcloud auth login`:
114*c8dee2aaSAndroid Build Coastguard Worker```
115*c8dee2aaSAndroid Build Coastguard Workerbuild:remote --google_credentials=/usr/local/google/home/<user>/.config/gcloud/application_default_credentials.json
116*c8dee2aaSAndroid Build Coastguard Worker```
117*c8dee2aaSAndroid Build Coastguard Worker
118*c8dee2aaSAndroid Build Coastguard Worker### Make local builds compatible with remote builds (e.g. better caching)
119*c8dee2aaSAndroid Build Coastguard WorkerAdd the following to `//bazel/user/buildrc` if you are on a Linux x64 box and want to be able to
120*c8dee2aaSAndroid Build Coastguard Workershare cached build results between things you build locally and build with `--config=linux_rbe`.
121*c8dee2aaSAndroid Build Coastguard Worker```
122*c8dee2aaSAndroid Build Coastguard Workerbuild --host_platform=//bazel/platform:linux_x64_hermetic
123*c8dee2aaSAndroid Build Coastguard Worker```
124*c8dee2aaSAndroid Build Coastguard WorkerFor example, if you are on a laptop, using `--config=linux_rbe` will speed up builds when you have
125*c8dee2aaSAndroid Build Coastguard Workeraccess to Internet, but then if you need to go offline, you can still build locally and use the
126*c8dee2aaSAndroid Build Coastguard Workerprevious build results from the remote builds.
127