Skip to content

Commit 0e4a6b0

Browse files
dereknolashanduur
andauthored
Use xx to enable CGO multiplatform builds (#456)
Signed-off-by: Derek Nola <[email protected]> Co-authored-by: Mateusz Urbanek <[email protected]>
1 parent 5de8386 commit 0e4a6b0

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ jobs:
1818
- name: Checkout
1919
uses: actions/checkout@v4
2020

21-
- name: Set up QEMU
22-
uses: docker/setup-qemu-action@v3
23-
2421
- name: Set up Docker Buildx
2522
uses: docker/setup-buildx-action@v3
2623

@@ -29,7 +26,7 @@ jobs:
2926

3027
- name: Compute checksum for each binary
3128
run: |
32-
arch=("amd64" "arm64" "arm")
29+
arch=("amd64" "arm64" "arm" "riscv64")
3330
cd dist/artifacts
3431
for a in "${arch[@]}"; do
3532
sha256sum kine-"${a}" > sha256sum-"${a}".txt

Dockerfile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,21 @@ EXPOSE 2379/tcp
5353
USER nobody
5454
ENTRYPOINT ["/bin/kine"]
5555

56+
FROM --platform=$BUILDPLATFORM tonistiigi/xx AS xx
57+
5658
FROM --platform=$BUILDPLATFORM golang:1.24-alpine3.20 AS multi-arch-build
57-
RUN apk -U add bash coreutils git gcc musl-dev vim less curl wget ca-certificates
59+
COPY --from=xx / /
60+
ARG TARGETOS
61+
ARG TARGETARCH
62+
ENV CGO_ENABLED=1
63+
RUN apk -U add bash coreutils git vim less curl wget ca-certificates clang lld
64+
RUN xx-apk add musl-dev gcc
5865
# go imports version gopls/v0.15.3
5966
# https://github.com/golang/tools/releases/latest
60-
RUN go install golang.org/x/tools/cmd/goimports@cd70d50baa6daa949efa12e295e10829f3a7bd46
67+
RUN xx-go install golang.org/x/tools/cmd/goimports@cd70d50baa6daa949efa12e295e10829f3a7bd46
6168
RUN rm -rf /go/src /go/pkg
6269
ENV SRC_DIR=/go/src/github.com/k3s-io/kine
6370
WORKDIR ${SRC_DIR}/
64-
ARG TARGETOS
65-
ARG TARGETARCH
6671
COPY ./scripts/buildx ./scripts/version ./scripts/
6772
COPY ./go.mod ./go.sum ./main.go ./
6873
COPY ./pkg ./pkg

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ build:
2424
-f Dockerfile --target=binary --output=. .
2525

2626
.PHONY: multi-arch-build
27-
PLATFORMS = linux/amd64,linux/arm64,linux/arm/v7
27+
PLATFORMS = linux/amd64,linux/arm64,linux/arm/v7,linux/riscv64
2828
multi-arch-build:
2929
docker buildx build --platform=$(PLATFORMS) --target=multi-arch-binary --output=type=local,dest=bin .
3030
mv bin/linux*/kine* bin/

scripts/buildx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
2-
# Builds kiune from multiple platforms and os
2+
# Builds kine for multiple platforms and os
33
# Intended to be run within a buildx container that provides the --platform flag
4+
# Also needed are the tonistiigi/xx build tools
45
set -e
56

67
source $(dirname $0)/version
@@ -9,9 +10,6 @@ cd $(dirname $0)/..
910

1011
mkdir -p bin
1112

12-
export GOOS=${TARGETOS}
13-
export GOARCH=${TARGETARCH}
14-
1513
if [ "$TARGETARCH" = "arm/v7" ]; then
1614
ARCH="-arm"
1715
else
@@ -23,11 +21,11 @@ if [ "$TARGETOS" != "linux" ]; then
2321
OPT_OS="-${TARGETOS}"
2422
fi
2523

26-
if [ "$TARGETOS" = "Linux" ]; then
24+
if [ "$TARGETOS" = "linux" ]; then
2725
OTHER_LINKFLAGS="-extldflags -static -s"
2826
fi
2927
LINKFLAGS="-X github.com/k3s-io/kine/pkg/version.Version=$VERSION"
3028
LINKFLAGS="-X github.com/k3s-io/kine/pkg/version.GitCommit=$COMMIT $LINKFLAGS"
3129

3230
echo Building Mutliplaform Kine
33-
CGO_CFLAGS="-DSQLITE_ENABLE_DBSTAT_VTAB=1 -DSQLITE_USE_ALLOCA=1" go build -ldflags "$LINKFLAGS $OTHER_LINKFLAGS" -tags nats -o bin/kine"${OPT_OS}${ARCH}"
31+
CGO_CFLAGS="-DSQLITE_ENABLE_DBSTAT_VTAB=1 -DSQLITE_USE_ALLOCA=1" xx-go build -ldflags "$LINKFLAGS $OTHER_LINKFLAGS" -tags nats -o bin/kine"${OPT_OS}${ARCH}"

0 commit comments

Comments
 (0)