diff --git a/cmd/bpf2go/main.go b/cmd/bpf2go/main.go index 55a462da0..03ff1f815 100644 --- a/cmd/bpf2go/main.go +++ b/cmd/bpf2go/main.go @@ -81,6 +81,7 @@ func run(stdout io.Writer, pkg, outputDir string, args []string) (err error) { 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") 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)") fs.SetOutput(stdout) fs.Usage = func() { @@ -152,6 +153,10 @@ func run(stdout io.Writer, pkg, outputDir string, args []string) (err error) { } } + if b2g.outputStem != "" && strings.ContainsRune(b2g.outputStem, filepath.Separator) { + 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") } @@ -240,6 +245,8 @@ type bpf2go struct { sourceFile string // Absolute path to a directory where .go are written outputDir string + // Alternative output stem. If empty, ident is used. + outputStem string // Valid go package name. pkg string // Valid go identifier. @@ -269,9 +276,13 @@ func (b2g *bpf2go) convert(tgt target, arches []string) (err error) { f.Close() } - stem := fmt.Sprintf("%s_%s", strings.ToLower(b2g.ident), tgt.clang) + outputStem := b2g.outputStem + if outputStem == "" { + outputStem = strings.ToLower(b2g.ident) + } + stem := fmt.Sprintf("%s_%s", outputStem, tgt.clang) if tgt.linux != "" { - stem = fmt.Sprintf("%s_%s_%s", strings.ToLower(b2g.ident), tgt.clang, tgt.linux) + stem = fmt.Sprintf("%s_%s_%s", outputStem, tgt.clang, tgt.linux) } objFileName := filepath.Join(b2g.outputDir, stem+".o")