Allow to build for a specific platform (#250)

This commit is contained in:
Sung Won Cho 2019-09-11 23:45:06 +10:00 committed by GitHub
commit df4866b4d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 19 deletions

View file

@ -2,9 +2,16 @@
#
# build.sh compiles dnote binary for target platforms. It is resonsible for creating
# distributable files that can be released by a human or a script.
# use: ./scripts/build.sh 0.4.8
#
# It can either cross-compile for different platforms using xgo, simply target a specific
# platform. Set GOOS and GOARCH environment variables to disable xgo and instead
# compile for a specific platform.
#
# use:
# ./scripts/build.sh 0.4.8
# GOOS=linux GOARCH=amd64 ./scripts/build.sh 0.4.8
set -eux
set -ex
version=$1
projectDir="$GOPATH/src/github.com/dnote/dnote"
@ -43,18 +50,33 @@ get_binary_name() {
build() {
platform=$1
arch=$2
# native indicates if the compilation is to take place natively on the host platform
# if not true, use xgo with Docker to cross-compile
native=$3
# build binary
destDir="$outputDir/$platform-$arch"
ldflags="-X main.apiEndpoint=https://api.dnote.io -X main.versionTag=$version"
tags="fts5"
mkdir -p "$destDir"
xgo \
-go "$goVersion" \
-ldflags "-X main.apiEndpoint=https://api.dnote.io -X main.versionTag=$version" \
--targets="$platform/$arch" \
--tags "fts5" \
--dest="$destDir" \
"$basedir"
if [ "$native" == true ]; then
GOOS="$platform" GOARCH="$arch" \
go build \
-ldflags "$ldflags" \
--tags "$tags" \
-o="$destDir/cli-$platform-$arch" \
"$basedir"
else
xgo \
-go "$goVersion" \
--targets="$platform/$arch" \
-ldflags "$ldflags" \
--tags "$tags" \
--dest="$destDir" \
"$basedir"
fi
binaryName=$(get_binary_name "$platform")
mv "$destDir/cli-${platform}-"* "$destDir/$binaryName"
@ -74,9 +96,13 @@ build() {
popd
}
# fetch tool
go get -u github.com/karalabe/xgo
if [ -z "$GOOS" ] && [ -z "$GOARCH" ]; then
# fetch tool
go get -u github.com/karalabe/xgo
build linux amd64
build darwin amd64
build windows amd64
build linux amd64
build darwin amd64
build windows amd64
else
build "$GOOS" "$GOARCH" true
fi