Skip to content

Commit 8b53d11

Browse files
jrifelmb
authored andcommitted
bpf2go: Allow multiple commands inside BPF2GO_CC
Allow multiple commands to be specified inside BPF2GO_CC. Example: ``` BPF2GO_CC="ccache clang" go generate ../pkg/a/b ``` Signed-off-by: Jordan Rife <[email protected]>
1 parent 2b837f5 commit 8b53d11

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

cmd/bpf2go/gen/compile.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77
"os"
88
"os/exec"
99
"path/filepath"
10+
"strings"
1011
)
1112

1213
type CompileArgs struct {
1314
// Which compiler to use.
1415
CC string
1516
// Command used to strip DWARF from the ELF.
1617
Strip string
17-
// Flags to pass to the compiler.
18+
// Flags to pass to the compiler. This may contain positional arguments as well.
1819
Flags []string
1920
// Absolute working directory
2021
Workdir string
@@ -27,9 +28,8 @@ type CompileArgs struct {
2728
DisableStripping bool
2829
}
2930

30-
// Compile C to a BPF ELF file.
31-
func Compile(args CompileArgs) error {
32-
// Default cflags that can be overridden by args.cFlags
31+
func insertDefaultFlags(flags []string) []string {
32+
// Default cflags that can be overridden by the user.
3333
overrideFlags := []string{
3434
// Code needs to be optimized, otherwise the verifier will often fail
3535
// to understand it.
@@ -40,7 +40,26 @@ func Compile(args CompileArgs) error {
4040
"-mcpu=v1",
4141
}
4242

43-
cmd := exec.Command(args.CC, append(overrideFlags, args.Flags...)...)
43+
insert := 0
44+
45+
// Find the first non-positional argument to support CC commands with
46+
// multiple components. E.g.: BPF2GO_CC="ccache clang" ...
47+
for ; insert < len(flags); insert++ {
48+
if strings.HasPrefix(flags[insert], "-") {
49+
break
50+
}
51+
}
52+
53+
result := append([]string(nil), flags[:insert]...)
54+
result = append(result, overrideFlags...)
55+
result = append(result, flags[insert:]...)
56+
57+
return result
58+
}
59+
60+
// Compile C to a BPF ELF file.
61+
func Compile(args CompileArgs) error {
62+
cmd := exec.Command(args.CC, insertDefaultFlags(args.Flags)...)
4463
cmd.Stderr = os.Stderr
4564

4665
inputDir := filepath.Dir(args.Source)

cmd/bpf2go/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,12 @@ func newB2G(stdout io.Writer, args []string) (*bpf2go, error) {
155155
return nil, errors.New("missing package, you should either set the go-package flag or the GOPACKAGE env")
156156
}
157157

158-
if b2g.cc == "" {
158+
// Allow CC like "ccache clang" to work.
159+
ccParts := strings.Fields(b2g.cc)
160+
if len(ccParts) == 0 {
159161
return nil, errors.New("no compiler specified")
160162
}
163+
b2g.cc = ccParts[0]
161164

162165
args, cFlags := splitCFlagsFromArgs(fs.Args())
163166

@@ -178,7 +181,7 @@ func newB2G(stdout io.Writer, args []string) (*bpf2go, error) {
178181
}
179182
}
180183

181-
b2g.cFlags = cFlags[:len(cFlags):len(cFlags)]
184+
b2g.cFlags = append(ccParts[1:], cFlags[:len(cFlags):len(cFlags)]...)
182185

183186
if len(args) < 2 {
184187
return nil, errors.New("expected at least two arguments")

0 commit comments

Comments
 (0)