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

variables:
  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

# Not possible with parallel, see https://gitlab.com/gitlab-org/gitlab/-/issues/254821
.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 --branch 5.1.2 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 ${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 -j$(nproc) -C /cppTango/build install
    - tar cf usr-local.tar /usr/local
  artifacts:
    when: on_success
    paths:
      - usr-local.tar

# Not possible with parallel, see https://gitlab.com/gitlab-org/gitlab/-/issues/254821
.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 --depth 1 --branch 6.0.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 ${BRANCH} --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-cppTango-main:
  extends: .build-cppTango-main
  variables:
    BRANCH: main

build-cppTango-9.3-backports:
  extends: .build-cppTango-9.3-backports
  variables:
    BRANCH: 9.3-backports

.build-tangoadmin-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-tangoadmin-linux-main:
  extends: .build-tangoadmin-linux
  needs:
    - build-cppTango-main
  script:
    - tar xf usr-local.tar -C /
    - cmake -DTANGO_WARNINGS_AS_ERRORS=ON -B build .
    - make -j$(nproc) -C build install

build-tangoadmin-linux-9.3-backports:
  extends: .build-tangoadmin-linux
  needs:
    - build-cppTango-9.3-backports
  script:
    - tar xf usr-local.tar -C /
    - cmake -B build .
    - make -j$(nproc) -C build install

build-tangoadmin-linux-stock:
  extends: .build-tangoadmin-linux
  script:
    - apt-get install -y libtango-dev
    - cmake -B build .
    - make -j$(nproc) -C build install
