2021-09-24 20:17:26 +00:00
# Bazel support
2023-04-29 23:25:00 +00:00
To get [Bazel ](https://bazel.build/ ) working with {fmt} you can copy the files `BUILD.bazel` , `WORKSPACE.bazel` , and `.bazelversion` from this folder (`support/bazel`) to the root folder of this project. This way {fmt} gets bazelized and can be used with Bazel (e.g. doing a `bazel build //...` on {fmt}).
2021-09-24 20:17:26 +00:00
2021-10-02 12:36:43 +00:00
## Using {fmt} as a dependency
2021-09-24 20:17:26 +00:00
2021-10-02 12:36:43 +00:00
The following minimal example shows how to use {fmt} as a dependency within a Bazel project.
2021-09-24 20:17:26 +00:00
2021-10-02 12:36:43 +00:00
The following file structure is assumed:
2021-09-24 20:17:26 +00:00
```
2021-10-02 12:36:43 +00:00
example
├── BUILD.bazel
├── main.cpp
└── WORKSPACE.bazel
2021-09-24 20:17:26 +00:00
```
2021-10-02 12:36:43 +00:00
*main.cpp*:
2021-09-24 20:17:26 +00:00
2021-10-02 12:36:43 +00:00
```c++
2021-09-24 20:17:26 +00:00
#include "fmt/core.h"
int main() {
2021-10-02 12:36:43 +00:00
fmt::print("The answer is {}\n", 42);
2021-09-24 20:17:26 +00:00
}
```
The expected output of this example is `The answer is 42` .
2021-10-02 12:36:43 +00:00
*WORKSPACE.bazel*:
2021-09-24 20:17:26 +00:00
```python
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "fmt",
2021-10-02 12:36:43 +00:00
branch = "master",
remote = "https://github.com/fmtlib/fmt",
patch_cmds = [
"mv support/bazel/.bazelversion .bazelversion",
"mv support/bazel/BUILD.bazel BUILD.bazel",
"mv support/bazel/WORKSPACE.bazel WORKSPACE.bazel",
],
# Windows-related patch commands are only needed in the case MSYS2 is not installed.
# More details about the installation process of MSYS2 on Windows systems can be found here:
# https://docs.bazel.build/versions/main/install-windows.html#installing-compilers-and-language-runtimes
# Even if MSYS2 is installed the Windows related patch commands can still be used.
patch_cmds_win = [
"Move-Item -Path support/bazel/.bazelversion -Destination .bazelversion",
"Move-Item -Path support/bazel/BUILD.bazel -Destination BUILD.bazel",
"Move-Item -Path support/bazel/WORKSPACE.bazel -Destination WORKSPACE.bazel",
],
2021-09-24 20:17:26 +00:00
)
```
2023-04-29 23:25:00 +00:00
In the *WORKSPACE* file, the {fmt} GitHub repository is fetched. Using the attribute `patch_cmds` the files `BUILD.bazel` , `WORKSPACE.bazel` , and `.bazelversion` are moved to the root of the {fmt} repository. This way the {fmt} repository is recognized as a bazelized workspace.
2021-10-02 12:36:43 +00:00
*BUILD.bazel*:
```python
cc_binary(
name = "Demo",
srcs = ["main.cpp"],
deps = ["@fmt"],
)
```
The *BUILD* file defines a binary named `Demo` that has a dependency to {fmt}.
To execute the binary you can run `bazel run //:Demo` .
2021-09-24 20:17:26 +00:00
2023-04-29 14:45:49 +00:00
# Using Bzlmod
The [Bazel Central Registry ](https://github.com/bazelbuild/bazel-central-registry/tree/main/modules/fmt ) also provides support for {fmt}.