Debian Codegraph @

cmd/bpf2go/gen/target.go

raw symbols SCIP
1 //go:build !windows
2
3 package gen
4
5 import (
6 "errors"
7 "fmt"
8 "go/build/constraint"
9 "maps"
10 "runtime"
11 "slices"
12 )
13
14 var ErrInvalidTarget = errors.New("unsupported target")
15
16 var targetsByGoArch = map[GoArch]Target{
17 "386": {"bpfel", "x86", ""},
18 "amd64": {"bpfel", "x86", ""},
19 "arm": {"bpfel", "arm", ""},
20 "arm64": {"bpfel", "arm64", ""},
21 "loong64": {"bpfel", "loongarch", ""},
22 "mips": {"bpfeb", "mips", ""},
23 "mipsle": {"bpfel", "", ""},
24 "mips64": {"bpfeb", "", ""},
25 "mips64le": {"bpfel", "", ""},
26 "ppc64": {"bpfeb", "powerpc", ""},
27 "ppc64le": {"bpfel", "powerpc", ""},
28 "riscv64": {"bpfel", "riscv", ""},
29 "s390x": {"bpfeb", "s390", ""},
30 "wasm": {"bpfel", "", "js"},
31 }
32
33 type Target struct {
34 // Clang arch string, used to define the clang -target flag, as per
35 // "clang -print-targets".
36 clang string
37 // Linux arch string, used to define __TARGET_ARCH_xzy macros used by
38 // https://github.com/libbpf/libbpf/blob/master/src/bpf_tracing.h
39 linux string
40 // GOOS override for use during tests.
41 goos string
42 }
43
44 // TargetsByGoArch returns all supported targets.
45 func TargetsByGoArch() map[GoArch]Target {
46 return maps.Clone(targetsByGoArch)
47 }
48
49 // IsGeneric returns true if the target will compile to generic BPF.
50 func (tgt *Target) IsGeneric() bool {
51 return tgt.linux == ""
52 }
53
54 // Suffix returns a a string suitable for appending to a file name to
55 // identify the target.
56 func (tgt *Target) Suffix() string {
57 // The output filename must not match any of the following patterns:
58 //
59 // *_GOOS
60 // *_GOARCH
61 // *_GOOS_GOARCH
62 //
63 // Otherwise it is interpreted as a build constraint by the Go toolchain.
64 stem := tgt.clang
65 if tgt.linux != "" {
66 stem = fmt.Sprintf("%s_%s", tgt.linux, tgt.clang)
67 }
68 return stem
69 }
70
71 // ObsoleteSuffix returns an obsolete suffix for a subset of targets.
72 //
73 // It's used to work around an old bug and should not be used in new code.
74 func (tgt *Target) ObsoleteSuffix() string {
75 if tgt.linux == "" {
76 return ""
77 }
78
79 return fmt.Sprintf("%s_%s", tgt.clang, tgt.linux)
80 }
81
82 // GoArch is a Go arch string.
83 //
84 // See https://go.dev/doc/install/source#environment for valid GOARCHes when
85 // GOOS=linux.
86 type GoArch string
87
88 type GoArches []GoArch
89
90 // Constraints is satisfied when GOARCH is any of the arches.
91 func (arches GoArches) Constraint() constraint.Expr {
92 var archConstraint constraint.Expr
93 for _, goarch := range arches {
94 tag := &constraint.TagExpr{Tag: string(goarch)}
95 archConstraint = orConstraints(archConstraint, tag)
96 }
97 return archConstraint
98 }
99
100 // FindTarget turns a list of identifiers into targets and their respective
101 // GoArches.
102 //
103 // The following are valid identifiers:
104 //
105 // - bpf: compile generic BPF for host endianness
106 // - bpfel: compile generic BPF for little endian
107 // - bpfeb: compile generic BPF for big endian
108 // - native: compile BPF for host target
109 // - $GOARCH: compile BPF for $GOARCH target
110 //
111 // Generic BPF can run on any target goarch with the correct endianness,
112 // but doesn't have access to some arch specific tracing functionality.
113 func FindTarget(id string) (Target, GoArches, error) {
114 switch id {
115 case "bpf", "bpfel", "bpfeb":
116 var goarches []GoArch
117 for arch, archTarget := range targetsByGoArch {
118 if archTarget.clang == id {
119 // Include tags for all goarches that have the same endianness.
120 goarches = append(goarches, arch)
121 }
122 }
123 slices.Sort(goarches)
124 return Target{id, "", ""}, goarches, nil
125
126 case "native":
127 id = runtime.GOARCH
128 fallthrough
129
130 default:
131 archTarget, ok := targetsByGoArch[GoArch(id)]
132 if !ok || archTarget.linux == "" {
133 return Target{}, nil, fmt.Errorf("%q: %w", id, ErrInvalidTarget)
134 }
135
136 var goarches []GoArch
137 for goarch, lt := range targetsByGoArch {
138 if lt == archTarget {
139 // Include tags for all goarches that have the same
140 // target.
141 goarches = append(goarches, goarch)
142 }
143 }
144
145 slices.Sort(goarches)
146 return archTarget, goarches, nil
147 }
148 }
149
150 func orConstraints(x, y constraint.Expr) constraint.Expr {
151 if x == nil {
152 return y
153 }
154
155 if y == nil {
156 return x
157 }
158
159 return &constraint.OrExpr{X: x, Y: y}
160 }