From 6d3719c09fb219f98fbb7958128ba2a2239d554a Mon Sep 17 00:00:00 2001 From: Andrew Lytvynov Date: Thu, 4 Feb 2021 12:39:27 -0800 Subject: [PATCH] Fix windows cross-compilation with CGO Install `mingw-w64` cross-compiler toolchain in the buildbox and pass magic flags to `go build` to use it. --- Makefile | 6 ++++++ build.assets/Dockerfile | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 559a952a4fed0..d84c83367553c 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,12 @@ TELEPORT_DEBUG ?= no GITTAG=v$(VERSION) BUILDFLAGS ?= $(ADDFLAGS) -ldflags '-w -s' CGOFLAG ?= CGO_ENABLED=1 +# Windows requires extra parameters to cross-compile with CGO. +ifeq ("$(OS)","windows") +BUILDFLAGS = $(ADDFLAGS) -ldflags '-w -s' -buildmode=exe +CGOFLAG = CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ +endif + GO_LINTERS ?= "unused,govet,typecheck,deadcode,goimports,varcheck,structcheck,bodyclose,staticcheck,ineffassign,unconvert,misspell,gosimple,golint" OS ?= $(shell go env GOOS) diff --git a/build.assets/Dockerfile b/build.assets/Dockerfile index b2cd6ada4129b..79828062181b2 100644 --- a/build.assets/Dockerfile +++ b/build.assets/Dockerfile @@ -19,7 +19,26 @@ ENV LANGUAGE="en_US.UTF-8" \ RUN apt-get update -y --fix-missing && \ apt-get -q -y upgrade && \ - apt-get install -q -y apt-utils curl gcc gcc-multilib git gzip libbpfcc-dev libc6-dev libpam-dev libsqlite3-0 locales make net-tools tar tree zip shellcheck && \ + apt-get install -q -y \ + apt-utils \ + curl \ + gcc \ + gcc-multilib \ + git \ + gzip \ + libbpfcc-dev \ + libc6-dev \ + libpam-dev \ + libsqlite3-0 \ + locales \ + make \ + mingw-w64 \ + net-tools \ + shellcheck \ + tar \ + tree \ + zip \ + && \ dpkg-reconfigure locales && \ apt-get -y autoclean && apt-get -y clean