diff --git a/Makefile b/Makefile index dcc969de..7e25d258 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: swagger license license-check build-embedded build-test +.PHONY: swagger license license-check build-embedded build-test cross-build swagger: scripts/swagger.sh @@ -34,3 +34,14 @@ build-test: echo "==> Build test FAILED." >&2; \ exit 1; \ fi + +cross-build: + @echo "==> Cross-compiling for linux/darwin/windows × amd64/arm64..." + @mkdir -p bin + docker build \ + --file docker/Dockerfile.cross \ + --target export \ + --output type=local,dest=./bin \ + . + @echo "==> Done. Binaries written to ./bin/" + @ls -lh bin/ diff --git a/docker-compose.yml b/docker-compose.yml index 65530fb0..786c0251 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ services: # build: # context: . # dockerfile: docker/Dockerfile - image: ghcr.io/rain-kl/wavelet:v0.1.0 + image: ghcr.io/rain-kl/wavelet:latest restart: unless-stopped env_file: .env environment: diff --git a/docker/Dockerfile.cross b/docker/Dockerfile.cross new file mode 100644 index 00000000..6bd8cba1 --- /dev/null +++ b/docker/Dockerfile.cross @@ -0,0 +1,52 @@ +# syntax=docker/dockerfile:1.7 +# Cross-platform build workflow — produces 6 static binaries under ./bin/ +# +# Usage: +# docker build -f docker/Dockerfile.cross --output ./bin . +# +# Or via Makefile: +# make cross-build + +ARG GO_VERSION=1.25 +ARG ALPINE_VERSION=3.23 + +FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS builder + +# git — needed by go mod download for VCS-stamped modules +# ca-certificates — needed for HTTPS during module download +RUN apk add --no-cache ca-certificates git + +WORKDIR /workspace + +# Cache dependency downloads separately from source +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . + +# Build matrix: GOOS × GOARCH +# CGO_ENABLED=0 ensures fully static, portable binaries (no libc dependency). +# -trimpath removes local path info; -s -w strips debug info for smaller output. +ARG BUILD_FLAGS="-trimpath -ldflags=-s -w" + +RUN set -e; \ + mkdir -p /out; \ + for GOOS in linux darwin windows; do \ + for GOARCH in amd64 arm64; do \ + EXT=""; \ + [ "$GOOS" = "windows" ] && EXT=".exe"; \ + OUTPUT="/out/wavelet_${GOOS}_${GOARCH}${EXT}"; \ + echo "==> Building ${OUTPUT}..."; \ + CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} \ + go build \ + -trimpath \ + -ldflags="-s -w" \ + -o "${OUTPUT}" \ + ./main.go; \ + done; \ + done + +# ── Export stage ───────────────────────────────────────────────────────────── +# `docker build --output ./bin .` will copy everything from /out into the host. +FROM scratch AS export +COPY --from=builder /out/ /