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

variables:
    TANGO_VERSION: 9.4.1
    MYSQL_VERSION: "5.7"
    MYSQL_VERSION_PATCH: "36"
    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 -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.1 --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-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
    - 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}" .. -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\TangoDatabase\bin") { Move-Item -Path "C:\Program Files\TangoDatabase\bin\Databaseds.exe" -Destination "artifacts\Databaseds-x64.exe"; }
    - if (Test-Path "C:\Program Files (x86)\TangoDatabase\bin") { Move-Item -Path "C:\Program Files (x86)\TangoDatabase\bin\Databaseds.exe" -Destination "artifacts\Databaseds-x86.exe"; }
  artifacts:
    paths:
      - artifacts
    expire_in: 1 day
  tags:
    - saas-windows-medium-amd64

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-template: &build-template
  tags:
    - amd64
    - docker
    - linux
  image: debian:bullseye
  before_script:
    - apt-get update
    - >
      apt-get install -y
      build-essential
      cmake
      git
      libcos4-dev
      libmariadb-dev
      libomniorb4-dev
      libomnithread4-dev
      libzmq3-dev
      omniidl
      pkg-config
    - mkdir build

build-main:
  needs: ["build-cppTango-main"]
  tags:
    - amd64
    - docker
    - linux
  image: debian:bullseye
  before_script:
    - apt-get update
    - >
      apt-get install -y
      build-essential
      cmake
      git
      libmariadb-dev
      libzmq3-dev
      pkg-config
    - mkdir build
  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

run-tests-with-mariadb:
  tags:
    - amd64
    - docker
    - linux
  image: debian:bullseye
  before_script:
    - apt-get update
    - >
      apt-get install -y
      build-essential
      cmake
      libboost-test-dev
      libtango-dev
      libmariadb-dev-compat
      mariadb-server
      python3-pip
      python-is-python3
      sudo
    - pip install gcovr==5.1
    - service mariadb restart
    - sudo mysql -u root -e "SET PASSWORD = PASSWORD('secret');"
    - echo "TANGO_HOST=127.0.0.1:10000" > /etc/tangorc
  script:
    - mkdir build
    - cd build
    - cmake -DTDB_ENABLE_COVERAGE=ON -DBUILD_TESTING=ON -DMYSQL_ADMIN=root -DMYSQL_ADMIN_PASSWD=secret ..
    - make
    - ctest --output-on-failure
    # coverage
    - mkdir coverage
    - cd coverage
    - >
      gcovr
      --cobertura coverage.xml
      --html-details report.html
      --html-self-contained
      --object-directory ${CI_PROJECT_DIR}/build/CMakeFiles/Databaseds.dir
      --root ${CI_PROJECT_DIR}
      --txt
  artifacts:
    when: always
    reports:
      junit: build/JU_*.xml
      coverage_report:
        coverage_format: cobertura
        path: build/coverage/coverage.xml
    expose_as: 'coverage report'
    paths: ['build/coverage/']
  coverage: '/^TOTAL.*\s+(\d+\%)$/'

run-tests-with-mysql:
  tags:
    - amd64
    - docker
    - linux
  image: ubuntu:lunar
  before_script:
    - apt-get update
    - >
      apt-get install -y
      build-essential
      cmake
      libboost-test-dev
      libtango-dev
      libmysqlclient-dev
      mysql-server
      sudo
    # work around https://gitlab.com/tango-controls/TangoDatabase/-/issues/43
    - echo -e '[mysqld]\ncharacter_set_server=latin1\ncollation_server=latin1_swedish_ci' > /etc/mysql/mysql.conf.d/tango.cnf
    - service mysql restart
    - sudo mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'secret';"
    - echo "TANGO_HOST=127.0.0.1:10000" > /etc/tangorc
  script:
    - mkdir build
    - cd build
    - cmake -DBUILD_TESTING=ON -DMYSQL_ADMIN=root -DMYSQL_ADMIN_PASSWD=secret ..
    - make
    - ctest --output-on-failure
  artifacts:
    reports:
      junit: build/JU_*.xml
