Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

Commit

Permalink
Fix cgo cross compilation
Browse files Browse the repository at this point in the history
Appearently our sqlite3 library uses C, so we need different gcc
compilers for different platforms.
  • Loading branch information
hashworks committed Feb 20, 2018
1 parent ed422b1 commit 1978da4
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions distribute.sh
@@ -1,6 +1,14 @@
#!/usr/bin/env bash

name=plexStats
export CGO_ENABLED=1

platforms=(
linux-amd64 windows-amd64
linux-386 windows-386
linux-armv5 linux-armv6 linux-armv7
#linux-armv8
)

checkCommand() {
which "$1" >/dev/null 2>&1
Expand All @@ -16,13 +24,6 @@ checkCommand zip

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# NOTE: The build process is somewhat similar to https://github.com/syncthing/syncthing, thanks for that!
platforms=(
linux-amd64 windows-amd64 darwin-amd64 dragonfly-amd64 freebsd-amd64 netbsd-amd64 openbsd-amd64 solaris-amd64
freebsd-386 linux-386 netbsd-386 openbsd-386 windows-386
linux-arm linux-arm64 linux-ppc64 linux-ppc64le
)

cd "$DIR"
commit="$(git rev-parse --short HEAD 2>/dev/null)"
date="$(date --iso-8601=seconds)"
Expand All @@ -42,18 +43,37 @@ rm -Rf ./bin/
mkdir ./bin/ 2>/dev/null

for plat in "${platforms[@]}"; do
echo Building "$plat" ...

GOOS="${plat%-*}"
GOARCH="${plat#*-}"
export GOOS="${plat%-*}"
export GOARCH="${plat#*-}"

if [ "$GOOS" != "windows" ]; then
tmpFile="/tmp/${name}/bin/${name}"
else
tmpFile="/tmp/${name}/bin/${name}.exe"
fi

GOOS="${plat%-*}" GOARCH="${plat#*-}" go build -ldflags '-X main.VERSION='"$version"' -X main.BUILD_COMMIT='"$commit"' -X main.BUILD_DATE='"$date" \
if [ "$CGO_ENABLED" == 1 ]; then
if [ "$plat" = "windows-amd64" ]; then
export CC=x86_64-w64-mingw32-gcc
elif [ "$plat" = "windows-386" ]; then
export CC=i686-w64-mingw32-gcc
elif [ "${GOARCH%v*}" = "arm" ]; then
export GOARM="${GOARCH#*v}"
if [ "GOARM" = 8 ]; then
unset GOARM=""
export CC=armv8l-linux-gnueabihf-gcc
export GOARCH="arm64"
else
export CC=arm-linux-gnueabihf-gcc
export GOARCH="arm"
fi
else
export CC=gcc
fi
fi

echo Building "$plat" with "$CC" ...
go build -ldflags '-extld='"$CC"' -X main.VERSION='"$version"' -X main.BUILD_COMMIT='"$commit"' -X main.BUILD_DATE='"$date" \
-o "$tmpFile" "$DIR"/*.go

if [ "$?" != 0 ]; then
Expand Down

0 comments on commit 1978da4

Please sign in to comment.