Skip to content

Commit

Permalink
Don't always use cgo (#752)
Browse files Browse the repository at this point in the history
Allow finer grained control of when cgo is used. Specifically, to _not_ use cgo when building the osquery-extension
  • Loading branch information
directionless authored Jul 30, 2021
1 parent 91374c0 commit d30592b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
26 changes: 11 additions & 15 deletions cmd/make/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
flDebug = fs.Bool("debug", false, "use a debug logger")
flBuildARCH = fs.String("arch", runtime.GOARCH, "Architecture to build for.")
flBuildOS = fs.String("os", runtime.GOOS, "Operating system to build for.")
flGoPath = fs.String("go", "", "Path for go binary. Will attempt auto detection")
flGoPath = fs.String("go", "", "Path for go binary. Will attempt auto detection.")
flRace = fs.Bool("race", false, "Build race-detector version of binaries.")
flStatic = fs.Bool("static", false, "Build a static binary.")
flStampVersion = fs.Bool("linkstamp", false, "Add version info with ldflags.")
Expand Down Expand Up @@ -70,22 +70,18 @@ func main() {
opts = append(opts, make.WithGoPath(*flGoPath))
}

b, err := make.New(opts...)
if err != nil {
logutil.Fatal(logger, "msg", "Failed to create builder", "err", err)

}
optsWithCgo := append(opts, make.WithCgo())

targetSet := map[string]func(context.Context) error{
"deps-go": b.DepsGo,
"install-tools": b.InstallTools,
"generate-tuf": b.GenerateTUF,
"launcher": b.BuildCmd("./cmd/launcher", fakeName("launcher", *flFakeData)),
"osquery-extension.ext": b.BuildCmd("./cmd/osquery-extension", "osquery-extension.ext"),
"tables.ext": b.BuildCmd("./cmd/launcher.ext", "tables.ext"),
"grpc.ext": b.BuildCmd("./cmd/grpc.ext", "grpc.ext"),
"package-builder": b.BuildCmd("./cmd/package-builder", "package-builder"),
"make": b.BuildCmd("./cmd/make", "make"),
"deps-go": make.New(opts...).DepsGo,
"install-tools": make.New(opts...).InstallTools,
"generate-tuf": make.New(opts...).GenerateTUF,
"launcher": make.New(optsWithCgo...).BuildCmd("./cmd/launcher", fakeName("launcher", *flFakeData)),
"osquery-extension.ext": make.New(opts...).BuildCmd("./cmd/osquery-extension", "osquery-extension.ext"),
"tables.ext": make.New(optsWithCgo...).BuildCmd("./cmd/launcher.ext", "tables.ext"),
"grpc.ext": make.New(opts...).BuildCmd("./cmd/grpc.ext", "grpc.ext"),
"package-builder": make.New(opts...).BuildCmd("./cmd/package-builder", "package-builder"),
"make": make.New(opts...).BuildCmd("./cmd/make", "make"),
}

if t := strings.Split(*flTargets, ","); len(t) != 0 && t[0] != "" {
Expand Down
22 changes: 18 additions & 4 deletions pkg/make/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Builder struct {
stampVersion bool
fakedata bool
notStripped bool
cgo bool

cmdEnv []string
execCC func(context.Context, string, ...string) *exec.Cmd
Expand All @@ -74,6 +75,12 @@ func WithArch(a string) Option {
}
}

func WithCgo() Option {
return func(b *Builder) {
b.cgo = true
}
}

func WithStatic() Option {
return func(b *Builder) {
b.static = true
Expand Down Expand Up @@ -104,7 +111,7 @@ func WithFakeData() Option {
}
}

func New(opts ...Option) (*Builder, error) {
func New(opts ...Option) *Builder {
b := Builder{
os: runtime.GOOS,
arch: runtime.GOARCH,
Expand All @@ -124,17 +131,24 @@ func New(opts ...Option) (*Builder, error) {
cmdEnv = append(cmdEnv, fmt.Sprintf("GOOS=%s", b.os))
cmdEnv = append(cmdEnv, fmt.Sprintf("GOARCH=%s", b.arch))

if b.cgo {
cmdEnv = append(cmdEnv, "CGO_ENABLED=1")
}

// Setup zig as cross compiler
// (This is mostly to support fscrypt on linux)
if b.os != runtime.GOOS {
cwd, err := os.Getwd()
if err != nil {
return nil, errors.Wrap(err, "getting cwd")
// panic here feels a little uncouth, but the
// caller here is a bunch simpler if we can
// return *Builder, and this error is
// exceedingly unlikely.
panic(fmt.Sprintf("Unable to get cwd: %s", err))
}

cmdEnv = append(
cmdEnv,
"CGO_ENABLED=1",
fmt.Sprintf("ZIGTARGET=%s", zigTarget(b.os, b.arch)),
fmt.Sprintf("CC=%s", filepath.Join(cwd, "tools", "zcc")),
fmt.Sprintf("CXX=%s", filepath.Join(cwd, "tools", "zxx")),
Expand All @@ -149,7 +163,7 @@ func New(opts ...Option) (*Builder, error) {

b.cmdEnv = cmdEnv

return &b, nil
return &b
}

func zigTarget(goos, goarch string) string {
Expand Down

0 comments on commit d30592b

Please sign in to comment.