# Docker image tag helper templates

.incorporate-templates-commit:
  variables:
    FDO_DISTRIBUTION_TAG: "${MESA_IMAGE_TAG}--${MESA_TEMPLATES_COMMIT}"

.incorporate-base-tag+templates-commit:
  variables:
    FDO_BASE_IMAGE: "${CI_REGISTRY_IMAGE}/${MESA_BASE_IMAGE}:${MESA_BASE_TAG}--${MESA_TEMPLATES_COMMIT}"
    FDO_DISTRIBUTION_TAG: "${MESA_IMAGE_TAG}--${MESA_BASE_TAG}--${MESA_TEMPLATES_COMMIT}"

.set-image:
  extends:
    - .incorporate-templates-commit
  variables:
    MESA_IMAGE: "$CI_REGISTRY_IMAGE/${MESA_IMAGE_PATH}:${FDO_DISTRIBUTION_TAG}"
  image: "$MESA_IMAGE"

.set-image-base-tag:
  extends:
    - .set-image
    - .incorporate-base-tag+templates-commit
  variables:
    MESA_IMAGE: "$CI_REGISTRY_IMAGE/${MESA_IMAGE_PATH}:${FDO_DISTRIBUTION_TAG}"

# Build the CI docker images.
#
# MESA_IMAGE_TAG is the tag of the docker image used by later stage jobs. If the
# image doesn't exist yet, the container stage job 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.
#
# After merging a change resulting in generating a new image to the
# main repository, it's recommended to remove the image from the source
# repository's container registry, so that the image from the main
# repository's registry will be used there as well.

.debian-container:
  variables:
    FDO_DISTRIBUTION_VERSION: bookworm-slim

.container:
  stage: container
  extends:
    - .container+build-rules
    - .incorporate-templates-commit
  variables:
    FDO_REPO_SUFFIX: $CI_JOB_NAME
    FDO_DISTRIBUTION_EXEC: 'bash .gitlab-ci/container/${CI_JOB_NAME}.sh'
    # no need to pull the whole repo to build the container image
    GIT_STRATEGY: none

.use-base-image:
  extends:
    - .container
    - .incorporate-base-tag+templates-commit

# Debian based x86_64 build image base
debian/x86_64_build-base:
  extends:
    - .fdo.container-build@debian
    - .container
    - .debian-container
  variables:
    MESA_IMAGE_TAG: &debian-x86_64_build-base "${DEBIAN_BASE_TAG}--${PKG_REPO_REV}"
  rules:
    # python-test requires debian/x86_64_build, which requires this job
    - !reference [python-test, rules]
    - !reference [.container, rules]

.use-debian/x86_64_build-base:
  extends:
    - .fdo.container-build@debian
    - .debian-container
    - .use-base-image
  variables:
    MESA_BASE_IMAGE: ${DEBIAN_X86_64_BUILD_BASE_IMAGE}
    MESA_BASE_TAG: *debian-x86_64_build-base
    MESA_ARTIFACTS_BASE_TAG: *debian-x86_64_build-base
  needs:
    - debian/x86_64_build-base

# Debian based x86_64 main build image
debian/x86_64_build:
  extends:
    - .use-debian/x86_64_build-base
  variables:
    MESA_IMAGE_TAG: &debian-x86_64_build ${DEBIAN_BUILD_TAG}
  rules:
    # python-test requires this job
    - !reference [python-test, rules]
    - !reference [.use-debian/x86_64_build-base, rules]

.use-debian/x86_64_build:
  extends:
    - .set-image-base-tag
  variables:
    MESA_BASE_TAG: *debian-x86_64_build-base
    MESA_IMAGE_PATH: ${DEBIAN_X86_64_BUILD_IMAGE_PATH}
    MESA_IMAGE_TAG: *debian-x86_64_build
  needs:
    - debian/x86_64_build

# Debian based x86_32 cross-build image
debian/x86_32_build:
  extends:
    - .use-debian/x86_64_build-base
  variables:
    MESA_IMAGE_TAG: &debian-x86_32_build ${DEBIAN_BUILD_TAG}

.use-debian/x86_32_build:
  extends:
    - .set-image-base-tag
  variables:
    MESA_BASE_TAG: *debian-x86_64_build-base
    MESA_IMAGE_PATH: "debian/x86_32_build"
    MESA_IMAGE_TAG: *debian-x86_32_build
  needs:
    - debian/x86_32_build

# Debian based ppc64el cross-build image
debian/ppc64el_build:
  extends:
    - .use-debian/x86_64_build-base
  variables:
    MESA_IMAGE_TAG: &debian-ppc64el_build ${DEBIAN_BUILD_TAG}

.use-debian/ppc64el_build:
  extends:
    - .set-image-base-tag
  variables:
    MESA_BASE_TAG: *debian-x86_64_build-base
    MESA_IMAGE_PATH: "debian/ppc64el_build"
    MESA_IMAGE_TAG: *debian-ppc64el_build
  needs:
    - debian/ppc64el_build

# Debian based s390x cross-build image
debian/s390x_build:
  extends:
    - .use-debian/x86_64_build-base
  variables:
    MESA_IMAGE_TAG: &debian-s390x_build ${DEBIAN_BUILD_TAG}

.use-debian/s390x_build:
  extends:
    - .set-image-base-tag
  variables:
    MESA_BASE_TAG: *debian-x86_64_build-base
    MESA_IMAGE_PATH: "debian/s390x_build"
    MESA_IMAGE_TAG: *debian-s390x_build
  needs:
    - debian/s390x_build

# Android NDK cross-build image
debian/android_build:
  extends:
    - .use-debian/x86_64_build-base
  variables:
    MESA_IMAGE_TAG: &debian-android_build ${DEBIAN_BUILD_TAG}
    ANDROID_SDK_VERSION: 33
    ANDROID_NDK: android-ndk-r25b

.use-debian/android_build:
  extends:
    - .set-image-base-tag
  variables:
    MESA_BASE_TAG: *debian-x86_64_build-base
    MESA_IMAGE_PATH: "debian/android_build"
    MESA_IMAGE_TAG: *debian-android_build
  needs:
    - debian/android_build

# Debian based x86_64 test image base
debian/x86_64_test-base:
  extends: debian/x86_64_build-base
  variables:
    MESA_IMAGE_TAG: &debian-x86_64_test-base "${DEBIAN_BASE_TAG}--${PKG_REPO_REV}--${KERNEL_TAG}"

.use-debian/x86_64_test-base:
  extends:
    - .fdo.container-build@debian
    - .debian-container
    - .use-base-image
  variables:
    MESA_BASE_IMAGE: ${DEBIAN_X86_64_TEST_BASE_IMAGE}
    MESA_BASE_TAG: *debian-x86_64_test-base
  needs:
    - debian/x86_64_test-base

# Debian based x86_64 test image for GL
debian/x86_64_test-gl:
  extends: .use-debian/x86_64_test-base
  variables:
    MESA_IMAGE_TAG: &debian-x86_64_test-gl ${DEBIAN_X86_64_TEST_GL_TAG}

.use-debian/x86_64_test-gl:
  extends:
    - .set-image-base-tag
  variables:
    MESA_BASE_TAG: *debian-x86_64_test-base
    MESA_IMAGE_PATH: ${DEBIAN_X86_64_TEST_IMAGE_GL_PATH}
    MESA_IMAGE_TAG: *debian-x86_64_test-gl
  needs:
    - debian/x86_64_test-gl

# Debian based x86_64 test image for VK
debian/x86_64_test-vk:
  extends: .use-debian/x86_64_test-base
  variables:
    MESA_IMAGE_TAG: &debian-x86_64_test-vk ${DEBIAN_X86_64_TEST_VK_TAG}

.use-debian/x86_64_test-vk:
  extends:
    - .set-image-base-tag
  variables:
    MESA_BASE_TAG: *debian-x86_64_test-base
    MESA_IMAGE_PATH: ${DEBIAN_X86_64_TEST_IMAGE_VK_PATH}
    MESA_IMAGE_TAG: *debian-x86_64_test-vk
  needs:
    - debian/x86_64_test-vk

# Debian based x86_64 test image for Android
debian/x86_64_test-android:
  extends: .use-debian/x86_64_test-base
  variables:
    MESA_IMAGE_TAG: &debian-x86_64_test-android ${DEBIAN_X86_64_TEST_ANDROID_TAG}
    ANDROID_NDK: android-ndk-r25b

.use-debian/x86_64_test-android:
  extends:
    - .set-image-base-tag
  variables:
    MESA_BASE_TAG: *debian-x86_64_test-base
    MESA_IMAGE_PATH: ${DEBIAN_X86_64_TEST_ANDROID_IMAGE_PATH}
    MESA_IMAGE_TAG: *debian-x86_64_test-android
  needs:
    - debian/x86_64_test-android

# Debian based ARM build image
debian/arm64_build:
  extends:
    - .fdo.container-build@debian
    - .container
    - .debian-container
  tags:
    - aarch64
  variables:
    MESA_IMAGE_TAG: &debian-arm64_build "${DEBIAN_BASE_TAG}--${PKG_REPO_REV}"

.use-debian/arm64_build:
  extends:
    - .set-image
  variables:
    MESA_IMAGE_PATH: "debian/arm64_build"
    MESA_IMAGE_TAG: *debian-arm64_build
    MESA_ARTIFACTS_TAG: *debian-arm64_build
  needs:
    - debian/arm64_build


# Alpine based x86_64 build image
.alpine/x86_64_build-base:
  extends:
    - .fdo.container-build@alpine
    - .container
  variables:
    FDO_DISTRIBUTION_VERSION: "edge"  # switch to 3.20 when gets released
    FDO_BASE_IMAGE: alpine:$FDO_DISTRIBUTION_VERSION  # since cbuild ignores it

# Alpine based x86_64 build image
alpine/x86_64_build:
  extends:
    - .alpine/x86_64_build-base
  variables:
    MESA_IMAGE_TAG: &alpine-x86_64_build ${ALPINE_X86_64_BUILD_TAG}

.use-alpine/x86_64_build:
  extends:
    - .set-image
  variables:
    MESA_IMAGE_PATH: "alpine/x86_64_build"
    MESA_IMAGE_TAG: *alpine-x86_64_build
  needs:
    - alpine/x86_64_build

# Alpine based x86_64 image for LAVA SSH dockerized client
alpine/x86_64_lava_ssh_client:
  extends:
    - .alpine/x86_64_build-base
  variables:
    MESA_IMAGE_TAG: &alpine-x86_64_lava_ssh_client ${ALPINE_X86_64_LAVA_SSH_TAG}

# Fedora based x86_64 build image
fedora/x86_64_build:
  extends:
    - .fdo.container-build@fedora
    - .container
  variables:
    FDO_DISTRIBUTION_VERSION: 38
    MESA_IMAGE_TAG: &fedora-x86_64_build ${FEDORA_X86_64_BUILD_TAG}

.use-fedora/x86_64_build:
  extends:
    - .set-image
  variables:
    MESA_IMAGE_PATH: "fedora/x86_64_build"
    MESA_IMAGE_TAG: *fedora-x86_64_build
  needs:
    - fedora/x86_64_build


.kernel+rootfs:
  timeout: 2h  # 24.0-only change
  extends:
    - .container+build-rules
    - .debian-container
  stage: container
  variables:
    GIT_STRATEGY: fetch
    MESA_ROOTFS_TAG: &kernel-rootfs ${KERNEL_ROOTFS_TAG}
    DISTRIBUTION_TAG: &distribution-tag-arm "${MESA_ROOTFS_TAG}--${KERNEL_TAG}--${MESA_ARTIFACTS_TAG}--${MESA_TEMPLATES_COMMIT}"
  script:
    - .gitlab-ci/container/lava_build.sh

kernel+rootfs_x86_64:
  extends:
    - .use-debian/x86_64_build-base
    - .kernel+rootfs
  image: "$FDO_BASE_IMAGE"
  variables:
    DEBIAN_ARCH: "amd64"
    DISTRIBUTION_TAG: &distribution-tag-x86_64 "${MESA_ROOTFS_TAG}--${KERNEL_TAG}--${MESA_ARTIFACTS_BASE_TAG}--${MESA_TEMPLATES_COMMIT}"

kernel+rootfs_arm64:
  extends:
    - .use-debian/arm64_build
    - .kernel+rootfs
  tags:
    - aarch64
  variables:
    DEBIAN_ARCH: "arm64"

kernel+rootfs_arm32:
  extends:
    - kernel+rootfs_arm64
  variables:
    DEBIAN_ARCH: "armhf"

# Cannot use anchors defined here from included files, so use extends: instead
.use-kernel+rootfs-arm:
  variables:
    DISTRIBUTION_TAG: *distribution-tag-arm
    MESA_ROOTFS_TAG: *kernel-rootfs

.use-kernel+rootfs-x86_64:
  variables:
    DISTRIBUTION_TAG: *distribution-tag-x86_64
    MESA_ROOTFS_TAG: *kernel-rootfs

# x86_64 image with ARM64 & ARM32 kernel & rootfs for baremetal testing
.debian/arm_test:
  extends:
    - .fdo.container-build@debian
    - .container
    - .debian-container
    # Don't want the .container rules
    - .container+build-rules
  variables:
    FDO_DISTRIBUTION_TAG: "${MESA_IMAGE_TAG}--${MESA_ROOTFS_TAG}--${KERNEL_TAG}--${MESA_TEMPLATES_COMMIT}"
    ARTIFACTS_PREFIX: "https://${S3_HOST}/mesa-lava"
    ARTIFACTS_SUFFIX: "${MESA_ROOTFS_TAG}--${KERNEL_TAG}--${MESA_ARTIFACTS_TAG}--${MESA_TEMPLATES_COMMIT}"
    MESA_ARTIFACTS_TAG: *debian-arm64_build
    MESA_ROOTFS_TAG: *kernel-rootfs

debian/arm32_test:
  extends:
    - .debian/arm_test
  needs:
    - kernel+rootfs_arm32
  variables:
    MESA_IMAGE_TAG: &debian-arm32_test "${DEBIAN_BASE_TAG}--${PKG_REPO_REV}"

debian/arm64_test:
  extends:
    - .debian/arm_test
  needs:
    - kernel+rootfs_arm64
  variables:
    MESA_IMAGE_TAG: &debian-arm64_test "${DEBIAN_BASE_TAG}--${PKG_REPO_REV}"

.use-debian/arm_test:
  variables:
    MESA_ROOTFS_TAG: *kernel-rootfs

.use-debian/arm32_test:
  image: "$CI_REGISTRY_IMAGE/${MESA_IMAGE_PATH}:${MESA_IMAGE_TAG}--${MESA_ROOTFS_TAG}--${KERNEL_TAG}--${MESA_TEMPLATES_COMMIT}"
  extends:
    - .use-debian/arm_test
  variables:
    MESA_IMAGE_PATH: "debian/arm32_test"
    MESA_IMAGE_TAG: *debian-arm32_test
  needs:
    - debian/arm_test

.use-debian/arm64_test:
  image: "$CI_REGISTRY_IMAGE/${MESA_IMAGE_PATH}:${MESA_IMAGE_TAG}--${MESA_ROOTFS_TAG}--${KERNEL_TAG}--${MESA_TEMPLATES_COMMIT}"
  extends:
    - .use-debian/arm_test
  variables:
    MESA_IMAGE_PATH: "debian/arm64_test"
    MESA_IMAGE_TAG: *debian-arm64_test
  needs:
    - debian/arm_test

# Native Windows docker builds
#
# Unlike the above Linux-based builds - including MinGW builds which
# cross-compile for Windows - which use the freedesktop ci-templates, we
# cannot use the same scheme here. As Windows lacks support for
# Docker-in-Docker, and Podman does not run natively on Windows, we have
# to open-code much of the same ourselves.
#
# This is achieved by first running in a native Windows shell instance
# (host PowerShell) in the container stage to build and push the image,
# then in the build stage by executing inside Docker.

.windows-docker-msvc:
  variables:
    MESA_IMAGE: "$CI_REGISTRY_IMAGE/${MESA_IMAGE_PATH}:${MESA_IMAGE_TAG}"
    MESA_UPSTREAM_IMAGE: "$CI_REGISTRY/$FDO_UPSTREAM_REPO/$MESA_IMAGE_PATH:${MESA_IMAGE_TAG}"
  extends:
    - .windows-docker-tags

.windows_container_build:
  inherit:
    default: [retry]
  extends:
    - .container
    - .windows-docker-msvc
    - .windows-shell-tags
  rules:
    - !reference [.microsoft-farm-container-rules, rules]
    - !reference [.container+build-rules, rules]
  variables:
    GIT_STRATEGY: fetch # we do actually need the full repository though
    MESA_BASE_IMAGE: None
  script:
    - .\.gitlab-ci\windows\mesa_container.ps1 $CI_REGISTRY $CI_REGISTRY_USER $CI_REGISTRY_PASSWORD $MESA_IMAGE $MESA_UPSTREAM_IMAGE ${DOCKERFILE} ${MESA_BASE_IMAGE}

windows_msvc:
  inherit:
    default: [retry]
  extends:
    - .windows_container_build
  variables:
    MESA_IMAGE_PATH: &windows_msvc_image_path ${WINDOWS_X64_MSVC_PATH}
    MESA_IMAGE_TAG: ${WINDOWS_X64_MSVC_TAG}
    DOCKERFILE: Dockerfile_msvc
    MESA_BASE_IMAGE: "mcr.microsoft.com/windows/server:ltsc2022"

windows_build_msvc:
  inherit:
    default: [retry]
  extends:
    - .windows_container_build
  rules:
    - !reference [.microsoft-farm-rules, rules]
    - !reference [.container+build-rules, rules]
  variables:
    MESA_IMAGE_PATH: &windows_build_image_path ${WINDOWS_X64_BUILD_PATH}
    MESA_IMAGE_TAG: &windows_build_image_tag ${WINDOWS_X64_MSVC_TAG}--${WINDOWS_X64_BUILD_TAG}
    DOCKERFILE: Dockerfile_build
    MESA_BASE_IMAGE_PATH: *windows_msvc_image_path
    MESA_BASE_IMAGE: "$CI_REGISTRY_IMAGE/${MESA_BASE_IMAGE_PATH}:${WINDOWS_X64_MSVC_TAG}"
  timeout: 2h 30m # LLVM takes ages
  needs:
    - windows_msvc

windows_test_msvc:
  inherit:
    default: [retry]
  extends:
    - .windows_container_build
  rules:
    - !reference [.microsoft-farm-rules, rules]
    - !reference [.container+build-rules, rules]
  variables:
    MESA_IMAGE_PATH: &windows_test_image_path ${WINDOWS_X64_TEST_PATH}
    MESA_IMAGE_TAG: &windows_test_image_tag ${WINDOWS_X64_MSVC_TAG}--${WINDOWS_X64_TEST_TAG}
    DOCKERFILE: Dockerfile_test
    MESA_BASE_IMAGE_PATH: *windows_msvc_image_path
    MESA_BASE_IMAGE: "$CI_REGISTRY_IMAGE/${MESA_BASE_IMAGE_PATH}:${WINDOWS_X64_MSVC_TAG}"
  timeout: 2h 30m
  needs:
    - windows_msvc

.use-windows_build_msvc:
  inherit:
    default: [retry]
  extends: .windows-docker-msvc
  image: "$MESA_IMAGE"
  variables:
    MESA_IMAGE_PATH: *windows_build_image_path
    MESA_IMAGE_TAG: *windows_build_image_tag
  needs:
    - windows_build_msvc

.use-windows_test_msvc:
  inherit:
    default: [retry]
  extends: .windows-docker-msvc
  image: "$MESA_IMAGE"
  variables:
    MESA_IMAGE_PATH: *windows_test_image_path
    MESA_IMAGE_TAG: *windows_test_image_tag
