~popey/phablet-tools/fix-1360582

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/sh
# This program is free software: you can redistribute it and/or modify it
# under the terms of the the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the applicable version of the GNU General Public
# License for more details.
#.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2014 Canonical, Ltd.
#
# Author: Sergio Schvezov <sergio.schvezov@canonical.com>

export LC_ALL=C

usage() {
cat <<EOF
usage: $0 [OPTIONS]

Creates and runs click apps

OPTIONS:
  -h	Show this message
  -s    Specify the serial of the device to install (see adb $ADBOPTS devices)

  --bzr-source 	bzr sources used to backreference the package
  --dir      	directory holding the package to build
  --provision   Install resulting click and run tests
  --no-clean    Don't clean temporary directories
  --extra-deps  Packages required in the chroot which are not part of the SDK
  --arch        Target architecture, requires a created click chroot
                (defaults to arch all)
  --framework   Target click framework, requires a created click chroot
                (defaults to ubuntu-sdk-13.10)
EOF
}

ADBOPTS=""
PROVISION=""
SOURCE=""
BZR_SOURCE=""
NO_CLEAN=""
EXTRA_DEPS=""
ARCH="all"
FRAMEWORK="ubuntu-sdk-13.10"
MAINTMODE=""

TEST_DIR='tests/autopilot'
DEVICE_USER='phablet'

ARGS=$(getopt -o s:h -l "maint-mode,extra-deps:,bzr-source:,provision,no-clean,framework:,dir:,arch:,help" -n "$0" -- "$@")

if [ $? -ne 0 ] ; then
    exit 1
fi
eval set -- "$ARGS"

while true; do
    case "$1" in
        -h|--help)
            usage
            exit 0
            ;;
        -s)
            shift
            ADBOPTS="-s $1"
            shift
            ;;
        --provision)
            shift
            PROVISION=1
            ;;
        --bzr-source)
            shift
            BZR_SOURCE="$1"
            shift
            ;;
        --dir)
            shift
            SOURCE="$(readlink -f $1)"
            shift
            ;;
        --no-clean)
            shift
            NO_CLEAN=1
            ;;
		--arch)
			shift
			ARCH="$1"
			shift
			;;
		--framework)
			shift
			FRAMEWORK="$1"
			shift
			;;
		--extra-deps)
			shift
			EXTRA_DEPS="$1"
			shift
			;;
		--maint-mode)
			shift
			MAINTMODE=1
			;;
        --)
            shift
            break
            ;;
    esac
done

builddir=$(mktemp -d)
installdir=$(mktemp -d)
if [ -z "$NO_CLEAN" ]; then
    trap 'rm -rf "$builddir" "$installdir"' EXIT
fi

if [ -z "$SOURCE" ]; then
	SOURCE="$(readlink -f $(pwd))"
fi

if [ ! -f $SOURCE/CMakeLists.txt ]; then
    echo "$SOURCE not a valid source dir"
    usage
    exit 1
fi

workdir=$(pwd)
cd $builddir

CMAKE_PARAMS="-DINSTALL_TESTS=off -DCLICK_MODE=on \
	-DBZR_REVNO=$(cd $SOURCE; bzr revno)"

if [ -n "$BZR_SOURCE" ]; then
	CMAKE_PARAMS="$CMAKE_PARAMS -DBZR_SOURCE=$BZR_SOURCE"
fi

set -e
if [ "$ARCH" = "all" ]; then
	cmake "$SOURCE" $CMAKE_PARAMS
	make DESTDIR=$installdir install
else
	if [ -n "$EXTRA_DEPS" ] && [ -z "$MAINTMODE" ]; then
		echo "click chroot still doesn\'t support sessions and deps outside the"
		echo "default sdk were requested, you can optionally use click maint"
		echo "to install $EXTRA_DEPS by running"
		echo "    click chroot -a$ARCH -f$FRAMEWORK maint apt-get update"
		echo "    click chroot -a$ARCH -f$FRAMEWORK maint apt-get install $EXTRA_DEPS"
		echo
		echo "Your chroot will be unclean from then on, so if you want to"
		echo "guarantee clean builds the chroot would need to be recreated."
		echo
		echo "Use the undocumented --maint-mode if you want this automated."
		exit 1
	fi
	cd "$SOURCE"
	if [ -n "$MAINTMODE" ]; then
		trap 'click chroot -a$ARCH -f$FRAMEWORK maint apt-get autoremove --yes $EXTRA_DEPS' \
		   	EXIT HUP INT TERM
		click chroot -a$ARCH -f$FRAMEWORK maint apt-get update
		click chroot -a$ARCH -f$FRAMEWORK maint apt-get install --yes $EXTRA_DEPS
	fi
	click chroot -a$ARCH -f$FRAMEWORK run cmake $CMAKE_PARAMS
	click chroot -a$ARCH -f$FRAMEWORK run make
    click chroot -a$ARCH -f$FRAMEWORK run make DESTDIR=$installdir install
fi

if [ ! -f "$installdir/manifest.json" ]; then
	echo Building failed, check environment
	exit 1
fi

click build $installdir
if [ "$workdir" != "$(pwd)" ]; then
	cp *.click $workdir
fi

if [ -n "$PROVISION" ]; then
    set -e
    for click in *.click; do
        adb $ADBOPTS push "$click" /tmp
        adb $ADBOPTS shell sudo -u $DEVICE_USER bash -ic "pkcon install-local /tmp/$click"
    done

    adb $ADBOPTS shell mkdir -p /home/$DEVICE_USER/autopilot
    adb $ADBOPTS push $SOURCE/tests/autopilot /home/$DEVICE_USER/autopilot
    adb $ADBOPTS shell chown -R "$DEVICE_USER":"$DEVICE_USER" /home/$DEVICE_USER/autopilot
    set +e

    echo Allowing autopilot to play well with apparmor
    phablet-config autopilot --dbus-probe enable
    echo "Ready to run autopilot"
fi

if [ -n "$NO_CLEAN" ]; then
    echo build dir was $builddir
    echo install dir was $installdir
fi