Skip to content

Commit

Permalink
tools: Fix the cross-build script (sourcenetwork#460)
Browse files Browse the repository at this point in the history
A cross build scripts that works for multiple target platforms and is located in `tools/scripts`.
Additionally, with this commit, the top-level `build` directory goes away.
  • Loading branch information
orpheuslummis authored May 24, 2022
1 parent 20e62a8 commit 6dcb8d0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ install:
build:
go build -o build/defradb cli/defradb/main.go

.PHONY: multi-build
multi-build:
echo "Compiling for multiple OS and Platforms"
GOOS=linux GOARCH=arm go build -o build/defradb-linux-arm cli/defradb/main.go
GOOS=linux GOARCH=arm64 go build -o build/defradb-linux-arm64 cli/defradb/main.go
GOOS=freebsd GOARCH=386 go build -o build/defradb-freebsd-386 cli/defradb/main.go
# Usage: make cross-build platforms="{platforms}"
# platforms is specified as a comma-separated list with no whitespace, e.g. "linux/amd64,linux/arm,linux/arm64"
# If none is specified, build for all platforms.
.PHONY: cross-build
cross-build:
bash tools/scripts/cross-build.sh $(platforms)

.PHONY: start
start: build
Expand Down
28 changes: 0 additions & 28 deletions build/go-cross-build.sh

This file was deleted.

39 changes: 39 additions & 0 deletions tools/scripts/cross-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

DEFRADB_MAIN="cli/defradb/main.go"
BUILD_DIR="build/"

platforms=$1
if [[ -z "${platforms}" ]]; then
echo "Building for all platforms"
# A subset of the comprehensive list found at https://go.dev/doc/install/source#environment
platforms=(
"windows/amd64"
"windows/arm64"
"windows/arm"
"linux/amd64"
"linux/arm64"
"linux/arm"
"darwin/amd64"
"darwin/arm64"
# "js/wasm"
)
else
platforms=(${platforms//,/ })
fi

for platform in "${platforms[@]}"
do
platform_split=(${platform//\// })
GOOS="${platform_split[0]}"
GOARCH="${platform_split[1]}"
output_name=$BUILD_DIR'defradb-'$GOOS'-'$GOARCH
if [ "$GOOS" = "windows" ]; then
output_name+='.exe'
fi
if ! env GOOS="$GOOS" GOARCH="$GOARCH" go build -o $output_name $DEFRADB_MAIN; then
echo 'An error has occurred! Aborting the script execution...'
exit 1
fi
echo "Completed: ${output_name}"
done

0 comments on commit 6dcb8d0

Please sign in to comment.