
variables:
  PREBUILD_CMAKE_VERSION: 3.18.2
  CMAKE_VERSION: 3.24.2

stages:
  - prebuild
  - build
  - test
  - package

# The boostrap build is too long for a job and you can build Cmake with Cmake but latest version requires at least Cmake 3.13
# Therefore we add job to prebuild a previous compatible Cmake with legacy system cmake to build after the final cmake
.prebuild:
  stage: prebuild
  script:
    - yum install -y openssl-devel
    # Get latest version of cmake available for i386 (cmake 3 not available for CentOS 7 x86) 
    - curl -s -OL https://cmake.org/files/v3.6/cmake-3.6.3-Linux-i386.tar.gz
    - tar -xzvf cmake-3.6.3-Linux-i386.tar.gz
    # Get source of a cmake buildable with latest version of cmake available for i386 
    - curl -s -OL https://github.com/Kitware/CMake/releases/download/v${PREBUILD_CMAKE_VERSION}/cmake-${PREBUILD_CMAKE_VERSION}-SHA-256.txt
    - curl -s -OL https://github.com/Kitware/CMake/releases/download/v${PREBUILD_CMAKE_VERSION}/cmake-${PREBUILD_CMAKE_VERSION}.tar.gz
    - echo "$(cat cmake-${PREBUILD_CMAKE_VERSION}-SHA-256.txt | grep cmake-${PREBUILD_CMAKE_VERSION}.tar.gz)" | sha256sum --check --status
    - tar -xzf cmake-${PREBUILD_CMAKE_VERSION}.tar.gz
    - mkdir build && cd build
    - ${CI_PROJECT_DIR}/cmake-3.6.3-Linux-i386/bin/cmake ../cmake-${PREBUILD_CMAKE_VERSION} -DCMAKE_INSTALL_PREFIX=${CI_PROJECT_DIR}/${NAME}
    - make
    - make install
  artifacts:
    name: ${NAME}
    paths:
      - ${NAME}

.build:
  stage: build
  script:
    - yum install -y openssl-devel
    - curl -s -OL https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}-SHA-256.txt
    - curl -s -OL https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}.tar.gz
    - echo "$(cat cmake-${CMAKE_VERSION}-SHA-256.txt | grep cmake-${CMAKE_VERSION}.tar.gz)" | sha256sum --check --status
    - tar -xzf cmake-${CMAKE_VERSION}.tar.gz
    - mkdir build && cd build
    - ${CI_PROJECT_DIR}/${PREBUILD_NAME}/bin/cmake ../cmake-${CMAKE_VERSION} -DCMAKE_INSTALL_PREFIX=${CI_PROJECT_DIR}/${NAME}
    - make
    - make install
  artifacts:
    name: ${NAME}
    paths:
      - ${NAME}

.test:
  stage: test
  script:
    - ${NAME}/bin/cmake --version | grep ${CMAKE_VERSION}

.package:
  stage: package
  script:
    - cd ${CI_PROJECT_DIR}/${NAME}
    - zip -r --symlinks ../${NAME}.zip *
    - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file ../${NAME}.zip "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/cmake/${CMAKE_VERSION}/"'

prebuild-el6-i686:
  extends: [".prebuild"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-el6-gcc73-x86
  variables:
    NAME: "cmake-${PREBUILD_CMAKE_VERSION}-linux-el6-i686"
    
linux-el6-i686:
  extends: [".build"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-el6-gcc73-x86
  variables:
    PREBUILD_NAME: "cmake-${PREBUILD_CMAKE_VERSION}-linux-el6-i686"
    NAME: "cmake-${CMAKE_VERSION}-linux-el6-i686"
  needs:
    - prebuild-el6-i686

test-el6-i686:
  extends: [".test"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-el6-gcc73-x86
  variables:
    NAME: "cmake-${CMAKE_VERSION}-linux-el6-i686"
  needs:
    - linux-el6-i686

package-el6-i686:
  extends: [".package"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-el6-gcc73-x86
  variables:
    NAME: "cmake-${CMAKE_VERSION}-linux-el6-i686"
  needs:
    - linux-el6-i686
    - test-el6-i686

prebuild-el7-i686:
  extends: [".prebuild"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-el7-gcc73-x86
  variables:
    NAME: "cmake-${PREBUILD_CMAKE_VERSION}-linux-el7-i686"

linux-el7-i686:
  extends: [".build"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-el7-gcc73-x86
  variables:
    PREBUILD_NAME: "cmake-${PREBUILD_CMAKE_VERSION}-linux-el7-i686"
    NAME: "cmake-${CMAKE_VERSION}-linux-el7-i686"
  needs:
    - prebuild-el7-i686

test-el7-i686:
  extends: [".test"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-el7-gcc73-x86
  variables:
    NAME: "cmake-${CMAKE_VERSION}-linux-el7-i686"
  needs:
    - linux-el7-i686

package-el7-i686:
  extends: [".package"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-el7-gcc73-x86
  variables:
    NAME: "cmake-${CMAKE_VERSION}-linux-el7-i686"
  needs:
    - linux-el7-i686
    - test-el7-i686
