Skip to content

Commit

Permalink
cmd/bpf2go: allow to specify native target
Browse files Browse the repository at this point in the history
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 <tobias@cilium.io>
  • Loading branch information
tklauser authored and lmb committed Dec 15, 2021
1 parent 5032de0 commit 59af061
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/bpf2go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
)
Expand Down Expand Up @@ -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 == "" {
Expand Down
17 changes: 17 additions & 0 deletions cmd/bpf2go/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"
Expand Down Expand Up @@ -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
Expand All @@ -167,6 +180,10 @@ func TestCollectTargets(t *testing.T) {
{"bpfel", "x86"}: linuxArchesLE["x86"],
},
},
{
[]string{"native"},
nativeTarget,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 59af061

Please sign in to comment.