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 lines.

The bpf2go command line interface remains unchanged to avoid breaking users.
In the future we can introduce a new -constraints flag and deprecate -tags.

Signed-off-by: Timo Beckers <timo@isovalent.com>
Co-developed-by: Lorenz Bauer <oss@lmb.io>
  • Loading branch information
ti-mo committed Feb 14, 2023
1 parent a4ed94c commit a8362ae
Show file tree
Hide file tree
Showing 27 changed files with 82 additions and 55 deletions.
57 changes: 57 additions & 0 deletions cmd/bpf2go/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"flag"
"go/build/constraint"
)

// buildTags is a comma-separated list of build tags.
//
// This follows the pre-Go 1.17 syntax and is kept for compatibility reasons.
type buildTags struct {
Expr constraint.Expr
}

var _ flag.Value = (*buildTags)(nil)

func (bt *buildTags) String() string {
if bt.Expr == nil {
return ""
}

return (bt.Expr).String()
}

func (bt *buildTags) Set(value string) error {
ct, err := constraint.Parse("// +build " + value)
if err != nil {
return err
}

bt.Expr = ct
return nil
}

func andConstraints(x, y constraint.Expr) constraint.Expr {
if x == nil {
return y
}

if y == nil {
return x
}

return &constraint.AndExpr{X: x, Y: y}
}

func orConstraints(x, y constraint.Expr) constraint.Expr {
if x == nil {
return y
}

if y == nil {
return x
}

return &constraint.OrExpr{X: x, Y: y}
}
25 changes: 11 additions & 14 deletions cmd/bpf2go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"go/build/constraint"
"go/token"
"io"
"os"
Expand Down Expand Up @@ -76,7 +77,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.Var(&b2g.tags, "tags", "Comma-separated list of Go build tags to include 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 @@ -157,10 +158,6 @@ func run(stdout io.Writer, pkg, outputDir string, args []string) (err error) {
return fmt.Errorf("-output-stem %q must not contain path separation characters", b2g.outputStem)
}

if strings.ContainsRune(b2g.tags, '\n') {
return fmt.Errorf("-tags mustn't contain new line characters")
}

targetArches := strings.Split(*flagTarget, ",")
if len(targetArches) == 0 {
return fmt.Errorf("no targets specified")
Expand Down Expand Up @@ -261,8 +258,8 @@ type bpf2go struct {
skipGlobalTypes bool
// C types to include in the generatd output.
cTypes cTypes
// Go tags included in the .go
tags string
// Build tags to be included in the output.
tags buildTags
// Base directory of the Makefile. Enables outputting make-style dependencies
// in .d files.
makeBase string
Expand Down Expand Up @@ -292,14 +289,14 @@ func (b2g *bpf2go) convert(tgt target, arches []string) (err error) {
return err
}

var tags []string
if len(arches) > 0 {
tags = append(tags, strings.Join(arches, " "))
}
if b2g.tags != "" {
tags = append(tags, b2g.tags)
var archConstraint constraint.Expr
for _, arch := range arches {
tag := &constraint.TagExpr{Tag: arch}
archConstraint = orConstraints(archConstraint, tag)
}

constraints := andConstraints(archConstraint, b2g.tags.Expr)

cFlags := make([]string, len(b2g.cFlags))
copy(cFlags, b2g.cFlags)
if tgt.linux != "" {
Expand Down Expand Up @@ -342,7 +339,7 @@ func (b2g *bpf2go) convert(tgt target, arches []string) (err error) {
ident: b2g.ident,
cTypes: b2g.cTypes,
skipGlobalTypes: b2g.skipGlobalTypes,
tags: tags,
constraints: constraints,
obj: objFileName,
out: goFile,
})
Expand Down
29 changes: 14 additions & 15 deletions cmd/bpf2go/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"fmt"
"go/build/constraint"
"go/token"
"io"
"os"
Expand All @@ -19,9 +20,7 @@ import (
const ebpfModule = "github.com/cilium/ebpf"

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

var (
tplFuncs = map[string]interface{}{
tplFuncs = template.FuncMap{
"tag": tag,
}
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
constraints constraint.Expr
cTypes []string
skipGlobalTypes bool
obj string
Expand Down Expand Up @@ -297,20 +296,20 @@ func output(args outputArgs) error {

ctx := struct {
*btf.GoFormatter
Module string
Package string
Tags []string
Name templateName
Maps map[string]string
Programs map[string]string
Types []btf.Type
TypeNames map[btf.Type]string
File string
Module string
Package string
Constraints constraint.Expr
Name templateName
Maps map[string]string
Programs map[string]string
Types []btf.Type
TypeNames map[btf.Type]string
File string
}{
gf,
ebpfModule,
args.pkg,
args.tags,
args.constraints,
templateName(args.ident),
maps,
programs,
Expand Down
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 a8362ae

Please sign in to comment.