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

bpf2go: optionally add suffix in output file names #1624

Merged
merged 1 commit into from
Jan 8, 2025
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
14 changes: 13 additions & 1 deletion cmd/bpf2go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type bpf2go struct {
outputDir string
// Alternative output stem. If empty, identStem is used.
outputStem string
// Suffix in generated file names such as _test.
outputSuffix string
// Valid go package name.
pkg string
// Valid go identifier.
Expand Down Expand Up @@ -105,6 +107,12 @@ func newB2G(stdout io.Writer, args []string) (*bpf2go, error) {
fs.Var(&b2g.cTypes, "type", "`Name` of a type to generate a Go declaration for, may be repeated")
fs.BoolVar(&b2g.skipGlobalTypes, "no-global-types", false, "Skip generating types for map keys and values, etc.")
fs.StringVar(&b2g.outputStem, "output-stem", "", "alternative stem for names of generated files (defaults to ident)")
outputSuffix := ""
if strings.HasSuffix(getEnv("GOFILE", ""), "_test.go") {
outputSuffix = "_test"
}
fs.StringVar(&b2g.outputSuffix, "output-suffix", outputSuffix,
"suffix in generated file names such as _test (default based on $GOFILE)")
outDir := fs.String("output-dir", "", "target directory of generated files (defaults to current directory)")
outPkg := fs.String("go-package", "", "package for output go file (default as ENV GOPACKAGE)")
fs.SetOutput(b2g.stdout)
Expand Down Expand Up @@ -183,6 +191,10 @@ func newB2G(stdout io.Writer, args []string) (*bpf2go, error) {
return nil, fmt.Errorf("-output-stem %q must not contain path separation characters", b2g.outputStem)
}

if strings.ContainsRune(b2g.outputSuffix, filepath.Separator) {
return nil, fmt.Errorf("-output-suffix %q must not contain path separation characters", b2g.outputSuffix)
}

targetArches := make(map[gen.Target]gen.GoArches)
for _, tgt := range strings.Split(*flagTarget, ",") {
target, goarches, err := gen.FindTarget(tgt)
Expand Down Expand Up @@ -295,7 +307,7 @@ func (b2g *bpf2go) convert(tgt gen.Target, goarches gen.GoArches) (err error) {
outputStem = strings.ToLower(b2g.identStem)
}

stem := fmt.Sprintf("%s_%s", outputStem, tgt.Suffix())
stem := fmt.Sprintf("%s_%s%s", outputStem, tgt.Suffix(), b2g.outputSuffix)

absOutPath, err := filepath.Abs(b2g.outputDir)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions cmd/bpf2go/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,30 @@ func TestParseArgs(t *testing.T) {
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(b2g.outputDir, outputDir))
})

t.Run("output suffix default", func(t *testing.T) {
t.Setenv(gopackageEnv, pkg)
b2g, err := newB2G(&bytes.Buffer{}, []string{stem, csource})
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(b2g.outputSuffix, ""))
})

t.Run("output suffix GOFILE=_test", func(t *testing.T) {
t.Setenv(gopackageEnv, pkg)
t.Setenv("GOFILE", "foo_test.go")
b2g, err := newB2G(&bytes.Buffer{}, []string{stem, csource})
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(b2g.outputSuffix, "_test"))
})

t.Run("output suffix custom", func(t *testing.T) {
t.Setenv(gopackageEnv, pkg)
t.Setenv("GOFILE", "foo_test.go")
args := []string{"-output-suffix", "_custom", stem, csource}
b2g, err := newB2G(&bytes.Buffer{}, args)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(b2g.outputSuffix, "_custom"))
})
}

func mustWriteFile(tb testing.TB, dir, name, contents string) {
Expand Down
Loading