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