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: add flag for alternative filename stem #770

Merged
merged 1 commit into from Sep 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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