Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions cmd/bpf2go/gen/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
)

type CompileArgs struct {
// Which compiler to use.
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
Expand All @@ -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.
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions cmd/bpf2go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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")
Expand Down
Loading