Skip to main content
The non-hermetic build from the last lesson has a lot of downsides. Let’s have Bazel manage the dependency instead. Many libraries already have BUILD files in the Bazel Central Registry (BCR). We can search for the libmagic library we used in the last lesson, and find that it’s there: https://registry.bazel.build/modules/libmagic This means we can avoid depending on the state of the local machine, and know that all developers will use the same version of this library. It’s also friendly for cross-compilation, since Bazel will build libmagic for the target platform, not the host. To install, add to MODULE.bazel :
And comment out the cxx_builtin_include_directories attribute we added in the previous lesson, which would conflict. This module ought to have a cc_library that we can add into our deps. Let’s see what’s in it:
We want the cc_library so that we statically-link the libmagic library into our application, so change BUILD to have deps = ["@libmagic//:lib"] Now when we run the application, we compile libmagic sources as part of our build. Note that we want to be hermetic, and the file command reads some “magic” data at runtime, so we supply that as well.
Then run bazel run magic $PWD/BUILD to get: MIME type: text/plain

What if the module isn’t on BCR?

It’s common practice to write your own BUILD files for third-party libraries. The conventional location is third_party/BUILD.libmagic for example. Please consider upstreaming your BUILD files to the Bazel Central Registry! This helps you, as other maintainers can improve the code. It also helps your fellow engineers who can skip this step next time.