983a649fd84671c5633a586426ae94b1f720a1e2
[iec.git] / src / type3_AndroidCloud / anbox-master / scripts / setup-partial-chroot.sh
1 #!/bin/bash
2 #
3 # Copyright © 2016 Canonical Ltd.
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License version 3,
7 # as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Taken from the Mir Project (https://launchpad.net/mir)
18
19 set -e
20
21 name=${0}
22
23 usage() {
24     echo "Usage: ${name} [options] mychroot-dir"
25     echo "options:"
26     echo "      -a arch Select architecture, i.e. armhf, arm64, ppc... Default is armhf"
27     echo "      -d dist Select distribution, i.e. vivid, wily. Default is vivid"
28     echo "      -r rep  Select an additional repository for bootstrap. Default is none"
29     echo
30     echo "please supply at least a directory to create partial chroot in. (eg, ./setup-partial-armhf-chroot.sh mychroot-dir)"
31 }
32
33 # Default to vivid as we don't seem to have any working wily devices right now.
34 # Also Jenkins expects this script to default to vivid (TODO: update CI?)
35 arch=armhf
36 dist=vivid
37 sourceid=0
38 repositories=
39 sources=
40
41 while getopts a:d:r:h opt; do
42     case $opt in
43         a)
44             arch=$OPTARG
45             ;;
46         d)
47             dist=$OPTARG
48             ;;
49         r)
50             repositories="$repositories $OPTARG"
51             ((++sourceid))
52             sources="$sources source$sourceid"
53             ;;
54         :)
55             echo "Option -$OPTARG requires an argument" 
56             usage
57             exit 1
58             ;;
59         h)
60             usage
61             exit 0
62             ;;
63         \?)
64             echo "Invalid option: -$OPTARG" 
65             usage
66             exit 1
67             ;;
68     esac
69 done
70
71 shift $((OPTIND-1))
72
73 if [ -z ${1} ]; then
74     usage
75     exit 1
76 fi
77
78 directory=${1}
79 echo "creating phablet-compatible $arch partial chroot for anbox compilation in directory ${directory}"
80
81 if [ ! -d ${directory} ]; then
82     mkdir -p ${directory} 
83 fi
84
85 DEBCONTROL=$(pwd)/../debian/control
86
87 pushd ${directory} > /dev/null
88
89 # Empty dpkg status file, so that ALL dependencies are listed with dpkg-checkbuilddeps
90 echo "" > status
91
92 # Manual error code checking is needed for dpkg-checkbuilddeps
93 set +e
94
95 # Parse dependencies from debian/control
96 # dpkg-checkbuilddeps returns non-zero when dependencies are not met and the list is sent to stderr
97 builddeps=$(dpkg-checkbuilddeps -a ${arch} --admindir=. ${DEBCONTROL} 2>&1 )
98 if [ $? -eq 0 ] ; then
99     exit 0 
100 fi
101 echo "${builddeps}"
102
103 # now turn exit on error option
104 set -e
105
106 # Sanitize dependencies list for submission to multistrap
107 # build-essential is not needed as we are cross-compiling
108 builddeps=$(echo ${builddeps} | sed -e 's/dpkg-checkbuilddeps://g' \
109                                     -e 's/error://g' \
110                                     -e 's/Unmet build dependencies://g' \
111                                     -e 's/build-essential:native//g')
112 builddeps=$(echo ${builddeps} | sed 's/([^)]*)//g')
113 builddeps=$(echo ${builddeps} | sed -e 's/abi-compliance-checker//g')
114 builddeps=$(echo ${builddeps} | sed -e 's/multistrap//g')
115
116 case ${arch} in
117     amd64 | i386 )
118         source_url=http://archive.ubuntu.com/ubuntu
119         ;;
120     * )
121         source_url=http://ports.ubuntu.com/ubuntu-ports
122         ;;
123 esac
124
125 echo "[General]
126 arch=${arch}
127 directory=${directory}
128 unpack=false
129 noauth=true
130 bootstrap=Ubuntu UbuntuUpdates UbuntuSecurity ${sources}
131
132 [Ubuntu]
133 packages=${builddeps}
134 source=${source_url}
135 suite=${dist}
136 components=main universe
137
138 [UbuntuUpdates]
139 packages=${builddeps}
140 source=${source_url}
141 suite=${dist}-updates
142 components=main universe
143
144 [UbuntuSecurity]
145 packages=${builddeps}
146 source=${source_url}
147 suite=${dist}-security
148 components=main universe
149 " > mstrap.conf
150
151 sourceid=0
152 for x in ${repositories};
153 do
154     ((++sourceid))
155     echo "[source${sourceid}]
156 source=${x}
157 suite=${dist}
158 " >> mstrap.conf
159 done
160
161 multistrap -f mstrap.conf 
162
163 rm -f var/cache/apt/archives/lock
164
165 # Remove libc libraries that confuse the cross-compiler
166 rm -f var/cache/apt/archives/libc-dev*.deb
167 rm -f var/cache/apt/archives/libc6*.deb
168
169 for deb in var/cache/apt/archives/* ; do
170     if [ ! -d ${deb} ] ; then
171         echo "unpacking: ${deb}"
172         dpkg -x ${deb} .
173     fi
174 done
175
176 # Fix up symlinks which asssumed the usual root path
177 for broken_symlink in $(find . -name \*.so -type l -xtype l) ; do
178     ln -sf $(pwd)$(readlink ${broken_symlink}) ${broken_symlink}
179 done
180
181 popd > /dev/null