build_inf_centos.sh: add image alias name
[pti/rtp.git] / scripts / build_inf_yocto / build_inf_yocto.sh
1 #!/bin/bash
2 #
3 # Copyright (C) 2019 Wind River Systems, Inc.
4 #
5 #  Licensed under the Apache License, Version 2.0 (the "License");
6 #  you may not use this file except in compliance with the License.
7 #  You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #  Unless required by applicable law or agreed to in writing, software
12 #  distributed under the License is distributed on an "AS IS" BASIS,
13 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #  See the License for the specific language governing permissions and
15 #  limitations under the License.
16
17 # Ensure we fail the job if any steps fail.
18 set -e -o pipefail
19
20 #########################################################################
21 # Variables
22 #########################################################################
23
24 # Only one bsp is supported now, there will be more in the future
25 SUPPORTED_BSP="intel-corei7-64 "
26
27 SRC_ORAN_BRANCH="master"
28 SRC_STX_BRANCH="master"
29 SRC_YP_BRANCH="warrior"
30
31 SRC_ORAN_URL="https://gerrit.o-ran-sc.org/r/pti/rtp"
32
33 SRC_STX_URL="https://opendev.org/starlingx/meta-starlingx"
34
35 SRC_YP_URL="\
36     git://git.yoctoproject.org/poky;commit=HEAD \
37     git://git.openembedded.org/meta-openembedded;commit=HEAD \
38     git://git.openembedded.org/meta-python2;commit=HEAD \
39     git://git.yoctoproject.org/meta-virtualization;commit=HEAD \
40     git://git.yoctoproject.org/meta-cloud-services;commit=HEAD \
41     git://git.yoctoproject.org/meta-security;commit=HEAD \
42     git://git.yoctoproject.org/meta-intel;commit=HEAD \
43     git://git.yoctoproject.org/meta-selinux;commit=HEAD \
44     git://git.yoctoproject.org/meta-dpdk;commit=HEAD \
45     git://git.yoctoproject.org/meta-anaconda;commit=HEAD \
46     https://github.com/intel-iot-devkit/meta-iot-cloud.git;commit=HEAD \
47 "
48
49 SUB_LAYER_META_OE="\
50     meta-oe \
51     meta-perl \
52     meta-python \
53     meta-networking \
54     meta-filesystems \
55     meta-webserver \
56     meta-initramfs \
57     meta-initramfs \
58     meta-gnome \
59 "
60
61 SUB_LAYER_META_CLOUD_SERVICES="meta-openstack"
62 SUB_LAYER_STX="\
63     meta-stx-cloud \
64     meta-stx-distro \
65     meta-stx-flock \
66     meta-stx-integ \
67     meta-stx-virt \
68 "
69
70 # For anaconda build
71 SUB_LAYER_META_OE_ANACONDA="\
72     meta-oe \
73     meta-python \
74     meta-filesystems \
75     meta-initramfs \
76     meta-networking \
77     meta-gnome \
78 "
79
80 SCRIPTS_DIR=$(dirname $(dirname $(readlink -f $0)))
81 TIMESTAMP=`date +"%Y%m%d_%H%M%S"`
82
83 #########################################################################
84 # Common Functions
85 #########################################################################
86
87 help_info () {
88 cat << ENDHELP
89 Usage:
90 $(basename $0) [-w WORKSPACE_DIR] [-b BSP] [-n] [-u] [-h] [-r Yes|No] [-e EXTRA_CONF]
91 where:
92     -w WORKSPACE_DIR is the path for the project
93     -b BPS is one of supported BSP: "${SUPPORTED_BSP}"
94        (default is intel-corei7-64 if not specified.)
95     -n dry-run only for bitbake
96     -u update the repo if it exists
97     -h this help info
98     -e EXTRA_CONF is the pat for extra config file
99     -r whether to inherit rm_work (default is Yes)
100 examples:
101 $0
102 $0 -w workspace_1234 -r no -e /path/to/extra_local.conf
103 ENDHELP
104 }
105
106 echo_step_start() {
107     [ -n "$1" ] && msg_step=$1
108     echo "#########################################################################################"
109     echo "## STEP START: ${msg_step}"
110     echo "#########################################################################################"
111 }
112
113 echo_step_end() {
114     [ -n "$1" ] && msg_step=$1
115     echo "#########################################################################################"
116     echo "## STEP END: ${msg_step}"
117     echo "#########################################################################################"
118     echo
119 }
120
121 echo_info () {
122     echo "INFO: $1"
123 }
124
125 echo_error () {
126     echo "ERROR: $1"
127 }
128
129 echo_cmd () {
130     echo
131     echo_info "$1"
132     echo "CMD: ${RUN_CMD}"
133 }
134
135 check_yn_rm_work () {
136     yn="$1"
137     case ${yn} in
138         [Yy]|[Yy]es)
139             RM_WORK="Yes"
140             ;;
141         [Nn]|[Nn]o)
142             RM_WORK="No"
143             ;;
144         *)
145             echo "Invalid arg for -r option."
146             help_info
147             exit 1
148             ;;
149     esac
150 }
151
152 check_valid_bsp () {
153     bsp="$1"
154     for b in ${SUPPORTED_BSP}; do
155         if [ "${bsp}" == "${b}" ]; then
156             BSP_VALID="${bsp}"
157             break
158         fi
159     done
160     if [ -z "${BSP_VALID}" ]; then
161         echo_error "${bsp} is not a supported BSP, the supported BSPs are: ${SUPPORTED_BSP}"
162         exit 1
163     fi
164 }
165
166
167 clone_update_repo () {
168     REPO_BRANCH=$1
169     REPO_URL=$2
170     REPO_NAME=$3
171     REPO_COMMIT=$4
172
173     if [ -d ${REPO_NAME}/.git ]; then
174         if [ "${SKIP_UPDATE}" == "Yes" ]; then
175             echo_info "The repo ${REPO_NAME} exists, skip updating for the branch ${REPO_BRANCH}"
176         else
177             echo_info "The repo ${REPO_NAME} exists, updating for the branch ${REPO_BRANCH}"
178             cd ${REPO_NAME}
179             git checkout ${REPO_BRANCH}
180             git pull
181             cd -
182         fi
183     else
184         RUN_CMD="git clone --branch ${REPO_BRANCH} ${REPO_URL} ${REPO_NAME}"
185         echo_cmd "Cloning the source of repo '${REPO_NAME}':"
186         ${RUN_CMD}
187
188         if [ -n "${REPO_COMMIT}" ]; then
189             cd ${REPO_NAME}
190             RUN_CMD="git checkout -b ${REPO_BRANCH}-${REPO_COMMIT} ${REPO_COMMIT}"
191             echo_cmd "Checkout the repo ${REPO_NAME} to specific commit: ${REPO_COMMIT}"
192             ${RUN_CMD}
193             cd -
194         fi
195     fi
196 }
197
198 source_env () {
199     build_dir=$1
200     cd ${SRC_LAYER_DIR}/poky
201     set ${build_dir}
202     source ./oe-init-build-env ${build_dir}
203 }
204
205 #########################################################################
206 # Parse cmd options
207 #########################################################################
208
209 DRYRUN=""
210 EXTRA_CONF=""
211 SKIP_UPDATE="Yes"
212 RM_WORK="Yes"
213 BSP="intel-corei7-64"
214
215 while getopts "w:b:e:r:unh" OPTION; do
216     case ${OPTION} in
217         w)
218             WORKSPACE=`readlink -f ${OPTARG}`
219             ;;
220         b)
221             check_valid_bsp ${OPTARG}
222             ;;
223         e)
224             EXTRA_CONF=`readlink -f ${OPTARG}`
225             ;;
226         n)
227             DRYRUN="-n"
228             ;;
229         u)
230             SKIP_UPDATE="No"
231             ;;
232         r)
233             check_yn_rm_work ${OPTARG}
234             ;;
235         h)
236             help_info
237             exit
238             ;;
239     esac
240 done
241
242 if [ -z ${WORKSPACE} ]; then
243     echo_info "No workspace specified, a directory 'workspace' will be created in current directory as the workspace"
244     WORKSPACE=`readlink -f workspace`
245 fi
246
247 if [ -n "${BSP_VALID}" ]; then
248     BSP="${BSP_VALID}"
249 fi
250
251 IMG_ARCH=${BSP}
252 if [ "${BSP}" == "intel-corei7-64"  ]; then
253     IMG_ARCH="x86-64"
254 fi
255
256 #########################################################################
257 # Functions for each step
258 #########################################################################
259 SRC_LAYER_DIR=${WORKSPACE}/src_layers
260 SRC_ORAN_DIR=${SRC_LAYER_DIR}/oran
261 SRC_STX_DIR=${SRC_LAYER_DIR}/meta-starlingx
262 PRJ_BUILD_DIR=${WORKSPACE}/prj_oran_stx
263 PRJ_BUILD_DIR_ANACONDA=${WORKSPACE}/prj_oran_inf_anaconda
264 PRJ_SHARED_DIR=${WORKSPACE}/prj_shared
265 PRJ_OUTPUT_DIR=${WORKSPACE}/prj_output
266 PRJ_SHARED_DL_DIR=${WORKSPACE}/prj_shared/downloads
267 PRJ_SHARED_SS_DIR=${WORKSPACE}/prj_shared/sstate-cache
268 SRC_SCRIPTS=${SRC_ORAN_DIR}/rtp/scripts/build_inf_yocto
269 SRC_META_PATCHES=${SRC_SCRIPTS}/meta-patches/src_stx
270 SRC_CONFIGS=${SRC_SCRIPTS}/configs
271 IMG_STX=stx-image-aio
272 IMG_ANACONDA=stx-image-aio-installer
273 IMG_INF=inf-image-aio-installer
274 ISO_STX=${PRJ_BUILD_DIR}/tmp/deploy/images/${BSP}/${IMG_STX}-${BSP}.iso
275 ISO_ANACONDA=${PRJ_BUILD_DIR_ANACONDA}/tmp-glibc/deploy/images/${BSP}/${IMG_ANACONDA}-${BSP}.iso
276 ISO_INF=${PRJ_BUILD_DIR_ANACONDA}/tmp-glibc/deploy/images/${BSP}/${IMG_INF}-${BSP}.iso
277 ISO_INF_ALIAS=${PRJ_OUTPUT_DIR}/inf-image-yocto-aio-${IMG_ARCH}.iso
278
279 prepare_workspace () {
280     msg_step="Create workspace for the build"
281     echo_step_start
282
283     mkdir -p ${PRJ_BUILD_DIR} ${SRC_ORAN_DIR} ${PRJ_BUILD_DIR_ANACONDA} \
284              ${PRJ_SHARED_DL_DIR} ${PRJ_SHARED_SS_DIR} ${PRJ_OUTPUT_DIR}
285
286     echo_info "The following directories are created in your workspace(${WORKSPACE}):"
287     echo_info "For all layers source: ${SRC_LAYER_DIR}"
288     echo_info "For StarlingX build project: ${PRJ_BUILD_DIR}"
289     echo_info "For anaconda (installer) build project: ${PRJ_BUILD_DIR_ANACONDA}"
290
291     echo_step_end
292 }
293
294 prepare_src () {
295     msg_step="Get the source code repos"
296     echo_step_start
297
298     # Clone the oran layer if it's not already cloned
299     # Check if the script is inside the repo
300     if cd ${SCRIPTS_DIR} && git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
301         CLONED_ORAN_REPO=`dirname ${SCRIPTS_DIR}`
302         echo_info "Use the cloned oran repo: ${CLONED_ORAN_REPO}"
303         mkdir -p ${SRC_ORAN_DIR}/rtp
304         cd ${SRC_ORAN_DIR}/rtp
305         rm -rf meta-stx-oran scripts
306         ln -sf ${CLONED_ORAN_REPO}/scripts scripts
307         ln -sf ${CLONED_ORAN_REPO}/meta-stx-oran meta-stx-oran
308     else
309         echo_info "Cloning oran layer:"
310         cd ${SRC_ORAN_DIR}
311         clone_update_repo ${SRC_ORAN_BRANCH} ${SRC_ORAN_URL} rtp
312     fi
313
314     echo_info "Cloning or update Yocto layers:"
315
316     cd ${SRC_LAYER_DIR}
317     for layer_url in ${SRC_YP_URL}; do
318         layer_name=$(basename ${layer_url%%;commit*})
319         layer_commit=$(basename ${layer_url##*;commit=})
320         clone_update_repo ${SRC_YP_BRANCH} ${layer_url%%;commit*} ${layer_name} ${layer_commit}
321     done
322
323     echo_info "Cloning or update meta-starlingx layers:"
324     clone_update_repo ${SRC_STX_BRANCH} ${SRC_STX_URL} $(basename ${SRC_STX_URL})
325
326     echo_step_end
327
328 # Not andy meta-patch is needed for the time being, but new ones may be needed and added
329 # sometime in the future, so just leave these code commented out here.
330 #    # Apply meta patches
331 #    for l in $(ls -1 ${SRC_META_PATCHES}); do
332 #        msg_step="Apply meta patches for ${l}"
333 #        echo_step_start
334 #        cd ${SRC_LAYER_DIR}/${l}
335 #
336 #        # backup current branch
337 #        local_branch=$(git rev-parse --abbrev-ref HEAD)
338 #        git branch -m "${local_branch}_${TIMESTAMP}"
339 #        git checkout -b ${local_branch} ${local_branch##*-}
340 #
341 #        for p in $(ls -1 ${SRC_META_PATCHES}/${l}); do
342 #            echo_info "Apllying patch: ${SRC_META_PATCHES}/${l}/${p}"
343 #            git am ${SRC_META_PATCHES}/${l}/${p}
344 #        done
345 #        echo_step_end
346 #    done
347 }
348
349 add_layer_stx_build () {
350     msg_step="Add required layers to the StarlingX build project"
351     echo_step_start
352
353     source_env ${PRJ_BUILD_DIR}
354     SRC_LAYERS=""
355     for layer_url in ${SRC_YP_URL} ${SRC_STX_URL}; do
356         layer_name=$(basename ${layer_url%%;commit*})
357         case ${layer_name} in
358         poky)
359             continue
360             ;;
361         meta-openembedded)
362             for sub_layer in ${SUB_LAYER_META_OE}; do
363                 SRC_LAYERS="${SRC_LAYERS} ${SRC_LAYER_DIR}/${layer_name}/${sub_layer}"
364             done
365             ;;
366         meta-cloud-services)
367             SRC_LAYERS="${SRC_LAYERS} ${SRC_LAYER_DIR}/${layer_name}"
368             for sub_layer in ${SUB_LAYER_META_CLOUD_SERVICES}; do
369                 SRC_LAYERS="${SRC_LAYERS} ${SRC_LAYER_DIR}/${layer_name}/${sub_layer}"
370             done
371             ;;
372         meta-starlingx)
373             for sub_layer in ${SUB_LAYER_STX}; do
374                 SRC_LAYERS="${SRC_LAYERS} ${SRC_STX_DIR}/${sub_layer}"
375             done
376             ;;
377         *)
378             SRC_LAYERS="${SRC_LAYERS} ${SRC_LAYER_DIR}/${layer_name}"
379             ;;
380
381         esac
382     done
383     SRC_LAYERS="${SRC_LAYERS} ${SRC_ORAN_DIR}/rtp/meta-stx-oran"
384
385
386     for src_layer in ${SRC_LAYERS}; do
387         RUN_CMD="bitbake-layers add-layer ${src_layer}"
388         echo_cmd "Add the ${src_layer} layer into the build project"
389         ${RUN_CMD}
390     done
391
392     echo_step_end
393 }
394
395 add_configs_stx_build () {
396     msg_step="Add extra configs into local.conf for StarlingX build"
397     echo_step_start
398
399     cd ${PRJ_BUILD_DIR}
400     echo_info "Adding the following extra configs into local.conf"
401     cat ${SRC_CONFIGS}/local-stx.conf | \
402         sed -e "s/@BSP@/${BSP}/" | tee -a conf/local.conf
403     cat ${SRC_CONFIGS}/local-mirrors.conf | tee -a conf/local.conf
404
405     echo "DL_DIR = '${PRJ_SHARED_DL_DIR}'" | tee -a conf/local.conf
406     echo "SSTATE_DIR = '${PRJ_SHARED_SS_DIR}'" | tee -a conf/local.conf
407
408     if [ "${RM_WORK}" == "Yes" ]; then
409         echo "INHERIT += 'rm_work'" | tee -a conf/local.conf
410         echo "RM_WORK_EXCLUDE += '${IMG_STX}'" | tee -a conf/local.conf
411     fi
412
413
414     if [ "${EXTRA_CONF}" != "" ] && [ -f "${EXTRA_CONF}" ]; then
415         cat ${EXTRA_CONF} | tee -a conf/local.conf
416     fi
417     echo_step_end
418 }
419
420 setup_stx_build () {
421     echo_step_start "Setup StarlingX build project"
422
423     add_layer_stx_build
424
425     cd ${PRJ_BUILD_DIR}
426     if ! grep -q 'Configs for StarlingX' conf/local.conf; then
427         add_configs_stx_build
428     else
429         echo_info "Nothing is added into local.conf"
430     fi
431
432     echo_step_end "Setup StarlingX build project"
433 }
434
435 build_stx_image () {
436     msg_step="Build StarlingX images"
437     echo_step_start
438
439     source_env ${PRJ_BUILD_DIR}
440
441     mkdir -p logs
442
443     RUN_CMD="bitbake ${DRYRUN} ${IMG_STX}"
444     echo_cmd "Build the ${IMG_STX} image"
445     bitbake ${DRYRUN} ${IMG_STX} 2>&1|tee logs/bitbake_${IMG_STX}_${TIMESTAMP}.log
446
447     echo_step_end
448
449     echo_info "Build succeeded, you can get the image in ${ISO_STX}"
450 }
451
452 add_layer_anaconda_build () {
453     msg_step="Add required layers to the anaconda (installer) build project"
454     echo_step_start
455
456     source_env ${PRJ_BUILD_DIR_ANACONDA}
457     SRC_LAYERS=""
458     for sub_layer in ${SUB_LAYER_META_OE_ANACONDA}; do
459         SRC_LAYERS="${SRC_LAYERS} ${SRC_LAYER_DIR}/meta-openembedded/${sub_layer}"
460     done
461     SRC_LAYERS="${SRC_LAYERS} ${SRC_LAYER_DIR}/meta-intel"
462     SRC_LAYERS="${SRC_LAYERS} ${SRC_LAYER_DIR}/meta-anaconda"
463     SRC_LAYERS="${SRC_LAYERS} ${SRC_STX_DIR}/meta-stx-distro"
464     SRC_LAYERS="${SRC_LAYERS} ${SRC_STX_DIR}/meta-stx-integ"
465     SRC_LAYERS="${SRC_LAYERS} ${SRC_ORAN_DIR}/rtp/meta-stx-oran"
466
467     for src_layer in ${SRC_LAYERS}; do
468         RUN_CMD="bitbake-layers add-layer ${src_layer}"
469         echo_cmd "Add the ${src_layer} layer into the build project"
470         ${RUN_CMD}
471     done
472
473     echo_step_end
474 }
475
476 add_configs_anaconda_build () {
477     msg_step="Add extra configs into local.conf for anaconda (installer) build"
478     echo_step_start
479
480     cd ${PRJ_BUILD_DIR_ANACONDA}
481     echo_info "Adding the following extra configs into local.conf"
482     cat ${SRC_CONFIGS}/local-anaconda.conf | \
483         sed -e "s/@BSP@/${BSP}/" \
484             -e "s|@TARGET_BUILD@|${PRJ_BUILD_DIR}|" \
485             | tee -a conf/local.conf
486     cat ${SRC_CONFIGS}/local-mirrors.conf | tee -a conf/local.conf
487
488     echo "DL_DIR = '${PRJ_SHARED_DL_DIR}'" | tee -a conf/local.conf
489     echo "SSTATE_DIR = '${PRJ_SHARED_SS_DIR}'" | tee -a conf/local.conf
490
491     if [ "${RM_WORK}" == "Yes" ]; then
492         echo "INHERIT += 'rm_work'" | tee -a conf/local.conf
493         echo "RM_WORK_EXCLUDE += '${IMG_ANACONDA}'" | tee -a conf/local.conf
494     fi
495
496     if [ "${EXTRA_CONF}" != "" ] && [ -f "${EXTRA_CONF}" ]; then
497         cat ${EXTRA_CONF} | tee -a conf/local.conf
498     fi
499
500     echo_step_end
501 }
502
503 setup_anaconda_build () {
504     echo_step_start "Setup anaconda (installer) build project"
505
506     add_layer_anaconda_build
507
508     cd ${PRJ_BUILD_DIR_ANACONDA}
509     if ! grep -q 'Configs for anaconda' conf/local.conf; then
510         add_configs_anaconda_build
511     else
512         echo_info "Nothing is added into local.conf"
513     fi
514
515     echo_step_end "Setup anaconda build project"
516 }
517
518 build_anaconda_image () {
519     echo_step_start "Build anaconda (installer) images"
520     source_env ${PRJ_BUILD_DIR_ANACONDA}
521
522     mkdir -p logs
523
524     if [ -f ${ISO_ANACONDA} ]; then
525         bitbake ${DRYRUN} -c clean ${IMG_ANACONDA}
526     fi
527     RUN_CMD="bitbake ${DRYRUN} ${IMG_ANACONDA}"
528     echo_cmd "Build the ${IMG_ANACONDA} image"
529     bitbake ${DRYRUN} ${IMG_ANACONDA} 2>&1|tee logs/bitbake_${IMG_ANACONDA}_${TIMESTAMP}.log
530
531     if [ -z "${DRYRUN}" ]; then
532         cp -Pf ${ISO_ANACONDA} ${ISO_INF}
533         cp -Pf ${ISO_ANACONDA} ${ISO_INF_ALIAS}
534     fi
535
536     echo_step_end
537
538     echo_info "Build succeeded, you can get the image in ${ISO_INF_ALIAS}"
539 }
540
541 #########################################################################
542 # Main process
543 #########################################################################
544
545 prepare_workspace
546 prepare_src
547 setup_stx_build
548 setup_anaconda_build
549 build_stx_image
550 build_anaconda_image