Skip to content

Commit

Permalink
cmd/bpf2go: read more flags from environment
Browse files Browse the repository at this point in the history
Signed-off-by: Puqns67 <me@puqns67.icu>
  • Loading branch information
Puqns67 committed Jan 6, 2024
1 parent 431f71b commit 2b11ad5
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions cmd/bpf2go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ func newB2G(stdout io.Writer, pkg, outputDir string, args []string) (*bpf2go, er
fs := flag.NewFlagSet("bpf2go", flag.ContinueOnError)
fs.StringVar(&b2g.cc, "cc", getEnv("BPF2GO_CC", "clang"),
"`binary` used to compile C to BPF ($BPF2GO_CC)")
fs.StringVar(&b2g.strip, "strip", getEnv("BPF2GO_STRIP", ""),
fs.StringVar(&b2g.strip, "strip", getEnv("BPF2GO_STRIP", "llvm-strip"),
"`binary` used to strip DWARF from compiled BPF ($BPF2GO_STRIP)")
fs.BoolVar(&b2g.disableStripping, "no-strip", false, "disable stripping of DWARF")
fs.BoolVar(&b2g.disableStripping, "no-strip", getBoolEnv("BPF2GO_NOSTRIP", false),
"disable stripping of DWARF ($BPF2GO_NOSTRIP)")
flagCFlags := fs.String("cflags", getEnv("BPF2GO_CFLAGS", ""),
"flags passed to the compiler, may contain quoted arguments ($BPF2GO_CFLAGS)")
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)")
flagTarget := fs.String("target", getEnv("BPF2GO_TARGET", "bpfel,bpfeb"),
"clang target(s) to compile for (comma separated) ($BPF2GO_TARGET)")
fs.StringVar(&b2g.makeBase, "makebase", getEnv("BPF2GO_MAKEBASE", ""),
"write make compatible depinfo files relative to `directory` ($BPF2GO_MAKEBASE)")
fs.Var(&b2g.cTypes, "type", "`Name` of a type to generate a Go declaration for, may be repeated")
Expand Down Expand Up @@ -218,15 +220,6 @@ func newB2G(stdout io.Writer, pkg, outputDir string, args []string) (*bpf2go, er
}
b2g.targetArches = targetArches

// Try to find a suitable llvm-strip, possibly with a version suffix derived
// from the clang binary.
if b2g.strip == "" {
b2g.strip = "llvm-strip"
if strings.HasPrefix(b2g.cc, "clang") {
b2g.strip += strings.TrimPrefix(b2g.cc, "clang")
}
}

return b2g, nil
}

Expand Down Expand Up @@ -275,6 +268,15 @@ func getEnv(key, defaultVal string) string {
return defaultVal
}

func getBoolEnv(key string, defaultVal bool) bool {
val, ok := os.LookupEnv(key)
if !ok {
return defaultVal
}
val = strings.ToLower(val)
return val == "true" || val == "yes" || val == "y" || val == "1"
}

func (b2g *bpf2go) convertAll() (err error) {
if _, err := os.Stat(b2g.sourceFile); os.IsNotExist(err) {
return fmt.Errorf("file %s doesn't exist", b2g.sourceFile)
Expand All @@ -283,8 +285,13 @@ func (b2g *bpf2go) convertAll() (err error) {
}

if !b2g.disableStripping {
b2g.strip, err = exec.LookPath(b2g.strip)
if err != nil {
// Try to find a suitable llvm-strip, possibly with a version suffix derived
// from the clang binary.
if b2g.strip == "llvm-strip" && strings.HasPrefix(b2g.cc, "clang") {
b2g.strip += strings.TrimPrefix(b2g.cc, "clang")
}

if b2g.strip, err = exec.LookPath(b2g.strip); err != nil {
return err
}
}
Expand Down

0 comments on commit 2b11ad5

Please sign in to comment.