From d30592bbcec072fd6988d4e2346b1765f705084b Mon Sep 17 00:00:00 2001 From: seph Date: Fri, 30 Jul 2021 09:47:38 -0400 Subject: [PATCH] Don't always use cgo (#752) Allow finer grained control of when cgo is used. Specifically, to _not_ use cgo when building the osquery-extension --- cmd/make/make.go | 26 +++++++++++--------------- pkg/make/builder.go | 22 ++++++++++++++++++---- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/cmd/make/make.go b/cmd/make/make.go index 8ccc00c13..a6d37494b 100644 --- a/cmd/make/make.go +++ b/cmd/make/make.go @@ -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.") @@ -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] != "" { diff --git a/pkg/make/builder.go b/pkg/make/builder.go index 1755d7405..e95dbdfbb 100644 --- a/pkg/make/builder.go +++ b/pkg/make/builder.go @@ -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 @@ -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 @@ -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, @@ -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")), @@ -149,7 +163,7 @@ func New(opts ...Option) (*Builder, error) { b.cmdEnv = cmdEnv - return &b, nil + return &b } func zigTarget(goos, goarch string) string {