diff --git a/cmd/bpf2go/main.go b/cmd/bpf2go/main.go index 92d72277b..2bcf111dd 100644 --- a/cmd/bpf2go/main.go +++ b/cmd/bpf2go/main.go @@ -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") @@ -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 } @@ -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) @@ -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 } }