Skip to content

Commit af54b55

Browse files
Enable Windows builds
This change adds the needed bits to enable Windows builds. This also adds a new make target to create the release files that will be uploaded as part of new releases. Signed-off-by: Gabriel Adrian Samfira <[email protected]>
1 parent e2b617e commit af54b55

File tree

6 files changed

+130
-31
lines changed

6 files changed

+130
-31
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ bin/
1616
# vendor/
1717
.vscode
1818
cmd/temp
19+
build/
20+
release/

Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ RUN git clone https://github.com/cloudbase/garm-provider-openstack /build/garm-p
1313

1414
RUN cd /build/garm && go build -o /bin/garm \
1515
-tags osusergo,netgo,sqlite_omit_load_extension \
16-
-ldflags "-linkmode external -extldflags '-static' -s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" \
16+
-ldflags "-extldflags '-static' -s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" \
1717
/build/garm/cmd/garm
1818
RUN mkdir -p /opt/garm/providers.d
19-
RUN cd /build/garm-provider-azure && go build -ldflags="-linkmode external -extldflags '-static' -s -w" -o /opt/garm/providers.d/garm-provider-azure .
20-
RUN cd /build/garm-provider-openstack && go build -ldflags="-linkmode external -extldflags '-static' -s -w" -o /opt/garm/providers.d/garm-provider-openstack .
19+
RUN cd /build/garm-provider-azure && go build -ldflags="-extldflags '-static' -s -w" -o /opt/garm/providers.d/garm-provider-azure .
20+
RUN cd /build/garm-provider-openstack && go build -ldflags="-extldflags '-static' -s -w" -o /opt/garm/providers.d/garm-provider-openstack .
2121

2222
FROM scratch
2323

Dockerfile.build-static

+2
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ RUN wget http://musl.cc/aarch64-linux-musl-cross.tgz -O /tmp/aarch64-linux-musl-
1212
ADD ./scripts/build-static.sh /build-static.sh
1313
RUN chmod +x /build-static.sh
1414

15+
ADD . /build/garm
16+
1517
CMD ["/bin/sh"]

Makefile

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@ USER_GROUP=$(shell ((docker --version | grep -q podman) && echo "0" || id -g))
77
ROOTDIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
88
GOPATH ?= $(shell go env GOPATH)
99
VERSION ?= $(shell git describe --tags --match='v[0-9]*' --dirty --always)
10+
GARM_REF ?= $(shell git rev-parse --abbrev-ref HEAD)
1011
GO ?= go
1112

1213

1314
default: build
1415

15-
.PHONY : build-static test install-lint-deps lint go-test fmt fmtcheck verify-vendor verify
16+
.PHONY : build-static test install-lint-deps lint go-test fmt fmtcheck verify-vendor verify create-release-files release
1617
build-static:
1718
@echo Building garm
1819
docker build --tag $(IMAGE_TAG) -f Dockerfile.build-static .
19-
docker run --rm -e USER_ID=$(USER_ID) -e USER_GROUP=$(USER_GROUP) -v $(PWD):/build/garm:z $(IMAGE_TAG) /build-static.sh
20-
@echo Binaries are available in $(PWD)/bin
20+
docker run --rm -e USER_ID=$(USER_ID) -e GARM_REF=$(GARM_REF) -e USER_GROUP=$(USER_GROUP) -v $(PWD)/build:/build/output:z $(IMAGE_TAG) /build-static.sh
21+
@echo Binaries are available in $(PWD)/build
22+
23+
create-release-files:
24+
./scripts/make-release.sh
25+
26+
release: build-static create-release-files
27+
28+
clean:
29+
@rm -rf ./bin ./build ./release
2130

2231
build:
2332
@echo Building garm ${VERSION}

scripts/build-static.sh

+49-25
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,67 @@
11
#!/bin/sh
22

33
GARM_SOURCE="/build/garm"
4-
BIN_DIR="$GARM_SOURCE/bin"
5-
git config --global --add safe.directory "$GARM_SOURCE"
4+
git config --global --add safe.directory /build/garm
5+
cd $GARM_SOURCE
66

7-
[ ! -d "$BIN_DIR" ] && mkdir -p "$BIN_DIR"
7+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
8+
if [ ! -z "$GARM_REF" ] && [ "$GARM_REF" != "$CURRENT_BRANCH" ];then
9+
git checkout $GARM_REF
10+
fi
11+
12+
cd $GARM_SOURCE
13+
14+
OUTPUT_DIR="/build/output"
15+
VERSION=$(git describe --tags --match='v[0-9]*' --dirty --always)
16+
BUILD_DIR="$OUTPUT_DIR/$VERSION"
17+
18+
19+
[ ! -d "$BUILD_DIR/linux" ] && mkdir -p "$BUILD_DIR/linux"
20+
[ ! -d "$BUILD_DIR/windows" ] && mkdir -p "$BUILD_DIR/windows"
821

922
export CGO_ENABLED=1
1023
USER_ID=${USER_ID:-$UID}
1124
USER_GROUP=${USER_GROUP:-$(id -g)}
1225

13-
mkdir -p $BIN_DIR/amd64 $BIN_DIR/arm64
26+
# Garm
1427
cd $GARM_SOURCE/cmd/garm
15-
go build -mod vendor \
16-
-o $BIN_DIR/amd64/garm \
28+
29+
# Linux
30+
GOOS=linux GOARCH=amd64 go build -mod vendor \
31+
-o $BUILD_DIR/linux/amd64/garm \
1732
-tags osusergo,netgo,sqlite_omit_load_extension \
18-
-ldflags "-linkmode external -extldflags '-static' -s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" .
19-
CC=aarch64-linux-musl-gcc GOARCH=arm64 go build \
33+
-ldflags "-extldflags '-static' -s -w -X main.Version=$VERSION" .
34+
GOOS=linux GOARCH=arm64 CC=aarch64-linux-musl-gcc go build \
2035
-mod vendor \
21-
-o $BIN_DIR/arm64/garm \
36+
-o $BUILD_DIR/linux/arm64/garm \
2237
-tags osusergo,netgo,sqlite_omit_load_extension \
23-
-ldflags "-linkmode external -extldflags '-static' -s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" .
24-
# GOOS=windows CC=x86_64-w64-mingw32-cc go build -mod vendor \
25-
# -o $BIN_DIR/amd64/garm.exe \
26-
# -tags osusergo,netgo,sqlite_omit_load_extension \
27-
# -ldflags "-s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" .
38+
-ldflags "-extldflags '-static' -s -w -X main.Version=$VERSION" .
2839

40+
# Windows
41+
GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-cc go build -mod vendor \
42+
-o $BUILD_DIR/windows/amd64/garm.exe \
43+
-tags osusergo,netgo,sqlite_omit_load_extension \
44+
-ldflags "-s -w -X main.Version=$VERSION" .
45+
46+
# garm-cli
2947
cd $GARM_SOURCE/cmd/garm-cli
30-
go build -mod vendor \
31-
-o $BIN_DIR/amd64/garm-cli \
48+
49+
# Linux
50+
GOOS=linux GOARCH=amd64 go build -mod vendor \
51+
-o $BUILD_DIR/linux/amd64/garm-cli \
3252
-tags osusergo,netgo,sqlite_omit_load_extension \
33-
-ldflags "-linkmode external -extldflags '-static' -s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" .
34-
CC=aarch64-linux-musl-gcc GOARCH=arm64 go build -mod vendor \
35-
-o $BIN_DIR/arm64/garm-cli \
53+
-ldflags "-extldflags '-static' -s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$VERSION" .
54+
GOOS=linux GOARCH=arm64 CC=aarch64-linux-musl-gcc go build -mod vendor \
55+
-o $BUILD_DIR/linux/arm64/garm-cli \
3656
-tags osusergo,netgo,sqlite_omit_load_extension \
37-
-ldflags "-linkmode external -extldflags '-static' -s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" .
38-
# GOOS=windows CGO_ENABLED=0 go build -mod vendor \
39-
# -o $BIN_DIR/amd64/garm-cli.exe \
40-
# -tags osusergo,netgo,sqlite_omit_load_extension \
41-
# -ldflags "-s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" .
57+
-ldflags "-extldflags '-static' -s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$VERSION" .
58+
59+
# Windows
60+
GOOS=windows GOARCH=amd64 go build -mod vendor \
61+
-o $BUILD_DIR/windows/amd64/garm-cli.exe \
62+
-tags osusergo,netgo,sqlite_omit_load_extension \
63+
-ldflags "-s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$VERSION" .
64+
4265

43-
chown $USER_ID:$USER_GROUP -R "$BIN_DIR"
66+
git checkout $CURRENT_BRANCH || true
67+
chown $USER_ID:$USER_GROUP -R "$OUTPUT_DIR"

scripts/make-release.sh

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/bash
2+
3+
echo $GARM_REF
4+
5+
VERSION=$(git describe --tags --match='v[0-9]*' --dirty --always)
6+
RELEASE="$PWD/release"
7+
8+
[ ! -d "$RELEASE" ] && mkdir -p "$RELEASE"
9+
10+
if [ ! -z "$GARM_REF" ]; then
11+
VERSION=$(git describe --tags --match='v[0-9]*' --always $GARM_REF)
12+
fi
13+
14+
echo $VERSION
15+
16+
if [ ! -d "build/$VERSION" ]; then
17+
echo "missing build/$VERSION"
18+
exit 1
19+
fi
20+
21+
# Windows
22+
23+
if [ ! -d "build/$VERSION/windows/amd64" ];then
24+
echo "missing build/$VERSION/windows/amd64"
25+
exit 1
26+
fi
27+
28+
WINDOWS_FILES=("garm.exe" "garm-cli.exe")
29+
30+
for file in ${WINDOWS_FILES[@]};do
31+
if [ ! -f "build/$VERSION/windows/amd64/$file" ];then
32+
echo "missing build/$VERSION/windows/amd64/$file"
33+
exit 1
34+
fi
35+
36+
pushd build/$VERSION/windows/amd64
37+
zip ${file%%.exe}-windows-amd64.zip $file
38+
sha256sum ${file%%.exe}-windows-amd64.zip > ${file%%.exe}-windows-amd64.zip.sha256
39+
mv ${file%%.exe}-windows-amd64.zip $RELEASE
40+
mv ${file%%.exe}-windows-amd64.zip.sha256 $RELEASE
41+
popd
42+
done
43+
44+
# Linux
45+
OS_ARCHES=("amd64" "arm64")
46+
FILES=("garm" "garm-cli")
47+
48+
for arch in ${OS_ARCHES[@]};do
49+
for file in ${FILES[@]};do
50+
if [ ! -f "build/$VERSION/linux/$arch/$file" ];then
51+
echo "missing build/$VERSION/linux/$arch/$file"
52+
exit 1
53+
fi
54+
55+
pushd build/$VERSION/linux/$arch
56+
tar czf ${file}-linux-$arch.tgz $file
57+
sha256sum ${file}-linux-$arch.tgz > ${file}-linux-$arch.tgz.sha256
58+
mv ${file}-linux-$arch.tgz $RELEASE
59+
mv ${file}-linux-$arch.tgz.sha256 $RELEASE
60+
popd
61+
done
62+
done

0 commit comments

Comments
 (0)