Skip to content

Commit

Permalink
#101: cross-compile for other architectures
Browse files Browse the repository at this point in the history
  • Loading branch information
xelalexv committed Jan 26, 2024
1 parent b78b46f commit 49d74bf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
25 changes: 15 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
#
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions hack/devenvutil
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
#
Expand Down

0 comments on commit 49d74bf

Please sign in to comment.