1#!/bin/bash
2###############################################################################
3#
4# This build script is for running the OpenBMC builds as Docker containers.
5#
6###############################################################################
7#
8# Script Variables:
9#  build_scripts_dir  The path of the openbmc-build-scripts directory.
10#                     Default: The directory containing this script
11#  http_proxy         The HTTP address of the proxy server to connect to.
12#                     Default: "", proxy is not setup if this is not set
13#  WORKSPACE          Path of the workspace directory where some intermediate
14#                     files and the images will be saved to.
15#                     Default: "~/{RandomNumber}"
16#  num_cpu            Number of cpu's to give bitbake, default is total amount
17#                     in system
18#
19# Docker Image Build Variables:
20#  BITBAKE_OPTS       Set to "-c populate_sdk" or whatever other BitBake options
21#                     you'd like to pass into the build.
22#                     Default: "", no options set
23#  build_dir          Path where the actual BitBake build occurs inside the
24#                     container, path cannot be located on network storage.
25#                     Default: "/tmp/openbmc"
26#  distro             The distro used as the base image for the build image:
27#                     fedora|ubuntu
28#                     Default: "ubuntu"
29#  img_name           The name given to the target build's docker image.
30#                     Default: "openbmc/${distro}:${imgtag}-${target}-${ARCH}"
31#  img_tag            The base docker image distro tag:
32#                     ubuntu: latest|16.04|14.04|trusty|xenial
33#                     fedora: 23|24|25
34#                     Default: "latest"
35#  target             The target we aim to build:
36#                     evb-ast2500|palmetto|qemu|qemux86-64
37#                     romulus|s2600wf|witherspoon|zaius|tiogapass|gsj
38#                     Default: "qemu"
39#  no_tar             Set to true if you do not want the debug tar built
40#                     Default: "false"
41#  nice_priority      Set nice priotity for bitbake command.
42#                     Nice:
43#                       Run with an adjusted niceness, which affects process
44#                       scheduling. Nice values range from -20 (most favorable
45#                       to the process) to 19 (least favorable to the process).
46#                     Default: "", nice is not used if nice_priority is not set
47#
48# Deployment Variables:
49#  obmc_dir           Path of the OpenBMC repo directory used as a reference
50#                     for the build inside the container.
51#                     Default: "${WORKSPACE}/openbmc"
52#  xtrct_small_copy_dir
53#                     Directory within build_dir that should be copied to
54#                     xtrct_path. The directory and all parents up to, but not
55#                     including, build_dir will be copied. For example, if
56#                     build_dir is set to "/tmp/openbmc" and this is set to
57#                     "build/tmp", the directory at xtrct_path will have the
58#                     following directory structure:
59#                     xtrct_path
60#                      | - build
61#                        | - tmp
62#                          ...
63#                     Can also be set to the empty string to copy the entire
64#                     contents of build_dir to xtrct_path.
65#                     Default: "deploy/images".
66#
67###############################################################################
68# Trace bash processing. Set -e so when a step fails, we fail the build
69set -xeo pipefail
70
71# Script Variables:
72build_scripts_dir=${build_scripts_dir:-"$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"}
73http_proxy=${http_proxy:-}
74WORKSPACE=${WORKSPACE:-${HOME}/${RANDOM}${RANDOM}}
75num_cpu=${num_cpu:-$(nproc)}
76
77# Docker Image Build Variables:
78build_dir=${build_dir:-/tmp/openbmc}
79distro=${distro:-ubuntu}
80img_tag=${img_tag:-latest}
81target=${target:-qemu}
82no_tar=${no_tar:-false}
83nice_priority=${nice_priority:-}
84
85# Deployment variables
86obmc_dir=${obmc_dir:-${WORKSPACE}/openbmc}
87ssc_dir=${HOME}
88xtrct_small_copy_dir=${xtrct_small_copy_dir:-deploy/images}
89xtrct_path="${obmc_dir}/build/tmp"
90xtrct_copy_timeout="300"
91
92PROXY=""
93
94# Determine the architecture
95ARCH=$(uname -m)
96
97# Determine the prefix of the Dockerfile's base image
98case ${ARCH} in
99  "ppc64le")
100    DOCKER_BASE="ppc64le/"
101    ;;
102  "x86_64")
103    DOCKER_BASE=""
104    ;;
105  *)
106    echo "Unsupported system architecture(${ARCH}) found for docker image"
107    exit 1
108esac
109
110# Timestamp for job
111echo "Build started, $(date)"
112
113# If the obmc_dir directory doesn't exist clone it in
114if [ ! -d ${obmc_dir} ]; then
115  echo "Clone in openbmc master to ${obmc_dir}"
116  git clone https://github.com/openbmc/openbmc ${obmc_dir}
117fi
118
119# Make and chown the xtrct_path directory to avoid permission errors
120if [ ! -d ${xtrct_path} ]; then
121  mkdir -p ${xtrct_path}
122fi
123chown ${UID}:${GROUPS} ${xtrct_path}
124
125# Work out what build target we should be running and set BitBake command
126MACHINE=""
127case ${target} in
128  palmetto)
129    LAYER_DIR="meta-ibm/meta-palmetto"
130    ;;
131  swift)
132    LAYER_DIR="meta-ibm/meta-witherspoon"
133    MACHINE="swift"
134    ;;
135  witherspoon)
136    LAYER_DIR="meta-ibm/meta-witherspoon"
137    ;;
138  witherspoon-128)
139    LAYER_DIR="meta-ibm/meta-witherspoon"
140    MACHINE="witherspoon-128"
141    ;;
142  evb-ast2500)
143    LAYER_DIR="meta-evb/meta-evb-aspeed/meta-evb-ast2500"
144    ;;
145  s2600wf)
146    LAYER_DIR="meta-intel/meta-s2600wf"
147    ;;
148  zaius)
149    LAYER_DIR="meta-ingrasys/meta-zaius"
150    ;;
151  romulus)
152    LAYER_DIR="meta-ibm/meta-romulus"
153    ;;
154  qemu)
155    LAYER_DIR="meta-phosphor"
156    # MACHINE defaults to `qemuarm` in this layer, no change necessary
157    ;;
158  qemux86-64)
159    LAYER_DIR="meta-phosphor"
160    # MACHINE defaults to `qemuarm` in this layer, change to `qemux86-64`
161    MACHINE="qemux86-64"
162    ;;
163  tiogapass)
164    LAYER_DIR="meta-facebook/meta-tiogapass"
165    ;;
166  gsj)
167    LAYER_DIR="meta-quanta/meta-gsj"
168    ;;
169  *)
170    exit 1
171    ;;
172esac
173
174BITBAKE_CMD="TEMPLATECONF=${LAYER_DIR}/conf source oe-init-build-env"
175
176# Configure Docker build
177if [[ "${distro}" == fedora ]];then
178
179  if [[ -n "${http_proxy}" ]]; then
180    PROXY="RUN echo \"proxy=${http_proxy}\" >> /etc/dnf/dnf.conf"
181  fi
182
183  Dockerfile=$(cat << EOF
184  FROM ${DOCKER_BASE}${distro}:${img_tag}
185
186  ${PROXY}
187
188  # Set the locale
189  RUN locale-gen en_US.UTF-8
190  ENV LANG en_US.UTF-8
191  ENV LANGUAGE en_US:en
192  ENV LC_ALL en_US.UTF-8
193
194  RUN dnf --refresh install -y \
195      bzip2 \
196      chrpath \
197      cpio \
198      diffstat \
199      findutils \
200      gcc \
201      gcc-c++ \
202      git \
203      make \
204      patch \
205      perl-bignum \
206      perl-Data-Dumper \
207      perl-Thread-Queue \
208      python-devel \
209      python3-devel \
210      SDL-devel \
211      socat \
212      subversion \
213      tar \
214      texinfo \
215      wget \
216      which \
217      iputils-ping
218
219  RUN grep -q ${GROUPS} /etc/group || groupadd -g ${GROUPS} ${USER}
220  RUN grep -q ${UID} /etc/passwd || useradd -d ${HOME} -m -u ${UID} -g ${GROUPS} ${USER}
221
222  USER ${USER}
223  ENV HOME ${HOME}
224  RUN /bin/bash
225EOF
226)
227
228elif [[ "${distro}" == ubuntu ]]; then
229
230  if [[ -n "${http_proxy}" ]]; then
231    PROXY="RUN echo \"Acquire::http::Proxy \\"\"${http_proxy}/\\"\";\" > /etc/apt/apt.conf.d/000apt-cacher-ng-proxy"
232  fi
233
234  Dockerfile=$(cat << EOF
235  FROM ${DOCKER_BASE}${distro}:${img_tag}
236
237  ${PROXY}
238
239  ENV DEBIAN_FRONTEND noninteractive
240
241  RUN apt-get update && apt-get install -yy \
242      build-essential \
243      chrpath \
244      debianutils \
245      diffstat \
246      gawk \
247      git \
248      libdata-dumper-simple-perl \
249      libsdl1.2-dev \
250      libthread-queue-any-perl \
251      locales \
252      python \
253      python3 \
254      socat \
255      subversion \
256      texinfo \
257      cpio \
258      wget \
259      iputils-ping
260
261  # Set the locale
262  RUN locale-gen en_US.UTF-8
263  ENV LANG en_US.UTF-8
264  ENV LANGUAGE en_US:en
265  ENV LC_ALL en_US.UTF-8
266
267  RUN grep -q ${GROUPS} /etc/group || groupadd -g ${GROUPS} ${USER}
268  RUN grep -q ${UID} /etc/passwd || useradd -d ${HOME} -m -u ${UID} -g ${GROUPS} ${USER}
269
270  USER ${USER}
271  ENV HOME ${HOME}
272  RUN /bin/bash
273EOF
274)
275fi
276
277# Create the Docker run script
278export PROXY_HOST=${http_proxy/#http*:\/\/}
279export PROXY_HOST=${PROXY_HOST/%:[0-9]*}
280export PROXY_PORT=${http_proxy/#http*:\/\/*:}
281
282mkdir -p ${WORKSPACE}
283
284# Determine command for bitbake image build
285bitbake_image="obmc-phosphor-image"
286if [ $no_tar = "false" ]; then
287    bitbake_image="${bitbake_image} obmc-phosphor-debug-tarball"
288fi
289
290cat > "${WORKSPACE}"/build.sh << EOF_SCRIPT
291#!/bin/bash
292
293set -xeo pipefail
294
295# Go into the OpenBMC directory, the build will handle changing directories
296cd ${obmc_dir}
297
298# Set up proxies
299export ftp_proxy=${http_proxy}
300export http_proxy=${http_proxy}
301export https_proxy=${http_proxy}
302
303mkdir -p ${WORKSPACE}/bin
304
305# Configure proxies for BitBake
306if [[ -n "${http_proxy}" ]]; then
307
308  cat > ${WORKSPACE}/bin/git-proxy << \EOF_GIT
309  #!/bin/bash
310  # \$1 = hostname, \$2 = port
311  PROXY=${PROXY_HOST}
312  PROXY_PORT=${PROXY_PORT}
313  exec socat STDIO PROXY:\${PROXY}:\${1}:\${2},proxyport=\${PROXY_PORT}
314EOF_GIT
315
316  chmod a+x ${WORKSPACE}/bin/git-proxy
317  export PATH=${WORKSPACE}/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}
318  git config core.gitProxy git-proxy
319
320  mkdir -p ~/.subversion
321
322  cat > ~/.subversion/servers << EOF_SVN
323  [global]
324  http-proxy-host = ${PROXY_HOST}
325  http-proxy-port = ${PROXY_PORT}
326EOF_SVN
327fi
328
329# Source our build env
330${BITBAKE_CMD}
331
332# Export MACHINE name when given for build target
333if [[ -n "${MACHINE}" ]]; then
334  export MACHINE="${MACHINE}"; export BB_ENV_EXTRAWHITE="$BB_ENV_EXTRAWHITE MACHINE"
335fi
336
337# Custom BitBake config settings
338cat >> conf/local.conf << EOF_CONF
339BB_NUMBER_THREADS = "$(nproc)"
340PARALLEL_MAKE = "-j$(nproc)"
341INHERIT += "rm_work"
342BB_GENERATE_MIRROR_TARBALLS = "1"
343DL_DIR="${ssc_dir}/bitbake_downloads"
344SSTATE_DIR="${ssc_dir}/bitbake_sharedstatecache"
345USER_CLASSES += "buildstats"
346INHERIT_remove = "uninative"
347TMPDIR="${build_dir}"
348EOF_CONF
349
350# Kick off a build
351if [[ -n "${nice_priority}" ]]; then
352    nice -${nice_priority} bitbake ${BITBAKE_OPTS} ${bitbake_image}
353else
354    bitbake ${BITBAKE_OPTS} ${bitbake_image}
355fi
356
357# Copy internal build directory into xtrct_path directory
358if [[ ${xtrct_small_copy_dir} ]]; then
359  mkdir -p ${xtrct_path}/${xtrct_small_copy_dir}
360  timeout ${xtrct_copy_timeout} cp -r ${build_dir}/${xtrct_small_copy_dir}/* ${xtrct_path}/${xtrct_small_copy_dir}
361else
362  timeout ${xtrct_copy_timeout} cp -r ${build_dir}/* ${xtrct_path}
363fi
364
365if [[ 0 -ne $? ]]; then
366  echo "Received a non-zero exit code from timeout"
367  exit 1
368fi
369
370EOF_SCRIPT
371
372chmod a+x ${WORKSPACE}/build.sh
373
374# Give the Docker image a name based on the distro,tag,arch,and target
375img_name=${img_name:-openbmc/${distro}:${img_tag}-${target}-${ARCH}}
376
377# Build the Docker image
378docker build -t ${img_name} - <<< "${Dockerfile}"
379
380# If obmc_dir or ssc_dir are ${HOME} or a subdirectory they will not be mounted
381mount_obmc_dir="-v ""${obmc_dir}"":""${obmc_dir}"" "
382mount_ssc_dir="-v ""${ssc_dir}"":""${ssc_dir}"" "
383if [[ "${obmc_dir}" = "${HOME}/"* || "${obmc_dir}" = "${HOME}" ]];then
384mount_obmc_dir=""
385fi
386if [[ "${ssc_dir}" = "${HOME}/"* || "${ssc_dir}" = "${HOME}" ]];then
387mount_ssc_dir=""
388fi
389
390# Run the Docker container, execute the build.sh script
391docker run \
392--cap-add=sys_admin \
393--cap-add=sys_nice \
394--net=host \
395--rm=true \
396-e WORKSPACE=${WORKSPACE} \
397-w "${HOME}" \
398-v "${HOME}":"${HOME}" \
399${mount_obmc_dir} \
400${mount_ssc_dir} \
401--cpus="$num_cpu" \
402-t ${img_name} \
403${WORKSPACE}/build.sh
404
405# To maintain function of resources that used an older path, add a link
406ln -sf ${xtrct_path}/deploy ${WORKSPACE}/deploy
407
408# Timestamp for build
409echo "Build completed, $(date)"
410