1#!/bin/bash
2###############################################################################
3#
4# This build script is for running the QEMU build in a container
5#
6# It expects to be run in with the qemu source present in the directory called
7# '$WORKSPACE/qemu', where WORKSPACE is an environment variable.
8#
9# In Jenkins configure the git SCM 'Additional Behaviours', 'check-out to a sub
10# directory' called 'qemu'.
11#
12# When building locally set WORKSPACE to be the directory above the qemu
13# checkout:
14#   git clone https://github.com/qemu/qemu
15#   WORKSPACE=$PWD/qemu ~/openbmc-build-scripts/qemu-build.sh
16#
17###############################################################################
18#
19# Script Variables:
20#  http_proxy         The HTTP address of the proxy server to connect to.
21#                     Default: "", proxy is not setup if this is not set
22#  WORKSPACE          Path of the workspace directory where the build will
23#                     occur, and output artifacts will be produced.
24#
25###############################################################################
26# Trace bash processing
27#set -x
28
29# Script Variables:
30http_proxy=${http_proxy:-}
31
32if [ -z ${WORKSPACE+x} ]; then
33    echo "Please set WORKSPACE variable"
34    exit 1
35fi
36
37# Determine the architecture
38ARCH=$(uname -m)
39
40# Docker Image Build Variables:
41img_name=qemu-build
42
43# Timestamp for job
44echo "Build started, $(date)"
45
46# Setup Proxy
47if [[ -n "${http_proxy}" ]]; then
48PROXY="RUN echo \"Acquire::http::Proxy \\"\"${http_proxy}/\\"\";\" > /etc/apt/apt.conf.d/000apt-cacher-ng-proxy"
49fi
50
51# Determine the prefix of the Dockerfile's base image
52case ${ARCH} in
53  "ppc64le")
54    DOCKER_BASE="ppc64le/"
55    ;;
56  "x86_64")
57    DOCKER_BASE=""
58    ;;
59  *)
60    echo "Unsupported system architecture(${ARCH}) found for docker image"
61    exit 1
62esac
63
64# Create the docker run script
65export PROXY_HOST=${http_proxy/#http*:\/\/}
66export PROXY_HOST=${PROXY_HOST/%:[0-9]*}
67export PROXY_PORT=${http_proxy/#http*:\/\/*:}
68
69cat > "${WORKSPACE}"/build.sh << EOF_SCRIPT
70#!/bin/bash
71
72set -x
73
74# Go into the build directory
75cd ${WORKSPACE}/qemu
76
77gcc --version
78git submodule update --init dtc
79# disable anything that requires us to pull in X
80./configure \
81    --target-list=arm-softmmu \
82    --disable-spice \
83    --disable-docs \
84    --disable-gtk \
85    --disable-smartcard \
86    --disable-usb-redir \
87    --disable-libusb \
88    --disable-sdl \
89    --disable-gnutls \
90    --disable-vte \
91    --disable-vnc \
92    --disable-werror \
93    --disable-vnc-png
94make clean
95make -j4
96
97EOF_SCRIPT
98
99chmod a+x "${WORKSPACE}"/build.sh
100
101# Configure docker build
102
103# !!!
104# Keep the base docker image in sync with the image under which we run the
105# resulting qemu binary.
106# !!!
107
108Dockerfile=$(cat << EOF
109FROM ${DOCKER_BASE}ubuntu:bionic
110
111${PROXY}
112
113ENV DEBIAN_FRONTEND noninteractive
114RUN apt-get update && apt-get install -yy --no-install-recommends \
115    bison \
116    bzip2 \
117    ca-certificates \
118    flex \
119    gcc \
120    git \
121    libc6-dev \
122    libfdt-dev \
123    libglib2.0-dev \
124    libpixman-1-dev \
125    make \
126    ninja-build \
127    python3-yaml \
128    iputils-ping
129
130RUN grep -q ${GROUPS[0]} /etc/group || groupadd -g ${GROUPS[0]} ${USER}
131RUN grep -q ${UID} /etc/passwd || useradd -d ${HOME} -m -u ${UID} -g ${GROUPS[0]} ${USER}
132USER ${USER}
133ENV HOME ${HOME}
134EOF
135)
136
137if ! docker build -t ${img_name} - <<< "${Dockerfile}" ; then
138  echo "Failed to build docker container."
139  exit 1
140fi
141
142docker run \
143    --rm=true \
144    -e WORKSPACE="${WORKSPACE}" \
145    -w "${HOME}" \
146    --user="${USER}" \
147    -v "${HOME}":"${HOME}" \
148    -t ${img_name} \
149    "${WORKSPACE}"/build.sh
150