57520a9b5e4d83ab2707c8f352ab8b52024e48c0
[csit.git] / resources / libraries / bash / qemu_build.sh
1 #!/bin/bash
2 # Copyright (c) 2016 Cisco and/or its affiliates.
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 QEMU_VERSION="qemu-2.5.0"
16 QEMU_DOWNLOAD_REPO="http://download.qemu-project.org/"
17 QEMU_DOWNLOAD_PACKAGE="${QEMU_VERSION}.tar.xz"
18 QEMU_PACKAGE_URL="${QEMU_DOWNLOAD_REPO}${QEMU_DOWNLOAD_PACKAGE}"
19 QEMU_INSTALL_DIR="/opt/${QEMU_VERSION}"
20 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
21 TARGET_LIST="x86_64-softmmu"
22
23 for i in "$@"; do
24     case $i in
25         --version=*)
26             QEMU_VERSION="${i#*=}"
27             shift ;;
28         --directory=*)
29             QEMU_INSTALL_DIR="${i#*=}"
30             shift ;;
31         --patch)
32             PATCH=1
33             shift ;;
34         --force)
35             FORCE=1
36             shift ;;
37         --target-list)
38             TARGET_LIST="${i#*=}"
39             shift ;;
40         *)
41             ;;
42     esac
43 done
44
45 if test "$(id -u)" -ne 0
46 then
47     echo "Please use root or sudo to be able to install into: ${QEMU_INSTALL_DIR}"
48     exit 1
49 fi
50
51 WORKING_DIR=$(mktemp -d) || \
52     { echo "Failed to create temporary working dir"; exit 1; }
53 trap "rm -r ${WORKING_DIR}" EXIT
54
55 if [ $FORCE ]
56 then
57     rm -rf ${QEMU_INSTALL_DIR}
58 else
59     test -d ${QEMU_INSTALL_DIR} && \
60         { echo "Qemu already installed: ${QEMU_INSTALL_DIR}"; exit 0; }
61 fi
62
63 # Download QEMU source code if no local copy exists
64 if [ ! -f /opt/${QEMU_DOWNLOAD_PACKAGE} ]; then
65     wget -P /opt -q ${QEMU_PACKAGE_URL} || \
66         { echo "Failed to download ${QEMU_VERSION}"; exit 1; }
67 fi
68
69 # Extract archive into temp directory
70 tar --strip-components 1 -xf /opt/${QEMU_DOWNLOAD_PACKAGE} -C ${WORKING_DIR} || \
71     { echo "Failed to extract ${QEMU_VERSION}.tar.xz"; exit 1; }
72
73 cd ${WORKING_DIR}
74 mkdir ${QEMU_INSTALL_DIR} || \
75     { echo "Failed to create ${QEMU_INSTALL_DIR}"; exit 1; }
76
77 # Apply additional patches
78 if [ $PATCH ]
79 then
80     chmod +x ${SCRIPT_DIR}/qemu_patches/${QEMU_VERSION}/*
81     run-parts --verbose --report  ${SCRIPT_DIR}/qemu_patches/${QEMU_VERSION}
82 fi
83
84 # Build
85 ./configure --target-list=${TARGET_LIST} --prefix=${QEMU_INSTALL_DIR} || \
86     { echo "Failed to configure ${QEMU_VERSION}"; exit 1; }
87 make -j`nproc` || \
88     { echo "Failed to compile ${QEMU_VERSION}"; exit 1; }
89 make install || \
90     { echo "Failed to install ${QEMU_VERSION}"; exit 1; }
91
92 echo QEMU ${QEMU_VERSION} ready