From 59af061990e142c695725a81c28d427dad6ae9a9 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 15 Dec 2021 11:10:09 +0100 Subject: [PATCH] cmd/bpf2go: allow to specify native target This allows to invoke `bpf2go` to build for the native architecture and have `__TARGET_ARCH_xxx` defined based on `GOARCH` without having to derive the architecture e.g. as part of the build system. For example this would be useful in github.com/cilium/pwru: //go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang -target native KProbePWRU ./bpf/kprobe_pwru.c -- -DOUTPUT_SKB -I./bpf/headers Signed-off-by: Tobias Klauser --- cmd/bpf2go/main.go | 5 +++++ cmd/bpf2go/main_test.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/cmd/bpf2go/main.go b/cmd/bpf2go/main.go index 0e115f08c..4ff3c1fd8 100644 --- a/cmd/bpf2go/main.go +++ b/cmd/bpf2go/main.go @@ -10,6 +10,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "sort" "strings" ) @@ -358,6 +359,10 @@ func collectTargets(targets []string) (map[target][]string, error) { sort.Strings(goarches) result[target{tgt, ""}] = goarches + case "native": + tgt = runtime.GOARCH + fallthrough + default: archTarget, ok := targetByGoArch[tgt] if !ok || archTarget.linux == "" { diff --git a/cmd/bpf2go/main_test.go b/cmd/bpf2go/main_test.go index afb59b251..8492b8696 100644 --- a/cmd/bpf2go/main_test.go +++ b/cmd/bpf2go/main_test.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "sort" "strings" "testing" @@ -142,6 +143,18 @@ func TestCollectTargets(t *testing.T) { sort.Strings(linuxArchesBE[i]) } + nativeTarget := make(map[target][]string) + for arch, archTarget := range targetByGoArch { + if arch == runtime.GOARCH { + if archTarget.clang == "bpfel" { + nativeTarget[archTarget] = linuxArchesLE[archTarget.linux] + } else { + nativeTarget[archTarget] = linuxArchesBE[archTarget.linux] + } + break + } + } + tests := []struct { targets []string want map[target][]string @@ -167,6 +180,10 @@ func TestCollectTargets(t *testing.T) { {"bpfel", "x86"}: linuxArchesLE["x86"], }, }, + { + []string{"native"}, + nativeTarget, + }, } for _, test := range tests {