1#!/bin/bash
2
3set -e
4set -o xtrace
5
6check_minio()
7{
8    MINIO_PATH="minio-packet.freedesktop.org/mesa-lava/$1/${DISTRIBUTION_TAG}/${DEBIAN_ARCH}"
9    if wget -q --method=HEAD "https://${MINIO_PATH}/done"; then
10        exit
11    fi
12}
13
14# If remote files are up-to-date, skip rebuilding them
15check_minio "mesa/mesa"
16check_minio "${CI_PROJECT_PATH}"
17
18. .gitlab-ci/container/container_pre_build.sh
19
20# Install rust, which we'll be using for deqp-runner.  It will be cleaned up at the end.
21. .gitlab-ci/build-rust.sh
22
23if [[ "$DEBIAN_ARCH" = "arm64" ]]; then
24    GCC_ARCH="aarch64-linux-gnu"
25    KERNEL_ARCH="arm64"
26    DEFCONFIG="arch/arm64/configs/defconfig"
27    DEVICE_TREES="arch/arm64/boot/dts/rockchip/rk3399-gru-kevin.dtb arch/arm64/boot/dts/amlogic/meson-gxl-s905x-libretech-cc.dtb arch/arm64/boot/dts/allwinner/sun50i-h6-pine-h64.dtb arch/arm64/boot/dts/amlogic/meson-gxm-khadas-vim2.dtb arch/arm64/boot/dts/qcom/apq8016-sbc.dtb arch/arm64/boot/dts/amlogic/meson-g12b-a311d-khadas-vim3.dtb"
28    KERNEL_IMAGE_NAME="Image"
29elif [[ "$DEBIAN_ARCH" = "armhf" ]]; then
30    GCC_ARCH="arm-linux-gnueabihf"
31    KERNEL_ARCH="arm"
32    DEFCONFIG="arch/arm/configs/multi_v7_defconfig"
33    DEVICE_TREES="arch/arm/boot/dts/rk3288-veyron-jaq.dtb arch/arm/boot/dts/sun8i-h3-libretech-all-h3-cc.dtb"
34    KERNEL_IMAGE_NAME="zImage"
35    . .gitlab-ci/create-cross-file.sh armhf
36else
37    GCC_ARCH="x86_64-linux-gnu"
38    KERNEL_ARCH="x86_64"
39    DEFCONFIG="arch/x86/configs/x86_64_defconfig"
40    DEVICE_TREES=""
41    KERNEL_IMAGE_NAME="bzImage"
42fi
43
44# Determine if we're in a cross build.
45if [[ -e /cross_file-$DEBIAN_ARCH.txt ]]; then
46    EXTRA_MESON_ARGS="--cross-file /cross_file-$DEBIAN_ARCH.txt"
47    EXTRA_CMAKE_ARGS="-DCMAKE_TOOLCHAIN_FILE=/toolchain-$DEBIAN_ARCH.cmake"
48
49    if [ $DEBIAN_ARCH = arm64 ]; then
50        RUST_TARGET="aarch64-unknown-linux-gnu"
51    elif [ $DEBIAN_ARCH = armhf ]; then
52        RUST_TARGET="armv7-unknown-linux-gnueabihf"
53    fi
54    rustup target add $RUST_TARGET
55    export EXTRA_CARGO_ARGS="--target $RUST_TARGET"
56
57    export ARCH=${KERNEL_ARCH}
58    export CROSS_COMPILE="${GCC_ARCH}-"
59fi
60
61apt-get update
62apt-get install -y automake \
63                   git \
64                   bc \
65                   cmake \
66                   wget \
67                   debootstrap \
68                   libboost-dev \
69                   libegl1-mesa-dev \
70                   libgbm-dev \
71                   libgles2-mesa-dev \
72                   libpcre3-dev \
73                   libpng-dev \
74                   libpython3-dev \
75                   libssl-dev \
76                   libvulkan-dev \
77                   libxcb-keysyms1-dev \
78                   python3-dev \
79                   python3-distutils \
80                   python3-serial \
81                   qt5-default \
82                   qt5-qmake \
83                   qtbase5-dev
84
85
86if [[ "$DEBIAN_ARCH" = "armhf" ]]; then
87	apt-get install -y libboost-dev:armhf \
88		libegl1-mesa-dev:armhf \
89		libelf-dev:armhf \
90		libgbm-dev:armhf \
91		libgles2-mesa-dev:armhf \
92		libpcre3-dev:armhf \
93		libpng-dev:armhf \
94		libpython3-dev:armhf \
95		libvulkan-dev:armhf \
96		libxcb-keysyms1-dev:armhf \
97               qtbase5-dev:armhf
98fi
99
100############### Build dEQP runner
101
102. .gitlab-ci/build-deqp-runner.sh
103mkdir -p /lava-files/rootfs-${DEBIAN_ARCH}/usr/bin
104mv /usr/local/bin/deqp-runner /lava-files/rootfs-${DEBIAN_ARCH}/usr/bin/.
105
106
107############### Build dEQP
108STRIP_CMD="${GCC_ARCH}-strip"
109DEQP_TARGET=surfaceless . .gitlab-ci/build-deqp.sh
110
111mv /deqp /lava-files/rootfs-${DEBIAN_ARCH}/.
112
113
114############### Build apitrace
115. .gitlab-ci/build-apitrace.sh
116mkdir -p /lava-files/rootfs-${DEBIAN_ARCH}/apitrace
117mv /apitrace/build /lava-files/rootfs-${DEBIAN_ARCH}/apitrace
118rm -rf /apitrace
119
120mkdir -p /lava-files/rootfs-${DEBIAN_ARCH}/waffle
121mv /waffle/build /lava-files/rootfs-${DEBIAN_ARCH}/waffle
122rm -rf /waffle
123
124
125############### Build renderdoc
126EXTRA_CMAKE_ARGS+=" -DENABLE_XCB=false"
127. .gitlab-ci/build-renderdoc.sh
128mkdir -p /lava-files/rootfs-${DEBIAN_ARCH}/renderdoc
129mv /renderdoc/build /lava-files/rootfs-${DEBIAN_ARCH}/renderdoc
130rm -rf /renderdoc
131
132
133############### Build libdrm
134EXTRA_MESON_ARGS+=" -D prefix=/libdrm"
135. .gitlab-ci/build-libdrm.sh
136mkdir -p /lava-files/rootfs-${DEBIAN_ARCH}/usr/lib/$GCC_ARCH
137find /libdrm/ -name lib\*\.so\* | xargs cp -t /lava-files/rootfs-${DEBIAN_ARCH}/usr/lib/$GCC_ARCH/.
138rm -rf /libdrm
139
140
141############### Cross-build kernel
142mkdir -p kernel
143wget -qO- ${KERNEL_URL} | tar -xz --strip-components=1 -C kernel
144pushd kernel
145
146############### Delete rust, since the tests won't be compiling anything.
147rm -rf /root/.rustup /root/.cargo
148
149# The kernel doesn't like the gold linker (or the old lld in our debians).
150# Sneak in some override symlinks during kernel build until we can update
151# debian (they'll get blown away by the rm of the kernel dir at the end).
152mkdir -p ld-links
153for i in /usr/bin/*-ld /usr/bin/ld; do
154    i=`basename $i`
155    ln -sf /usr/bin/$i.bfd ld-links/$i
156done
157export PATH=`pwd`/ld-links:$PATH
158
159if [ -n "$INSTALL_KERNEL_MODULES" ]; then
160    # Disable all modules in defconfig, so we only build the ones we want
161    sed -i 's/=m/=n/g' ${DEFCONFIG}
162fi
163
164./scripts/kconfig/merge_config.sh ${DEFCONFIG} ../.gitlab-ci/${KERNEL_ARCH}.config
165make ${KERNEL_IMAGE_NAME}
166for image in ${KERNEL_IMAGE_NAME}; do
167    cp arch/${KERNEL_ARCH}/boot/${image} /lava-files/.
168done
169
170if [[ -n ${DEVICE_TREES} ]]; then
171    make dtbs
172    cp ${DEVICE_TREES} /lava-files/.
173fi
174
175if [ -n "$INSTALL_KERNEL_MODULES" ]; then
176    make modules
177    INSTALL_MOD_PATH=/lava-files/rootfs-${DEBIAN_ARCH}/ make modules_install
178fi
179
180if [[ ${DEBIAN_ARCH} = "arm64" ]] && which mkimage > /dev/null; then
181    make Image.lzma
182    mkimage \
183        -f auto \
184        -A arm \
185        -O linux \
186        -d arch/arm64/boot/Image.lzma \
187        -C lzma\
188        -b arch/arm64/boot/dts/qcom/sdm845-cheza-r3.dtb \
189        /lava-files/cheza-kernel
190fi
191
192popd
193rm -rf kernel
194
195############### Create rootfs
196set +e
197debootstrap \
198    --variant=minbase \
199    --arch=${DEBIAN_ARCH} \
200     --components main,contrib,non-free \
201    buster \
202    /lava-files/rootfs-${DEBIAN_ARCH}/ \
203    http://deb.debian.org/debian
204
205cat /lava-files/rootfs-${DEBIAN_ARCH}/debootstrap/debootstrap.log
206set -e
207
208cp .gitlab-ci/create-rootfs.sh /lava-files/rootfs-${DEBIAN_ARCH}/.
209cp .gitlab-ci/container/llvm-snapshot.gpg.key /lava-files/rootfs-${DEBIAN_ARCH}/.
210chroot /lava-files/rootfs-${DEBIAN_ARCH} sh /create-rootfs.sh
211rm /lava-files/rootfs-${DEBIAN_ARCH}/create-rootfs.sh
212rm /lava-files/rootfs-${DEBIAN_ARCH}/llvm-snapshot.gpg.key
213du -ah /lava-files/rootfs-${DEBIAN_ARCH} | sort -h | tail -100
214pushd /lava-files/rootfs-${DEBIAN_ARCH}
215  tar cvzf /lava-files/lava-rootfs.tgz .
216popd
217
218if [ ${DEBIAN_ARCH} = arm64 ]; then
219    # Pull down a specific build of qcomlt/release/qcomlt-5.4 8c79b3d12355
220    # ("Merge tag 'v5.4.23' into release/qcomlt-5.4"), where I used the
221    # .config from
222    # http://snapshots.linaro.org/96boards/dragonboard820c/linaro/debian/457/config-5.4.0-qcomlt-arm64
223    # with the following merged in:
224    #
225    # CONFIG_DRM=y
226    # CONFIG_DRM_MSM=y
227    # CONFIG_ATL1C=y
228    #
229    # Reason: 5.5 has a big stack of oopses and warns on db820c.  4.14-5.4
230    # linaro kernel binaries (see above .config link) have these as modules
231    # and distributed the modules only in the debian system, not the initrd,
232    # so they're very hard to extract (involving simg2img and loopback
233    # mounting).  4.11 is missing d72fea538fe6 ("drm/msm: Fix the check for
234    # the command size") so it can't actually run fredreno.  qcomlt-4.14 is
235    # unstable at boot (~10% instaboot rate).  The 5.4 qcomlt kernel with msm
236    # built in seems like the easiest way to go.
237    wget https://people.freedesktop.org/~anholt/qcomlt-5.4-msm-build/Image.gz -O Image.gz \
238         -O /lava-files/db820c-kernel
239    wget https://people.freedesktop.org/~anholt/qcomlt-5.4-msm-build/apq8096-db820c.dtb \
240         -O /lava-files/db820c.dtb
241
242    # Make a gzipped copy of the Image for db410c.
243    gzip -k /lava-files/Image
244
245    # Add missing a630 firmware, added to debian packge in apr 2020
246    wget https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/plain/qcom/a630_gmu.bin \
247         -O /lava-files/rootfs-arm64/lib/firmware/qcom/a630_gmu.bin
248    wget https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/plain/qcom/a630_sqe.fw \
249         -O /lava-files/rootfs-arm64/lib/firmware/qcom/a630_sqe.fw
250fi
251
252. .gitlab-ci/container/container_post_build.sh
253
254############### Upload the files!
255if [ -n "$UPLOAD_FOR_LAVA" ]; then
256    ci-fairy minio login $CI_JOB_JWT
257    FILES_TO_UPLOAD="lava-rootfs.tgz \
258                     $KERNEL_IMAGE_NAME"
259
260    if [[ -n $DEVICE_TREES ]]; then
261        FILES_TO_UPLOAD="$FILES_TO_UPLOAD $(basename -a $DEVICE_TREES)"
262    fi
263
264    for f in $FILES_TO_UPLOAD; do
265        ci-fairy minio cp /lava-files/$f \
266            minio://${MINIO_PATH}/$f
267    done
268
269    touch /lava-files/done
270    ci-fairy minio cp /lava-files/done minio://${MINIO_PATH}/done
271fi
272
273