• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright (c) 2021 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15#
16# Aux functions
17#
18
19function print_help
20{
21    HELP_MESSAGE="
22    It is the bootstrap script for Panda on Ubuntu 18.04 or 20.04.
23
24    This script installs all necessary packages for building and testing Panda
25    in your local environment, given that your environment is Ubuntu 18.04 or 20.04.
26    (detected with the contents of /etc/os-release).
27
28    The script should run with superuser privileges.
29
30    EXAMPLE
31
32    $ ./scripts/install-deps-ubuntu --help
33    $ ./scripts/install-deps-ubuntu --install=x86 --install=arm-all --install=dev
34
35    or
36
37    $ ./scripts/install-deps-ubuntu -h
38    $ ./scripts/install-deps-ubuntu -i=x86 -i=arm-all -i=dev
39
40    SYNOPSIS
41
42    $0 [OPTIONS]
43
44    OPTIONS
45
46    --help              | -h              Show this message and exit.
47
48    --install=dev       | -i=dev          Install tools needed for development.
49
50    --install=arm-all   | -i=arm-all      Install extra packages for cross-compiling for AArch32 and AArch64.
51
52    --install=x86       | -i=x86          Install extra packages for cross-compiling for x86.
53
54    --install=windows   | -i=windows      Install extra packages for cross-compiling for Windows.
55
56    --install=fuzzing   | -i=fuzzing      Install extra packages for fuzzing.
57
58    CAVEAT
59
60    * Packages for cross-compiling for aarch64 and x86 cannot co-exist, so the
61    script (read: apt) will replace any conflicting packages on each run.
62    * However, packages for cross-compiling for aarch64 and 32-bit ARM can
63    co-exist, so they are in a single 'arm-all' dependency list.
64    "
65
66    echo "$HELP_MESSAGE"
67}
68
69function install_dep
70{
71    local fname=$1
72
73    if [[ ! -f "$fname" ]] ; then
74        echo "FATAL: Dependency list $fname not found."
75        exit 1
76    fi
77
78    echo "Processing $fname"
79    grep --color=never -o '^[^#]*' "$fname" | xargs apt install -y --no-install-recommends -o Dpkg::Options::="--force-overwrite"
80}
81
82function install_qemu
83{
84    local needed_version="6.0.0"
85
86    local aarch64_version=$(qemu-aarch64 --version | grep version | awk '{print $3}')
87    local arm_version=$(qemu-arm --version | grep version | awk '{print $3}')
88
89    if [[ "$aarch64_version" == "$needed_version" && "$arm_version" == "$needed_version" ]] ; then
90        return
91    fi
92
93    local src_dir=$(mktemp -d)
94    local qemu_name="qemu-$needed_version"
95    local archive_name="$src_dir/$qemu_name.tar.xz"
96
97    echo "Dowloading qemu sources to $archive_name"
98    curl --progress https://download.qemu.org/$qemu_name.tar.xz --output "$archive_name"
99
100    echo "Extracting $archive_name"
101    tar -C $src_dir -xf $archive_name
102
103    echo "Installing qemu build dependencies"
104    apt-get install libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev pkg-config flex bison ninja
105
106    cd "$src_dir/$qemu_name"
107    ./configure --target-list=aarch64-linux-user,arm-linux-user
108    make -j install
109}
110
111function enable_llvm_repo
112{
113    local llvm_url=http://apt.llvm.org
114
115    local repo_name="deb ${llvm_url}/${UBUNTU_CODENAME}/  llvm-toolchain-${UBUNTU_CODENAME}-12  main"
116    curl https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - \
117        && echo -e $repo_name > /etc/apt/sources.list.d/llvm-12.list \
118        && apt-get update
119}
120
121#
122# Main logic
123#
124
125#
126# Parse command-line arguments
127#
128
129# Set default flag values
130INSTALL_CI_TOOLS=no
131INSTALL_DEV=no
132INSTALL_CROSS_ARM_ALL=no
133INSTALL_CROSS_x86=no
134INSTALL_CROSS_WINDOWS=no
135INSTALL_FUZZING_TOOLS=no
136INSTALL_ARM_DEV=no
137SRC_LIST_STR='# This file is generated automatically by Panda install-deps-ubuntu script. DO NOT EDIT!!!\n'
138
139
140for i in "$@"
141do
142    case $i in
143    -h|--help)
144        print_help
145        exit 0
146        ;;
147    -i=*|--install=*)
148        FLAG_ARG=${i//[-a-zA-Z0-9]*=/}
149        if [[ $FLAG_ARG == "dev" ]] ; then
150            if [[ $INSTALL_ARM_DEV == "yes" ]] ; then
151                echo "FATAL: Parameter --install=dev excludes --install=arm-dev"
152                exit 1
153            else
154                INSTALL_DEV=yes
155            fi
156        fi
157        if [[ $FLAG_ARG == "arm-all" ]] ; then
158            INSTALL_CROSS_ARM_ALL=yes
159        fi
160        if [[ $FLAG_ARG == "x86" ]] ; then
161            INSTALL_CROSS_x86=yes
162        fi
163        if [[ $FLAG_ARG == "windows" ]] ; then
164            INSTALL_CROSS_WINDOWS=yes
165        fi
166        if [[ $FLAG_ARG == "ci-tools" ]] ; then
167            INSTALL_CI_TOOLS=yes
168        fi
169        if [[ $FLAG_ARG == "fuzzing" ]] ; then
170            if [[ $(arch) == "x86_64" ]] ; then
171               INSTALL_FUZZING_TOOLS=yes
172            else
173               echo "Fuzzing is supported only for amd64. Options will have no effect"
174            fi
175        fi
176        if [[ $FLAG_ARG == "arm-dev" ]] ; then
177            if [[ $INSTALL_DEV == "yes" ]] ; then
178                echo "FATAL: Parameter --install=arm-dev excludes --install=dev"
179                exit 1
180            else
181                INSTALL_ARM_DEV=yes
182            fi
183        fi
184      ;;
185    *)
186      echo "Error: Unsupported flag $i" >&2
187      exit 1
188      ;;
189  esac
190done
191
192#
193# Check 'sudo' and if script is running on Ubuntu
194#
195
196if [[ $(id -u) -ne 0 ]] ; then
197    echo "FATAL: Please run as root."
198    exit 1
199fi
200
201#
202# Check specific Ubuntu version
203#
204
205UBUNTU_NAME=ubuntu
206
207if [ ! -f /etc/os-release ]; then
208    echo "FATAL: /etc/os-release not found. Exiting..."
209    exit 1
210else
211    . /etc/os-release
212
213    if [[ $NAME == "Ubuntu" ]]; then
214        apt-get update \
215        && dpkg -l | grep curl  || apt-get -y install curl \
216        && dpkg -l | grep gnupg || apt-get -y install gnupg \
217        && apt-get update
218    else
219        echo "FATAL: Only Ubuntu is supported. This is not. Exiting..."
220        exit 1
221    fi
222fi
223
224if [[ $VERSION_ID == "18.04" ]]; then
225    echo "Installing packages for Ubuntu 18.04 LTS."
226    UBUNTU_NAME=ubuntu-18-04
227elif [[ $VERSION_ID == "20.04" ]]; then
228    echo "Installing packages for Ubuntu 20.04 LTS."
229    UBUNTU_NAME=ubuntu-20-04
230else
231    echo "Trying to install packages for Ubuntu with unpinned versions."
232fi
233
234set -e
235
236#
237# Install dependencies
238#
239
240SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
241NEED_INSTALL_ADDITIONAL_FILES=false
242
243install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-build"
244
245if [[ "x$INSTALL_CI_TOOLS" == "xyes" ]] ; then
246    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-ci"
247fi
248
249if [[ "x$INSTALL_FUZZING_TOOLS" == "xyes" ]] ; then
250    enable_llvm_repo
251    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-fuzzing"
252fi
253
254if [[ "x$INSTALL_DEV" == "xyes" ]] ; then
255    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-dev"
256fi
257
258if [[ "x$INSTALL_CROSS_ARM_ALL" == "xyes" ]] ; then
259    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-cross-arm-all"
260    install_qemu
261fi
262
263if [[ "x$INSTALL_CROSS_x86" == "xyes" ]] ; then
264    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-cross-x86"
265fi
266
267if [[ "x$INSTALL_CROSS_WINDOWS" == "xyes" ]] ; then
268    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-cross-windows"
269fi
270
271if [[ "x$INSTALL_ARM_DEV" == "xyes" ]] ; then
272    install_dep "$SCRIPT_DIR/dep-lists/$UBUNTU_NAME-arm-dev"
273    install_qemu
274fi
275