Home | History | Annotate | Download | only in debuginfo
      1 #!/bin/bash -e
      2 
      3 # Copyright 2012, The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #     http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 # Creates and builds projects from a RenderScript testcase and a set of Java templates
     18 
     19 HELP=no
     20 VERBOSE=no
     21 MINSDK=1
     22 TARGET=1
     23 NAME=""
     24 OUT_DIR=
     25 ACTIVITY=""
     26 PACKAGE=""
     27 SDK=""
     28 TESTCASE_PATH=""
     29 DRIVER=""
     30 
     31 check_param ()
     32 {
     33     if [ -z "$2" ]; then
     34         echo "ERROR: Missing parameter after option '$1'"
     35         exit 1
     36     fi
     37 }
     38 
     39 check_required_param()
     40 {
     41     if [ -z "$1" ]; then
     42         echo "ERROR: Missing required parameter $2"
     43         exit 1
     44     fi
     45 }
     46 
     47 run ()
     48 {
     49     if [ "$VERBOSE" = "yes" ] ; then
     50         echo "## COMMAND: $@"
     51     fi
     52     $@ 2>&1
     53 }
     54 
     55 process_template()
     56 {
     57   src=$1
     58   dest=$2
     59   sed -e "s/%ACTIVITY%/$3/g" -e "s/%PACKAGE%/$4/g" -e "s/%TESTCASE%/$5/g" -e "s/%MINSDK%/$6/g" < $src > $dest;
     60   echo "processed $src ==> $dest"
     61 }
     62 
     63 while [ -n "$1" ]; do
     64     opt="$1"
     65     case "$opt" in
     66         --help|-h|-\?)
     67             HELP=yes
     68             ;;
     69         --verbose|-v)
     70             VERBOSE=yes
     71             ;;
     72         --sdk)
     73             check_param $1 $2
     74             SDK="$2"
     75             ;;
     76         --name)
     77             check_param $1 $2
     78             NAME="$2"
     79             ;;
     80         --out)
     81             check_param $1 $2
     82             OUT_DIR="$2"
     83             ;;
     84         --activity)
     85             check_param $1 $2
     86             ACTIVITY="$2"
     87             ;;
     88         --package)
     89             check_param $1 $2
     90             PACKAGE="$2"
     91             ;;
     92         --minsdk)
     93             check_param $1 $2
     94             MINSDK="$2"
     95             ;;
     96         --target)
     97             check_param $1 $2
     98             TARGET="$2"
     99             ;;
    100         --testcase)
    101             check_param $1 $2
    102             TESTCASE_PATH="$2"
    103             ;;
    104         --driver)
    105             check_param $1 $2
    106             DRIVER="${2%/}"
    107             ;;
    108         -*) # unknown options
    109             echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
    110             exit 1
    111         ;;
    112         *)  # Simply record parameter
    113             if [ -z "$PARAMETERS" ] ; then
    114                 PARAMETERS="$opt"
    115             else
    116                 PARAMETERS="$PARAMETERS $opt"
    117             fi
    118             ;;
    119     esac
    120     shift
    121 done
    122 
    123 if [ "$HELP" = "yes" ] ; then
    124     echo "Usage: $PROGNAME [options]"
    125     echo ""
    126     echo "Build a test project from a RS testcase and a java driver template."
    127     echo ""
    128     echo "Required Parameters:"
    129     echo "    --sdk                Location of Android SDK installation"
    130     echo "    --out <path>         Location of your project directory"
    131     echo "    --testcase <name>    The .rs testcase file with which to build the project"
    132     echo "    --driver <name>      The java template directory with which to build the project"
    133     echo ""
    134     echo "Optional Parameters (reasonable defaults are used if not specified)"
    135     echo "    --activity <name>    Name for your default Activity class"
    136     echo "    --package <name>     Package namespace for your project"
    137     echo "    --target <name>      Android build target. Execute 'android list targets' to list available targets and their ID's."
    138     echo "    --minsdk <name>      minSdkVersion attribute to embed in AndroidManifest.xml of test project."
    139     echo "    --help|-h|-?         Print this help"
    140     echo "    --verbose|-v         Enable verbose mode"
    141     echo ""
    142     exit 0
    143 fi
    144 
    145 # Verify required parameters are non-empty
    146 check_required_param "$SDK" "--sdk"
    147 check_required_param "$OUT_DIR" "--out"
    148 check_required_param "$TESTCASE_PATH" "--testcase"
    149 check_required_param "$DRIVER" "--driver"
    150 
    151 # Compute name of testcase
    152 TESTCASE=`basename $TESTCASE_PATH .rs`
    153 
    154 # Compute activity, appname, and java package, if not specified via parameters
    155 if [ -z "$ACTIVITY" ]; then
    156   ACTIVITY="$TESTCASE";
    157 fi
    158 
    159 if [ -z "$NAME" ]; then
    160   NAME="$ACTIVITY"
    161 fi
    162 
    163 if [ -z "$PACKAGE" ]; then
    164   PACKAGE=com.android.test.rsdebug.$TESTCASE
    165 fi
    166 
    167 # Create the project
    168 run $SDK/tools/android create project --target $TARGET --name $NAME --path $OUT_DIR --activity $ACTIVITY --package $PACKAGE
    169 
    170 if [ $? != 0 ] ; then
    171     echo "ERROR: Could not create Android project."
    172     echo "       Check parameters and try again."
    173     exit 1
    174 fi
    175 
    176 # Compute name of destination source directory
    177 DEST_SRC_DIR=$OUT_DIR/src/`echo $PACKAGE | sed 's/\./\//g'`
    178 
    179 if [ ! -d "$DRIVER" ]; then
    180   # If driver directory does not exist, try to fix it up by searching the
    181   # testcase directory as well
    182   DRIVER=`dirname $TESTCASE_PATH`/"$DRIVER"
    183   if [ ! -d $DRIVER ]; then
    184     echo "unable to find driver in $DRIVER, please check --driver"
    185     exit 1;
    186   fi
    187 fi
    188 
    189 echo "Copying driver template from $DRIVER -> $DEST_SRC_DIR"
    190 if [ ! -d "$DEST_SRC_DIR" ]; then
    191   echo "Error, destination directory does not exist: $DEST_SRC_DIR";
    192   exit 1;
    193 fi
    194 echo "Performing template substitutions:"
    195 echo "    %ACTIVITY% ==> $ACTIVITY"
    196 echo "    %PACKAGE% ==> $PACKAGE"
    197 echo "    %TESTCASE% ==> $TESTCASE"
    198 echo "    %MINSDK% ==> $MINSDK"
    199 SUBST_PARAMS="$ACTIVITY $PACKAGE $TESTCASE $MINSDK"
    200 
    201 # If it exists, use contents of driver-common directory to seed
    202 # the testcase project
    203 DRIVER_COMMON="`dirname $TESTCASE_PATH`/driver-common"
    204 if [ -d $DRIVER_COMMON ]; then
    205   echo "Found common driver directory: $DRIVER_COMMON"
    206   ls $DRIVER_COMMON/SRC/*.java.template | while read src; do
    207     SRC_BASENAME=`basename $src .java.template`;
    208     dest=$DEST_SRC_DIR/`echo $SRC_BASENAME | sed "s/ACTIVITY/$ACTIVITY/g"`.java
    209     process_template $src $dest $SUBST_PARAMS
    210   done;
    211 
    212   # Copy AndroidManifest.xml
    213   COMMON_MANIFEST="$DRIVER_COMMON/AndroidManifest.xml"
    214   if [ -e $COMMON_MANIFEST ]; then
    215     process_template $COMMON_MANIFEST $OUT_DIR/AndroidManifest.xml $SUBST_PARAMS
    216   fi
    217 fi
    218 
    219 # Copy Java source to project directory.
    220 ls $DRIVER/*.java.template | while read src; do
    221   SRC_BASENAME=`basename $src .java.template`
    222   dest=$DEST_SRC_DIR/`echo $SRC_BASENAME | sed "s/ACTIVITY/$ACTIVITY/g"`.java
    223   process_template $src $dest $SUBST_PARAMS
    224 done;
    225 
    226 # Copy AndroidManifest.xml override, if it exists
    227 OVERRIDE_MANIFEST="$DRIVER/AndroidManifest.xml"
    228 if [ -e $OVERRIDE_MANIFEST ]; then
    229   process_template $OVERRIDE_MANIFEST $OUT_DIR/AndroidManifest.xml $SUBST_PARAMS
    230 fi
    231 
    232 # Copy RS testcase to project directory.
    233 TESTCASE_DEST=$DEST_SRC_DIR/`basename $TESTCASE_PATH`
    234 process_template $TESTCASE_PATH $TESTCASE_DEST $SUBST_PARAMS
    235 
    236 # Buid signed and aligned apk
    237 cd $OUT_DIR
    238 run ant clean debug install
    239 
    240 if [ $? != 0 ] ; then
    241     echo "ERROR: Apk build and install failed"
    242     exit 1
    243 fi
    244 
    245 exit 0
    246