xref: /aosp_15_r20/external/pigweed/docs/showcases/sense/tutorial/bazel_cloud.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1.. _showcase-sense-tutorial-bazel_cloud:
2
3==============================
413. Use Bazel's cloud features
5==============================
6.. caution::
7
8   This section is optional. Feel free to skip to the next one if you're
9   impatient to :ref:`try the air quality app <showcase-sense-tutorial-prod>`!
10
11One of Bazel's defining features is that it's a cloud build system. Team
12members can easily `share artifacts
13<https://app.buildbuddy.io/invocation/f8bc4845-a38d-4c62-b939-14238168ba46>`__
14(including test logs), builds can be run in parallel on hundreds of machines in
15the cloud, and build artifacts that were built once (by anyone) don't need to
16be rebuilt from scratch.
17
18This section gives you a taste of these features of Bazel using `BuildBuddy
19<https://www.buildbuddy.io/>`_.
20
21----------------
22BuildBuddy setup
23----------------
24To use cloud features, you need to get set up with some cloud provider.
25
26.. note::
27
28   Googlers: see additional instructions at `go/pw-sense-googlers
29   <http://go/pw-sense-googlers#buildbuddy-integration>`_.
30
31#. Go to https://app.buildbuddy.io/ and log in via Google or GitHub.
32#. Click `Quickstart Guide <https://app.buildbuddy.io/docs/setup/>`__.
33#. In **1. Configure your .bazelrc** enable the following options:
34
35   * API Key
36   * Enable cache
37   * Full cache
38
39   .. caution::
40
41      :bug:`364781685`: Sense does not support remote execution yet, so don't
42      enable that option.
43
44#. Copy the provided snippet to your ``.bazelrc``.
45
46---------------------
47Review and share logs
48---------------------
49Let's go back to what we learned in :ref:`showcase-sense-tutorial-hosttests` and run a test.
50
51#. Open ``//modules/blinky/blinky_test.cc``.
52
53#. Make the ``Toggle`` test fail by changing one of the expected values.
54   Example:
55
56   .. code-block:: c++
57
58      TEST_F(BlinkyTest, Toggle) {
59        // ...
60        auto event = FirstActive();
61        ASSERT_NE(event, monochrome_led_.events().end());
62        EXPECT_EQ(event->state, State::kInactive);   // add this line
63        // EXPECT_EQ(event->state, State::kActive);  // comment out this line
64        EXPECT_GE(ToMs(event->timestamp - start), kIntervalMs * 0);
65        start = event->timestamp;
66        // ...
67      }
68
69   .. caution:: Remember to save your changes!
70   .. tab-set::
71
72      .. tab-item:: VS Code
73         :sync: vsc
74
75         In **Bazel Build Targets** expand **//modules/blinky**, then right-click
76         **:blinky_test (cc_test)**, then select **Test target**.
77
78         .. figure:: https://storage.googleapis.com/pigweed-media/sense/20240802/test_target_v2.png
79            :alt: Selecting Test target
80
81            Starting ``blinky_test``
82
83         A task launches a terminal. You should see ``blinky_test`` fail, and a
84         BuildBuddy invocation link printed:
85
86         .. code-block:: console
87
88            //modules/blinky:blinky_test         FAILED in 0.4s
89            INFO: Streaming build results to: https://app.buildbuddy.io/invocation/f8bc4845-a38d-4c62-b939-14238168ba46
90
91      .. tab-item:: CLI
92         :sync: cli
93
94         Run the following command. You should see output similar to what's
95         shown after the command. The key line starts with ``INFO: Streaming
96         build results to:``. It contains the BuildBuddy invocation link.
97
98         .. code-block:: console
99
100            $ bazelisk test //modules/blinky:blinky_test
101            # ...
102            //modules/blinky:blinky_test         FAILED in 0.4s
103              /home/kayce/.cache/bazel/_bazel_kayce/27fcdd448f61589ce2692618b3237728/execroot/showcase-rp2/bazel-out/k8-fastbuild/testlogs/modules/blinky/blinky_test/test.log
104
105            Executed 1 out of 1 test: 1 fails locally.
106            INFO: Streaming build results to: https://app.buildbuddy.io/invocation/f8bc4845-a38d-4c62-b939-14238168ba46
107
108#. Click the provided link. Some highlights to notice:
109
110   * The full log of the failed test.
111   * The command line invocation used to generate it.
112   * A timing profile showing how long Bazel took to build and execute the test.
113
114#. Click "Share" (in the top-right corner) and broaden the permissions to make
115   the invocation link shareable. Then show it off to your friends and
116   coworkers!
117
118--------------
119Remote caching
120--------------
121Bazel supports remote caching: if you (or someone else in your organization)
122already built an artifact, you can simply retrieve it from the cache instead of
123building it from scratch. Let's test it out.
124
125
126#. Remove all local Bazel build results.
127
128   .. code-block:: console
129
130      $ bazelisk clean
131
132#. Run ``blinky_test`` again. It should be quite fast, even though you
133   discarded all the build artifacts. Bazel simply downloads them from the
134   remote cache that your previous invocation populated!
135
136   You can click the **CACHE** tab in the BuildBuddy invocation UI to see more
137   details on cache performance (how many hits there were, how much data was
138   uploaded and downloaded, etc).
139
140-------
141Summary
142-------
143In this section, you've gotten a taste of the cloud features of Bazel:
144generating easily shareable URLs for build and test invocations, and speeding
145up your builds by leveraging remote caching.
146
147Next, head over to :ref:`showcase-sense-tutorial-prod` to try
148out the air quality monitoring application.
149