Skip to content

Commit

Permalink
Merge pull request #156 from gabriel-samfira/build-windows
Browse files Browse the repository at this point in the history
Enable Windows builds
  • Loading branch information
gabriel-samfira authored Aug 18, 2023
2 parents c9cf114 + af54b55 commit aa2b42f
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ bin/
# vendor/
.vscode
cmd/temp
build/
release/
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ RUN git clone https://github.com/cloudbase/garm-provider-openstack /build/garm-p

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

FROM scratch

Expand Down
2 changes: 2 additions & 0 deletions Dockerfile.build-static
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ RUN wget http://musl.cc/aarch64-linux-musl-cross.tgz -O /tmp/aarch64-linux-musl-
ADD ./scripts/build-static.sh /build-static.sh
RUN chmod +x /build-static.sh

ADD . /build/garm

CMD ["/bin/sh"]
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@ USER_GROUP=$(shell ((docker --version | grep -q podman) && echo "0" || id -g))
ROOTDIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
GOPATH ?= $(shell go env GOPATH)
VERSION ?= $(shell git describe --tags --match='v[0-9]*' --dirty --always)
GARM_REF ?= $(shell git rev-parse --abbrev-ref HEAD)
GO ?= go


default: build

.PHONY : build-static test install-lint-deps lint go-test fmt fmtcheck verify-vendor verify
.PHONY : build-static test install-lint-deps lint go-test fmt fmtcheck verify-vendor verify create-release-files release
build-static:
@echo Building garm
docker build --tag $(IMAGE_TAG) -f Dockerfile.build-static .
docker run --rm -e USER_ID=$(USER_ID) -e USER_GROUP=$(USER_GROUP) -v $(PWD):/build/garm:z $(IMAGE_TAG) /build-static.sh
@echo Binaries are available in $(PWD)/bin
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
@echo Binaries are available in $(PWD)/build

create-release-files:
./scripts/make-release.sh

release: build-static create-release-files

clean:
@rm -rf ./bin ./build ./release

build:
@echo Building garm ${VERSION}
Expand Down
74 changes: 49 additions & 25 deletions scripts/build-static.sh
Original file line number Diff line number Diff line change
@@ -1,43 +1,67 @@
#!/bin/sh

GARM_SOURCE="/build/garm"
BIN_DIR="$GARM_SOURCE/bin"
git config --global --add safe.directory "$GARM_SOURCE"
git config --global --add safe.directory /build/garm
cd $GARM_SOURCE

[ ! -d "$BIN_DIR" ] && mkdir -p "$BIN_DIR"
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ ! -z "$GARM_REF" ] && [ "$GARM_REF" != "$CURRENT_BRANCH" ];then
git checkout $GARM_REF
fi

cd $GARM_SOURCE

OUTPUT_DIR="/build/output"
VERSION=$(git describe --tags --match='v[0-9]*' --dirty --always)
BUILD_DIR="$OUTPUT_DIR/$VERSION"


[ ! -d "$BUILD_DIR/linux" ] && mkdir -p "$BUILD_DIR/linux"
[ ! -d "$BUILD_DIR/windows" ] && mkdir -p "$BUILD_DIR/windows"

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

mkdir -p $BIN_DIR/amd64 $BIN_DIR/arm64
# Garm
cd $GARM_SOURCE/cmd/garm
go build -mod vendor \
-o $BIN_DIR/amd64/garm \

# Linux
GOOS=linux GOARCH=amd64 go build -mod vendor \
-o $BUILD_DIR/linux/amd64/garm \
-tags osusergo,netgo,sqlite_omit_load_extension \
-ldflags "-linkmode external -extldflags '-static' -s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" .
CC=aarch64-linux-musl-gcc GOARCH=arm64 go build \
-ldflags "-extldflags '-static' -s -w -X main.Version=$VERSION" .
GOOS=linux GOARCH=arm64 CC=aarch64-linux-musl-gcc go build \
-mod vendor \
-o $BIN_DIR/arm64/garm \
-o $BUILD_DIR/linux/arm64/garm \
-tags osusergo,netgo,sqlite_omit_load_extension \
-ldflags "-linkmode external -extldflags '-static' -s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" .
# GOOS=windows CC=x86_64-w64-mingw32-cc go build -mod vendor \
# -o $BIN_DIR/amd64/garm.exe \
# -tags osusergo,netgo,sqlite_omit_load_extension \
# -ldflags "-s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" .
-ldflags "-extldflags '-static' -s -w -X main.Version=$VERSION" .

# Windows
GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-cc go build -mod vendor \
-o $BUILD_DIR/windows/amd64/garm.exe \
-tags osusergo,netgo,sqlite_omit_load_extension \
-ldflags "-s -w -X main.Version=$VERSION" .

# garm-cli
cd $GARM_SOURCE/cmd/garm-cli
go build -mod vendor \
-o $BIN_DIR/amd64/garm-cli \

# Linux
GOOS=linux GOARCH=amd64 go build -mod vendor \
-o $BUILD_DIR/linux/amd64/garm-cli \
-tags osusergo,netgo,sqlite_omit_load_extension \
-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)" .
CC=aarch64-linux-musl-gcc GOARCH=arm64 go build -mod vendor \
-o $BIN_DIR/arm64/garm-cli \
-ldflags "-extldflags '-static' -s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$VERSION" .
GOOS=linux GOARCH=arm64 CC=aarch64-linux-musl-gcc go build -mod vendor \
-o $BUILD_DIR/linux/arm64/garm-cli \
-tags osusergo,netgo,sqlite_omit_load_extension \
-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)" .
# GOOS=windows CGO_ENABLED=0 go build -mod vendor \
# -o $BIN_DIR/amd64/garm-cli.exe \
# -tags osusergo,netgo,sqlite_omit_load_extension \
# -ldflags "-s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" .
-ldflags "-extldflags '-static' -s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$VERSION" .

# Windows
GOOS=windows GOARCH=amd64 go build -mod vendor \
-o $BUILD_DIR/windows/amd64/garm-cli.exe \
-tags osusergo,netgo,sqlite_omit_load_extension \
-ldflags "-s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$VERSION" .


chown $USER_ID:$USER_GROUP -R "$BIN_DIR"
git checkout $CURRENT_BRANCH || true
chown $USER_ID:$USER_GROUP -R "$OUTPUT_DIR"
62 changes: 62 additions & 0 deletions scripts/make-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash

echo $GARM_REF

VERSION=$(git describe --tags --match='v[0-9]*' --dirty --always)
RELEASE="$PWD/release"

[ ! -d "$RELEASE" ] && mkdir -p "$RELEASE"

if [ ! -z "$GARM_REF" ]; then
VERSION=$(git describe --tags --match='v[0-9]*' --always $GARM_REF)
fi

echo $VERSION

if [ ! -d "build/$VERSION" ]; then
echo "missing build/$VERSION"
exit 1
fi

# Windows

if [ ! -d "build/$VERSION/windows/amd64" ];then
echo "missing build/$VERSION/windows/amd64"
exit 1
fi

WINDOWS_FILES=("garm.exe" "garm-cli.exe")

for file in ${WINDOWS_FILES[@]};do
if [ ! -f "build/$VERSION/windows/amd64/$file" ];then
echo "missing build/$VERSION/windows/amd64/$file"
exit 1
fi

pushd build/$VERSION/windows/amd64
zip ${file%%.exe}-windows-amd64.zip $file
sha256sum ${file%%.exe}-windows-amd64.zip > ${file%%.exe}-windows-amd64.zip.sha256
mv ${file%%.exe}-windows-amd64.zip $RELEASE
mv ${file%%.exe}-windows-amd64.zip.sha256 $RELEASE
popd
done

# Linux
OS_ARCHES=("amd64" "arm64")
FILES=("garm" "garm-cli")

for arch in ${OS_ARCHES[@]};do
for file in ${FILES[@]};do
if [ ! -f "build/$VERSION/linux/$arch/$file" ];then
echo "missing build/$VERSION/linux/$arch/$file"
exit 1
fi

pushd build/$VERSION/linux/$arch
tar czf ${file}-linux-$arch.tgz $file
sha256sum ${file}-linux-$arch.tgz > ${file}-linux-$arch.tgz.sha256
mv ${file}-linux-$arch.tgz $RELEASE
mv ${file}-linux-$arch.tgz.sha256 $RELEASE
popd
done
done

0 comments on commit aa2b42f

Please sign in to comment.