# CI pipeline configuration

default:
   image: gmulas/np-tmcode

# Switch from branch pipelines to merge request pipelines when a merge request is created
workflow:
  rules:
    # Ensures next rules do not block triggered pipelines
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS && $CI_PIPELINE_SOURCE == "push"
      when: never
    # always run for merge requests
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    # never run a branch pipeline if there is an open merge request for that branch
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    # the only allowed branch pipeline is that on the default branch
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

stages:
   - compatibility
   - build
   - run
   - test

compatibility_stage:
   stage: compatibility
   tags: ["np-tmcode"]
   allow_failure: true
   artifacts:
      paths:
         - build_*
      exclude:
         - ".git*"
         - ".git/**/*"
      expire_in: 2 hours
   script:
      # bash commands to be executed
      - pwd
      - hostname
      - echo $CI_COMMIT_SHA
      - echo $CI_COMMIT_BRANCH
      - echo "Getting system info ..."
      - cat /etc/os-release
      - echo "Running make with gnu compilers version 11..."
      - mkdir build_gnu11
      - cd build_gnu11
      - cp -r ../build/* .
      - CXX=g++-11 F77=gfortran-11 ./configure
      - make clean
      - make -j
      - echo "Running make with gnu compilers version 12..."
      - cd ..
      - rm -rf build_gnu11
      - mkdir build_gnu12
      - cd build_gnu12
      - cp -r ../build/* .
      - CXX=g++-12 F77=gfortran-12 ./configure
      - make clean
      - make -j
      - echo "Running make with gnu compilers version 13..."
      - cd ..
      - rm -rf build_gnu12
      - mkdir build_gnu13
      - cd build_gnu13
      - cp -r ../build/* .
      - CXX=g++-13 F77=gfortran-13 ./configure
      - make clean
      - make -j
      - echo "Running make with gnu compilers version 14..."
      - cd ..
      - rm -rf build_gnu13
      - mkdir build_gnu14
      - cd build_gnu14
      - cp -r ../build/* .
      - CXX=g++-14 F77=gfortran-14 ./configure
      - make clean
      - make -j
      - echo "Running make with flang version 16 and clang version 16..."
      - cd ..
      - rm -rf build_gnu14
      - mkdir build_clang16
      - cd build_clang16
      - cp -r ../build/* .
      - CXX=clang++-16 F77=flang-new-16 ./configure --with-fflags="-O3" --with-include="-I/usr/include/c++/13 -I/usr/include/x86_64-linux-gnu/c++/13" --with-ldflags="-L/usr/lib/llvm-16/lib -L/usr/lib/gcc/x86_64-linux-gnu/13" --disable-openmp
      - make clean
      - make -j
      - echo "Running make with flang version 17 and clang version 17..."
      - cd ..
      - rm -rf build_clang16
      - mkdir build_clang17
      - cd build_clang17
      - cp -r ../build/* .
      - CXX=clang++-17 F77=flang-new-17 ./configure --with-fflags="-O3" --with-include="-I/usr/include/c++/13 -I/usr/include/x86_64-linux-gnu/c++/13" --with-ldflags="-L/usr/lib/llvm-17/lib -L/usr/lib/gcc/x86_64-linux-gnu/13"  --disable-openmp
      - make clean
      - make -j
      - echo "Running make with Intel ifort and Intel icpx..."
      - cd ..
      - rm -rf build_clang17
      - mkdir build_ifort_icpx
      - cd build_ifort_icpx
      - cp -r ../build/* .
      - export LD_LIBRARY_PATH=/opt/intel/oneapi/compiler/2024.2/lib
      - export PATH=/opt/intel/oneapi/compiler/2024.2/bin:$PATH
      - CXX=icpx F77=ifort ./configure --with-fflags="-O3 -diag-disable=10448" --disable-openmp
      - make clean
      - make -j
      - echo "Running make with Intel ifx and Intel icpx..."
      - cd ..
      - rm -rf build_ifort_icpx
      - mkdir build_ifx_icpx
      - cd build_ifx_icpx
      - cp -r ../build/* .
      - export LD_LIBRARY_PATH=/opt/intel/oneapi/compiler/2024.2/lib
      - export PATH=/opt/intel/oneapi/compiler/2024.2/bin:$PATH
      - CXX=icpx F77=ifx ./configure --with-fflags="-O3" --disable-openmp
      - make clean
      - make -j
   
building_stage:
   stage: build
   tags: ["np-tmcode"]
   allow_failure: false
   artifacts:
      paths:
         - build/cluster/*
         - build/sphere/*
         - build/testing/*
         - build/trapping/*
         - build/libnptm/*
         - doc/build/*
      exclude:
         - ".git*"
         - ".git/**/*"
      expire_in: 2 hours
   script:
      # bash commands to be executed
      - pwd
      - hostname
      - echo $CI_COMMIT_SHA
      - echo $CI_COMMIT_BRANCH
      - echo "Getting system info ..."
      - cat /etc/os-release
      - cd build
      - echo "Configuring with default compilers (MAGMA disabled)..."
      - ./configure --without-magma
      - make clean
      - echo "Building the default configuration..."
      - make -j
      - echo "Building the documentation..."
      - cd ../doc/src
      - doxygen config.dox
      - cd ../build/latex
      - make

running_stage:
   stage: run
   tags: ["np-tmcode"]
   allow_failure: false
   needs:
      - job: building_stage
   dependencies:
      - building_stage
   artifacts:
      paths:
         - build/cluster/c_*
         - build/sphere/c_*
         #- build/trapping/*
      exclude:
         - ".git*"
         - ".git/**/*"
      expire_in: 2 hours
   script:
      # bash commands to be executed
      - pwd
      - hostname
      - echo $CI_COMMIT_SHA
      - echo $CI_COMMIT_BRANCH
      - cd build/sphere
      - echo "Running np_sphere"
      - chmod +x np_sphere
      - ./np_sphere
      - cd ../cluster
      - echo "Running np_cluster"
      - chmod +x np_cluster
      - OMP_NUM_THREADS=1 ./np_cluster
      
testing_stage:
   stage: test
   tags: ["np-tmcode"]
   allow_failure: false
   needs:
      - job: building_stage
      - job: running_stage
   dependencies:
      - building_stage
      - running_stage
   artifacts:
      paths:
         - build/cluster/pycompare.html
         - build/sphere/pycompare.html
      exclude:
         - ".git*"
         - ".git/**/*"
      expire_in: 2 hours
   script:
      # bash commands to be executed
      - pwd
      - hostname
      - echo $CI_COMMIT_SHA
      - echo $CI_COMMIT_BRANCH
      - cd build/sphere
      - export FFILE=../../test_data/sphere/OSPH
      - echo "Comparing output of SPHERE"
      - python3 ../../src/scripts/pycompare.py --no-progress --ffile=$FFILE --cfile=c_OSPH --html
      - echo "Checking consistency among legacy and HDF5 configuration files"
      - ../testing/test_TEDF ../../test_data/sphere/DEDFB c_TEDF c_TEDF.hd5
      - cd ../cluster
      - echo "Comparing output of CLUSTER"
      - export FFILE=../../test_data/cluster/OCLU
      - python3 ../../src/scripts/pycompare.py --no-progress --ffile=$FFILE --cfile=c_OCLU --html
      - echo "Testing cluster with 24 spheres"
      - OMP_NUM_THREADS=1 OMPI_ALLOW_RUN_AS_ROOT=1 OMPI_ALLOW_RUN_AS_ROOT_CONFIRM=1 mpirun -n 2 ./np_cluster ../../test_data/cluster/DEDFB_24 ../../test_data/cluster/DCLU_24 .
      - echo "Comparing output of CLUSTER with 24 spheres"
      - export FFILE=../../test_data/cluster/OCLU_24
      - python3 ../../src/scripts/pycompare.py --no-progress --ffile=$FFILE --cfile=c_OCLU --html
      - echo "Checking consistency among legacy and HDF5 configuration files"
      - ../testing/test_TEDF ../../test_data/cluster/DEDFB_24 c_TEDF c_TEDF.hd5
      - echo "Checking consistency among legacy and HDF5 TM files"
      - ../testing/test_TTMS c_TTMS c_TTMS.hd5