# vim: set expandtab shiftwidth=2 tabstop=8 textwidth=0 filetype=yaml:
#
# This CI uses the freedesktop.org ci-templates.
# Please see the ci-templates documentation for details:
# https://freedesktop.pages.freedesktop.org/ci-templates/

.templates_sha: &template_sha ef5e4669b7500834a17ffe9277e15fbb6d977fff # see https://docs.gitlab.com/ee/ci/yaml/#includefile

# FDO_DISTRIBUTION_TAG is the tag of the docker image used for the build jobs.
# If the image doesn't exist yet, the docker-image stage generates it.
#
# In order to generate a new image, one should generally change the tag.
# While removing the image from the registry would also work, that's not
# recommended except for ephemeral images during development: Replacing an
# image after a significant amount of time might pull in newer versions of
# gcc/clang or other packages, which might break the build with older commits
# using the same tag.
variables:
    FDO_UPSTREAM_REPO:    'xorg/xserver'
    MESON_BUILDDIR:       'build'
    REPO_URL_XORGPROTO:   'https://gitlab.freedesktop.org/xorg/proto/xorgproto.git'
    XORG_DEBIAN_VERSION:  'bullseye-slim'
    XORG_DEBIAN_EXEC:     'env FDO_CI_CONCURRENT=${FDO_CI_CONCURRENT} bash .gitlab-ci/debian-install.sh'
    XORG_DEBIAN_TAG:      '2025-03-07-libdrm.1'
    XORG_FREEBSD_VERSION: '14.2'
    XORG_FREEBSD_EXEC:    ''
    XORG_FREEBSD_TAG:     '2025-05-27-xcb'

include:
  - project: 'freedesktop/ci-templates'
    ref: *template_sha
    file:
      - '/templates/debian.yml'
      - '/templates/freebsd.yml'
      - '/templates/ci-fairy.yml'
  - template: Security/SAST.gitlab-ci.yml

stages:
    - docker-image
    - build-and-test
    - drivers
    - test

.ci-run-policy:
  # Retry jobs after runner system failures
  retry:
    max: 2
    when:
      - runner_system_failure
  # Cancel CI run if a newer commit is pushed to the same branch
  interruptible: true

# This is everything but the DDXen
.dix_paths: &dix_paths
  # Directories
  - .gitlab-ci/**/*
  - composite/**/*
  - config/**/*
  - damageext/**/*
  - dbe/**/*
  - dix/**/*
  - doc/**/*
  - dri3/**/*
  - exa/**/*
  - fb/**/*
  - glamor/**/*
  - glx/**/*
  - include/**/*
  - m4/**/*
  - man/**/*
  - mi/**/*
  - miext/**/*
  - os/**/*
  - present/**/*
  - pseudoramiX/**/*
  - randr/**/*
  - record/**/*
  - render/**/*
  - test/**/*
  - Xext/**/*
  - xfixes/**/*
  - Xi/**/*
  - xkb/**/*
  # Files
  - hw/meson.build
  - .gitlab-ci.yml
  - meson*
  - xorg-server.m4
  - xorg-server.pc.in
  - xserver.ent.in

.xorg_paths: &xorg_paths
  - hw/xfree86/**/*

.all_ddx_paths:
  - hw/**/*

.debian:
  variables:
    FDO_DISTRIBUTION_VERSION:   '$XORG_DEBIAN_VERSION'
    FDO_DISTRIBUTION_EXEC:      '$XORG_DEBIAN_EXEC'
    FDO_DISTRIBUTION_TAG:       '$XORG_DEBIAN_TAG'

.freebsd:
  tags:
    - kvm
  variables:
    FDO_DISTRIBUTION_TAG:       '$XORG_FREEBSD_TAG'
    FDO_DISTRIBUTION_VERSION:   '$XORG_FREEBSD_VERSION'
    FDO_DISTRIBUTION_EXEC:      ''
    FDO_DISTRIBUTION_PACKAGES:  'git gcc pkgconf autoconf automake libtool xorg-macros xorgproto bash meson ninja pixman xtrans libXau libXdmcp libXfont2 libxkbfile libxcvt libpciaccess font-util libepoll-shim libxvmc xcb-util xcb-util-wm'
    FDO_IMAGE_SIZE:             '30G'

debian-bullseye:
  extends:
    - .fdo.container-build@debian
    - .ci-run-policy
    - .debian
  stage: docker-image
  variables:
    GIT_STRATEGY: none

freebsd-image:
  extends:
    - .fdo.qemu-build@freebsd@x86_64
    - .freebsd
  stage: docker-image
  variables:
    GIT_STRATEGY:               none

.xorg-image@debian:
    extends:
        - .fdo.distribution-image@debian
        - .debian

.xorg-image@freebsd:
    extends:
        - .fdo.distribution-image@freebsd
        - .freebsd
    variables:
        GIT_DEPTH:                1
        PKG_CONFIG_PATH:          /usr/share/pkgconfig:/usr/lib/pkgconfig:/usr/pkg/share/pkgconfig:/usr/pkg/lib/pkgconfig:/usr/local/libdata/pkgconfig

.common-build-and-test:
    extends:
        - .xorg-image@debian
        - .ci-run-policy
    stage: build-and-test
    artifacts:
        when: always
        paths:
            - $MESON_BUILDDIR/meson-logs/
            - $MESON_BUILDDIR/test/piglit-results/
    variables:
        MESON_ARGS: -Dc_args="-fno-common" -Dprefix=/usr -Dxephyr=true -Dwerror=true -Dxcsecurity=true -Dxv=true -Dxvmc=true
        CCACHE_COMPILERCHECK: content
        CCACHE_DIR: /cache/xserver/cache
        LC_ALL: C.UTF-8
    before_script:
        - export CCACHE_BASEDIR="$PWD"
        - export PATH="/usr/lib/ccache:$PATH"
        - ccache --show-stats
    after_script:
        - ccache --show-stats

meson:
    extends: .common-build-and-test
    script:
        - .gitlab-ci/meson-build.sh --run-test
        - .gitlab-ci/check-piglit-results.sh
        - .gitlab-ci/manpages-check
        - .gitlab-ci/check-ddx-build.sh
    variables:
      XTEST_DIR: /root/xts
      PIGLIT_DIR: /root/piglit
      LP_NUM_THREADS: 0
      MESON_DDX_BUILD_ARGS: >
        -Dxorg=${BUILD_XORG} -Dxephyr=${BUILD_XEPHYR} -Dxvfb=${BUILD_XVFB} -Dxnest=${BUILD_XNEST}
      BUILD_XEPHYR: true
      BUILD_XNEST: true
      BUILD_XORG: true
      BUILD_XVFB: true
      MESON_EXTRA_ARGS: ${MESON_DDX_BUILD_ARGS}

meson-noglamor:
    extends: meson
    variables:
        MESON_EXTRA_ARGS: -Dglamor=false ${MESON_DDX_BUILD_ARGS}

mingw-cross-build:
    extends: .common-build-and-test
    script:
        - .gitlab-ci/meson-build.sh --run-install
    variables:
      MESON_ARGS: --cross-file=.gitlab-ci/cross-i686-w64-mingw32.txt -Dglx=false -Dlisten_tcp=true -Dxvmc=true -Dxv=true

freebsd:
    stage: build-and-test
    extends:
        - .xorg-image@freebsd
    variables:
        MESON_ARGS: -Dglx=false -Dglamor=false -Dudev=false -Dudev_kms=false -Dxvmc=true -Dxv=true
    script:
      # running of of disk space without this
      # needed until https://gitlab.freedesktop.org/freedesktop/ci-templates/-/issues/67 is fixed
      - git gc
      - git clone --depth=1 $REPO_URL_XORGPROTO dep.xorgproto
      - /app/vmctl start
      - set +e
      - scp -r $PWD "vm:"
      # need to install extra packages, as long as image cant be rebuilt
      - /app/vmctl exec "pkg update && pkg add libxvmc xcb-util xcb-util-wm"
      # need to install newer xorgproto
      - /app/vmctl exec "cd $CI_PROJECT_NAME/dep.xorgproto && ./autogen.sh --prefix=/usr && make && make install"
      - /app/vmctl exec "cd $CI_PROJECT_NAME && PKG_CONFIG_PATH=\"$PKG_CONFIG_PATH\" MESON_ARGS=\"$MESON_ARGS\" MESON_BUILDDIR=\"$MESON_BUILDDIR\" .gitlab-ci/meson-build.sh --skip-test" && touch .success
      # test not working yet, so skipped
      # - scp -r vm:$CI_PROJECT_NAME/test-results.xml .
      - /app/vmctl stop
      - set -e
      - test -e .success || exit 1

meson-dist:
    extends: .common-build-and-test
    artifacts:
        when: always
        paths:
            - $MESON_BUILDDIR/meson-logs/
            - $MESON_BUILDDIR/xserver-*/$MESON_BUILDDIR/meson-logs/
            - xserver-tarball/install/
    script:
        - .gitlab-ci/meson-build.sh --run-dist
        - mkdir xserver-tarball
        - tar xf $MESON_BUILDDIR/meson-dist/xserver-*.tar.xz -C xserver-tarball --strip-components=1
        - .gitlab-ci/meson-build.sh -C xserver-tarball --skip-test --skip-dist --run-install
    variables:
      MESON_DIST_ARGS: --no-tests
      MESON_EXTRA_ARGS: -Dprefix=$CI_PROJECT_DIR/xserver-tarball/install/

xf86-driver-build-test:
    extends:
        - .xorg-image@debian
        - .ci-run-policy
    stage: drivers
    parallel:
        matrix:
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-input-elographics
              SHA: xf86-input-elographics-1.4.4
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-input-evdev
              SHA: xf86-input-evdev-2.11.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-input-joystick
              SHA: fa296d27fb479107fa6328a7565f665de2de98d3 # 5 commits ahead of 1.6.4
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-input-libinput
              SHA: xf86-input-libinput-1.5.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-input-mouse
              SHA: xf86-input-mouse-1.9.5
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-input-synaptics
              SHA: xf86-input-synaptics-1.10.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-input-vmmouse
              SHA: 3bbcb6a143bb3f53e5b776fb70a4933229c1781a # 2 commits ahead of 13.2.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-input-void
              SHA: b43e11eeb8b96aa87da910991da8b005d9847783 # 4 commits ahead of 1.4.2
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-amdgpu
              SHA: 2cb59a92de4a19909682a35e4f31f3c02de2899f # 18 commits ahead of 23.0.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-apm
              SHA: 196784e691b8f8739792439434ffa002e9f5cdfa # 14 commits ahead of 1.3.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-ark
              SHA: 109745d5c7e6982ee0dabbc5a233c1f2667ad5c9 # 9 commits ahead of 0.7.4
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-ast
              SHA: db56de34bdf70f1904dba50d98774aaa950a2ca5 # 10 commits ahead of 1.1.6
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-ati
              SHA: c610c037369500089baddd86aa3cf8046f3dc48b # 11 commits ahead of 22.0.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-chips
              SHA: 4503aece04cc8860df18ce946633b5449a0cb47b # 7 commits ahead of 1.5.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-cirrus
              SHA: 15d68316524c593bd67a6ea59b5c746a94c6c1f6 # 10 commits ahead of 1.6.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-dummy
              SHA: 3a407a34c4a2b6b263a535b6aa7b49dd70784fdf # 9 commits ahead of 0.4.1
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-fbdev
              SHA: c8d9f3be7e95689d793bb2f45a5dddf409e46811 # 15 commits ahead of 0.5.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-i128
              SHA: 4fd0309834deca78e5ef6cad5ecba8864a308ba5 # 7 commits ahead of 1.4.1
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-i740
              SHA: d610334264e82a18477b9a5c3a4095c49c18f47b # 11 commits ahead of 1.4.0
            - REPO: https://gitlab.freedesktop.org/metux/xf86-video-intel
              SHA: submit/fixes-for-xserver # temporary
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-mach64
              SHA: 878048e3aec04c6bb8034f21afdfff37e3528c5f # 20 commits ahead of 6.9.7
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-mga
              SHA: 66ee371516c48c30b67684317a8e3c24ba7f1f4e # 11 commits ahead of 2.0.1
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-neomagic
              SHA: 69cafdeebf7c338ede076424f9bcb97f26ede7a8 # 8 commits ahead of 1.3.1
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-nested
              SHA: 86b6dc3bb6d78f40905ecc63df3635b284214836 # no release yet
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-nouveau
              SHA: xf86-video-nouveau-1.0.18
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-nv
              SHA: a8608636da97fdd880edb1bce919a0d3029f24dd # 15 commits ahead of 2.1.23
            - REPO: https://gitlab.freedesktop.org/metux/xf86-video-qxl
              SHA: incubate # temporary
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-r128
              SHA: bc21fcdffa623bb8e7150e1da69aeaed6cd7c5d2 # 1 commits ahead of 6.13.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-rendition
              SHA: 49d50f1fdeb4dc81895fec81be9fae1dc78e5974 # 14 commits ahead of 4.2.7
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-s3virge
              SHA: 8834f100114ba136d52590e711054ab063306421 # 11 commits ahead of 1.11.1
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-savage
              SHA: 09f6c06f0a793c7d297f152227b551c128f20875 # 8 commits ahead of 2.4.1
            - REPO: https://gitlab.freedesktop.org/metux/xf86-video-siliconmotion
              SHA: incubate # temporary
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-sis
              SHA: ee451146c0afd735b50488588f82f3f158e9da03 # 68 commits ahead of 0.12.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-sisusb
              SHA: ccff93b681968463d00793a9a7c5c2db8301634f # 30 commits ahead of 0.12.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-suncg14
              SHA: 471e04fe84abaf2f868fb85566d99495142692a9 # 9 commits ahead of 1.1.3
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-suncg3
              SHA: 8162844221f6d59e540c3c3a146a9c581d413949 # 9 commits ahead of 1.1.3
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-suncg6
              SHA: 37a28d0d9dbb9b0c6631aa7b09d558d5f552c627 # 10 commits ahead of 1.1.3
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-sunffb
              SHA: 6100635debdda392ca420e87c5b0d70bd22dfed8 # 14 commits ahead of 1.2.3
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-sunleo
              SHA: 6d56a007f3d47fa43e95f1a0c8619be00b0c1c97 # 10 commits ahead of 1.2.3
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-suntcx
              SHA: c1c0e384b95da18b81793612b90f693891f2e476 # 9 commits ahead of 1.1.3
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-tdfx
              SHA: 5253278119a4db07b5a01856f0de34e576f4dcdd # 9 commits ahead of 1.5.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-trident
              SHA: fddaa2084b9aac51a845ee1ede8c498cbc9330cf # 13 commits ahead of 1.4.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-v4l
              SHA: eb1e55874cbacf7455f70269581a877e88e3bf37 # 15 commits ahead of 0.3.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-vbox
              SHA: ec4bc6b8e9d1a83526573d27afd099debbd5b86b # 7 commits ahead of 1.0.1
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-vesa
              SHA: 5b33e95acb831768c52d491de079a1adcae762e0 # 10 commits ahead of 2.6.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-vmware
              SHA: 92cc4534146dd84c0d843759cdb30f75abddddd6 # 6 commits ahead 13.4.0
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-voodoo
              SHA: c70353ddf49d557c596a47b835b6b8d8bbe35ebe # 15 commits ahead of 1.2.6
            - REPO: https://gitlab.freedesktop.org/xorg/driver/xf86-video-xgi
              SHA: e73ff14a7f1b7562ff447aad527641cc35f557ae # 49 commits ahead 1.6.1
    script:
        # built xserver is taken from meson-dist job
        - export PKG_CONFIG_PATH=$(pwd)/xserver-tarball/install/lib/x86_64-linux-gnu/pkgconfig
        - export ACLOCAL_PATH=$(pwd)/xserver-tarball/install/share/aclocal/
        - unset MESON_EXTRA_ARGS
        - DRIVER=$(basename $REPO)
        - git clone "$REPO" "$DRIVER"
        - git -C "$DRIVER" checkout "$SHA"
        - |
          if [[ -e "$DRIVER/meson.build" ]] && [[ "$DRIVER" != "xf86-video-intel" ]]; then
            .gitlab-ci/meson-build.sh -C "$DRIVER" --skip-test
          else
             pushd "$DRIVER" || exit 1
             ./autogen.sh && make
          fi
    needs:
        - meson-dist
    dependencies:
        - meson-dist
    variables:
        GIT_DEPTH: 1
        MESON_ARGS: -Dprefix=/usr/
        MESON_EXTRA_ARGS: -Dxnest=false -Dxvfb=false -Dxquartz=false -Ddocs=false

#
# Verify that the merge request has the allow-collaboration checkbox ticked
#
check-merge-request:
  extends:
    - .fdo.ci-fairy
  needs: []
  stage: test
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
  script:
    - ci-fairy check-merge-request --require-allow-collaboration --junit-xml=results.xml
  artifacts:
    when: on_failure
    reports:
      junit: results.xml
  allow_failure: true

#
# Workflow rules needed due to:
# https://gitlab.freedesktop.org/freedesktop/freedesktop/-/issues/438
#
workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    - if: $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_PIPELINE_SOURCE == 'push'
