Skip to content

Commit

Permalink
cmd/bpf2go: distinguish goarch / linux / clang targets
Browse files Browse the repository at this point in the history
The bpf2go target selection code is currently pretty confusing since
it deals with three different but related concepts:

1. GOARCH values
2. clang -target names
3. libbpf / linux target names

Introduce a type goarch and document type target properly.

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
  • Loading branch information
lmb committed Jan 16, 2024
1 parent a8be855 commit 00aa2c3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
36 changes: 22 additions & 14 deletions cmd/bpf2go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"sort"
"strings"

"golang.org/x/exp/slices"

"github.com/cilium/ebpf"
)

Expand Down Expand Up @@ -47,7 +49,7 @@ Options:
//
// Targets without a Linux string can't be used directly and are only included
// for the generic bpf, bpfel, bpfeb targets.
var targetByGoArch = map[string]target{
var targetByGoArch = map[goarch]target{
"386": {"bpfel", "x86"},
"amd64": {"bpfel", "x86"},
"amd64p32": {"bpfel", ""},
Expand Down Expand Up @@ -96,7 +98,7 @@ type bpf2go struct {
// Valid go identifier.
identStem string
// Targets to build for.
targetArches map[target][]string
targetArches map[target][]goarch
// C compiler.
cc string
// Command used to strip DWARF.
Expand Down Expand Up @@ -298,7 +300,7 @@ func (b2g *bpf2go) convertAll() (err error) {
return nil
}

func (b2g *bpf2go) convert(tgt target, arches []string) (err error) {
func (b2g *bpf2go) convert(tgt target, goarches []goarch) (err error) {
removeOnError := func(f *os.File) {
if err != nil {
os.Remove(f.Name())
Expand All @@ -323,8 +325,8 @@ func (b2g *bpf2go) convert(tgt target, arches []string) (err error) {
}

var archConstraint constraint.Expr
for _, arch := range arches {
tag := &constraint.TagExpr{Tag: arch}
for _, goarch := range goarches {
tag := &constraint.TagExpr{Tag: string(goarch)}
archConstraint = orConstraints(archConstraint, tag)
}

Expand Down Expand Up @@ -419,17 +421,23 @@ func (b2g *bpf2go) convert(tgt target, arches []string) (err error) {
}

type target struct {
// Clang arch string, used to define the clang -target flag, as per
// "clang -print-targets".
clang string
// Linux arch string, used to define __TARGET_ARCH_xzy macros used by
// https://github.com/libbpf/libbpf/blob/master/src/bpf_tracing.h
linux string
}

type goarch string

func printTargets(w io.Writer) {
var arches []string
for arch, archTarget := range targetByGoArch {
for goarch, archTarget := range targetByGoArch {
if archTarget.linux == "" {
continue
}
arches = append(arches, arch)
arches = append(arches, string(goarch))
}
sort.Strings(arches)

Expand All @@ -442,32 +450,32 @@ func printTargets(w io.Writer) {

var errInvalidTarget = errors.New("unsupported target")

func collectTargets(targets []string) (map[target][]string, error) {
result := make(map[target][]string)
func collectTargets(targets []string) (map[target][]goarch, error) {
result := make(map[target][]goarch)
for _, tgt := range targets {
switch tgt {
case "bpf", "bpfel", "bpfeb":
var goarches []string
var goarches []goarch
for arch, archTarget := range targetByGoArch {
if archTarget.clang == tgt {
// Include tags for all goarches that have the same endianness.
goarches = append(goarches, arch)
}
}
sort.Strings(goarches)
slices.Sort(goarches)
result[target{tgt, ""}] = goarches

case "native":
tgt = runtime.GOARCH
fallthrough

default:
archTarget, ok := targetByGoArch[tgt]
archTarget, ok := targetByGoArch[goarch(tgt)]
if !ok || archTarget.linux == "" {
return nil, fmt.Errorf("%q: %w", tgt, errInvalidTarget)
}

var goarches []string
var goarches []goarch
for goarch, lt := range targetByGoArch {
if lt == archTarget {
// Include tags for all goarches that have the same
Expand All @@ -476,7 +484,7 @@ func collectTargets(targets []string) (map[target][]string, error) {
}
}

sort.Strings(goarches)
slices.Sort(goarches)
result[archTarget] = goarches
}
}
Expand Down
26 changes: 13 additions & 13 deletions cmd/bpf2go/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"

"github.com/go-quicktest/qt"
"github.com/google/go-cmp/cmp"
"golang.org/x/exp/slices"
)

func TestRun(t *testing.T) {
Expand Down Expand Up @@ -127,9 +127,9 @@ func TestDisableStripping(t *testing.T) {
}

func TestCollectTargets(t *testing.T) {
clangArches := make(map[string][]string)
linuxArchesLE := make(map[string][]string)
linuxArchesBE := make(map[string][]string)
clangArches := make(map[string][]goarch)
linuxArchesLE := make(map[string][]goarch)
linuxArchesBE := make(map[string][]goarch)
for arch, archTarget := range targetByGoArch {
clangArches[archTarget.clang] = append(clangArches[archTarget.clang], arch)
if archTarget.clang == "bpfel" {
Expand All @@ -139,18 +139,18 @@ func TestCollectTargets(t *testing.T) {
linuxArchesBE[archTarget.linux] = append(linuxArchesBE[archTarget.linux], arch)
}
for i := range clangArches {
sort.Strings(clangArches[i])
slices.Sort(clangArches[i])
}
for i := range linuxArchesLE {
sort.Strings(linuxArchesLE[i])
slices.Sort(linuxArchesLE[i])
}
for i := range linuxArchesBE {
sort.Strings(linuxArchesBE[i])
slices.Sort(linuxArchesBE[i])
}

nativeTarget := make(map[target][]string)
nativeTarget := make(map[target][]goarch)
for arch, archTarget := range targetByGoArch {
if arch == runtime.GOARCH {
if arch == goarch(runtime.GOARCH) {
if archTarget.clang == "bpfel" {
nativeTarget[archTarget] = linuxArchesLE[archTarget.linux]
} else {
Expand All @@ -162,25 +162,25 @@ func TestCollectTargets(t *testing.T) {

tests := []struct {
targets []string
want map[target][]string
want map[target][]goarch
}{
{
[]string{"bpf", "bpfel", "bpfeb"},
map[target][]string{
map[target][]goarch{
{"bpf", ""}: nil,
{"bpfel", ""}: clangArches["bpfel"],
{"bpfeb", ""}: clangArches["bpfeb"],
},
},
{
[]string{"amd64", "386"},
map[target][]string{
map[target][]goarch{
{"bpfel", "x86"}: linuxArchesLE["x86"],
},
},
{
[]string{"amd64", "arm64be"},
map[target][]string{
map[target][]goarch{
{"bpfeb", "arm64"}: linuxArchesBE["arm64"],
{"bpfel", "x86"}: linuxArchesLE["x86"],
},
Expand Down

0 comments on commit 00aa2c3

Please sign in to comment.