1#!/usr/bin/env bash
2
3# Copyright 2017 The Rust Project Developers. See the COPYRIGHT
4# file at the top-level directory of this distribution and at
5# http://rust-lang.org/COPYRIGHT.
6#
7# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10# option. This file may not be copied, modified, or distributed
11# except according to those terms.
12
13set -ex
14
15URL=https://dl.google.com/android/repository/sys-img/android
16
17main() {
18    local arch="${1}"
19    local name="${2}"
20    local dest=/system
21    local td
22    td="$(mktemp -d)"
23
24    apt-get install --no-install-recommends e2tools
25
26    pushd "${td}"
27    curl --retry 5 -O "${URL}/${name}"
28    unzip -q "${name}"
29
30    local system
31    system="$(find . -name system.img)"
32    mkdir -p ${dest}/{bin,lib,lib64}
33
34    # Extract android linker and libraries to /system
35    # This allows android executables to be run directly (or with qemu)
36    if [ "${arch}" = "x86_64" ] || [ "${arch}" = "arm64" ]; then
37        e2cp -p "${system}:/bin/linker64" "${dest}/bin/"
38        e2cp -p "${system}:/lib64/libdl.so" "${dest}/lib64/"
39        e2cp -p "${system}:/lib64/libc.so" "${dest}/lib64/"
40        e2cp -p "${system}:/lib64/libm.so" "${dest}/lib64/"
41    else
42        e2cp -p "${system}:/bin/linker" "${dest}/bin/"
43        e2cp -p "${system}:/lib/libdl.so" "${dest}/lib/"
44        e2cp -p "${system}:/lib/libc.so" "${dest}/lib/"
45        e2cp -p "${system}:/lib/libm.so" "${dest}/lib/"
46    fi
47
48    # clean up
49    apt-get purge --auto-remove -y e2tools
50
51    popd
52
53    rm -rf "${td}"
54}
55
56main "${@}"
57