Skip to content

Commit

Permalink
bpf2go,examples: remove deprecated +build tags from generated Go sources
Browse files Browse the repository at this point in the history
To follow suit with the new //go:build convention of build tags, this commit
converts bpf2go to no longer emit the deprecated +build ones. Specify that
currently, all given tags are ORed together.

Signed-off-by: Timo Beckers <timo@isovalent.com>
  • Loading branch information
ti-mo committed Dec 13, 2022
1 parent 9e4f448 commit e994bf4
Show file tree
Hide file tree
Showing 26 changed files with 23 additions and 38 deletions.
12 changes: 7 additions & 5 deletions cmd/bpf2go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func run(stdout io.Writer, pkg, outputDir string, args []string) (err error) {
fs.StringVar(&b2g.strip, "strip", "", "`binary` used to strip DWARF from compiled BPF (default \"llvm-strip\")")
fs.BoolVar(&b2g.disableStripping, "no-strip", false, "disable stripping of DWARF")
flagCFlags := fs.String("cflags", "", "flags passed to the compiler, may contain quoted arguments")
fs.StringVar(&b2g.tags, "tags", "", "list of Go build tags to include in generated files")
fs.StringVar(&b2g.tags, "tags", "", "space-separated list of Go build tags to be ORed together in generated files")
flagTarget := fs.String("target", "bpfel,bpfeb", "clang target(s) to compile for (comma separated)")
fs.StringVar(&b2g.makeBase, "makebase", "", "write make compatible depinfo files relative to `directory`")
fs.Var(&b2g.cTypes, "type", "`Name` of a type to generate a Go declaration for, may be repeated")
Expand Down Expand Up @@ -261,7 +261,8 @@ type bpf2go struct {
skipGlobalTypes bool
// C types to include in the generatd output.
cTypes cTypes
// Go tags included in the .go
// Go tags included in the .go output. Must be space-separated and will be
// ORed together.
tags string
// Base directory of the Makefile. Enables outputting make-style dependencies
// in .d files.
Expand Down Expand Up @@ -292,12 +293,13 @@ func (b2g *bpf2go) convert(tgt target, arches []string) (err error) {
return err
}

var tags []string
// Strings in the inner slice will be ORed, all outer items will be ANDed.
var tags [][]string
if len(arches) > 0 {
tags = append(tags, strings.Join(arches, " "))
tags = append(tags, arches)
}
if b2g.tags != "" {
tags = append(tags, b2g.tags)
tags = append(tags, strings.Split(b2g.tags, " "))
}

cFlags := make([]string, len(b2g.cFlags))
Expand Down
23 changes: 16 additions & 7 deletions cmd/bpf2go/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import (
const ebpfModule = "github.com/cilium/ebpf"

const commonRaw = `// Code generated by bpf2go; DO NOT EDIT.
{{- range .Tags }}
// +build {{ . }}
{{- end }}
//go:build {{ join .Tags }}
package {{ .Package }}
Expand Down Expand Up @@ -161,8 +159,9 @@ var {{ .Name.Bytes }} []byte
`

var (
tplFuncs = map[string]interface{}{
"tag": tag,
tplFuncs = template.FuncMap{
"tag": tag,
"join": joinBuildTags,
}
commonTemplate = template.Must(template.New("common").Funcs(tplFuncs).Parse(commonRaw))
)
Expand Down Expand Up @@ -220,7 +219,7 @@ func (n templateName) CloseHelper() string {
type outputArgs struct {
pkg string
ident string
tags []string
tags [][]string
cTypes []string
skipGlobalTypes bool
obj string
Expand Down Expand Up @@ -299,7 +298,7 @@ func output(args outputArgs) error {
*btf.GoFormatter
Module string
Package string
Tags []string
Tags [][]string
Name templateName
Maps map[string]string
Programs map[string]string
Expand Down Expand Up @@ -382,3 +381,13 @@ func sortTypes(typeNames map[btf.Type]string) ([]btf.Type, error) {
func tag(str string) string {
return "`ebpf:\"" + str + "\"`"
}

// joinBuildTags joins all inner strings by " || ", all outer strings by ") && (", and
// wraps the whole string in ().
func joinBuildTags(tags [][]string) string {
var and []string
for _, or := range tags {
and = append(and, strings.Join(or, " || "))
}
return fmt.Sprintf("(%s)", strings.Join(and, ") && ("))
}
1 change: 0 additions & 1 deletion cmd/bpf2go/test/test_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion cmd/bpf2go/test/test_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/cgroup_skb/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/cgroup_skb/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/fentry/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/fentry/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/kprobe/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/kprobe/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/kprobe_percpu/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/kprobe_percpu/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/kprobepin/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/kprobepin/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/ringbuffer/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/ringbuffer/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/tcprtt/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/tcprtt/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions examples/tcprtt_sockops/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions examples/tcprtt_sockops/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/tcprtt_sockops/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build linux
// +build linux

// This program demonstrates attaching an eBPF program to
// a cgroupv2 path and using sockops to process TCP socket events.
Expand Down
1 change: 0 additions & 1 deletion examples/tracepoint_in_c/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/tracepoint_in_c/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/uretprobe/bpf_bpfel_x86.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/xdp/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/xdp/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e994bf4

Please sign in to comment.