From 49d74bf6cd4e479c1705fcc9c14c9e3139093559 Mon Sep 17 00:00:00 2001 From: Alexander Vollschwitz Date: Fri, 26 Jan 2024 10:41:30 +0100 Subject: [PATCH] #101: cross-compile for other architectures --- Makefile | 25 +++++++++++++++---------- README.md | 2 ++ hack/devenvutil | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index f2e9b30..01f0705 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ SHELL = /bin/bash REPO = dregsy -DREGSY_VERSION = $$(git describe --always --tag --dirty) +DREGSY_VERSION = $(shell git describe --always --tag --dirty) SKOPEO_VERSION = v1.14.1 # https://github.com/containers/skopeo/releases ROOT = $(shell pwd) @@ -87,6 +87,8 @@ GOARCH = $(shell ./hack/devenvutil get_architecture) # running ${DIM}make clean${NRM}. That way you can force a clean build/test, where all # dependencies are retrieved & built inside the container. # +# ${ITL}CROSS=y${NRM} set this to build binaries for various platforms & architectures +# # ${ITL}TEST_ALPINE=n${NRM} when using this with the test target, tests will not be performed # ${ITL}TEST_UBUNTU=n${NRM} for the respective image (${ITL}Alpine${NRM} or ${ITL}Ubuntu${NRM} based) # @@ -166,15 +168,18 @@ publish: dregsy: prep # build the ${ITL}dregsy${NRM} binary # - echo "os: $(GOOS), arch: $(GOARCH)" - docker run --rm --user $(shell id -u):$(shell id -g) \ - -v $(shell pwd)/$(BINARIES):/go/bin $(CACHE_VOLS) \ - -v $(shell pwd):/go/src/$(REPO) -w /go/src/$(REPO) \ - -e CGO_ENABLED=0 -e GOOS=$(GOOS) -e GOARCH=$(GOARCH) \ - $(GO_IMAGE)@sha256:$(GO_IMAGE_DIGEST_$(GOARCH)) bash -c \ - "go mod tidy && go build -v -tags netgo -installsuffix netgo \ - -ldflags \"-w -X main.DregsyVersion=$(DREGSY_VERSION)\" \ - -o $(BINARIES)/dregsy ./cmd/dregsy/" + rm -f $(BINARIES)/dregsy +ifneq ($(CROSS),y) + $(call utils, build_binary dregsy $(GOOS) $(GOARCH) keep) +else + $(call utils, build_binary dregsy linux amd64 keep) + $(call utils, build_binary dregsy linux arm64) + $(call utils, build_binary dregsy linux arm) + $(call utils, build_binary dregsy linux 386) +endif + unzip -q $(BINARIES)/dregsy_*_linux_amd64.zip -d $(BINARIES) 2>/dev/null \ + || true + cd $(BINARIES); sha256sum dregsy_*.zip > checksums.txt .PHONY: imgdregsy diff --git a/README.md b/README.md index 8ab3635..114b862 100644 --- a/README.md +++ b/README.md @@ -317,6 +317,8 @@ Logging behavior can be changed with these environment variables: | `LOG_METHODS` | include method names in log messages | `true`, `false` | ### Running Natively +Pre-built binaries are provided in the release section of this project. Note however that currently, only the `linux_amd64` flavor is tested upon release. All other binaries are untested. Feedback is welcome! + If you run *dregsy* natively on your system, with relay type `docker`, the *Docker* daemon of your system will be used as the relay for all sync tasks, so all synced images will wind up in the *Docker* storage of that daemon. ### Running Inside a Container diff --git a/hack/devenvutil b/hack/devenvutil index fe8984c..1f8932e 100755 --- a/hack/devenvutil +++ b/hack/devenvutil @@ -143,6 +143,41 @@ function remove_test_images { done } +# +# build command binary +# +# $1 command +# $2 target OS +# $3 target architecture +# +function build_binary { + + local binary="${BINARIES}/$1" + + local extra_env + [[ "$3" != "arm" ]] || extra_env="-e GOARM=7" + [[ "$3" != "mips" ]] || extra_env="-e GOMIPS=softfloat" + + local digest_ref="GO_IMAGE_DIGEST_${GOARCH}" + + echo -e "building ${binary} for $2/$3, using build image ${GO_IMAGE}@sha256:${!digest_ref}" + # shellcheck disable=SC2086 + docker run --rm --user "$(id -u):$(id -g)" \ + -v "${ROOT}/${BINARIES}:/go/bin" ${CACHE_VOLS} \ + -v "${ROOT}:/go/src/${REPO}" -w "/go/src/${REPO}" \ + -e CGO_ENABLED=0 -e GOOS="$2" -e GOARCH="$3" ${extra_env} \ + ${GO_IMAGE}@sha256:${!digest_ref} bash -c \ + "go mod tidy && go build -v -tags netgo -installsuffix netgo \ + -ldflags \"-w -X main.DregsyVersion=${DREGSY_VERSION}\" \ + -o \"${binary}\" \"./cmd/$1/\"" + + local specifier="_${DREGSY_VERSION}_$2_$3" + zip -j "../${binary}${specifier}.zip" "../${binary}" + rm -f "../${binary}" + + echo +} + # # $1 test image variant (`alpine` or `ubuntu`) #