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
19HELP=no
20VERBOSE=no
21MINSDK=1
22TARGET=1
23NAME=""
24OUT_DIR=
25ACTIVITY=""
26PACKAGE=""
27SDK=""
28TESTCASE_PATH=""
29DRIVER=""
30
31check_param ()
32{
33    if [ -z "$2" ]; then
34        echo "ERROR: Missing parameter after option '$1'"
35        exit 1
36    fi
37}
38
39check_required_param()
40{
41    if [ -z "$1" ]; then
42        echo "ERROR: Missing required parameter $2"
43        exit 1
44    fi
45}
46
47run ()
48{
49    if [ "$VERBOSE" = "yes" ] ; then
50        echo "## COMMAND: $@"
51    fi
52    $@ 2>&1
53}
54
55process_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
63while [ -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
121done
122
123if [ "$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
143fi
144
145# Verify required parameters are non-empty
146check_required_param "$SDK" "--sdk"
147check_required_param "$OUT_DIR" "--out"
148check_required_param "$TESTCASE_PATH" "--testcase"
149check_required_param "$DRIVER" "--driver"
150
151# Compute name of testcase
152TESTCASE=`basename $TESTCASE_PATH .rs`
153
154# Compute activity, appname, and java package, if not specified via parameters
155if [ -z "$ACTIVITY" ]; then
156  ACTIVITY="$TESTCASE";
157fi
158
159if [ -z "$NAME" ]; then
160  NAME="$ACTIVITY"
161fi
162
163if [ -z "$PACKAGE" ]; then
164  PACKAGE=com.android.test.rsdebug.$TESTCASE
165fi
166
167# Create the project
168run $SDK/tools/android create project --target $TARGET --name $NAME --path $OUT_DIR --activity $ACTIVITY --package $PACKAGE
169
170if [ $? != 0 ] ; then
171    echo "ERROR: Could not create Android project."
172    echo "       Check parameters and try again."
173    exit 1
174fi
175
176# Compute name of destination source directory
177DEST_SRC_DIR=$OUT_DIR/src/`echo $PACKAGE | sed 's/\./\//g'`
178
179if [ ! -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
187fi
188
189echo "Copying driver template from $DRIVER -> $DEST_SRC_DIR"
190if [ ! -d "$DEST_SRC_DIR" ]; then
191  echo "Error, destination directory does not exist: $DEST_SRC_DIR";
192  exit 1;
193fi
194echo "Performing template substitutions:"
195echo "    %ACTIVITY% ==> $ACTIVITY"
196echo "    %PACKAGE% ==> $PACKAGE"
197echo "    %TESTCASE% ==> $TESTCASE"
198echo "    %MINSDK% ==> $MINSDK"
199SUBST_PARAMS="$ACTIVITY $PACKAGE $TESTCASE $MINSDK"
200
201# If it exists, use contents of driver-common directory to seed
202# the testcase project
203DRIVER_COMMON="`dirname $TESTCASE_PATH`/driver-common"
204if [ -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
217fi
218
219# Copy Java source to project directory.
220ls $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
224done;
225
226# Copy AndroidManifest.xml override, if it exists
227OVERRIDE_MANIFEST="$DRIVER/AndroidManifest.xml"
228if [ -e $OVERRIDE_MANIFEST ]; then
229  process_template $OVERRIDE_MANIFEST $OUT_DIR/AndroidManifest.xml $SUBST_PARAMS
230fi
231
232# Copy RS testcase to project directory.
233TESTCASE_DEST=$DEST_SRC_DIR/`basename $TESTCASE_PATH`
234process_template $TESTCASE_PATH $TESTCASE_DEST $SUBST_PARAMS
235
236# Buid signed and aligned apk
237cd $OUT_DIR
238run ant clean debug install
239
240if [ $? != 0 ] ; then
241    echo "ERROR: Apk build and install failed"
242    exit 1
243fi
244
245exit 0
246