Skip to content

Commit

Permalink
fixup! bpf2go,examples: remove deprecated +build tags from generated …
Browse files Browse the repository at this point in the history
…Go sources
  • Loading branch information
lmb committed Jan 13, 2023
1 parent e994bf4 commit 43ba79b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 38 deletions.
66 changes: 52 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", "", "space-separated list of Go build tags to be ORed together in generated files")
fs.Var(&b2g.constraints, "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 @@ -239,6 +236,33 @@ func (ct *cTypes) Set(value string) error {
return nil
}

// buildTags is a comma-separated list of build constraints.
//
// This is the pre-Go 1.17 syntax and is kept for compatibility reasons.
type buildTags struct {
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
}

type bpf2go struct {
stdout io.Writer
// Absolute path to a .c file.
Expand All @@ -261,9 +285,8 @@ type bpf2go struct {
skipGlobalTypes bool
// C types to include in the generatd output.
cTypes cTypes
// Go tags included in the .go output. Must be space-separated and will be
// ORed together.
tags string
// Build constraints to be included in the output.
constraints buildTags
// Base directory of the Makefile. Enables outputting make-style dependencies
// in .d files.
makeBase string
Expand Down Expand Up @@ -293,13 +316,28 @@ func (b2g *bpf2go) convert(tgt target, arches []string) (err error) {
return err
}

// Strings in the inner slice will be ORed, all outer items will be ANDed.
var tags [][]string
var archConstraint constraint.Expr
if len(arches) > 0 {
tags = append(tags, arches)
archConstraint = &constraint.TagExpr{Tag: arches[0]}
for _, arch := range arches[1:] {
archConstraint = &constraint.OrExpr{
X: archConstraint,
Y: &constraint.TagExpr{Tag: arch},
}
}
}
if b2g.tags != "" {
tags = append(tags, strings.Split(b2g.tags, " "))

var constraints constraint.Expr
switch {
case archConstraint != nil && b2g.constraints.Expr != nil:
constraints = &constraint.AndExpr{
X: b2g.constraints.Expr,
Y: archConstraint,
}
case archConstraint != nil:
constraints = archConstraint
case b2g.constraints.Expr != nil:
constraints = b2g.constraints.Expr
}

cFlags := make([]string, len(b2g.cFlags))
Expand Down Expand Up @@ -344,7 +382,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
38 changes: 14 additions & 24 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,7 +20,7 @@ import (
const ebpfModule = "github.com/cilium/ebpf"

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

var (
tplFuncs = template.FuncMap{
"tag": tag,
"join": joinBuildTags,
"tag": tag,
}
commonTemplate = template.Must(template.New("common").Funcs(tplFuncs).Parse(commonRaw))
)
Expand Down Expand Up @@ -219,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 @@ -296,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 Expand Up @@ -381,13 +381,3 @@ 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, ") && ("))
}

0 comments on commit 43ba79b

Please sign in to comment.