corso/build/build.sh
Vaibhav Kamra 7919d5a321
Fix build script for Mac M1 (#1257)
## Description

`uname -m` will be `arm64` not `arm`

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [x] 🐹 Trivial/Minor

## Test Plan

<!-- How will this be tested prior to merging.-->
- [x] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
2022-10-20 18:44:51 +00:00

82 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
set -e
ROOT=$(dirname $(dirname $(readlink -f $0)))
GOVER=1.18 # go version
CORSO_BUILD_CACHE="/tmp/.corsobuild" # shared persistent cache
# Figure out os and architecture
case "$(uname -m)" in
x86_64) GOARCH="amd64" ;;
aarch64) GOARCH="arm64" ;;
arm64) GOARCH="arm64" ;;
i386) GOARCH="386" ;;
*) echo "Unknown architecture" && exit 0 ;;
esac
case "$(uname)" in
Linux) GOOS="linux" ;;
Darwin) GOOS="darwin" ;; # TODO: verify this
*) echo "Unknown OS" && exit 0 ;;
esac
PLATFORMS="$GOOS/$GOARCH" # default platform
TAG="alcionai/corso" # default image tag
usage() {
echo "Usage: $(basename $0) <binary|image> [--platforms ...] [--tag ...]"
echo ""
echo "OPTIONS"
echo " -p|--platforms Platforms to build for (default: $PLATFORMS)"
echo " Specify multiple platforms using ',' (eg: linux/amd64,darwin/arm)"
echo " -t|--tag Tag for container image (default: $TAG)"
}
MODE="binary"
case "$1" in
binary) MODE="binary" && shift ;;
image) MODE="image" && shift ;;
-h | --help) usage && exit 0 ;;
*) usage && exit 1 ;;
esac
while [ "$#" -gt 0 ]; do
case "$1" in
-p | --platforms) PLATFORMS="$2" && shift ;;
-t | --tag) TAG="$2" && shift ;;
*) echo "Invalid argument $1" && usage && exit 1 ;;
esac
shift
done
if [ "$MODE" == "binary" ]; then
mkdir -p ${CORSO_BUILD_CACHE} # prep env
for platform in ${PLATFORMS/,/ }; do
IFS='/' read -r -a platform_split <<<"$platform"
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
printf "Building for %s...\r" "$platform"
docker run --rm \
--mount type=bind,src=${ROOT},dst="/app" \
--mount type=bind,src=${CORSO_BUILD_CACHE},dst=${CORSO_BUILD_CACHE} \
--env GOMODCACHE=${CORSO_BUILD_CACHE}/mod --env GOCACHE=${CORSO_BUILD_CACHE}/cache \
--env GOOS=${GOOS} --env GOARCH=${GOARCH} \
--workdir "/app/src" \
golang:${GOVER} \
go build -o corso -ldflags "${CORSO_BUILD_LDFLAGS}"
mkdir -p ${ROOT}/bin/${GOOS}-${GOARCH}
mv ${ROOT}/src/corso ${ROOT}/bin/${GOOS}-${GOARCH}/corso
echo Corso $platform binary available in ${ROOT}/bin/${GOOS}-${GOARCH}/corso
done
else
echo Building "$TAG" image for "$PLATFORMS"
docker buildx build --tag ${TAG} \
--platform ${PLATFORMS} \
--file ${ROOT}/build/Dockerfile \
--build-arg CORSO_BUILD_LDFLAGS="$CORSO_BUILD_LDFLAGS" \
--load ${ROOT}
echo Built container image "$TAG"
fi