Skip to content

Commit

Permalink
Makefile: set BPF2GO_ environment vars ambiently, remove from go:gene…
Browse files Browse the repository at this point in the history
…rate

This patch removes the -cc and -cflags from all 'go:generate' statements across
the codebase. Instead, ambiently provide BPF2GO_ env vars to the 'make' call
in container-all.

This allows running `go generate ./...` on distributions where clang binaries
don't have their version numbers suffixed.

Signed-off-by: Timo Beckers <timo@isovalent.com>
  • Loading branch information
ti-mo committed Sep 27, 2023
1 parent 4f779f5 commit 0667fa5
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 31 deletions.
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ container-all:
--env CFLAGS="-fdebug-prefix-map=/ebpf=." \
--env HOME="/tmp" \
"${IMAGE}:${VERSION}" \
make all
make BPF2GO_CC="$(CLANG)" BPF2GO_FLAGS="$(CFLAGS)" all

# (debug) Drop the user into a shell inside the container as root.
container-shell:
Expand All @@ -81,9 +81,6 @@ all: format $(addsuffix -el.elf,$(TARGETS)) $(addsuffix -eb.elf,$(TARGETS)) gene
ln -srf testdata/loader-$(CLANG)-el.elf testdata/loader-el.elf
ln -srf testdata/loader-$(CLANG)-eb.elf testdata/loader-eb.elf

# $BPF_CLANG is used in go:generate invocations.
generate: export BPF_CLANG := $(CLANG)
generate: export BPF_CFLAGS := $(CFLAGS)
generate:
go generate ./...

Expand Down
11 changes: 8 additions & 3 deletions cmd/bpf2go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ This will emit `foo_bpfel.go` and `foo_bpfeb.go`, with types using `foo`
as a stem. The two files contain compiled BPF for little and big
endian systems, respectively.

## Environment Variables

You can use environment variables to affect all bpf2go invocations
across a project, e.g. to set specific C flags:

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cflags "$BPF_CFLAGS" foo path/to/src.c
BPF2GO_FLAGS="-O2 -g -Wall -Werror $(CFLAGS)" go generate ./...

Alternatively, by exporting `$BPF2GO_FLAGS` from your build system, you can
control all builds from a single location.

By exporting `$BPF_CFLAGS` from your build system you can then control
all builds from a single location.
Most bpf2go arguments can be controlled this way. See `bpf2go -h` for an
up-to-date list.

## Generated types

Expand Down
3 changes: 1 addition & 2 deletions cmd/bpf2go/test/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
// specific API.
package test

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG test ../testdata/minimal.c
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go test ../testdata/minimal.c
3 changes: 1 addition & 2 deletions examples/cgroup_skb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf cgroup_skb.c -- -I../headers
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go bpf cgroup_skb.c -- -I../headers

func main() {
// Allow the current process to lock memory for eBPF resources.
Expand Down
3 changes: 1 addition & 2 deletions examples/fentry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS -type event bpf fentry.c -- -I../headers
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type event bpf fentry.c -- -I../headers

func main() {
stopper := make(chan os.Signal, 1)
Expand Down
3 changes: 1 addition & 2 deletions examples/kprobe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf kprobe.c -- -I../headers
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go bpf kprobe.c -- -I../headers

const mapKey uint32 = 0

Expand Down
3 changes: 1 addition & 2 deletions examples/kprobe_percpu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf kprobe_percpu.c -- -I../headers
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go bpf kprobe_percpu.c -- -I../headers

const mapKey uint32 = 0

Expand Down
3 changes: 1 addition & 2 deletions examples/kprobepin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf kprobe_pin.c -- -I../headers
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go bpf kprobe_pin.c -- -I../headers

const (
mapKey uint32 = 0
Expand Down
3 changes: 1 addition & 2 deletions examples/ringbuffer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (
"golang.org/x/sys/unix"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS -type event bpf ringbuffer.c -- -I../headers
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type event bpf ringbuffer.c -- -I../headers

func main() {
// Name of the kernel function to trace.
Expand Down
3 changes: 1 addition & 2 deletions examples/tcprtt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS -type event bpf tcprtt.c -- -I../headers
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type event bpf tcprtt.c -- -I../headers

func main() {
stopper := make(chan os.Signal, 1)
Expand Down
3 changes: 1 addition & 2 deletions examples/tcprtt_sockops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ import (
"golang.org/x/sys/unix"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags "linux" -cc $BPF_CLANG -cflags $BPF_CFLAGS -type rtt_event bpf tcprtt_sockops.c -- -I../headers
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -tags "linux" -type rtt_event bpf tcprtt_sockops.c -- -I../headers

func main() {
stopper := make(chan os.Signal, 1)
Expand Down
3 changes: 1 addition & 2 deletions examples/tracepoint_in_c/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
"github.com/cilium/ebpf/rlimit"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf tracepoint.c -- -I../headers
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go bpf tracepoint.c -- -I../headers

const mapKey uint32 = 0

Expand Down
3 changes: 1 addition & 2 deletions examples/uretprobe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import (
"golang.org/x/sys/unix"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS -target amd64 -type event bpf uretprobe.c -- -I../headers
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64 -type event bpf uretprobe.c -- -I../headers

const (
// The path to the ELF binary containing the function to trace.
Expand Down
3 changes: 1 addition & 2 deletions examples/xdp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (
"github.com/cilium/ebpf/link"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf xdp.c -- -I../headers
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go bpf xdp.c -- -I../headers

func main() {
if len(os.Args) < 2 {
Expand Down

0 comments on commit 0667fa5

Please sign in to comment.