Skip to content

Commit

Permalink
build: prepare release binaries
Browse files Browse the repository at this point in the history
Compile static binaries for i386 and x86_64.

Use docker to simplify cross-compilation process.
  • Loading branch information
nradchenko committed Dec 22, 2020
1 parent 57354cf commit 9f6adb9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
15 changes: 15 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.git/
.gitignore

.idea/

.dockerignore
Dockerfile

cmd/mp707/mp707

LICENSE
README.md

release/
scripts/
12 changes: 5 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
*
!*/
!**/*.go
!go.mod
!LICENSE
!Makefile
!README.md
.idea/

release/

cmd/mp707/mp707
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ARG FROM=golang:1.15-buster

FROM $FROM as builder

ARG DEVUAN_KEY=BB23C00C61FC752C

# Debian's libudev-dev package doesn't include a static library (libudev.a) needed for static linking.
# Fortunately we can install libeudev-dev package from Devuan repositories which provides udev-compatible
# development files and a static library as well.
RUN echo deb http://deb.devuan.org/merged ascii main > /etc/apt/sources.list.d/devuan.list
RUN gpg --keyserver keys.gnupg.net --recv-key $DEVUAN_KEY && gpg -a --export $DEVUAN_KEY | apt-key add -

RUN apt update && apt install --no-install-recommends -y libusb-1.0-0-dev libeudev-dev

WORKDIR /go/mp707
ADD . .
RUN make static

FROM scratch
COPY --from=builder /go/mp707/cmd/mp707/mp707 /
ENTRYPOINT ["/mp707"]
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
SLUG = $(shell head -1 go.mod | cut -d/ -f2-)
NAME = mp707

CGO_ENABLED = 1
GO111MODULE = on

export SLUG
export NAME
export CGO_ENABLED
export GO111MODULE

Expand All @@ -25,6 +28,7 @@ test:
.PHONY: clean
clean:
rm -vf cmd/$(NAME)/$(NAME)
rm -rvf release

.PHONY: build
build: cmd/$(NAME)/$(NAME)
Expand All @@ -35,3 +39,8 @@ static: build

cmd/$(NAME)/$(NAME):
cd cmd/$(NAME); go build -v -ldflags "$(LDFLAGS)"

release: release/$(NAME).linux.386 release/$(NAME).linux.amd64

release/$(NAME).linux.%:
GOARCH=$* OUTPUT=$@ scripts/release.sh
24 changes: 24 additions & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -exo pipefail

mkdir -p "$(dirname "$OUTPUT")"

FROM=${FROM:-golang:1.15-buster}
GOARCH=${GOARCH:amd64}

DIGEST=$(docker manifest inspect "$FROM" | jq -r '.manifests[] | select(.platform.architecture == "'"$GOARCH"'") | .digest')
FROM=$FROM@$DIGEST

TAG=$SLUG:$GOARCH

docker build \
--pull \
-t "$TAG" \
--build-arg FROM="$FROM" \
.

CONTAINER=$(docker create "$TAG")

docker cp "$CONTAINER":/"$NAME" "$OUTPUT"
docker rm "$CONTAINER"

0 comments on commit 9f6adb9

Please sign in to comment.