1#!/bin/bash
2###############################################################################
3#
4# This build script is for running the QEMU build as a container with the
5# option of launching the container with Docker or Kubernetes.
6#
7###############################################################################
8#
9# Variables used for in the build:
10#  WORKSPACE    = Path of the workspace directory where some intermediate files
11#                 and the images will be saved to.
12#  qemudir      = Path of the directory that holds the QEMU repo, if none
13#                 exists will clone in the OpenBMC/QEMU repo to WORKSPACE.
14#  builddir     = Path of the directory that is created within the docker
15#                 container where the build is actually done. Done this way to
16#                 allow external volumes to be used for the qemudir.
17#
18# Optional Variables:
19#  launch       = job|pod
20#                 Can be left blank to launch via Docker if not using
21#                 Kubernetes to launch the container.
22#                 Job lets you keep a copy of job and container logs on the
23#                 api, can be useful if not using Jenkins as you can run the
24#                 job again via the api without needing this script.
25#                 Pod launches a container which runs to completion without
26#                 saving anything to the api when it completes.
27#  imgname      = Defaults to qemu-build with the arch as its tag, can be
28#                 changed or passed to give a specific name to created image.
29#  http_proxy   = The HTTP address for the proxy server you wish to connect to.
30#
31###############################################################################
32
33# Trace bash processing
34set -x
35
36# Default variables
37WORKSPACE=${WORKSPACE:-${HOME}/${RANDOM}${RANDOM}}
38http_proxy=${http_proxy:-}
39launch=${launch:-}
40qemudir=${qemudir:-${WORKSPACE}/qemu}
41builddir=${builddir:-/tmp/qemu}
42ARCH=$(uname -m)
43imgname=${imgname:-qemu-build:${ARCH}}
44
45# Timestamp for job
46echo "Build started, $(date)"
47
48# Setup Proxy
49if [[ -n "${http_proxy}" ]]; then
50PROXY="RUN echo \"Acquire::http::Proxy \\"\"${http_proxy}/\\"\";\" > /etc/apt/apt.conf.d/000apt-cacher-ng-proxy"
51fi
52
53# Determine the prefix of the Dockerfile's base image
54case ${ARCH} in
55  "ppc64le")
56    DOCKER_BASE="ppc64le/"
57    ;;
58  "x86_64")
59    DOCKER_BASE=""
60    ;;
61  *)
62    echo "Unsupported system architecture(${ARCH}) found for docker image"
63    exit 1
64esac
65
66# If there is no qemu directory, git clone in the openbmc mirror
67if [ ! -d ${qemudir} ]; then
68  echo "Clone in openbmc master to ${qemudir}"
69  git clone https://github.com/openbmc/qemu ${qemudir}
70fi
71
72# Create the docker run script
73export PROXY_HOST=${http_proxy/#http*:\/\/}
74export PROXY_HOST=${PROXY_HOST/%:[0-9]*}
75export PROXY_PORT=${http_proxy/#http*:\/\/*:}
76
77mkdir -p ${WORKSPACE}
78
79cat > "${WORKSPACE}"/build.sh << EOF_SCRIPT
80#!/bin/bash
81
82set -x
83
84# create a copy of the qemudir in /qemu to use as the build directory
85cp -a ${qemudir}/. ${builddir}
86
87# Go into the build directory
88cd ${builddir}
89
90gcc --version
91git submodule update --init dtc
92# disable anything that requires us to pull in X
93./configure \
94    --target-list=arm-softmmu \
95    --disable-spice \
96    --disable-docs \
97    --disable-gtk \
98    --disable-smartcard \
99    --disable-usb-redir \
100    --disable-libusb \
101    --disable-sdl \
102    --disable-gnutls \
103    --disable-vte \
104    --disable-vnc \
105    --disable-vnc-png
106make -j4
107
108cp -a ${builddir}/arm-softmmu/. ${WORKSPACE}/arm-softmmu/
109EOF_SCRIPT
110
111chmod a+x ${WORKSPACE}/build.sh
112
113# Configure docker build
114Dockerfile=$(cat << EOF
115FROM ${DOCKER_BASE}ubuntu:16.04
116
117${PROXY}
118
119ENV DEBIAN_FRONTEND noninteractive
120RUN apt-get update && apt-get install -yy --no-install-recommends \
121    bison \
122    flex \
123    gcc \
124    git \
125    libc6-dev \
126    libfdt-dev \
127    libglib2.0-dev \
128    libpixman-1-dev \
129    make \
130    python-yaml \
131    python3-yaml
132
133RUN grep -q ${GROUPS} /etc/group || groupadd -g ${GROUPS} ${USER}
134RUN grep -q ${UID} /etc/passwd || useradd -d ${HOME} -m -u ${UID} -g ${GROUPS} ${USER}
135USER ${USER}
136RUN mkdir ${builddir}
137ENV HOME ${HOME}
138EOF
139)
140
141# If Launch is left empty will create a docker container
142if [[ "${launch}" == "" ]]; then
143
144  docker build -t ${imgname} - <<< "${Dockerfile}"
145  if [[ "$?" -ne 0 ]]; then
146    echo "Failed to build docker container."
147    exit 1
148  fi
149  mountqemu="-v ""${qemudir}"":""${qemudir}"" "
150  if [[ "${qemudir}" = "${HOME}/"* || "${qemudir}" = "${HOME}" ]]; then
151    mountqemu=""
152  fi
153  docker run \
154      --rm=true \
155      -e WORKSPACE=${WORKSPACE} \
156      -w "${HOME}" \
157      -v "${HOME}":"${HOME}" \
158      ${mountqemu} \
159      -t ${imgname} \
160      ${WORKSPACE}/build.sh
161elif [[ "${launch}" == "pod" || "${launch}" == "job" ]]; then
162  . ./kubernetes/kubernetes-launch.sh QEMU-build true true
163else
164  echo "Launch Parameter is invalid"
165fi
166
167
168