1#!/bin/bash -ex
2
3test -f /etc/os-release && cat "$_"
4
5# Construct $cc such that it matches what `make` will chose when taking
6# CROSS_COMPILE into account.  Do not modify $CC directly, as that will cause
7# `make` to double-account CROSS_COMPILE.
8cc="${CROSS_COMPILE}${CC}"
9
10$cc --version
11
12# random config or default config
13if [[ "${RANDCONFIG}" == "y" ]]; then
14
15    cp -f xen/tools/kconfig/allrandom.config xen/allrandom.config.tmp
16
17    # Append job-specific fixed configuration
18    echo "${EXTRA_FIXED_RANDCONFIG}" >> xen/allrandom.config.tmp
19
20    make -j$(nproc) -C xen KCONFIG_ALLCONFIG=allrandom.config.tmp randconfig
21
22    # RANDCONFIG implies HYPERVISOR_ONLY
23    HYPERVISOR_ONLY="y"
24else
25    # Start off with arch's defconfig
26    make -C xen defconfig
27
28    echo "CONFIG_DEBUG=${debug}" >> xen/.config
29
30    if [[ -n "${EXTRA_XEN_CONFIG}" ]]; then
31        echo "${EXTRA_XEN_CONFIG}" >> xen/.config
32    fi
33
34    make -j$(nproc) -C xen olddefconfig
35fi
36
37# Save the config file before building because build failure causes the script
38# to exit early -- bash is invoked with -e.
39cp xen/.config xen-config
40
41# Directory for the artefacts to be dumped into
42mkdir -p binaries
43
44collect_xen_artefacts()
45{
46    local f
47
48    for f in xen/xen xen/xen.efi; do
49        if [[ -f $f ]]; then
50            cp $f binaries/
51        fi
52    done
53}
54
55if [[ "${CPPCHECK}" == "y" ]] && [[ "${HYPERVISOR_ONLY}" == "y" ]]; then
56    # Cppcheck analysis invokes Xen-only build
57    xen/scripts/xen-analysis.py --run-cppcheck --cppcheck-misra -- -j$(nproc)
58
59    # Preserve artefacts
60    collect_xen_artefacts
61    cp xen/cppcheck-report/xen-cppcheck.txt xen-cppcheck.txt
62elif [[ "${HYPERVISOR_ONLY}" == "y" ]]; then
63    # Xen-only build
64    make -j$(nproc) xen
65
66    # Preserve artefacts
67    collect_xen_artefacts
68else
69    # Full build.  Figure out our ./configure options
70    cfgargs=("--prefix=/usr")
71    cfgargs+=("--enable-docs")
72
73    # booleans for which compiler is in use
74    cc_is_gcc="$($cc --version | grep -q gcc && echo "y" || :)"
75    cc_is_clang="$($cc --version | grep -q clang && echo "y" || :)"
76
77    # The compiler version as an integer.  e.g. GCC 4.9.2 => 0x040902
78    cc_ver="$($cc -dumpversion | awk -F. '{ printf "0x%02x%02x%02x", $1, $2, $3 }')"
79
80    if [[ "${cc_is_clang}" == "y" ]]; then
81        # SeaBIOS cannot be built with clang
82        cfgargs+=("--with-system-seabios=/usr/share/no-seabios.bin")
83        # iPXE cannot be built with clang
84        cfgargs+=("--with-system-ipxe=/usr/share/no-ipxe.pxe")
85        # newlib cannot be built with clang so we cannot build stubdoms
86        cfgargs+=("--disable-stubdom")
87    fi
88
89    if ldd /bin/ls | grep -q musl; then
90        # disable --disable-werror for QEMUU when building with MUSL
91        cfgargs+=("--with-extra-qemuu-configure-args=\"--disable-werror\"")
92    fi
93
94    # QEMU is only for those who ask
95    if [[ "$BUILD_QEMU_XEN" != "y" ]]; then
96        cfgargs+=("--with-system-qemu=/bin/false")
97    fi
98
99    # SeaBIOS requires GCC 4.6 or later
100    if [[ "${cc_is_gcc}" == "y" && "${cc_ver}" -lt 0x040600 ]]; then
101        cfgargs+=("--with-system-seabios=/usr/share/no-seabios.bin")
102    fi
103
104    ./configure "${cfgargs[@]}"
105    make -j$(nproc) dist
106
107    # Preserve artefacts
108    # Note: Some smoke tests depending on finding binaries/xen on a full build
109    # even though dist/ contains everything, while some containers don't even
110    # build Xen
111    (cd dist/install; find | cpio -o -H newc | gzip) > binaries/xen-tools.cpio.gz
112    collect_xen_artefacts
113fi
114