From df4866b4d47b850897e25c81f41e48dfe0c99dcf Mon Sep 17 00:00:00 2001 From: Sung Won Cho Date: Wed, 11 Sep 2019 23:45:06 +1000 Subject: [PATCH] Allow to build for a specific platform (#250) --- CONTRIBUTING.md | 7 +++++- Makefile | 8 +++--- pkg/cli/scripts/build.sh | 54 +++++++++++++++++++++++++++++----------- 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6bdbd118..cfed335c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -32,8 +32,13 @@ You can build either a development version or a production version: # Build a development version for your platform and place it in your `PATH`. make debug=true build-cli -# Build a production version +# Build a production version for all platforms make version=v0.1.0 build-cli + +# Build a production version for a specific platform +# Note: You cannot cross-compile using this method because Dnote uses CGO +# and requires the OS specific headers. +GOOS=[insert OS] GOARCH=[insert arch] make version=v0.1.0 build-cli ``` ### Test diff --git a/Makefile b/Makefile index ede091a4..5a19a1af 100644 --- a/Makefile +++ b/Makefile @@ -79,7 +79,7 @@ build-web: build-server: build-web ifndef version - $(error version is required. Usage: make version=v0.1.0 build-server) + $(error version is required. Usage: make version=0.1.0 build-server) endif @echo "==> building server" @@ -93,7 +93,7 @@ ifeq ($(debug), true) else ifndef version - $(error version is required. Usage: make version=v0.1.0 build-cli) + $(error version is required. Usage: make version=0.1.0 build-cli) endif @echo "==> building cli" @@ -104,7 +104,7 @@ endif ## release release-cli: build-cli ifndef version - $(error version is required. Usage: make version=v0.1.0 release-cli) + $(error version is required. Usage: make version=0.1.0 release-cli) endif ifndef HUB $(error please install hub) @@ -128,7 +128,7 @@ endif release-server: build-server ifndef version - $(error version is required. Usage: make version=v0.1.0 release-server) + $(error version is required. Usage: make version=0.1.0 release-server) endif ifndef HUB $(error please install hub) diff --git a/pkg/cli/scripts/build.sh b/pkg/cli/scripts/build.sh index f2946d6d..2d276d31 100755 --- a/pkg/cli/scripts/build.sh +++ b/pkg/cli/scripts/build.sh @@ -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