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
48    PROXY="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    "aarch64")
60        DOCKER_BASE="arm64v8/"
61        ;;
62    *)
63        echo "Unsupported system architecture(${ARCH}) found for docker image"
64        exit 1
65esac
66
67# Create the docker run script
68export PROXY_HOST=${http_proxy/#http*:\/\/}
69export PROXY_HOST=${PROXY_HOST/%:[0-9]*}
70export PROXY_PORT=${http_proxy/#http*:\/\/*:}
71
72cat > "${WORKSPACE}"/build.sh << EOF_SCRIPT
73#!/bin/bash
74
75set -x
76
77# Go into the build directory
78cd ${WORKSPACE}/qemu
79
80gcc --version
81git submodule update --init dtc
82# disable anything that requires us to pull in X
83./configure \
84    --target-list=arm-softmmu \
85    --disable-spice \
86    --disable-docs \
87    --disable-gtk \
88    --disable-smartcard \
89    --disable-usb-redir \
90    --disable-libusb \
91    --disable-sdl \
92    --disable-gnutls \
93    --disable-vte \
94    --disable-vnc \
95    --disable-werror
96make clean
97make -j4
98
99EOF_SCRIPT
100
101chmod a+x "${WORKSPACE}"/build.sh
102
103# Configure docker build
104
105# !!!
106# Keep the base docker image in sync with the image under which we run the
107# resulting qemu binary.
108# !!!
109
110Dockerfile=$(cat << EOF
111FROM ${DOCKER_BASE}ubuntu:jammy
112
113${PROXY}
114
115ENV DEBIAN_FRONTEND noninteractive
116RUN apt-get update && apt-get install -yy --no-install-recommends \
117    bison \
118    bzip2 \
119    ca-certificates \
120    flex \
121    gcc \
122    git \
123    libc6-dev \
124    libfdt-dev \
125    libglib2.0-dev \
126    libpixman-1-dev \
127    libslirp-dev \
128    make \
129    ninja-build \
130    python3-venv \
131    python3-yaml \
132    iputils-ping
133
134RUN grep -q ${GROUPS[0]} /etc/group || groupadd -g ${GROUPS[0]} ${USER}
135RUN grep -q ${UID} /etc/passwd || useradd -d ${HOME} -m -u ${UID} -g ${GROUPS[0]} ${USER}
136USER ${USER}
137ENV HOME ${HOME}
138EOF
139)
140
141if ! docker build -t ${img_name} - <<< "${Dockerfile}" ; then
142    echo "Failed to build docker container."
143    exit 1
144fi
145
146docker run \
147    --rm=true \
148    -e WORKSPACE="${WORKSPACE}" \
149    -w "${HOME}" \
150    --user="${USER}" \
151    -v "${HOME}":"${HOME}" \
152    -t ${img_name} \
153    "${WORKSPACE}"/build.sh
154