]> git.feebdaed.xyz Git - 0xmirror/ebpf.git/commitdiff
bpf2go: Allow multiple commands inside BPF2GO_CC
authorJordan Rife <jrife@google.com>
Tue, 29 Jul 2025 14:01:51 +0000 (15:01 +0100)
committerLorenz Bauer <lmb@users.noreply.github.com>
Tue, 29 Jul 2025 15:57:58 +0000 (16:57 +0100)
Allow multiple commands to be specified inside BPF2GO_CC. Example:

```
BPF2GO_CC="ccache clang" go generate ../pkg/a/b
```

Signed-off-by: Jordan Rife <jrife@google.com>
cmd/bpf2go/gen/compile.go
cmd/bpf2go/main.go

index efd05fb6d9f716499d4695842dd481e3cce90817..954c81d00020210cef93d389c8609df5f697f5f3 100644 (file)
@@ -7,6 +7,7 @@ import (
        "os"
        "os/exec"
        "path/filepath"
+       "strings"
 )
 
 type CompileArgs struct {
@@ -14,7 +15,7 @@ type CompileArgs struct {
        CC string
        // Command used to strip DWARF from the ELF.
        Strip string
-       // Flags to pass to the compiler.
+       // Flags to pass to the compiler. This may contain positional arguments as well.
        Flags []string
        // Absolute working directory
        Workdir string
@@ -27,9 +28,8 @@ type CompileArgs struct {
        DisableStripping bool
 }
 
-// Compile C to a BPF ELF file.
-func Compile(args CompileArgs) error {
-       // Default cflags that can be overridden by args.cFlags
+func insertDefaultFlags(flags []string) []string {
+       // Default cflags that can be overridden by the user.
        overrideFlags := []string{
                // Code needs to be optimized, otherwise the verifier will often fail
                // to understand it.
@@ -40,7 +40,26 @@ func Compile(args CompileArgs) error {
                "-mcpu=v1",
        }
 
-       cmd := exec.Command(args.CC, append(overrideFlags, args.Flags...)...)
+       insert := 0
+
+       // Find the first non-positional argument to support CC commands with
+       // multiple components. E.g.: BPF2GO_CC="ccache clang" ...
+       for ; insert < len(flags); insert++ {
+               if strings.HasPrefix(flags[insert], "-") {
+                       break
+               }
+       }
+
+       result := append([]string(nil), flags[:insert]...)
+       result = append(result, overrideFlags...)
+       result = append(result, flags[insert:]...)
+
+       return result
+}
+
+// Compile C to a BPF ELF file.
+func Compile(args CompileArgs) error {
+       cmd := exec.Command(args.CC, insertDefaultFlags(args.Flags)...)
        cmd.Stderr = os.Stderr
 
        inputDir := filepath.Dir(args.Source)
index b8e8deebb0003500fdaefc124d83a6e8997bb7a6..a0da0838e68a41148f494aa8b55bfcfb912e45d1 100644 (file)
@@ -155,9 +155,12 @@ func newB2G(stdout io.Writer, args []string) (*bpf2go, error) {
                return nil, errors.New("missing package, you should either set the go-package flag or the GOPACKAGE env")
        }
 
-       if b2g.cc == "" {
+       // Allow CC like "ccache clang" to work.
+       ccParts := strings.Fields(b2g.cc)
+       if len(ccParts) == 0 {
                return nil, errors.New("no compiler specified")
        }
+       b2g.cc = ccParts[0]
 
        args, cFlags := splitCFlagsFromArgs(fs.Args())
 
@@ -178,7 +181,7 @@ func newB2G(stdout io.Writer, args []string) (*bpf2go, error) {
                }
        }
 
-       b2g.cFlags = cFlags[:len(cFlags):len(cFlags)]
+       b2g.cFlags = append(ccParts[1:], cFlags[:len(cFlags):len(cFlags)]...)
 
        if len(args) < 2 {
                return nil, errors.New("expected at least two arguments")