---
include:
  - project: 'tango-controls/gitlab-ci-templates'
    file: 'ArchiveWithSubmodules.gitlab-ci.yml'

variables:
  TANGO_VERSION: 9.5.0
  MSVC_VERSION: v142
  GENERATOR_VERSION: Visual Studio 16 2019
  DEPS_PATH: C:/dependencies
  LIBTANGO: libtango_${TANGO_VERSION}_${MSVC_VERSION}_${ARCH}_static_debug
  TANGO_PATH: ${DEPS_PATH}/${LIBTANGO}
  CMAKE_BUILD_TYPE: Debug
  DEBIAN_FRONTEND: noninteractive
  GIT_SUBMODULE_STRATEGY: recursive

# 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

build-cppTango-9.3-backports:
  image: debian:bullseye
  tags:
    - docker, linux, amd64
  before_script:
    - apt-get update
    - >
      apt-get install -y
      build-essential
      cmake
      git
      libcos4-dev
      libomniorb4-dev
      libomnithread4-dev
      libzmq3-dev
      omniidl
      pkg-config
    # Install tango-idl
    - git clone -b 5.1.2 --depth 1 https://gitlab.com/tango-controls/tango-idl.git /idl
    - cmake -B /idl/build -DCMAKE_INSTALL_PREFIX=/usr/local/tango-idl /idl
    - make -C /idl/build install
    # Install cppzmq
    - git clone -b v4.7.1 --depth 1 https://github.com/zeromq/cppzmq.git /cppzmq
    - cmake -B /cppzmq/build -DCPPZMQ_BUILD_TESTS=OFF /cppzmq
    - make -C /cppzmq/build install
    # Clone cppTango
    - git clone -b 9.3-backports --depth 1 https://gitlab.com/tango-controls/cppTango.git /cppTango
    - >
      cmake -B /cppTango/build
      -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
      -DBUILD_TESTING=OFF
      -DIDL_BASE=/usr/local/tango-idl
      /cppTango
  script:
    - make -C /cppTango/build install
    - tar cf usr-local.tar /usr/local
  artifacts:
    when: on_success
    paths:
      - usr-local.tar

build-cppTango-main:
  image: debian:bullseye
  tags:
    - docker, linux, amd64
  before_script:
    - apt-get update
    - >
      apt-get install -y
      build-essential
      curl
      cmake
      git
      python3-dev
      libssl-dev
      libzmq3-dev
      pkg-config
    # Install omniORB 4.3.1
    - curl -L -o omniORB.tar.bz2 https://sourceforge.net/projects/omniorb/files/omniORB/omniORB-4.3.1/omniORB-4.3.1.tar.bz2/download
    - mkdir -p /omniorb/build
    - tar xaf omniORB.tar.bz2 --directory=/omniorb --strip-components=1
    - cd /omniorb/build
    - ../configure --prefix=/usr/local --with-openssl
    - make -j$(nproc) all
    - make install
    - cd ${CI_PROJECT_DIR}
    # Install tango-idl
    - git clone -b 6.0-alpha2 --depth 1 https://gitlab.com/tango-controls/tango-idl.git /idl
    - cmake -B /idl/build -DCMAKE_INSTALL_PREFIX=/usr/local/tango-idl /idl
    - make -j$(nproc) -C /idl/build install
    # Install cppzmq
    - git clone -b v4.7.1 --depth 1 https://github.com/zeromq/cppzmq.git /cppzmq
    - cmake -B /cppzmq/build -DCPPZMQ_BUILD_TESTS=OFF /cppzmq
    - make -j$(nproc) -C /cppzmq/build install
    # Clone cppTango
    - git clone -b main --recurse-submodules --depth 1 https://gitlab.com/tango-controls/cppTango.git /cppTango
    - >
      cmake -B /cppTango/build
      -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
      -DBUILD_TESTING=OFF
      -Dtangoidl_ROOT=/usr/local/tango-idl
      -DTANGO_USE_JPEG=OFF
      /cppTango
  script:
    - make -j$(nproc) -C /cppTango/build install
    - tar cf usr-local.tar /usr/local
  artifacts:
    when: on_success
    paths:
      - usr-local.tar

.build-universaltest-win:
  rules:
    - if: '$ARCH == "x64"'
      variables:
        DOWNLOAD_URL: "https://gitlab.com/tango-controls/cppTango/-/package_files/96952389/download"
    - if: '$ARCH == "x86"'
      variables:
        DOWNLOAD_URL: "https://gitlab.com/tango-controls/cppTango/-/package_files/96952288/download"
  before_script:
    - choco install cmake curl -y
    - $env:Path += ";C:\Program Files\CMake\bin"
    - $env:Path += ";C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin"
    - msbuild /version
    - if (Test-Path "${DEPS_PATH}") { Remove-Item "${DEPS_PATH}" -Recurse -Force; }
    - New-Item "${DEPS_PATH}" -ItemType Directory
      # See LIBTANGO constant at the top for the exact name
    - curl.exe -qLS "${DOWNLOAD_URL}" -o "${DEPS_PATH}/download"
    - 7z x "${DEPS_PATH}/download" -o"${DEPS_PATH}"
  script:
    - New-Item build -ItemType Directory
    - cd build
    - cmake -G"${GENERATOR_VERSION}" -A"${PLATFORM}" .. -DTango_ROOT="${TANGO_PATH}" -DTango_FORCE_STATIC=ON
    - cmake --build . --config Debug
    - cmake --install . --config Debug
  after_script:
    - New-Item artifacts -ItemType Directory
    - if (Test-Path "C:\Program Files\UniversalTest\bin") { Move-Item -Path "C:\Program Files\UniversalTest\bin\UniversalTest.exe" -Destination "artifacts\UniversalTest-x64.exe"; }
    - if (Test-Path "C:\Program Files (x86)\UniversalTest\bin") { Move-Item -Path "C:\Program Files (x86)\UniversalTest\bin\UniversalTest.exe" -Destination "artifacts\UniversalTest-x86.exe"; }
  artifacts:
    paths:
      - artifacts
    expire_in: 1 day
  tags:
    - shared-windows

build-universaltest-win-64bit:
  extends: .build-universaltest-win
  variables:
    ARCH: x64
    PLATFORM: x64

build-universaltest-win-32bit:
  extends: .build-universaltest-win
  variables:
    ARCH: x86
    PLATFORM: Win32

.build-universaltest-linux:
  image: debian:bullseye
  tags:
    - docker, linux, amd64
  before_script:
    - apt-get update
    - >
      apt-get install -y
      build-essential
      cmake
      git
      libcos4-dev
      libomniorb4-dev
      libomnithread4-dev
      libzmq3-dev
      omniidl
      pkg-config
    - mkdir build

build-universaltest-linux-main:
  extends: .build-universaltest-linux
  needs:
    - build-cppTango-main
  script:
    - tar xf usr-local.tar -C /
    - cmake -DTANGO_WARNINGS_AS_ERRORS=ON -B build .
    - make -C build install

build-universaltest-linux-9.3-backports:
  extends: .build-universaltest-linux
  needs:
    - build-cppTango-9.3-backports
  script:
    - tar xf usr-local.tar -C /
    - cmake -B build .
    - make -C build install

build-universaltest-linux-stock:
  extends: .build-universaltest-linux
  script:
    - apt-get install -y libtango-dev
    - cmake -B build .
    - make -C build install

build-universaltest-linux-main-newest-supported-cmake:
  extends: .build-universaltest-linux
  needs:
    - build-cppTango-main
  script:
    # Extract tango
    - tar xf usr-local.tar -C /
    # Install custom cmake
    - apt-get install curl
    - apt-get remove -y cmake
    - curl -L https://cmake.org/files/v3.26/cmake-3.26.4-linux-x86_64.sh -o /tmp/cmake-install.sh
    - chmod +x /tmp/cmake-install.sh
    - cd /usr/local
    - /tmp/cmake-install.sh --skip-license --exclude-subdir
    # Build DS
    - cd ${CI_PROJECT_DIR}
    - cmake -Werror=dev -DTANGO_WARNINGS_AS_ERRORS=ON -B build .
    - make -C build install

build-universaltest-linux-main-oldest-supported-cmake:
  extends: .build-universaltest-linux
  needs:
    - build-cppTango-main
  script:
    # Extract tango
    - tar xf usr-local.tar -C /
    # Install custom cmake
    - apt-get install curl
    - apt-get remove -y cmake
    - curl -L https://cmake.org/files/v3.18/cmake-3.18.6-Linux-x86_64.sh -o /tmp/cmake-install.sh
    - chmod +x /tmp/cmake-install.sh
    - cd /usr/local
    - /tmp/cmake-install.sh --skip-license --exclude-subdir
    # Build DS
    - cd ${CI_PROJECT_DIR}
    - cmake -Werror=dev -DTANGO_WARNINGS_AS_ERRORS=ON -B build .
    - make -C build install

release-builds:
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  tags:
    - docker, linux, amd64
  needs:
    - build-universaltest-linux-stock
    - build-universaltest-linux-9.3-backports
    - build-universaltest-linux-main
    - build-universaltest-win-32bit
    - build-universaltest-win-64bit
  variables:
    # WARNING! This variable name is used in ArchiveWithSubmodules.gitlab-ci.yml template
    PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}"
  before_script:
    - apk update
    - apk add curl
    - apk --no-cache add findutils
  script:
    - cd artifacts
    - >
      find . -type f -printf "%f\n" -exec curl --fail --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:
        - name: 'UniversalTest-x64.exe'
          url: "${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}/UniversalTest-x64.exe"
        - name: 'UniversalTest-x86.exe'
          url: "${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}/UniversalTest-x86.exe"
  rules:
    - if: $CI_COMMIT_TAG
      when: on_success
