CMake vs Bazel In Practise - Real Code, Real Results
Is Bazel a good alternative to CMake, or any of the other C/C++ build systems? My answer so far has been "don't know because I've never tried Bazel." Today, I'm going to change that, and put Bazel head-to-head against CMake.
Here's how it works:
- I've got two small projects, and I'm going to build them using CMake and Bazel
- I'm going to try to get both build systems to do the same thing, within reason
If you're not familiar with CMake & Bazel:
- CMake is the de-facto standard build system for C/C++. You'll find C++ projects using it all over the internet
- Bazel, is an ambitious alternative build system from Google, that aims to be fast, multi-programming-language, multi-platform, scalable, and extensible
Sounds like they want it to be the ultimate build system to rule all build systems. Very ambitious. Okay, let's get started
Installation
The first step is installing Bazel, and here we hit our first little hurdle. Bazel's documentation recommends using Bazelisk over installing Bazel directly, which in turn needs Chocolatey for installation on Windows. Ugh! Needlessly complicated.
Now, certain developers will say: "what're you complaining about? You only need to do it once, and if you work for a large company then your Workstation will come preconfigured." All true, but we've got multiple dependencies, and hence, multiple layers of complexity, and we haven't even started using it. I hope this isn't a sign of things to come...
Project 1
Let's start with the first project, which is a simple Hello World project that I swiped from "The CMake Tutorial." It's designed to demonstrate compiling multiple source files into one program.
Both CMake and Bazel are very straightforward, although with Bazel you also need a MODULE.bazel file. It's empty, but it tells Bazel that this is a Bazel project. Don't ask me why; it's how Bazel was designed.
Easy as. Too easy. Let's move on to project 2...
Project 2
This project links to the RayLib library. Raylib is a great little game/multimedia programming library. You can open a window and draw something with a few lines of code. The CMake script uses the FetchContent module to download and build RayLib's source-code if needed. If Raylib version 4.5.0 is already installed, then it'll use that. Otherwise, it'll fetch the code, and build it automatically.
Here's where I started having trouble with Bazel. Raylib has a CMake build script, and Bazel's documentation has big gaps in this area. After scouring the internet for clues, I pieced together a script, that then failed with cryptic error messages. And try as I might, I simply couldn't get it to build. No usable I ended up going to bed very late, and rather frustrated.
This morning I gave it another shot, but using Linux instead of Windows. And this time, I got it working. There are three files involved:
- .bazelrc to enable bzlmod
- MODULE.bazel - handles fetching the dependency
- BUILD - the script that puts it all together
NOTE: Want the source-code? All the code shown in the video can be downloaded from the Kea Campus by Creator and higher tier members.
It still won't build on Windows, but at least I did get Bazel to fetch and build Raylib, and then link it into my little test program.
I'm sure that it would have worked perfectly if Raylib had a Bazel build script. However, CMake is the de-facto build system, so it's very likely that you're going to use third-party libraries that are built using CMake.
At this point, it's relatively common for software developers to go on a rant. "Bazel sucks! It's horrible! I hate Bazel and nobody should us it!"
I'm not going to do that. Yes, Bazel is the one and only build system in this video series that has been an absolute pain in the butt to use, and that wasted hours of my life. And yes, it feels a lot like CMake did decades ago, with cryptic error messages, horrible documentation, etc. But that's actually good news. Decades later, CMake has improved a lot, to the point where I've gone from avoiding it like the plague, to using it. So there's hope that, if I try Bazel again a decade or two from now, that it'll also be much easier to use.
In the meantime, though, I'm going to stick to CMake. CMake is still my go-to for new projects, and I recommend learning how to use it properly, even if you decide to use Bazel or something else. As I said previously, it's the de-facto C++ build system, so you're going to encounter it sooner or later.
I've created The CMake Tutorial to help you get up to speed with CMake fast. Save yourself a ton of time and the frustration that goes with trying to piece it together on your own.
Click here to learn more about The CMake Tutorial.
That's it for now. See you next time...
Post your comment