Update Android and RPi instructions

This commit is contained in:
Milan Nikolic 2018-05-06 12:47:05 +02:00
parent 30c57a1102
commit 9d293edb6e
5 changed files with 20 additions and 202 deletions

View file

@ -2,28 +2,26 @@
To compile example to shared library you will need [Android NDK](https://developer.android.com/ndk/downloads/index.html).
To build Android apk you will need [Android SDK](http://developer.android.com/sdk/index.html#Other).
Download and unpack archives somewhere.
Go must be cross compiled for android. There is a bootstrap.sh script that you can use to compile Go for android/arm and android/arm64.
Export path to Android NDK, point to location where you have unpacked archive:
export ANDROID_NDK_HOME=/opt/android-ndk
Compile Go and android_native_app_glue, /usr/local is prefix where Go and Android toolchains will be installed:
Add toolchain bin directory to PATH:
./bootstrap.sh /usr/local
export PATH=${ANDROID_NDK_HOME}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:${PATH}
After build is complete point GOROOT to new Go installation in /usr/local, and add toolchain bin directory to PATH:
Export sysroot path:
export GOROOT=/usr/local/go
export PATH=/usr/local/android-arm7/bin:${PATH}
export ANDROID_SYSROOT=${ANDROID_NDK_HOME}/platforms/android-16/arch-arm
And compile shared library:
CGO_CFLAGS="-I/usr/local/android-arm7/include" CGO_LDFLAGS="-L/usr/local/android-arm7/lib" \
CC=arm-linux-androideabi-gcc CGO_ENABLED=1 GOOS=android GOARCH=arm \
${GOROOT}/bin/go build -v -x -buildmode=c-shared -ldflags="-s -w -extldflags=-Wl,-soname,libexample.so" \
CC=arm-linux-androideabi-gcc \
CGO_CFLAGS="-I${ANDROID_SYSROOT}/usr/include --sysroot=${ANDROID_SYSROOT}" \
CGO_LDFLAGS="-L${ANDROID_SYSROOT}/usr/lib --sysroot=${ANDROID_SYSROOT}" \
CGO_ENABLED=1 GOOS=android GOARCH=arm \
go build -buildmode=c-shared -ldflags="-s -w -extldflags=-Wl,-soname,libexample.so" \
-o=android/libs/armeabi-v7a/libexample.so
To build apk export path to Android SDK, point to location where you unpacked archive:

View file

@ -1,137 +0,0 @@
#!/bin/bash
##### Bootstrap Go android/arm and android/arm64 with Android standalone toolchains.
if [[ -z "$1" ]]; then
echo "Usage: bootstrap.sh <install prefix>"
exit 1
fi
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
OS="windows"
elif [[ "$OSTYPE" == "darwin"* ]]; then
OS="darwin"
elif [[ "$OSTYPE" == "linux-gnu" ]]; then
OS="linux"
fi
MYCC=clang
MYCXX=clang++
###################################################
echo "##### Check requirements"
for x in "python" "tar" "unzip" "curl" "cmake" "grep" "sed" "awk"; do
which ${x} || MISSING=true
done
if [[ "$MISSING" = true ]]; then
exit 1
fi
API_VERSION_ARM=15
API_VERSION_ARM64=21
BUILD_TOOLS_VERSION=26.0.2
if [[ -z "$GO_VERSION" ]]; then
# go1.9.2
GO_VERSION=`curl -s https://golang.org/dl/ | grep 'id="go' | head -n1 | awk -F'"' '{print $4}'`
fi
if [[ -z "$NDK_VERSION" ]]; then
# r15c
NDK_VERSION=`curl -s https://developer.android.com/ndk/downloads/index.html | grep 'id="stable-downloads"' | awk -F'(' '{print $2}' | awk -F')' '{print $1}'`
fi
if [[ -z "$TOOLS_VERSION" ]]; then
# 3859397
TOOLS_VERSION=`curl -s https://developer.android.com/studio/index.html | grep sdk-tools-linux | grep -o '[0-9]*' | head -n1`
fi
INSTALL_PREFIX="$1"
export PATH=${INSTALL_PREFIX}/android-arm/bin:${INSTALL_PREFIX}/android-arm64/bin:${PATH}
BUILD_DIR=`mktemp -d`
mkdir -p ${BUILD_DIR}/bootstrap || exit 1
function finish {
echo; echo "##### Remove build directory"
rm -rf ${BUILD_DIR}
}
trap finish EXIT
###################################################
if [[ -z "$ANDROID_NDK_HOME" ]]; then
echo; echo "##### Download NDK ${NDK_VERSION}"
cd ${BUILD_DIR} && curl -L -O --progress-bar https://dl.google.com/android/repository/android-ndk-${NDK_VERSION}-${OS}-x86_64.zip; tar -xzf android-ndk-${NDK_VERSION}-${OS}-x86_64.zip >/dev/null 2>&1 || unzip -q android-ndk-${NDK_VERSION}-${OS}-x86_64.zip || exit -1
ANDROID_NDK_HOME=${BUILD_DIR}/android-ndk-${NDK_VERSION}
fi
if [[ "$OS" == "windows" && "$OSTYPE" == "msys" ]]; then
# Fix for python in MSYS2 shell
grep -q 'MSYS' ${ANDROID_NDK_HOME}/build/tools/make_standalone_toolchain.py || sed -i "s/platform\.system() == 'Windows'/platform.system() == 'Windows' or platform.system().startswith('MSYS')/" ${ANDROID_NDK_HOME}/build/tools/make_standalone_toolchain.py
# Export path to mingw64
export PATH=/mingw64/bin:${PATH}
fi
echo; echo "##### Make standalone Android toolchains"
${ANDROID_NDK_HOME}/build/tools/make_standalone_toolchain.py --api=${API_VERSION_ARM} --arch=arm --install-dir=${INSTALL_PREFIX}/android-arm -v || exit 1
${ANDROID_NDK_HOME}/build/tools/make_standalone_toolchain.py --api=${API_VERSION_ARM64} --arch=arm64 --install-dir=${INSTALL_PREFIX}/android-arm64 -v || exit 1
###################################################
echo; echo "##### Download Go ${GO_VERSION} binaries"
if [[ "$OS" == "windows" ]]; then
cd ${BUILD_DIR}/bootstrap && curl -L -O --progress-bar http://storage.googleapis.com/golang/${GO_VERSION}.${OS}-amd64.zip; tar -xzf ${GO_VERSION}.${OS}-amd64.zip >/dev/null 2>&1 || unzip -q ${GO_VERSION}.${OS}-amd64.zip || exit 1
else
cd ${BUILD_DIR}/bootstrap && curl -L --progress-bar http://storage.googleapis.com/golang/${GO_VERSION}.${OS}-amd64.tar.gz | tar -xz || exit 1
fi
echo; echo "##### Download Go ${GO_VERSION} source"
cd ${BUILD_DIR} && curl -L --progress-bar http://storage.googleapis.com/golang/${GO_VERSION}.src.tar.gz | tar -xz && cd ${BUILD_DIR}/go/src || exit 1
if [[ "$OS" == "windows" && "$OSTYPE" == "msys" ]]; then
# Fix/Hack for MSYS2 shell
sed -i '/^eval/c\export $(./cmd/dist/dist env -p | grep -v PATH | xargs)' ${BUILD_DIR}/go/src/make.bash
# Fix/Hack for MSYS2 shell
sed -i 's/old_bin_files go gofmt/old_bin_files go.exe gofmt.exe/' ${BUILD_DIR}/go/src/make.bash
fi
echo; echo "##### Compile Go ${GO_VERSION} for host"
GOROOT_BOOTSTRAP=${BUILD_DIR}/bootstrap/go ./make.bash || exit 1
echo; echo "##### Compile Go ${GO_VERSION} for arm-linux-androideabi"
GOROOT_BOOTSTRAP=${BUILD_DIR}/bootstrap/go CC_FOR_TARGET=arm-linux-androideabi-${MYCC} GOOS=android GOARCH=arm CGO_ENABLED=1 ./make.bash --no-clean || exit 1
echo; echo "##### Compile Go ${GO_VERSION} for aarch64-linux-android"
GOROOT_BOOTSTRAP=${BUILD_DIR}/bootstrap/go CC_FOR_TARGET=aarch64-linux-android-${MYCC} GOOS=android GOARCH=arm64 CGO_ENABLED=1 ./make.bash --no-clean || exit 1
cp -r -f ${BUILD_DIR}/go ${INSTALL_PREFIX}
###################################################
echo; echo "##### Compile android_native_app_glue"
mkdir -p ${BUILD_DIR}/native_app_glue/jni
cp -r ${ANDROID_NDK_HOME}/sources/android/native_app_glue/* ${BUILD_DIR}/native_app_glue/jni/
echo "APP_ABI := armeabi-v7a arm64-v8a" > ${BUILD_DIR}/native_app_glue/jni/Application.mk
cd ${BUILD_DIR}/native_app_glue && ${ANDROID_NDK_HOME}/ndk-build V=1 || exit 1
cp ${BUILD_DIR}/native_app_glue/obj/local/armeabi-v7a/libandroid_native_app_glue.a ${INSTALL_PREFIX}/android-arm/lib/
cp ${BUILD_DIR}/native_app_glue/obj/local/arm64-v8a/libandroid_native_app_glue.a ${INSTALL_PREFIX}/android-arm64/lib/
cp ${ANDROID_NDK_HOME}/sources/android/native_app_glue/android_native_app_glue.h ${INSTALL_PREFIX}/android-arm/include/
cp ${ANDROID_NDK_HOME}/sources/android/native_app_glue/android_native_app_glue.h ${INSTALL_PREFIX}/android-arm64/include/
###################################################
echo; echo "##### Download sdk tools"
cd ${BUILD_DIR} && curl -L -O --progress-bar https://dl.google.com/android/repository/sdk-tools-${OS}-${TOOLS_VERSION}.zip; tar -xzf sdk-tools-${OS}-${TOOLS_VERSION}.zip >/dev/null 2>&1 || unzip -q sdk-tools-${OS}-${TOOLS_VERSION}.zip || exit -1
mkdir -p ${INSTALL_PREFIX}/android-tools
cp -r -f ${BUILD_DIR}/tools/* ${INSTALL_PREFIX}/android-tools/
echo; echo "##### Install build tools and platforms"
yes | ${INSTALL_PREFIX}/android-tools/bin/sdkmanager --licenses
${INSTALL_PREFIX}/android-tools/bin/sdkmanager "build-tools;${BUILD_TOOLS_VERSION}"
${INSTALL_PREFIX}/android-tools/bin/sdkmanager "platforms;android-${API_VERSION_ARM}"
${INSTALL_PREFIX}/android-tools/bin/sdkmanager "platforms;android-${API_VERSION_ARM64}"

View file

@ -1,20 +1,18 @@
### Raspberry Pi example
To compile example for Raspberry Pi you will need RPi toolchain and userspace libraries, and Go must be cross compiled for linux/arm with that toolchain.
To cross compile example for Raspberry Pi you will need [RPi toolchain](https://github.com/raspberrypi/tools/tree/master/arm-bcm2708) and [userspace libraries](https://github.com/raspberrypi/firmware) (opt/vc).
There is a bootstrap.sh script that you can use to cross compile Go and OpenAL.
Export path to RPi toolchain:
Compile Go and OpenAL, /usr/local is prefix where Go and RPi toolchain and libraries will be installed:
export RPI_HOME=/opt/tools/arm-bcm2708/arm-linux-gnueabihf
./bootstrap.sh /usr/local
Add toolchain bin directory to PATH:
After build is complete point GOROOT to new Go installation in /usr/local, and add toolchain bin directory to PATH:
export GOROOT=/usr/local/go
export PATH=/usr/local/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin:${PATH}
export PATH=${RPI_HOME}/bin:${PATH}
And compile example:
CGO_CFLAGS="-I/usr/local/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/include -I/usr/local/vc/include -I/usr/local/vc/include/interface/vcos -I/usr/local/vc/include/interface/vmcs_host/linux -I/usr/local/vc/include/interface/vcos/pthreads" \
CGO_LDFLAGS="-L/usr/local/vc/lib -L/usr/local/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/lib -ldl" \
CC=arm-linux-gnueabihf-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=6 ${GOROOT}/bin/go build -v -x
CC=arm-linux-gnueabihf-gcc \
CGO_CFLAGS="-I/opt/vc/include -I/opt/vc/include/interface/vcos -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/include/interface/vcos/pthreads --sysroot=${RPI_HOME}/arm-linux-gnueabihf/sysroot" \
CGO_LDFLAGS="-L/opt/vc/lib -L/opt/vc/lib64 --sysroot=${RPI_HOME}/arm-linux-gnueabihf/sysroot" \
CGO_ENABLED=1 GOOS=linux GOARCH=arm go build

View file

@ -1,41 +0,0 @@
#!/bin/sh
if [ -z "$1" ]; then
echo "Usage: bootstrap.sh <install prefix>"
exit 1
fi
GO_OS="linux"
GO_ARCH="amd64"
GO_VERSION=`curl -s https://golang.org/dl/ | grep 'id="go' | head -n1 | awk -F'"' '{print $4}'`
INSTALL_PREFIX="$1"
export PATH=${INSTALL_PREFIX}/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin:${PATH}
BUILD_DIR=`mktemp -d`
mkdir -p ${BUILD_DIR}/bootstrap
echo "##### Download toolchain"
cd ${BUILD_DIR} && git clone --depth=1 --branch=master https://github.com/raspberrypi/tools.git
cp -r -f ${BUILD_DIR}/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64 ${INSTALL_PREFIX}
echo "##### Download userspace libraries"
cd ${BUILD_DIR} && git clone --depth=1 --branch=master https://github.com/raspberrypi/firmware.git
cp -r -f ${BUILD_DIR}/firmware/hardfp/opt/vc ${INSTALL_PREFIX}
echo "##### Download Go binaries"
cd ${BUILD_DIR}/bootstrap && curl -s -L http://storage.googleapis.com/golang/${GO_VERSION}.${GO_OS}-${GO_ARCH}.tar.gz | tar xz
echo "##### Download Go source"
cd ${BUILD_DIR} && curl -s -L http://storage.googleapis.com/golang/${GO_VERSION}.src.tar.gz | tar xz && cd ${BUILD_DIR}/go/src
echo "##### Compile Go for host"
GOROOT_BOOTSTRAP=${BUILD_DIR}/bootstrap/go ./make.bash || exit 1
echo "##### Compile Go for arm-linux-gnueabihf"
GOROOT_BOOTSTRAP=${BUILD_DIR}/bootstrap/go CC_FOR_TARGET=arm-linux-gnueabihf-gcc GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=1 ./make.bash --no-clean || exit 1
cp -r -f ${BUILD_DIR}/go ${INSTALL_PREFIX}
echo "##### Remove build directory"
rm -rf ${BUILD_DIR}

View file

@ -3,7 +3,7 @@
package raylib
/*
#cgo linux,arm LDFLAGS: -lbrcmGLESv2 -lbrcmEGL -lpthread -lrt -lm -lbcm_host -lvcos -lvchiq_arm -ldl
#cgo linux,arm LDFLAGS: -L/opt/vc/lib -L/opt/vc/lib64 -lbrcmGLESv2 -lbrcmEGL -lpthread -lrt -lm -lbcm_host -lvcos -lvchiq_arm -ldl
#cgo linux,arm CFLAGS: -DPLATFORM_RPI -DGRAPHICS_API_OPENGL_ES2 -Iexternal -I/opt/vc/include -I/opt/vc/include/interface/vcos -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/include/interface/vcos/pthreads
*/
import "C"