Skip to content

Bazel Bites: A Tasty Metaphor for Streamlined Builds

When you search the internet for details about Bazel, you’ll likely come across something like this:

Bazel is a build system. It’s used to build and test software, converting source code into artifacts, such as executable programs.

Bazel’s headline, “{Fast, Correct} - Choose two,” emphasizes its focus on scalability and hermeticity. It’s language agnostic, allowing you to use Bazel to build projects written in a variety of different programming languages.

If this seems confusing or overwhelming at first, don’t worry – we’ve cooked up a different approach that could help. We’ve concocted this article using a slightly rare (yes, we went there!) cooking metaphor.

Bazel explained through cooking

Bazel explained through cooking

We all need to eat, that’s a universal truth. And since many of us have a basic grasp of how to prepare one or more dishes, let’s leverage your knowledge of cooking to dive into a comprehensive introduction to Bazel.

Unpacking The Recipe: BUILD files

When it comes to cooking, the first thing that likely springs to mind is a recipe. A recipe provides a list of ingredients and step-by-step instructions of actions so that, hopefully, you end up with something delicious. Similarly, in Bazel, the BUILD file acts as the recipe. It contains statements that serve as either ingredients or steps in the process (recipe).

The journey of turning raw ingredients into a finished dish mirrors the software build process, starting with the source code (ingredients) and ending with output artifacts (the dish). And Bazel? It plays the role of head chef, orchestrating the BUILD file and bringing everything together.

The Bazel Kitchen

The Bazel Kitchen

Gathering Ingredients: Source Code

Much like finding all the ingredients for a recipe, gathering source code for a build can involve various sources. You might grow some yourself (develop your own code/make your own codebase), purchase them from a supermarket (use a third-party library), or harvest ingredients from a community garden (find code from an open source repository). Of course, all of these methods are flexible, you can mix, match, and combine them until you have all the ingredients you need for your dish(es).

Get Cooking: Bazel Rules

Recipes have different instructions that can vary depending on the cook. A recipe written for a newbie provides a lot more detail, while professional chefs prefer more concise instructions. There’s a range of measurements, steps, descriptions, and language that can range from simple to complex.

Whether it’s a simple task like boiling water or a more complex one like blanching broccoli or making a Béarnaise sauce, it’s important to follow a logical structure and use clear language. To get the best results, instructions should be easy to understand and follow.

Bazel has rules that make the recipe easy for everyone to follow:

  • filegroup is an example of a simple Bazel rule, allowing a group of source files to be given a descriptive name.
  • genrule is more involved. This rule allows Bazel to create files using existing Bash commands when told to.
  • cc_library is even more complex. It maps C++ header libraries to source files, defining which sources have access to which libraries.

You can also craft your own methods for preparing something. You may find some shortcuts for things that your grandfather regularly used in his recipe books. These would be similar to Bazel’s custom rules.

There are many pre-existing rules that can be found in open-source repositories and built-in rules. Both are commonly used, tested thoroughly, and proven to be effective. That’s not to say you can’t improve them! Much like tweaking recipes, you can customize Bazel rules to fit your needs, or simply follow them as they are.

Mixing Processes: Bazel Macros

Similar to how some chefs will blend spices to create a unique dish, developers can leverage Bazel to combine processes. These are called Bazel Macros, much like signature blends of spices; they take ingredients like spices and turn them into reusable units. They’re a lot like mini-recipes and the Béarnaise sauce we mentioned earlier. Bazel Macros add efficiency, modularity, organization, and sustainability to your build.

The Kitchen: Your Build Environment

Don’t forget the most important part! The kitchen, the heart of the recipe, where it all comes together. For Bazel, the kitchen is the machine where the build is executed.

A kitchen has standard appliances that you continually use for years. Think of a stove, an oven, fridge or a freezer. You’ll need them for most food prep and they can be used over and over again without being consumed. These appliances are much like your machine's CPU and RAM.

While they can be used repeatedly, they can’t be used for something else simultaneously. For instance, your stove has four cooking burners (much like computer cores) that can be used at the same time, but not more. Similarly, your fridge can hold a certain amount of groceries but when it’s full, you need to make space in order to store something else (just like computer memory).

Essential Utensils: Managing Toolchains

Cooking is made infinitely easier with utensils: tools like ladles, spatulas, pots, pans, knives, maybe a blender or food processor. In the context of a build, these utensils correspond to external tools that your build requires. Some external tools you can use are compilers, toolchains, and scripts.

But just like you don’t need every tool for every recipe, you also don’t need every tool for every build. Sometimes you need to acquire a specific utensil to cook new recipes. For example, if you want to expand your code base to include a new programming language, you might need to install a different compiler.

You’ll buy additional tools or update the ones you already have more often than you will replace large appliances like the stove, oven, fridge, or freezer. This is similar to how build updates work: you can update your toolchain or even start using a different compiler, such as Clang instead of GCC without necessarily updating the machine you are building your software on. At the same time, if you move house (use a different machine) or refurbish your kitchen (update some of your computer parts, e.g. get more memory), you can usually continue to use the same tools.

The Final Dish: Artifacts

Once you finish a recipe, you end up with a dish or multiple dishes that make up a full meal. In the case of Bazel, your finished artifacts are the end result. When using Bazel to compile C++ source code, the executable binary is the artifact. You may run a taste test, or just a test, and after you verified everything tastes good, dinner is served. Bon Appétit!

We hope this cooking metaphor provides a clearer perspective on Bazel, its functionalities, and how it can help streamline your build process. Stay tuned for another course of Bazel Bites!