Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated +build Go build tags #888

Merged
merged 3 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ KeepEmptyLinesAtTheStartOfBlocks: false
TabWidth: 4
UseTab: ForContinuationAndIndentation
ColumnLimit: 1000
# Go compiler comments need to stay unindented.
CommentPragmas: '^go:.*'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting!

...
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 example_sock_elf_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build linux
// +build linux

package ebpf_test

Expand Down
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.

2 changes: 1 addition & 1 deletion examples/cgroup_skb/cgroup_skb.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build ignore
//go:build ignore

#include "common.h"

Expand Down
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.

2 changes: 1 addition & 1 deletion examples/fentry/fentry.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build ignore
//go:build ignore

#include "common.h"

Expand Down
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.

2 changes: 1 addition & 1 deletion examples/kprobe/kprobe.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build ignore
//go:build ignore

#include "common.h"

Expand Down
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.

2 changes: 1 addition & 1 deletion examples/kprobe_percpu/kprobe_percpu.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build ignore
//go:build ignore

#include "common.h"

Expand Down
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.

2 changes: 1 addition & 1 deletion examples/kprobepin/kprobe_pin.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build ignore
//go:build ignore

#include "common.h"

Expand Down
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.

2 changes: 1 addition & 1 deletion examples/ringbuffer/ringbuffer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build ignore
//go:build ignore

#include "common.h"

Expand Down
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: 1 addition & 1 deletion examples/tcprtt/tcprtt.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build ignore
//go:build ignore

#include "common.h"

Expand Down
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
2 changes: 1 addition & 1 deletion examples/tcprtt_sockops/tcprtt_sockops.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build ignore
//go:build ignore

#include "common.h"

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.

Loading