variables:
    TANGO_VERSION: 9.4.1
    MYSQL_VERSION: "5.7"
    MYSQL_VERSION_PATCH: "36"
    MSVC_VERSION: v141
    GENERATOR_VERSION: Visual Studio 16 2019
    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

# 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

# Not possible with parallel, see https://gitlab.com/gitlab-org/gitlab/-/issues/254821
.build-cppTango-template: &build-cppTango-template
  image: debian:bullseye
  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 ${BRANCH} --depth 1 https://gitlab.com/tango-controls/cppTango.git /cppTango
    - >
      cmake -B /cppTango/build
      -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
      -DBUILD_TESTING=OFF
      -DTANGO_IDL_BASE=/usr/local/tango-idl
      -DIDL_BASE=/usr/local/tango-idl
      -DTANGO_USE_JPEG=OFF
      -DTANGO_USE_USING_NAMESPACE=OFF
      /cppTango
  script:
    - make -C /cppTango/build install
    - tar cf usr-local.tar /usr/local
  artifacts:
    when: on_success
    paths:
      - usr-local.tar

.build-template-win:
  rules:
    - if: '$ARCH == "x64"'
      variables:
        DOWNLOAD_URL: "https://gitlab.com/tango-controls/cppTango/-/package_files/67360212/download"
        MYSQL: "mysql-${MYSQL_VERSION}.${MYSQL_VERSION_PATCH}-winx64"
        MYSQL_PATH: "${DEPS_PATH}/${MYSQL}"
    - if: '$ARCH == "x86"'
      variables:
        DOWNLOAD_URL: "https://gitlab.com/tango-controls/cppTango/-/package_files/67361143/download"
        MYSQL: "mysql-${MYSQL_VERSION}.${MYSQL_VERSION_PATCH}-win32"
        MYSQL_PATH: "${DEPS_PATH}/${MYSQL}"
  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 -qSL "${DOWNLOAD_URL}" -o "${DEPS_PATH}/download"
    - 7z x "${DEPS_PATH}/download" -o"${DEPS_PATH}"
    - wget.exe "https://dev.mysql.com/get/Downloads/MySQL-${MYSQL_VERSION}/${MYSQL}.zip" -P "${DEPS_PATH}"
    - 7z x "${DEPS_PATH}/${MYSQL}.zip" -o"${DEPS_PATH}"
  script:
    - New-Item build -ItemType Directory
    - cd build
      # We set MySQL_VERSION to avoid a try_run invocation.  The binary
      # requires the VC redistributable which we do not have on the CI
    - cmake -G"${GENERATOR_VERSION}" -A"${PLATFORM}" ../TangoAccessControl -DTango_ROOT="${TANGO_PATH}" -DMySQL_ROOT="${MYSQL_PATH}" -DTango_FORCE_STATIC=ON -DMySQL_VERSION="${MYSQL_VERSION}.${MYSQL_VERSION_PATH}"
    - cmake --build . --config Release
    - cmake --install .
  after_script:
    - New-Item artifacts -ItemType Directory
    - if (Test-Path "C:\Program Files\TangoAccessControl\bin") { Move-Item -Path "C:\Program Files\TangoAccessControl\bin\TangoAccessControl.exe" -Destination "artifacts\TangoAccessControl-x64.exe"; }
    - if (Test-Path "C:\Program Files (x86)\TangoAccessControl\bin") { Move-Item -Path "C:\Program Files (x86)\TangoAccessControl\bin\TangoAccessControl.exe" -Destination "artifacts\TangoAccessControl-x86.exe"; }
  artifacts:
    paths:
      - artifacts
    expire_in: 1 day
  tags:
    - shared-windows

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

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


build-cppTango-main:
  variables:
    BRANCH: main
  <<: *build-cppTango-template

build-cppTango-9.3-backports:
  variables:
    BRANCH: 9.3-backports
  <<: *build-cppTango-template

.build-template: &build-template
  image: debian:bullseye
  before_script:
    - apt-get update
    - >
      apt-get install -y
      build-essential
      cmake
      git
      libcos4-dev
      libmariadb-dev-compat
      libomniorb4-dev
      libomnithread4-dev
      libzmq3-dev
      omniidl
      pkg-config
    - mkdir build

build-main:
  <<: *build-template
  needs: ["build-cppTango-main"]
  script:
    - tar xf usr-local.tar -C /
    - cmake -B build TangoAccessControl
    - 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 TangoAccessControl
    - make -C build install

build-stock:
  <<: *build-template
  script:
    - apt-get install -y libtango-dev
    - cmake -B build TangoAccessControl
    - 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 -B build TangoAccessControl
    - 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 -B build TangoAccessControl
    - make -C build install
