@@ -7,14 +7,15 @@ import (
7
7
"os"
8
8
"os/exec"
9
9
"path/filepath"
10
+ "strings"
10
11
)
11
12
12
13
type CompileArgs struct {
13
14
// Which compiler to use.
14
15
CC string
15
16
// Command used to strip DWARF from the ELF.
16
17
Strip string
17
- // Flags to pass to the compiler.
18
+ // Flags to pass to the compiler. This may contain positional arguments as well.
18
19
Flags []string
19
20
// Absolute working directory
20
21
Workdir string
@@ -27,9 +28,8 @@ type CompileArgs struct {
27
28
DisableStripping bool
28
29
}
29
30
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.
33
33
overrideFlags := []string {
34
34
// Code needs to be optimized, otherwise the verifier will often fail
35
35
// to understand it.
@@ -40,7 +40,26 @@ func Compile(args CompileArgs) error {
40
40
"-mcpu=v1" ,
41
41
}
42
42
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 )... )
44
63
cmd .Stderr = os .Stderr
45
64
46
65
inputDir := filepath .Dir (args .Source )
0 commit comments