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

variables:
    TANGO_VERSION: 9.4.1
    MSVC_VERSION: v141
    GENERATOR_VERSION: Visual Studio 17 2022
    DEPS_PATH: C:/dependencies
    LIBTANGO: libtango_${TANGO_VERSION}_${MSVC_VERSION}_${ARCH}_static_release
    TANGO_PATH: ${DEPS_PATH}/${LIBTANGO}
    DEBIAN_FRONTEND: noninteractive
    CMAKE_BUILD_TYPE: Debug
    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 --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.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 -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
      -DTANGO_USE_TELEMETRY=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-template: &build-template
  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-template-win:
  rules:
    - if: '$ARCH == "x64"'
      variables:
        DOWNLOAD_URL: "https://gitlab.com/tango-controls/cppTango/-/package_files/67360212/download"
    - if: '$ARCH == "x86"'
      variables:
        DOWNLOAD_URL: "https://gitlab.com/tango-controls/cppTango/-/package_files/67361143/download"
  before_script:
    - choco install cmake curl -y
    - 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 Release
    - cmake --install .
  after_script:
    - New-Item artifacts -ItemType Directory
    - if (Test-Path "C:\Program Files\starter\bin") { Move-Item -Path "C:\Program Files\starter\bin\Starter.exe" -Destination "artifacts\Starter-x64.exe"; }
    - if (Test-Path "C:\Program Files (x86)\starter\bin") { Move-Item -Path "C:\Program Files (x86)\starter\bin\Starter.exe" -Destination "artifacts\Starter-x86.exe"; }
  artifacts:
    paths:
      - artifacts
    expire_in: 1 day
  tags:
    - saas-windows-medium-amd64

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

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

build-main:
  <<: *build-template
  needs: ["build-cppTango-main"]
  script:
    - tar xf usr-local.tar -C /
    - cmake -DTANGO_WARNINGS_AS_ERRORS=ON -B build .
    - make -C build install

build-9.3-backports:
  <<: *build-template
  needs: ["build-cppTango-9.3-backports"]
  script:
    - tar xf usr-local.tar -C /
    - cmake -B build .
    - make -C build install

build-stock:
  <<: *build-template
  script:
    - apt-get install -y libtango-dev
    - cmake -B build .
    - make -C build install

build-main-newest-supported-cmake:
  <<: *build-template
  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-main-oldest-supported-cmake:
  <<: *build-template
  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
