Skip to content

Commit

Permalink
bpf2go: add flag for alternative output stem
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusWichelmann committed Sep 26, 2022
1 parent 682bccb commit 7673bb0
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions cmd/bpf2go/main.go
Expand Up @@ -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() {
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 7673bb0

Please sign in to comment.