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