-
Notifications
You must be signed in to change notification settings - Fork 183
Bazel Compilation
Use this repository for a minimal reproducible example:
https://github.com/armfazh/circl-bazel-adventure
To compile and run our Go application, just type:
bazel run //:circl-bazel-adventureYou should see random keys printed to the console!
In Issue #537, it was reported that CIRCL failed to build with Bazel.
TL;DR Update rules_go to v0.55.0
Thanks to @patrickmscott who work on the solution PR #4298.
The rules_go is the official Bazel extension that teaches Bazel how to build and test Go programs. It provides the essential rules—like go_binary, go_library, and go_test—that translate your Go source code into targets Bazel can understand, compile, and manage.
The issue was that rules_go was not properly including header files used in CIRCL.
In Go, there exist two types of headers (i.e., files with .h extension):
- C Headers - Used for including C code (when compling with CGO=1 support).
- Assembler Headers - Used in assembler files to include definitions and macros. These files are not related to C nor CGO support.
CIRCL is a heavy-optimized library that uses assembler routines and relies a lot in assembler headers.
To compound the complexity of the problem, it is also possible to include assembler headers inside of another assembler header (even if it is defined in a different package of the same project).
To reproduce the issue, set rules_go version to <=0.54.0.
- bazel_dep(name = "rules_go", version = "")
+ bazel_dep(name = "rules_go", version = "0.54.0")The output looks like this
$ bazel run //:circl-bazel-adventure
Use --sandbox_debug to see verbose messages from the sandbox and retain the
sandbox build root for debugging
external/gazelle++go_deps+com_github_cloudflare_circl/dh/x25519/curve_amd64.s:8:
#include:
open external/gazelle++go_deps+com_github_cloudflare_circl/math/fp25519/fp_amd64.h: no such file or directoryThe error points to the abscense of the assembler header file fp_amd64.h (which includes prime field arithmetic for the AMD64 architecture).
Several hacks were suggested in the meantime.
Update rules_go version to 0.55.0.
- bazel_dep(name = "rules_go", version = "0.54.0")
+ bazel_dep(name = "rules_go", version = "0.55.0")and compilation just works.
So, what's going on in that BUILD.bazel file? Let's break it down.
load("@rules_go//go:def.bzl", "go_binary", "go_library")
go_library(
name = "circl-bazel-adventure_lib",
srcs = ["main.go"],
importpath = "github.com/user/circl-bazel-adventure",
visibility = ["//visibility:private"],
deps = ["@com_github_cloudflare_circl//dh/x25519:go_default_library"],
)
go_binary(
name = "circl-bazel-adventure",
embed = [":circl-bazel-adventure_lib"],
visibility = ["//visibility:public"],
)-
load(...): This line imports the Go rules we need from the Bazel Go rules library. These are the building blocks for our Go project. -
go_library(...): This rule defines a Go library. We've named itgo-bazel-adventure_liband told it that our source code is inmain.go. Think of this as compiling the reusable parts of our code. -
go_binary(...): This rule creates the final executable program. We've named itgo-bazel-adventureand told it to "embed" or include ourgo-bazel-adventure_lib.
And that's the core of it! You define your code and its dependencies, and Bazel figures out the rest.