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