cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

androidbuild.sh (3075B)


      1#!/bin/bash
      2
      3SOURCES=()
      4MKSOURCES=""
      5CURDIR=`pwd -P`
      6
      7# Fetch sources
      8if [[ $# -ge 2 ]]; then
      9    for src in ${@:2}
     10    do
     11        SOURCES+=($src)
     12        MKSOURCES="$MKSOURCES $(basename $src)"
     13    done
     14else
     15    if [ -n "$1" ]; then
     16        while read src
     17        do
     18            SOURCES+=($src)
     19            MKSOURCES="$MKSOURCES $(basename $src)"
     20        done
     21    fi
     22fi
     23
     24if [ -z "$1" ] || [ -z "$SOURCES" ]; then
     25    echo "Usage: androidbuild.sh com.yourcompany.yourapp < sources.list"
     26    echo "Usage: androidbuild.sh com.yourcompany.yourapp source1.c source2.c ...sourceN.c"
     27    echo "To copy SDL source instead of symlinking: COPYSOURCE=1 androidbuild.sh ... "
     28    echo "You can pass additional arguments to ndk-build with the NDKARGS variable: NDKARGS=\"-s\" androidbuild.sh ..."
     29    exit 1
     30fi
     31
     32
     33
     34SDLPATH="$( cd "$(dirname "$0")/.." ; pwd -P )"
     35
     36NDKBUILD=`which ndk-build`
     37if [ -z "$NDKBUILD" ];then
     38    echo "Could not find the ndk-build utility, install Android's NDK and add it to the path"
     39    exit 1
     40fi
     41
     42ANDROID=`which android`
     43if [ -z "$ANDROID" ];then
     44    echo "Could not find the android utility, install Android's SDK and add it to the path"
     45    exit 1
     46fi
     47
     48ANT=`which ant`
     49
     50if [ -z "$ANT" ];then
     51    echo "Could not find the ant utility, install Android's SDK and add it to the path"
     52    exit 1
     53fi
     54
     55NCPUS="1"
     56case "$OSTYPE" in
     57    darwin*)
     58        NCPU=`sysctl -n hw.ncpu`
     59        ;; 
     60    linux*)
     61        if [ -n `which nproc` ]; then
     62            NCPUS=`nproc`
     63        fi  
     64        ;;
     65  *);;
     66esac
     67
     68APP="$1"
     69APPARR=(${APP//./ })
     70BUILDPATH="$SDLPATH/build/$APP"
     71
     72# Start Building
     73
     74rm -rf $BUILDPATH
     75mkdir -p $BUILDPATH
     76
     77cp -r $SDLPATH/android-project/* $BUILDPATH
     78
     79# Copy SDL sources
     80mkdir -p $BUILDPATH/jni/SDL
     81if [ -z "$COPYSOURCE" ]; then
     82    ln -s $SDLPATH/src $BUILDPATH/jni/SDL
     83    ln -s $SDLPATH/include $BUILDPATH/jni/SDL
     84else
     85    cp -r $SDLPATH/src $BUILDPATH/jni/SDL
     86    cp -r $SDLPATH/include $BUILDPATH/jni/SDL
     87fi
     88
     89cp -r $SDLPATH/Android.mk $BUILDPATH/jni/SDL
     90sed -i "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/jni/src/Android.mk
     91sed -i "s|org\.libsdl\.app|$APP|g" $BUILDPATH/AndroidManifest.xml
     92
     93# Copy user sources
     94for src in "${SOURCES[@]}"
     95do
     96    cp $src $BUILDPATH/jni/src
     97done
     98
     99# Create an inherited Activity
    100cd $BUILDPATH/src
    101for folder in "${APPARR[@]}"
    102do
    103    mkdir -p $folder
    104    cd $folder
    105done
    106
    107ACTIVITY="${folder}Activity"
    108sed -i "s|SDLActivity|$ACTIVITY|g" $BUILDPATH/AndroidManifest.xml
    109sed -i "s|SDLActivity|$APP|g" $BUILDPATH/build.xml
    110
    111# Fill in a default Activity
    112echo "package $APP;" >  "$ACTIVITY.java"
    113echo "import org.libsdl.app.SDLActivity;" >> "$ACTIVITY.java"
    114echo "public class $ACTIVITY extends SDLActivity {}" >> "$ACTIVITY.java"
    115
    116# Update project and build
    117cd $BUILDPATH
    118android update project --path $BUILDPATH
    119$NDKBUILD -j $NCPUS $NDKARGS
    120$ANT debug
    121
    122cd $CURDIR
    123
    124APK="$BUILDPATH/bin/$APP-debug.apk"
    125
    126if [ -f "$APK" ]; then
    127    echo "Your APK is ready at $APK"
    128    echo "To install to your device: "
    129    echo "cd  $BUILDPATH"
    130    echo "ant debug install"
    131    exit 0
    132fi
    133
    134echo "There was an error building the APK"
    135exit 1