variables:
  PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}"
  # https://gitlab.com/gitlab-org/gitlab-runner/-/issues/3194
  ErrorActionPreference: stop

# See: https://docs.gitlab.com/ce/ci/yaml/README.html#workflowrules-templates
workflow:
  rules:
    - if: $CI_MERGE_REQUEST_IID
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

windows-build:
  tags:
    - desy
    - windows
  needs:
    - job: pre-commit
      artifacts: false
    - job: ubuntu-24.04
      artifacts: false
  variables:
    JPEG_VERSION: v2
    TANGO_IDL_TAG: 6.0.2
    ZMQ_VERSION: 4.0.5-2
    OMNI_VERSION: 4.3.3
    CATCH_VERSION: 3.5.1
    PYTHON_VERSION: 3.7.8 # omniidl.exe was built against 3.7, so it also depends on python37.dll
    OTEL_VERSION: v8
    PYVER: "py37"
    CMAKE_GENERATOR: "Visual Studio 17 2022"
    CMAKE_DISABLE_PRECOMPILE_HEADERS: "ON"
    TANGO_JPEG_ROOT: dependencies/libjpeg
    TANGO_ZMQ_ROOT: dependencies/zeromq
    TANGO_CPPZMQ_ROOT: dependencies/zeromq # coincides with TANGO_ZMQ_ROOT
    TANGO_OMNI_ROOT: dependencies/omniORB
    TANGO_IDL_SOURCE: dependencies/source/tango-idl
    TANGO_IDL_ROOT: dependencies/tango-idl
    TANGO_CATCH_ROOT: dependencies/catch
    TANGO_CATCH_SOURCE: dependencies/source/Catch2-${CATCH_VERSION}
    OTEL_ROOT: dependencies/otel
    WIX_TOOLSET_LOCATION: dependencies/wix
    PYTHON_LOCATION: dependencies/python
    TANGO_WARNINGS_AS_ERRORS: "ON"
    BUILD_TESTING: "ON"
    # BEGIN local testing
    # For local testing on Windows machine do the following:
    # - Make sure you have git, cmake and gitlab-runner installed
    # - Remove the `rules` and `parallel/matrix` statements and replace with the variables below
    # - Move the job into .gitlab-ci.yml
    # - Open powershell
    # - git commit -m "changes" :/ ; ~/Downloads/gitlab-runner-windows-amd64.exe exec shell --shell powershell 'windows-build'
    # GIT_CLEAN_FLAGS: "none" #  see https://docs.gitlab.com/ee/ci/runners/configure_runners.html#git-clean-flags
    # ARCHITECTURE: "x64"
    # PY_ARCH: "amd64"
    # VC_ARCH_VER: "${ARCHITECTURE}-msvc15"
    # CMAKE_BUILD_TYPE: "Release"
    # LIBRARY_TYPE: "Shared"
    # TELEMETRY_USAGE: "With Otel"
    # DEPENDENCY_LOCATION: "Installed Deps"
    # END local testing
  parallel:
    matrix:
      - CMAKE_BUILD_TYPE:
        - "Release"
        - "Debug"
        LIBRARY_TYPE:
        - "Shared"
        - "Static"
        ARCHITECTURE:
        - "x64"
        - "win32"
        TELEMETRY_USAGE:
        - "With Otel"
        - "No Otel"
        DEPENDENCY_LOCATION:
        - "Installed Deps"
        - "Build Deps"
  rules:
    - if: '$TELEMETRY_USAGE == "With Otel" && $LIBRARY_TYPE == "Shared"'
      # currently not supported
      when: never
    - if: '$DEPENDENCY_LOCATION == "Build Deps" && (($TELEMETRY_USAGE == "No Otel" && $LIBRARY_TYPE == "Static") || $CMAKE_BUILD_TYPE == "Debug" || $ARCHITECTURE == "win32")'
      # Checking all the permutations is overkill, we only need to test that the normal TangoConfig.cmake.in works on
      # Windows and testing Release Shared No Otel and Release Static With Otel  on x64 is enough for this.
      when: never
    - if: '($CI_COMMIT_TAG == null || $CI_COMMIT_TAG =~ /^.*-dev$/) && $ARCHITECTURE == "win32" && ($LIBRARY_TYPE == "Static" || $CMAKE_BUILD_TYPE == "Debug")'
      when: manual
      allow_failure: true
    - if: '($CI_COMMIT_TAG == null || $CI_COMMIT_TAG =~ /^.*-dev$/) && $LIBRARY_TYPE == "Static" && ($TELEMETRY_USAGE == "No Otel" || $CMAKE_BUILD_TYPE == "Debug")'
      when: manual
      allow_failure: true
    - if: '$ARCHITECTURE == "x64"'
      variables:
        VC_ARCH_VER: "${ARCHITECTURE}-msvc15"
        PY_ARCH: "amd64"
      when: on_success
    - if: '$ARCHITECTURE == "win32"'
      variables:
        VC_ARCH_VER: "${ARCHITECTURE}-msvc15"
        PY_ARCH: "${ARCHITECTURE}"
      when: on_success
  script:
    - $PSVersionTable
    - try { & '.\ci\windows-build.ps1' } catch { Write-Host $_; exit 1 }
    - try { & '.\ci\windows-test.ps1' } catch { Write-Host $_; exit 1 }
  artifacts:
    when: always
    paths:
      - build/tests/catch2_test_logs
      - build/*.zip
      - build/*.msi

release-builds:
  variables:
    MSVC_VERSION: "v143"
    RELEASE_URL_PREFIX: "${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}"
    FILENAME_PREFIX: "libtango_${CI_COMMIT_TAG}_${MSVC_VERSION}"
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  needs:
    - job: windows-build
      optional: true
  rules:
    - if: $CI_COMMIT_TAG && $CI_COMMIT_TAG !~ /^.*-dev$/
      when: on_success
  before_script:
    - apk update
    - apk add curl
    - apk --no-cache add findutils
  script:
    - cd build
    - >
      find . -type f \( -name "*.zip" -o -name "*.msi" \) -printf "%f\n" -exec curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file {} "${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}/{}" \;
  release:
    name: "Release $CI_COMMIT_TAG"
    tag_name: "$CI_COMMIT_TAG"
    description: "Release $CI_COMMIT_TAG"
    ref: "$CI_COMMIT_SHA"
    assets:
      links:
        # iteration order is from back to front:
        # msi/zip -> debug/release -> otel/{} -> static/shared -> x86/x64
        - name: "${FILENAME_PREFIX}_x86_static_debug.msi"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x86_static_debug.msi"

        - name: "${FILENAME_PREFIX}_x86_static_debug.zip"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x86_static_debug.zip"

        - name: "${FILENAME_PREFIX}_x86_static_release.msi"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x86_static_release.msi"

        - name: "${FILENAME_PREFIX}_x86_static_release.zip"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x86_static_release.zip"

        - name: "${FILENAME_PREFIX}_x86_static_otel_debug.msi"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x86_static_otel_debug.msi"

        - name: "${FILENAME_PREFIX}_x86_static_otel_debug.zip"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x86_static_otel_debug.zip"

        - name: "${FILENAME_PREFIX}_x86_static_otel_release.msi"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x86_static_otel_release.msi"

        - name: "${FILENAME_PREFIX}_x86_static_otel_release.zip"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x86_static_otel_release.zip"

        - name: "${FILENAME_PREFIX}_x86_shared_debug.msi"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x86_shared_debug.msi"

        - name: "${FILENAME_PREFIX}_x86_shared_debug.zip"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x86_shared_debug.zip"

        - name: "${FILENAME_PREFIX}_x86_shared_release.msi"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x86_shared_release.msi"

        - name: "${FILENAME_PREFIX}_x86_shared_release.zip"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x86_shared_release.zip"

        - name: "${FILENAME_PREFIX}_x64_static_debug.msi"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x64_static_debug.msi"

        - name: "${FILENAME_PREFIX}_x64_static_debug.zip"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x64_static_debug.zip"

        - name: "${FILENAME_PREFIX}_x64_static_release.msi"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x64_static_release.msi"

        - name: "${FILENAME_PREFIX}_x64_static_release.zip"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x64_static_release.zip"

        - name: "${FILENAME_PREFIX}_x64_static_otel_debug.msi"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x64_static_otel_debug.msi"

        - name: "${FILENAME_PREFIX}_x64_static_otel_debug.zip"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x64_static_otel_debug.zip"

        - name: "${FILENAME_PREFIX}_x64_static_otel_release.msi"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x64_static_otel_release.msi"

        - name: "${FILENAME_PREFIX}_x64_static_otel_release.zip"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x64_static_otel_release.zip"

        - name: "${FILENAME_PREFIX}_x64_shared_debug.msi"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x64_shared_debug.msi"

        - name: "${FILENAME_PREFIX}_x64_shared_debug.zip"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x64_shared_debug.zip"

        - name: "${FILENAME_PREFIX}_x64_shared_release.msi"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x64_shared_release.msi"

        - name: "${FILENAME_PREFIX}_x64_shared_release.zip"
          url: "${RELEASE_URL_PREFIX}/${FILENAME_PREFIX}_x64_shared_release.zip"
