include:
    - project: 'steam/steam-ci-pipeline'
      file: '/steam-gitlab-ci-common.yml'

variables:
    BUILD_IMAGE: registry.gitlab.steamos.cloud/steamrt/scout/sdk:beta
    STEAM_CI_USE_BINARIES_FROM: build
    STEAM_CI_DEPENDENCIES: >-
        debhelper
        glslang-tools
        gtk-doc-tools
        libelf-dev
        libegl1-mesa-dev
        libgl1-mesa-dev
        libgles2-mesa-dev
        libglib2.0-dev
        libglib2.0-doc
        libjson-glib-dev
        libtheora-dev
        libva-dev
        libvdpau-dev
        libvulkan-dev
        libx11-dev
        libxau-dev
        libxcb1-dev
        libxcomposite-dev
        locales
        meson
        pandoc
        python3
        python3.5
        zlib1g

    IMAGES_DOWNLOAD_URL: ''
    IMAGES_DOWNLOAD_CREDENTIAL: ''

    DEBIAN_FRONTEND: noninteractive

    SCOUT_DOCKER_REGISTRY: registry.gitlab.steamos.cloud
    SCOUT_DOCKER_IMAGE: steamrt/scout/sdk:beta
    SCOUT_I386_DOCKER_IMAGE: steamrt/scout/sdk/i386:beta
    SCOUT_APT_SOURCES_FILE: ''
    DEVEL_DOCKER_REGISTRY: ''
    DEVEL_DOCKER_IMAGE: ''
    # Set non-empty to allow
    CI_ALLOW_MISSING_SOURCES: ''

    # Work around _srt_rm_rf behaving oddly on overlayfs:
    # we use FTW_MOUNT to make sure we won't accidentally traverse
    # between filesystems, but on overlayfs files can appear to be on
    # different devices
    STEAM_CI_TMPDIR: "${CI_PROJECT_DIR}/debian/tmpdir"

stages:
    - build
    - relocatable-install
    - test

package:
    extends: .build_package
    variables:
        STEAM_CI_DEB_BUILD: full    # debuild --build=full, aka -F
        STEAM_CI_INSTALL_SCRIPT: |
            apt-get -y install libcapsule-tools pkg-create-dbgsym

package:i386:
    extends: .build_package
    variables:
        BUILD_IMAGE: registry.gitlab.steamos.cloud/steamrt/scout/sdk/i386:beta
        STEAM_CI_DEB_BUILD: any     # debuild --build=any, aka -B
        STEAM_CI_INSTALL_SCRIPT: |
            apt-get -y install libcapsule-tools pkg-create-dbgsym

build:devel:
    stage: build
    tags:
        - docker
        - linux
    rules:
        - if: '$DEVEL_DOCKER_REGISTRY != "" && $DEVEL_DOCKER_IMAGE != ""'
    image: "${DEVEL_DOCKER_REGISTRY}/${DEVEL_DOCKER_IMAGE}"
    script:
        - |
            set -eux

            apt-get -y --no-install-recommends install \
            clang \
            clang-tools \
            ${NULL+}

            mkdir -p -m700 "${STEAM_CI_TMPDIR}"
            export TMPDIR="${STEAM_CI_TMPDIR}"

            mkdir -p _build
            meson --werror -Dman=true _build/devel
            ninja -C _build/devel
            ninja -C _build/devel install
            meson test --verbose -C _build/devel

            export CC=clang
            export CXX=clang++

            meson \
                -Db_lundef=false \
                -Db_sanitize=address,undefined \
                --werror \
                _build/clang-asan
            ninja -C _build/clang-asan
            ninja -C _build/clang-asan scan-build
            ninja -C _build/clang-asan install
            meson test --verbose -C _build/clang-asan

    artifacts:
        paths:
            - _build/devel/meson-logs/*.txt
            - _build/clang-asan/meson-logs/*.txt
        when: always

.prepare_scout: &prepare_scout
    - |
        set -eux

        if [ -n "${SCOUT_APT_SOURCES_FILE}" ]; then
            cp "${SCOUT_APT_SOURCES_FILE}" /etc/apt/sources.list
            echo >> /etc/apt/sources.list

            if [ -n "${STEAM_CI_EXTRA_APT_SOURCES-}" ]; then
              echo "${STEAM_CI_EXTRA_APT_SOURCES}" | while read -r first rest; do
                if [ "x$first" = xboth ]; then
                  echo "deb $rest" >> /etc/apt/sources.list
                  echo "deb-src $rest" >> /etc/apt/sources.list
                else
                  echo "$first $rest" >> /etc/apt/sources.list
                fi
              done
            fi

            apt-get -y update
        fi

        # We need up-to-date packages for the relocatable install to
        # be able to get its source code
        apt-get -y dist-upgrade

        apt-get -y --no-install-recommends install \
        libcapsule-tools \
        ${NULL+}

        # g++ 4.6 is too old (see also debian/rules)
        export CC=gcc-4.8
        export CXX=g++-4.8

        mkdir -p -m700 "${STEAM_CI_TMPDIR}"
        export TMPDIR="${STEAM_CI_TMPDIR}"

        mkdir -p _build/scout

build:scout:
    stage: build
    tags:
        - docker
        - linux
    image: "${SCOUT_DOCKER_REGISTRY}/${SCOUT_DOCKER_IMAGE}"
    script:
        - *prepare_scout
        - |
            meson \
              --prefix="$(pwd)/_build/scout/prefix" \
              -Dsrcdir=src \
              --werror \
              _build/scout
            ninja -C _build/scout
            G_MESSAGES_DEBUG=all meson test --verbose -C _build/scout
            ninja -C _build/scout install
    artifacts:
        paths:
            - _build/scout/prefix
            - _build/scout/meson-logs/*.txt
        when: always

build:scout-i386:
    stage: build
    tags:
        - docker
        - linux
    image: "${SCOUT_DOCKER_REGISTRY}/${SCOUT_I386_DOCKER_IMAGE}"
    script:
        - *prepare_scout
        - |
            meson \
              --prefix="$(pwd)/_build/scout/prefix" \
              -Dsrcdir=src \
              -Dbin=false \
              -Dpressure_vessel=false \
              --werror \
              _build/scout-i386
            ninja -C _build/scout-i386
            G_MESSAGES_DEBUG=all meson test --verbose -C _build/scout-i386
            ninja -C _build/scout-i386 install
    artifacts:
        paths:
            - _build/scout/prefix
            - _build/scout-i386/meson-logs/*.txt
        when: always

relocatable-install:
    stage: relocatable-install
    needs:
        - build:scout
        - build:scout-i386
    tags:
        - docker
        - linux
    image: "${SCOUT_DOCKER_REGISTRY}/${SCOUT_DOCKER_IMAGE}"
    script:
        - *prepare_scout
        - |
            apt-get -y --no-install-recommends install \
            bubblewrap \
            libcapsule0 \
            libcapsule-tools \
            libcapsule-tools-relocatable:amd64 \
            libcapsule-tools-relocatable:i386 \
            libglib2.0-dev \
            libxau-dev \
            meson \
            ${NULL+}

            rm -fr _build/scout/relocatable-install
            _build/scout/prefix/lib/pressure-vessel/relocatable/bin/pressure-vessel-build-relocatable-install \
              --output _build/scout/relocatable-install \
              --archive "$(pwd)/_build/scout" \
              ${CI_ALLOW_MISSING_SOURCES:+--allow-missing-sources} \
              ${NULL+}
            prove -epython3.5 -v ./tests/pressure-vessel/relocatable-install.py :: \
              "$(pwd)/_build/scout/relocatable-install"
    artifacts:
        paths:
            - _build/scout/pressure-vessel-*-bin.tar.gz
            - _build/scout/pressure-vessel-*-bin+src.tar.gz
        when: on_success

.prepare_test: &prepare_test
    - |
        set -eux

        PRESSURE_VESSEL=$(ls _build/scout/pressure-vessel-*-bin.tar.gz | head -1)
        mkdir -p _build/depot-template/common

        if [ -n "${IMAGES_DOWNLOAD_URL}" ] && [ -n "${IMAGES_DOWNLOAD_CREDENTIAL}" ]; then
            python3 ./pressure-vessel/populate-depot.py \
            --depot=_build/depot \
            --source-dir=_build/depot-template \
            --include-sdk \
            --unpack-runtimes \
            --credential-env IMAGES_DOWNLOAD_CREDENTIAL \
            --images-uri "${IMAGES_DOWNLOAD_URL}"/steamrt-SUITE/snapshots \
            --pressure-vessel "${PRESSURE_VESSEL}" \
            scout \
            soldier \
            ${NULL+}
        else
            python3 ./pressure-vessel/populate-depot.py \
            --depot=_build/depot \
            --source-dir=_build/depot-template \
            --unpack-runtimes \
            --pressure-vessel "${PRESSURE_VESSEL}" \
            --version latest-steam-client-public-beta \
            scout \
            ${NULL+}
        fi

.i386_dependencies: &i386_dependencies
    - |
        set -eux

        dpkg --add-architecture i386
        apt-get -y update
        apt-get -y upgrade
        apt-get -y --no-install-recommends install \
        libc6-i386 \
        libgl1-mesa-dri:i386 \
        ${NULL+}

.test_template:
    needs:
        - relocatable-install
    stage: test
    tags:
        - docker
        - linux
    variables:
        STEAM_CI_DEPENDENCIES: >-
            libgl1-mesa-dri
            locales
            python3
    artifacts:
        paths:
            - _build/artifacts
        when: always

test:debian-10:
    extends: .test_template
    image: debian:buster-slim
    script:
        - *prepare_test
        - *i386_dependencies
        - |
            set -eux

            export AUTOPKGTEST_ARTIFACTS="$(pwd)/_build/artifacts"
            export PRESSURE_VESSEL_TEST_CONTAINERS="$(pwd)/_build/depot"
            python3 tests/pressure-vessel/containers.py

test:ubuntu-18.04:
    extends: .test_template
    image: ubuntu:18.04
    script:
        - *prepare_test
        - *i386_dependencies
        - |
            set -eux

            export AUTOPKGTEST_ARTIFACTS="$(pwd)/_build/artifacts"
            export PRESSURE_VESSEL_TEST_CONTAINERS="$(pwd)/_build/depot"
            python3 tests/pressure-vessel/containers.py

test:ubuntu-20.04:
    extends: .test_template
    image: ubuntu:20.04
    script:
        - *prepare_test
        - *i386_dependencies
        - |
            set -eux

            export AUTOPKGTEST_ARTIFACTS="$(pwd)/_build/artifacts"
            export PRESSURE_VESSEL_TEST_CONTAINERS="$(pwd)/_build/depot"
            python3 tests/pressure-vessel/containers.py

test:archlinux:
    extends: .test_template
    image: archlinux:latest
    # Do not use the global before_script because it is only for Debian based
    # distros
    before_script:
        - |
            set -eux

            # Enable multilib repository
            echo -e "\n[multilib]\nInclude = /etc/pacman.d/mirrorlist" >> /etc/pacman.conf

            pacman -Syu --needed --noconfirm --noprogressbar \
            base-devel \
            git \
            lib32-glibc \
            lib32-mesa \
            python \
            python-chardet \
            python-six \
            python-tappy \
            sudo \
            ${NULL+}

            tempdir="$(mktemp -d)"
            git clone --branch debian/buster https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.steamos.cloud/packaging/python-debian.git "$tempdir/python-debian"
            export PYTHONPATH="$tempdir/python-debian/lib"

    script:
        - *prepare_test
        - |
            set -eux

            export AUTOPKGTEST_ARTIFACTS="$(pwd)/_build/artifacts"
            export PRESSURE_VESSEL_TEST_CONTAINERS="$(pwd)/_build/depot"
            python3 tests/pressure-vessel/containers.py

autopkgtest:
    stage: test
    extends: .autopkgtest
    needs:
        - package
        - package:i386
    variables:
        STEAM_CI_INSTALL_SCRIPT: |
            # We need up-to-date packages for the relocatable install to
            # be able to get its source code
            apt-get -y dist-upgrade

            # Install the packages under test. We're not too worried about
            # minimal dependencies here
            dpkg -i \
            debian/tmp/artifacts/build/libsteam-runtime-tools-0-0_*.deb \
            debian/tmp/artifacts/build/libsteam-runtime-tools-0-0-dbgsym_*_*.*deb \
            debian/tmp/artifacts/build/libsteam-runtime-tools-0-dev_*.deb \
            debian/tmp/artifacts/build/libsteam-runtime-tools-0-helpers_*.deb \
            debian/tmp/artifacts/build/libsteam-runtime-tools-0-helpers-dbgsym_*_*.*deb \
            debian/tmp/artifacts/build/libsteam-runtime-tools-0-tests_*_amd64.deb \
            debian/tmp/artifacts/build/pressure-vessel-relocatable_*_amd64.deb \
            debian/tmp/artifacts/build/pressure-vessel-relocatable-dbgsym_*_amd64.*deb \
            debian/tmp/artifacts/build/pressure-vessel-libs*.deb \
            debian/tmp/artifacts/build/steam-runtime-tools-bin_*_amd64.deb \
            debian/tmp/artifacts/build/steam-runtime-tools-bin-dbgsym_*_amd64.*deb \
            ${NULL+}
            apt-get -y -f install

# vim:set sw=4 sts=4 et:
