From 24638de665a09a01f6373d3a9273e1949acd0795 Mon Sep 17 00:00:00 2001 From: Oleksii Kliukin Date: Thu, 11 May 2017 15:58:26 +0200 Subject: [PATCH] Linux race-enabled builds inside Docker. (#123) Cross-platform go builds with linux as a target platform and race enabled require to link against the linux so and have the cross platform compiler installed, which is a pain. To avoid this we build everything in Linux-native environment in a container. Note that running such builds requires replacing the operator Docker base image from Alpine to something with a real glibc (i.e. ubuntu), since the target binary needs a dynamic linker. Use -a flag in order to rebuild all packages, since we might have built them on a wrong platform. --- Makefile | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 002ca33e7..ac82fbb68 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,13 @@ .PHONY: clean local linux macos docker push scm-source.json +BINARY ?= postgres-operator +BUILD_FLAGS ?= -v -i ifeq ($(RACE),1) - GOFLAGS=-race + BUILD_FLAGS += -race -a + CGO_ENABLED=1 endif -BINARY ?= postgres-operator -BUILD_FLAGS ?= -v -LOCAL_BUILD_FLAGS ?= $(BUILD_FLAGS) -i +LOCAL_BUILD_FLAGS ?= $(BUILD_FLAGS) LDFLAGS ?= -X=main.version=$(VERSION) DOCKERFILE = docker/Dockerfile IMAGE ?= pierone.example.com/acid/$(BINARY) @@ -33,13 +34,13 @@ linux: build/linux/${BINARY} macos: build/macos/${BINARY} build/${BINARY}: ${SOURCES} - go build -o $@ $(LOCAL_BUILD_FLAGS) -ldflags "$(LDFLAGS)" $^ + CGO_ENABLED=${CGO_ENABLED} go build -o $@ $(LOCAL_BUILD_FLAGS) -ldflags "$(LDFLAGS)" $^ build/linux/${BINARY}: ${SOURCES} - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o $@ ${BUILD_FLAGS} -ldflags "$(LDFLAGS)" $^ + GOOS=linux GOARCH=amd64 CGO_ENABLED=${CGO_ENABLED} go build -o $@ ${BUILD_FLAGS} -ldflags "$(LDFLAGS)" $^ build/macos/${BINARY}: ${SOURCES} - GOOS=darwin GOARCH=amd64 go build -o $@ ${BUILD_FLAGS} -ldflags "$(LDFLAGS)" $^ + GOOS=darwin GOARCH=amd64 CGO_ENABLED=${CGO_ENABLED} go build -o $@ ${BUILD_FLAGS} -ldflags "$(LDFLAGS)" $^ docker-context: scm-source.json linux mkdir -p docker/build/ @@ -48,6 +49,9 @@ docker-context: scm-source.json linux docker: ${DOCKERFILE} docker-context cd docker && docker build --rm -t "$(IMAGE):$(TAG)" . +indocker-race: + docker run --rm -v "${GOPATH}":"${GOPATH}" -e GOPATH="${GOPATH}" -e RACE=1 -w ${PWD} golang:1.8.1 bash -c "make linux" + push: docker push "$(IMAGE):$(TAG)"