From 5dd744b410281739044824e7b0c351b28554759d Mon Sep 17 00:00:00 2001 From: Rebecca Mahany-Horton Date: Thu, 20 Apr 2023 16:36:52 -0400 Subject: [PATCH] Cross-compile for Linux (#1152) --- .github/workflows/go.yml | 9 ++++----- Makefile | 2 -- cmd/make/make.go | 4 ++-- pkg/make/builder.go | 3 +++ 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 9b3b0e6a3..99aca75d9 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -60,14 +60,13 @@ jobs: - name: Run govulncheck run: go install golang.org/x/vuln/cmd/govulncheck@latest; govulncheck ./... + - name: Set up zig + if: ${{ contains(matrix.os, 'ubuntu') }} + uses: goto-bus-stop/setup-zig@v2 + - name: Build - if: ${{ ! contains(matrix.os, 'ubuntu') }} run: make -j2 github-build - - name: Build (No Cross Compiles) - if: ${{ contains(matrix.os, 'ubuntu') }} - run: make -j2 github-build-no-cross - - name: Lipo run: make github-lipo if: ${{ contains(matrix.os, 'macos') }} diff --git a/Makefile b/Makefile index a8225e056..08526b96a 100644 --- a/Makefile +++ b/Makefile @@ -81,8 +81,6 @@ build/darwin.universal/%: lipo_% ## GITHUB_TARGETS=launcher grpc.ext tables.ext package-builder GITHUB_ARCHS=amd64 arm64 -# linux cross compiles aren't working. Disable for now -github-build-no-cross: $(foreach t, $(GITHUB_TARGETS), build_$(t)) github-build: $(foreach t, $(GITHUB_TARGETS), $(foreach a, $(GITHUB_ARCHS), build_$(t)_noop_$(a))) github-lipo: $(foreach t, $(GITHUB_TARGETS), lipo_$(t)) github-launcherapp: $(foreach a, $(GITHUB_ARCHS) universal, build/darwin.$(a)/Kolide.app) diff --git a/cmd/make/make.go b/cmd/make/make.go index 39e660f39..f3f0862f9 100644 --- a/cmd/make/make.go +++ b/cmd/make/make.go @@ -81,7 +81,7 @@ func main() { // on linux (for fscrypt) optsMaybeCgo := opts if *flBuildOS == "linux" { - // overwrite with append, since optsMaybyeCgo was a shallow clone + // overwrite with append, since optsMaybeCgo was a shallow clone optsMaybeCgo = append(opts, make.WithCgo()) } @@ -91,7 +91,7 @@ func main() { "generate-tuf": make.New(opts...).GenerateTUF, "launcher": make.New(optsMaybeCgo...).BuildCmd("./cmd/launcher", fakeName("launcher", *flFakeData)), "tables.ext": make.New(optsMaybeCgo...).BuildCmd("./cmd/launcher.ext", "tables.ext"), - "grpc.ext": make.New(opts...).BuildCmd("./cmd/grpc.ext", "grpc.ext"), + "grpc.ext": make.New(optsMaybeCgo...).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"), } diff --git a/pkg/make/builder.go b/pkg/make/builder.go index a6ddc7cc7..d4668bab2 100644 --- a/pkg/make/builder.go +++ b/pkg/make/builder.go @@ -503,6 +503,9 @@ func (b *Builder) BuildCmd(src, appName string) func(context.Context) error { var ldFlags []string if b.static { ldFlags = append(ldFlags, "-d -linkmode internal") + } else if b.os == "linux" && b.arch != runtime.GOARCH { + // Cross-compiling for Linux requires external linking + ldFlags = append(ldFlags, "-linkmode external") } if !b.notStripped {