From 200992b27216dc43bd42d2fc57db032c1a08e8c8 Mon Sep 17 00:00:00 2001 From: folecr Date: Fri, 27 Jul 2012 12:35:20 -0700 Subject: [PATCH] Cleaner build script * Quoted parameters for bash directives * Forgo the extra _LOCAL variables by testing environment variables * Better path assumptions * References * http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in * http://stackoverflow.com/questions/228544/how-to-tell-if-a-string-is-not-defined-in-a-bash-shell-script --- samples/HelloCpp/proj.android/build_native.sh | 68 +++++++++++-------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/samples/HelloCpp/proj.android/build_native.sh b/samples/HelloCpp/proj.android/build_native.sh index 791b5c05e2..d4dc72ba5b 100755 --- a/samples/HelloCpp/proj.android/build_native.sh +++ b/samples/HelloCpp/proj.android/build_native.sh @@ -1,6 +1,6 @@ -# set params -NDK_ROOT_LOCAL=/home/james/Software/android/android-ndk-r8b -COCOS2DX_ROOT_LOCAL=/home/james/Project/cocos2d-x +APPNAME="HelloCpp" + +# options buildexternalsfromsource= @@ -8,7 +8,7 @@ usage(){ cat << EOF usage: $0 [options] -Build C/C++ native code using Android NDK +Build C/C++ code for $APPNAME using Android NDK OPTIONS: -s Build externals from source @@ -16,7 +16,7 @@ OPTIONS: EOF } -while getopts "s" OPTION; do +while getopts "sh" OPTION; do case "$OPTION" in s) buildexternalsfromsource=1 @@ -28,44 +28,56 @@ exit 0 esac done -# try to get global variable -if [ $NDK_ROOT"aaa" != "aaa" ]; then -echo "use global definition of NDK_ROOT: $NDK_ROOT" -NDK_ROOT_LOCAL=$NDK_ROOT +# paths + +if [ -z "${NDK_ROOT+aaa}" ]; then +# ... if NDK_ROOT is not set, use "$HOME/bin/android-ndk" + NDK_ROOT="$HOME/bin/android-ndk" fi -if [ $COCOS2DX_ROOT"aaa" != "aaa" ]; then -echo "use global definition of COCOS2DX_ROOT: $COCOS2DX_ROOT" -COCOS2DX_ROOT_LOCAL=$COCOS2DX_ROOT +if [ -z "${COCOS2DX_ROOT+aaa}" ]; then +# ... if COCOS2DX_ROOT is not set +# ... find current working directory + DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +# ... use paths relative to current directory + COCOS2DX_ROOT="$DIR/../../.." + APP_ROOT="$DIR/.." + APP_ANDROID_ROOT="$DIR" +else + APP_ROOT="$COCOS2DX_ROOT/samples/$APPNAME" + APP_ANDROID_ROOT="$COCOS2DX_ROOT/samples/$APPNAME/proj.android" fi -HELLOWORLD_ROOT=$COCOS2DX_ROOT_LOCAL/samples/HelloCpp/proj.android +echo "NDK_ROOT = $NDK_ROOT" +echo "COCOS2DX_ROOT = $COCOS2DX_ROOT" +echo "APP_ROOT = $APP_ROOT" +echo "APP_ANDROID_ROOT = $APP_ANDROID_ROOT" # make sure assets is exist -if [ -d $HELLOWORLD_ROOT/assets ]; then -rm -rf $HELLOWORLD_ROOT/assets +if [ -d "$APP_ANDROID_ROOT/assets" ]; then + rm -rf $APP_ANDROID_ROOT/assets fi -mkdir $HELLOWORLD_ROOT/assets +mkdir $APP_ANDROID_ROOT/assets # copy resources -for file in $COCOS2DX_ROOT_LOCAL/samples/HelloCpp/Resources/* +for file in $APP_ROOT/Resources/* do -if [ -d $file ]; then -cp -rf $file $HELLOWORLD_ROOT/assets +if [ -d "$file" ]; then + cp -rf $file $APP_ANDROID_ROOT/assets fi -if [ -f $file ]; then -cp $file $HELLOWORLD_ROOT/assets +if [ -f "$file" ]; then + cp $file $APP_ANDROID_ROOT/assets fi done -if [[ $buildexternalsfromsource ]]; then -echo "Building external dependencies from source" -$NDK_ROOT_LOCAL/ndk-build -C $HELLOWORLD_ROOT $* \ -NDK_MODULE_PATH=${COCOS2DX_ROOT_LOCAL}:${COCOS2DX_ROOT_LOCAL}/cocos2dx/platform/third_party/android/source +if [[ "$buildexternalsfromsource" ]]; then + echo "Building external dependencies from source" + $NDK_ROOT/ndk-build -C $APP_ANDROID_ROOT $* \ + NDK_MODULE_PATH=${COCOS2DX_ROOT}:${COCOS2DX_ROOT}/cocos2dx/platform/third_party/android/source else -echo "Using prebuilt externals" -$NDK_ROOT_LOCAL/ndk-build -C $HELLOWORLD_ROOT $* \ -NDK_MODULE_PATH=${COCOS2DX_ROOT_LOCAL}:${COCOS2DX_ROOT_LOCAL}/cocos2dx/platform/third_party/android/prebuilt + echo "Using prebuilt externals" + $NDK_ROOT/ndk-build -C $APP_ANDROID_ROOT $* \ + NDK_MODULE_PATH=${COCOS2DX_ROOT}:${COCOS2DX_ROOT}/cocos2dx/platform/third_party/android/prebuilt fi