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

variables:
    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"

    BUILD_IMAGE: '${SCOUT_DOCKER_REGISTRY}/${SCOUT_DOCKER_IMAGE}'

    # These need to be configured in
    # https://gitlab.steamos.cloud/groups/steamrt/-/settings/ci_cd
    # Hostname of the machine that receives pressure-vessel releases
    PRESSURE_VESSEL_CI_UPLOAD_HOST: ''
    # Create a File variable with the public key(s) of P_V_CI_UPLOAD_HOST,
    # in the usual ~/.ssh/known_hosts format:
    # upload-host.example ssh-rsa AAA...
    # upload-host.example ecdsa-sha2-nistp256 AAA...
    # upload-host.example ssh-ed25519 AAA...
    PRESSURE_VESSEL_CI_UPLOAD_HOST_SSH_PUBLIC_KEYS_FILE: ''
    # Path on P_V_CI_UPLOAD_HOST: /srv/VHOST/www/pressure-vessel/snapshots
    PRESSURE_VESSEL_CI_UPLOAD_PATH: ''
    # Similar path on P_V_CI_UPLOAD_HOST for unreleased test-builds
    PRESSURE_VESSEL_CI_UPLOAD_PLAYGROUND_PATH: ''
    # User to log in on P_V_CI_UPLOAD_HOST
    PRESSURE_VESSEL_CI_UPLOAD_USER: ''
    # Create a File variable with a private key authorized for P_V_CI_UPLOAD_USER
    PRESSURE_VESSEL_CI_UPLOAD_SSH_PRIVATE_KEY_FILE: ''

stages:
    - build
    - relocatable-install
    - deploy
    - test

package:
    extends: .build_package
    variables:
        BUILD_IMAGE: '${SCOUT_DOCKER_REGISTRY}/${SCOUT_DOCKER_IMAGE}'
        DEB_BUILD_SNAPSHOT_RELEASE_TAGS: 'yes'
        STEAM_CI_DEB_BUILD: full    # debuild --build=full, aka -F
        STEAM_CI_INSTALL_SCRIPT: |
            apt-get -y install pkg-create-dbgsym

package:i386:
    extends: .build_package
    variables:
        BUILD_IMAGE: '${SCOUT_DOCKER_REGISTRY}/${SCOUT_I386_DOCKER_IMAGE}'
        DEB_BUILD_SNAPSHOT_RELEASE_TAGS: 'yes'
        STEAM_CI_DEB_BUILD: any     # debuild --build=any, aka -B
        STEAM_CI_INSTALL_SCRIPT: |
            apt-get -y install 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

        # 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:debug:
    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 \
            libglib2.0-dev:amd64 \
            libglib2.0-dev:i386 \
            libxau-dev:amd64 \
            libxau-dev:i386 \
            meson \
            waffle-utils-multiarch:amd64 \
            waffle-utils-multiarch:i386 \
            ${NULL+}

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

relocatable-install:production:
    stage: relocatable-install
    needs:
        - package
        - package:i386
    tags:
        - docker
        - linux
    image: "${SCOUT_DOCKER_REGISTRY}/${SCOUT_DOCKER_IMAGE}"
    script:
        - *prepare_scout
        - |
            apt-get -y --no-install-recommends install \
            bubblewrap \
            libglib2.0-dev:amd64 \
            libglib2.0-dev:i386 \
            libxau-dev:amd64 \
            libxau-dev:i386 \
            meson \
            waffle-utils-multiarch:amd64 \
            waffle-utils-multiarch:i386 \
            ${NULL+}

            dpkg -i \
            debian/tmp/artifacts/build/*_amd64.*deb \
            debian/tmp/artifacts/build/libsteam-runtime-tools-0-0-dbgsym_*_i386.*deb \
            debian/tmp/artifacts/build/libsteam-runtime-tools-0-0_*_i386.deb \
            debian/tmp/artifacts/build/libsteam-runtime-tools-0-dev_*_i386.deb \
            debian/tmp/artifacts/build/libsteam-runtime-tools-0-helpers-dbgsym_*_i386.*deb \
            debian/tmp/artifacts/build/libsteam-runtime-tools-0-helpers_*_i386.deb \
            debian/tmp/artifacts/build/libsteam-runtime-tools-0-relocatable-libs_*_i386.deb \
            debian/tmp/artifacts/build/pressure-vessel-libs-i386_*_i386.deb \
            ${NULL+}

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

.prepare_test: &prepare_test
    - |
        set -eux

        PRESSURE_VESSEL=_build/production/pressure-vessel-bin.tar.gz
        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:production
    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

deploy:
    stage: deploy
    tags:
        - docker
        - linux
    needs:
        - package
        - package:i386
        - relocatable-install:production
    rules:
        - if: '$DEVEL_DOCKER_REGISTRY == ""'
          when: never
        - if: '$DEVEL_DOCKER_IMAGE == ""'
          when: never
        - if: '$PRESSURE_VESSEL_CI_UPLOAD_HOST == ""'
          when: never
        - if: '$PRESSURE_VESSEL_CI_UPLOAD_HOST_SSH_PUBLIC_KEYS_FILE == ""'
          when: never
        - if: '$CI_COMMIT_TAG && $PRESSURE_VESSEL_CI_UPLOAD_PATH == ""'
          when: never
        - if: >-
            $PRESSURE_VESSEL_CI_UPLOAD_PATH == ""
            && $PRESSURE_VESSEL_CI_UPLOAD_PLAYGROUND_PATH == ""
          when: never
        - if: '$PRESSURE_VESSEL_CI_UPLOAD_USER == ""'
          when: never
        - if: '$PRESSURE_VESSEL_CI_UPLOAD_SSH_PRIVATE_KEY_FILE == ""'
          when: never
        - if: '$CI_COMMIT_TAG'
          when: always
        # To debug changes to this CI step, change this to when: always
        # or when: manual, and push to a protected branch
        - when: never
    image: "${DEVEL_DOCKER_REGISTRY}/${DEVEL_DOCKER_IMAGE}"
    variables:
        STEAM_CI_DEPENDENCIES: >-
            openssh-client
    script:
        - |
            if ! [ -f "$PRESSURE_VESSEL_CI_UPLOAD_HOST_SSH_PUBLIC_KEYS_FILE" ]; then
                echo "P_V_CI_UPLOAD_HOST_SSH_PUBLIC_KEYS_FILE must be of type File" >&2
                exit 1
            fi
            if ! [ -f "$PRESSURE_VESSEL_CI_UPLOAD_SSH_PRIVATE_KEY_FILE" ]; then
                echo "P_V_CI_UPLOAD_SSH_PRIVATE_KEY_FILE must be of type File" >&2
                exit 1
            fi
            chmod 0600 "$PRESSURE_VESSEL_CI_UPLOAD_SSH_PRIVATE_KEY_FILE"

            if [ -n "${CI_COMMIT_TAG-}" ]; then
                path="$PRESSURE_VESSEL_CI_UPLOAD_PATH"
            else
                path="$PRESSURE_VESSEL_CI_UPLOAD_PLAYGROUND_PATH"
            fi

            ./pressure-vessel/upload-artifacts.py \
            --host="$PRESSURE_VESSEL_CI_UPLOAD_HOST" \
            --path="$path" \
            --login="$PRESSURE_VESSEL_CI_UPLOAD_USER" \
            --ssh-known-hosts="$PRESSURE_VESSEL_CI_UPLOAD_HOST_SSH_PUBLIC_KEYS_FILE" \
            --ssh-private-key="$PRESSURE_VESSEL_CI_UPLOAD_SSH_PRIVATE_KEY_FILE" \
            ${NULL+}

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