From 55dd52f38622046fab63f7d383ddfa75d0aee667 Mon Sep 17 00:00:00 2001 From: Milan Nikolic Date: Sun, 29 Jan 2017 00:58:16 +0100 Subject: [PATCH] Add Android instructions and bootstrap script --- README.md | 4 ++ examples/android/example/README.md | 36 +++++++++++ examples/android/example/bootstrap.sh | 93 +++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 examples/android/example/README.md create mode 100755 examples/android/example/bootstrap.sh diff --git a/README.md b/README.md index bc276e6..2cd1893 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,10 @@ Golang bindings for [raylib](http://www.raylib.com/), a simple and easy-to-use l brew install glfw3 brew install openal-soft +##### Android + +[Android example](https://github.com/gen2brain/raylib-go/tree/master/examples/android/example). + ### Installation go get -v github.com/gen2brain/raylib-go diff --git a/examples/android/example/README.md b/examples/android/example/README.md new file mode 100644 index 0000000..5f096c8 --- /dev/null +++ b/examples/android/example/README.md @@ -0,0 +1,36 @@ +### Android example + +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 and OpenAL 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, OpenAL and android_native_app_glue, /usr/local is prefix where Go and Android toolchains will be installed: + + ./bootstrap.sh /usr/local + +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/android-arm7/bin:${PATH} + +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 -work -v -x -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: + + export ANDROID_HOME=/opt/android-sdk + +And build apk with ant: + + cd android + ant clean debug + +If everything is successfully built apk can be found in bin/ directory. diff --git a/examples/android/example/bootstrap.sh b/examples/android/example/bootstrap.sh new file mode 100755 index 0000000..60287e7 --- /dev/null +++ b/examples/android/example/bootstrap.sh @@ -0,0 +1,93 @@ +#!/bin/sh + +if [ -z "$ANDROID_NDK_HOME" ]; then + echo "You must define ANDROID_NDK_HOME before starting. It should point to your NDK directories." + exit 1 +fi + +if [ -z "$1" ]; then + echo "Usage: bootstrap.sh " + 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}'` + +OPENAL_VERSION="1.17.2" + +INSTALL_PREFIX="$1" +export PATH=${INSTALL_PREFIX}/android-arm7/bin:${INSTALL_PREFIX}/android-arm64/bin:${PATH} + +BUILD_DIR=`mktemp -d` +mkdir -p ${BUILD_DIR}/bootstrap + +echo "Make standalone android toolchains" +${ANDROID_NDK_HOME}/build/tools/make-standalone-toolchain.sh --platform=android-9 --install-dir=${INSTALL_PREFIX}/android-arm7 --toolchain=arm-linux-androideabi-4.9 +${ANDROID_NDK_HOME}/build/tools/make-standalone-toolchain.sh --platform=android-21 --install-dir=${INSTALL_PREFIX}/android-arm64 --toolchain=aarch64-linux-android-4.9 + +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-androideabi" +GOROOT_BOOTSTRAP=${BUILD_DIR}/bootstrap/go CC_FOR_TARGET=arm-linux-androideabi-gcc GOOS=android GOARCH=arm CGO_ENABLED=1 ./make.bash --no-clean || exit 1 + +echo "Compile Go for aarch64-linux-android" +GOROOT_BOOTSTRAP=${BUILD_DIR}/bootstrap/go CC_FOR_TARGET=aarch64-linux-android-gcc GOOS=android GOARCH=arm64 CGO_ENABLED=1 ./make.bash --no-clean || exit 1 + +cp -r -f ${BUILD_DIR}/go ${INSTALL_PREFIX} + +echo "Compile OpenAL" + +cd ${BUILD_DIR} && curl -s -L http://kcat.strangesoft.net/openal-releases/openal-soft-${OPENAL_VERSION}.tar.bz2 | tar -xj + +cat << EOF > ${BUILD_DIR}/openal-soft-${OPENAL_VERSION}/android-arm7.cmake +set(TOOLCHAIN_PREFIX arm-linux-androideabi) +set(CMAKE_C_COMPILER \${TOOLCHAIN_PREFIX}-gcc) +set(CMAKE_FIND_ROOT_PATH \${INSTALL_PREFIX}/android-arm7) +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +EOF + +cat << EOF > ${BUILD_DIR}/openal-soft-${OPENAL_VERSION}/android-arm64.cmake +set(TOOLCHAIN_PREFIX aarch64-linux-android) +set(CMAKE_C_COMPILER \${TOOLCHAIN_PREFIX}-gcc) +set(CMAKE_FIND_ROOT_PATH \${INSTALL_PREFIX}/android-arm64) +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +EOF + +mkdir -p ${BUILD_DIR}/openal-soft-${OPENAL_VERSION}/build-arm7 +cd ${BUILD_DIR}/openal-soft-${OPENAL_VERSION}/build-arm7 +cmake -DLIBTYPE=STATIC -DCMAKE_TOOLCHAIN_FILE=../android-arm7.cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX}/android-arm7 .. +make -j $(nproc) && make install + +mkdir -p ${BUILD_DIR}/openal-soft-${OPENAL_VERSION}/build-arm64 +cd ${BUILD_DIR}/openal-soft-${OPENAL_VERSION}/build-arm64 +cmake -DLIBTYPE=STATIC -DCMAKE_TOOLCHAIN_FILE=../android-arm64.cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX}/android-arm64 .. +make -j $(nproc) && make install + +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 + +cp ${BUILD_DIR}/native_app_glue/obj/local/armeabi-v7a/libandroid_native_app_glue.a ${INSTALL_PREFIX}/android-arm7/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-arm7/include/ +cp ${ANDROID_NDK_HOME}/sources/android/native_app_glue/android_native_app_glue.h ${INSTALL_PREFIX}/android-arm64/include/ + +echo "Remove build directory" +rm -rf ${BUILD_DIR}