3. Build the app#
You can start building right away. There’s no need to manually install dependencies or toolchains; Sense takes full advantage of Bazel’s ability to automate dependency and toolchain management. Try building an app now:
In Bazel Build Targets right-click the //apps/blinky folder and select Build Package Recursively. A task launches in a VS Code terminal.
Bazel builds all targets that it finds within the
//apps/blinky
directory. If there were targets in subdirectories, those would get built, too. A successful build looks similar to this:INFO: Found 17 targets... INFO: Elapsed time: 174.103s, Critical Path: 13.34s INFO: 2375 processes: 480 internal, 1895 linux-sandbox. INFO: Build completed successfully, 2375 total actions
Tip
When you want to build just a single target, you can use Build Target instead. This is useful when you know you only need to build a single target (such as compiling a binary for a specific platform) and want to do it quickly. Here we had you build all the
blinky
targets in one go because you’ll be using a lot of them in later parts of the tutorial anyways.Once the build finishes, press any key to close the task’s terminal.
Run the following command:
$ bazelisk build //apps/blinky/...
A successful build looks similar to this:
$ bazelisk build //apps/blinky/...
INFO: Analyzed 17 targets (464 packages loaded, 28991 targets configured).
INFO: From Linking external/rules_libusb~~libusb~libusb/libusb-1.0.so:
# ...
INFO: Found 17 targets...
INFO: Elapsed time: 314.300s, Critical Path: 26.73s
INFO: 2496 processes: 582 internal, 1914 linux-sandbox.
INFO: Build completed successfully, 2496 total actions
Tip
Pigweed recommends always running bazelisk
rather than bazel
because bazelisk
ensures that you always run the correct version
of Bazel, as defined in a project’s .bazelversion
file. In some
cases bazel
also does the right thing, but it’s easier to remember
to just always use bazelisk
.
Troubleshooting
Warnings during the build. As long as you see
Build completed successfully
you should be able to complete the rest of the tutorial. We generally work to remove all these warnings but they pop up from time-to-time as we continue to iterate on the Sense codebase.Long build times. Two minutes is typical for the first build. Pigweed builds a lot of things from source, such as the Protocol Buffer compiler,
libusb
, and more.
Summary#
You’ve now got some familiarity with how to build binaries in Bazel-based projects.
One interesting thing about Bazel: it’s not actually necessary to build an app before flashing it. Imagine you have a Bazel target that automates the process of flashing a binary to some hardware. You will see an example of this later in 7. Flash your Pico. If that flashing target depends on another target for actually building the binary, Bazel figures out that it must do the “build the binary” target before it can do the “flash the hardware” target.
Next, head over to 4. Explore C++ code intelligence to learn how to use the Pigweed extension for VS Code to navigate a codebase that supports multiple hardware platforms. If you’re not using VS Code you can skip ahead to 5. Run host tests because this code intelligence feature is currently only supported in VS Code.