# Gmsh - Copyright (C) 1997-2024 C. Geuzaine, J.-F. Remacle
#
# See the LICENSE.txt file in the Gmsh root directory for license information.
# Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.

variables:
  EXTRA_OPTION: ""

.ssh_config: &ssh_config
  before_script:
    - echo "$SSH_TOKEN" > ~/.ssh/id_rsa
    - echo "Host *" > ~/.ssh/config
    - echo "StrictHostKeyChecking no" >> ~/.ssh/config
    - chmod 700 ~/.ssh/id_rsa ~/.ssh/config

# ----------------------------------------------
# Continuous integration builds for all branches
# ----------------------------------------------

linux_ci:
  image: onelab/ubuntu20.04
  script:
    - mkdir build
    - cd build
    - export CXXFLAGS=-Werror
    - cmake ..
    - make -j 8
    - make doc
    - ctest -j 8 --output-on-failure
    - valgrind --leak-check=full --error-exitcode=1 ./gmsh ../tutorials/t5.geo -3
  tags:
    - linux64
    - docker
  except:
    - tags

linux_compatibility_ci:
  image: onelab/ubuntu20.04
  script:
    - mkdir build
    - cd build
    - cmake -DDEFAULT=0 ..
    - make -j 8
    - cmake -DENABLE_PARSER=1 ..
    - make -j 8
    - cmake -DENABLE_POST=1 ..
    - make -j 8
    - cmake -DENABLE_MESH=1 -DENABLE_BUILD_DYNAMIC=1 -DENABLE_PRIVATE_API=1 -DENABLE_EIGEN=0 -DENABLE_WRAP_PYTHON=1 -DENABLE_PETSC=1 ..
    - make -j 8
  tags:
    - linux64
    - docker
  except:
    - tags

windows_ci:
  script:
    - md build
    - cd build
    - c:\cygwin64\bin\bash -c "/usr/bin/cmake -DCMAKE_PREFIX_PATH='/usr/local/opencascade;/usr/local;/usr/x86_64-w64-mingw32/sys-root/mingw' -DCMAKE_C_COMPILER=/usr/bin/x86_64-w64-mingw32-gcc.exe -DCMAKE_CXX_COMPILER=/usr/bin/x86_64-w64-mingw32-g++.exe -DCMAKE_RC_COMPILER=/usr/bin/x86_64-w64-mingw32-windres.exe -DENABLE_OS_SPECIFIC_INSTALL=1 .. && ls"
    - c:\cygwin64\bin\bash -c "/usr/bin/make -j 8"
    - c:\cygwin64\bin\bash -c "/usr/bin/ctest -j 8 --output-on-failure && ls"
  tags:
    - windows64
    - shared
  except:
    - tags

windows_msvc_ci:
  script:
    - md build
    - cd build
    - cmake -DENABLE_OPENMP=0 ..
    - msbuild package.vcxproj
  tags:
    - windows64
    - shared
  except:
    - tags

# ------------------------------------------
# Official Linux builds (master branch only)
# ------------------------------------------

.linux_official: &linux_official
  only:
    - master@gmsh/gmsh
  <<: *ssh_config
  script:
    - mkdir build
    - cd build
    - cmake -DGMSH_HOST=gmsh.info -DENABLE_PETSC=1 -DPETSC_ARCH=real_mumps_seq -DPETSC_DIR=/petsc-3.14.4 ${EXTRA_OPTION} ..
    - make package -j 8
    - PKG=`ls gmsh-*.tar*`
    - scp ${PKG} geuzaine@gmsh.info:.wwwgmsh/bin/Linux/${PKG/\.tar\.gz/\.tgz}
    - ctest -j 8 --output-on-failure
  tags:
    - linux64
    - docker

linux_official_snapshot:
  image: onelab/debian.stretch.64bit
  variables:
    EXTRA_OPTION: "-DCMAKE_EXE_LINKER_FLAGS=-static-libstdc++"
  <<: *linux_official
  except:
    - tags

linux_official_release:
  image: onelab/debian.stretch.64bit
  variables:
    EXTRA_OPTION: "-DGMSH_RELEASE=1 -DCMAKE_EXE_LINKER_FLAGS=-static-libstdc++"
  <<: *linux_official
  only:
    - /^gmsh_.*$/

linux-sdk_official_snapshot:
  image: onelab/debian.stretch.64bit
  variables:
    EXTRA_OPTION: "-DENABLE_BUILD_DYNAMIC=1 -DINSTALL_SDK_README=1"
  <<: *linux_official
  except:
    - tags
  artifacts:
    paths:
      - build/_CPack_Packages/
      - build/version.txt
    expire_in: 1day

linux-sdk_official_release:
  image: onelab/debian.stretch.64bit
  variables:
    EXTRA_OPTION: "-DGMSH_RELEASE=1 -DENABLE_BUILD_DYNAMIC=1 -DINSTALL_SDK_README=1"
  <<: *linux_official
  only:
    - /^gmsh_.*$/
  artifacts:
    paths:
      - build/_CPack_Packages/
      - build/version.txt
    expire_in: 1day

linux-nox-sdk_official_snapshot:
  image: onelab/debian.stretch.64bit
  variables:
    EXTRA_OPTION: "-DGMSH_EXTRA_VERSION=-nox -DENABLE_FLTK=0 -DENABLE_OCC_CAF=0 -DENABLE_BUILD_DYNAMIC=1 -DINSTALL_SDK_README=1"
  <<: *linux_official
  except:
    - tags
  artifacts:
    paths:
      - build/_CPack_Packages/
    expire_in: 1day

# --------------------------------------------
# Official Windows builds (master branch only)
# --------------------------------------------

.windows_official: &windows_official
  only:
    - master@gmsh/gmsh
  script:
    - md build
    - cd build
    - c:\cygwin64\bin\bash -c "/usr/bin/cmake -DGMSH_HOST=gmsh.info -DCMAKE_PREFIX_PATH='/usr/local;/usr/x86_64-w64-mingw32/sys-root/mingw' -DCMAKE_C_COMPILER=/usr/bin/x86_64-w64-mingw32-gcc.exe -DCMAKE_CXX_COMPILER=/usr/bin/x86_64-w64-mingw32-g++.exe -DCMAKE_Fortran_COMPILER=/usr/bin/x86_64-w64-mingw32-gfortran.exe -DCMAKE_RC_COMPILER=/usr/bin/x86_64-w64-mingw32-windres.exe -DENABLE_OS_SPECIFIC_INSTALL=1 -DENABLE_PETSC=1 -DPETSC_ARCH=real_mumps_seq -DPETSC_DIR=/home/geuzaine/src/petsc ${EXTRA_OPTION} .."
    - c:\cygwin64\bin\bash -c "/usr/bin/make package -j 4"
    - c:\cygwin64\bin\bash -c "/usr/bin/scp -o StrictHostKeyChecking=no -i /home/geuzaine/.ssh/id_rsa gmsh-*.zip geuzaine@gmsh.info:.wwwgmsh/bin/Windows/"
    - c:\cygwin64\bin\bash -c "/usr/bin/ctest -j 4 --output-on-failure"

windows_official_snapshot:
  <<: *windows_official
  tags:
    - windows64
    - official
  except:
    - tags

windows_official_release:
  variables:
    EXTRA_OPTION: "-DGMSH_RELEASE=1"
  <<: *windows_official
  tags:
    - windows64
    - official
  only:
    - /^gmsh_.*$/

windows-sdk_official_snapshot:
  variables:
    EXTRA_OPTION: "-DENABLE_OS_SPECIFIC_INSTALL=0 -DENABLE_BUILD_DYNAMIC=1 -DINSTALL_SDK_README=1"
  <<: *windows_official
  tags:
    - windows64
    - official
  except:
    - tags
  artifacts:
    paths:
      - build/_CPack_Packages/
    expire_in: 1day

windows-sdk_official_release:
  variables:
    EXTRA_OPTION: "-DGMSH_RELEASE=1 -DENABLE_OS_SPECIFIC_INSTALL=0 -DENABLE_BUILD_DYNAMIC=1 -DINSTALL_SDK_README=1"
  <<: *windows_official
  tags:
    - windows64
    - official
  only:
    - /^gmsh_.*$/
  artifacts:
    paths:
      - build/_CPack_Packages/
    expire_in: 1day

# ------------------------------------------
# Official macOS builds (master branch only)
# ------------------------------------------

.macos_official: &macos_official
  only:
    - master@gmsh/gmsh
  script:
    - mkdir build
    - cd build
    - cmake -DGMSH_HOST=gmsh.info -DENABLE_CAIRO=0 -DENABLE_OS_SPECIFIC_INSTALL=1 -DENABLE_PETSC=1 -DPETSC_ARCH=real_mumps_seq -DPETSC_DIR=/Users/geuzaine/src/petsc ${EXTRA_OPTION} ..
    - make package -j 4
    - PKG=`ls gmsh-*.[dt][ma][gr]*`
    - '[[ ${PKG} == *.dmg ]] && xcrun notarytool submit ${PKG} --key /Users/geuzaine/AuthKey_4R6P5NYF3T.p8 --key-id 4R6P5NYF3T --issuer 69a6de7c-0b3a-47e3-e053-5b8c7c11a4d1 --wait'
    - '[[ ${PKG} == *.dmg ]] && xcrun stapler staple ${PKG}'
    - scp ${PKG} geuzaine@gmsh.info:.wwwgmsh/bin/macOS/${PKG/\.tar\.gz/\.tgz}
    - ctest -j 4 --output-on-failure

macosx_official_snapshot:
  <<: *macos_official
  tags:
    - macos64
    - official
  except:
    - tags

macosx_official_release:
  variables:
    EXTRA_OPTION: "-DGMSH_RELEASE=1"
  <<: *macos_official
  tags:
    - macos64
    - official
  only:
    - /^gmsh_.*$/

macosx-sdk_official_snapshot:
  variables:
    EXTRA_OPTION: "-DENABLE_OS_SPECIFIC_INSTALL=0 -DENABLE_BUILD_DYNAMIC=1 -DINSTALL_SDK_README=1"
  <<: *macos_official
  tags:
    - macos64
    - official
  except:
    - tags
  artifacts:
    paths:
      - build/_CPack_Packages/
    expire_in: 1day

macosx-sdk_official_release:
  variables:
    EXTRA_OPTION: "-DGMSH_RELEASE=1 -DENABLE_OS_SPECIFIC_INSTALL=0 -DENABLE_BUILD_DYNAMIC=1 -DINSTALL_SDK_README=1"
  <<: *macos_official
  tags:
    - macos64
    - official
  only:
    - /^gmsh_.*$/
  artifacts:
    paths:
      - build/_CPack_Packages/
    expire_in: 1day

macosarm_official_snapshot:
  <<: *macos_official
  tags:
    - macos64arm
    - official
  except:
    - tags

macosarm_official_release:
  variables:
    EXTRA_OPTION: "-DGMSH_RELEASE=1"
  <<: *macos_official
  tags:
    - macos64arm
    - official
  only:
    - /^gmsh_.*$/

macosarm-sdk_official_snapshot:
  variables:
    EXTRA_OPTION: "-DENABLE_OS_SPECIFIC_INSTALL=0 -DENABLE_BUILD_DYNAMIC=1 -DINSTALL_SDK_README=1"
  <<: *macos_official
  tags:
    - macos64arm
    - official
  except:
    - tags
  artifacts:
    paths:
      - build/_CPack_Packages/
    expire_in: 1day

macosarm-sdk_official_release:
  variables:
    EXTRA_OPTION: "-DGMSH_RELEASE=1 -DENABLE_OS_SPECIFIC_INSTALL=0 -DENABLE_BUILD_DYNAMIC=1 -DINSTALL_SDK_README=1"
  <<: *macos_official
  tags:
    - macos64arm
    - official
  only:
    - /^gmsh_.*$/
  artifacts:
    paths:
      - build/_CPack_Packages/
    expire_in: 1day

# ----------------------------------------------
# Official source snapshots (master branch only)
# ----------------------------------------------

.source_official: &source_official
  image: onelab/ubuntu20.04
  only:
    - master@gmsh/gmsh
  <<: *ssh_config
  script:
    - mkdir build_src
    - cd build_src
    - cmake ${EXTRA_OPTION} ..
    - make package_source
    - PKG=`ls gmsh-*.tar*`
    - scp ${PKG} geuzaine@gmsh.info:.wwwgmsh/src/${PKG/\.tar\.gz/\.tgz}
  tags:
    - linux64
    - docker

source_official_snapshot:
  <<: *source_official
  except:
    - tags

source_official_release:
  variables:
    EXTRA_OPTION: "-DGMSH_RELEASE=1"
  <<: *source_official
  only:
    - /^gmsh_.*$/

# ----------------------------------------------
# Official documentation (master branch only)
# ----------------------------------------------

doc_official_snapshot:
  stage: .post
  image: onelab/ubuntu20.04
  only:
    - master@gmsh/gmsh
  <<: *ssh_config
  script:
    - mkdir build_doc
    - cd build_doc
    - cmake ..
    - make doc
    - scp gmsh-*-doc.tgz geuzaine@gmsh.info:.wwwgmsh/doc.tgz
    - ssh geuzaine@gmsh.info "cd .wwwgmsh/dev && tar zxvf ../doc.tgz"
  tags:
    - linux64
    - docker
  except:
    - tags

doc_official_release:
  stage: .post
  image: onelab/ubuntu20.04
  only:
    - master@gmsh/gmsh
  <<: *ssh_config
  script:
    - mkdir build_doc
    - cd build_doc
    - cmake -DGMSH_RELEASE=1 ..
    - make doc
    - scp gmsh-*-doc.tgz geuzaine@gmsh.info:.wwwgmsh/doc.tgz
    - ssh geuzaine@gmsh.info "cd .wwwgmsh && tar zxvf doc.tgz"
    - scp ../doc/gmsh.html geuzaine@gmsh.info:.wwwgmsh/
  tags:
    - linux64
    - docker
  only:
    - /^gmsh_.*$/

# ----------------------------------------------
# Cookbook
# ----------------------------------------------

doc_cookbook:
  stage: .post
  image: onelab/ubuntu20.04
  only:
    - gmsh/gmsh
  <<: *ssh_config
  script:
    - mdbook build doc/cookbook
    - scp -r doc/cookbook/book geuzaine@gmsh.info:.wwwgmsh/doc/cookbook
  tags:
    - linux64
    - docker

# ----------------------------------------------
# PyPi package
# ----------------------------------------------

pypi_official_snapshot:
  stage: .post
  image: onelab/ubuntu20.04
  only:
    - master@gmsh/gmsh
  <<: *ssh_config
  dependencies:
    - linux-sdk_official_snapshot
    - linux-nox-sdk_official_snapshot
    - windows-sdk_official_snapshot
    - macosx-sdk_official_snapshot
    - macosarm-sdk_official_snapshot
  script:
    - cd build
    - python3 ../utils/pypi/sdktowheel.py _CPack_Packages/Linux/TGZ/gmsh-git-Linux64-sdk/ manylinux_2_24_x86_64
    - python3 ../utils/pypi/sdktowheel.py _CPack_Packages/Linux/TGZ/gmsh-nox-git-Linux64-sdk/ manylinux_2_24_x86_64
    - python3 ../utils/pypi/sdktowheel.py _CPack_Packages/CYGWIN/ZIP/gmsh-git-Windows64-sdk/ win_amd64
    - python3 ../utils/pypi/sdktowheel.py _CPack_Packages/Darwin/TGZ/gmsh-git-MacOSX-sdk/ macosx_10_15_x86_64
    - python3 ../utils/pypi/sdktowheel.py _CPack_Packages/Darwin/TGZ/gmsh-git-MacOSARM-sdk/ macosx_12_0_arm64
    - scp gmsh-*.dev1+nox*.whl geuzaine@gmsh.info:.wwwgmsh/python-packages-dev-nox/gmsh/
    - scp gmsh-*.dev1-*.whl geuzaine@gmsh.info:.wwwgmsh/python-packages-dev/gmsh/
  tags:
    - linux64
    - docker
  except:
    - tags

pypi_official_release:
  stage: .post
  image: onelab/ubuntu20.04
  only:
    - master@gmsh/gmsh
  <<: *ssh_config
  dependencies:
    - linux-sdk_official_release
    - windows-sdk_official_release
    - macosx-sdk_official_release
    - macosarm-sdk_official_release
  script:
    - echo "[distutils]" > ~/.pypirc
    - echo "index-servers = pypi" >> ~/.pypirc
    - echo "[pypi]" >> ~/.pypirc
    - echo "username = __token__" >> ~/.pypirc
    - echo "password = $PYPI_TOKEN" >> ~/.pypirc
    - cd build
    - VERSION=`cat version.txt`
    - python3 ../utils/pypi/sdktowheel.py _CPack_Packages/Linux/TGZ/gmsh-${VERSION}-Linux64-sdk/ manylinux_2_24_x86_64
    - python3 ../utils/pypi/sdktowheel.py _CPack_Packages/CYGWIN/ZIP/gmsh-${VERSION}-Windows64-sdk/ win_amd64
    - python3 ../utils/pypi/sdktowheel.py _CPack_Packages/Darwin/TGZ/gmsh-${VERSION}-MacOSX-sdk/ macosx_10_15_x86_64
    - python3 ../utils/pypi/sdktowheel.py _CPack_Packages/Darwin/TGZ/gmsh-${VERSION}-MacOSARM-sdk/ macosx_12_0_arm64
    - scp gmsh*.whl geuzaine@gmsh.info:.wwwgmsh/python-packages/gmsh/
    - twine upload gmsh*.whl
    - ssh geuzaine@gmsh.info "cd .wwwgmsh/bin/Linux && rm -f gmsh-stable-Linux64* && ln -s gmsh-${VERSION}-Linux64.tgz gmsh-stable-Linux64.tgz && ln -s gmsh-${VERSION}-Linux64-sdk.tgz gmsh-stable-Linux64-sdk.tgz"
    - ssh geuzaine@gmsh.info "cd .wwwgmsh/bin/Windows && rm -f gmsh-stable-Windows64* && ln -s gmsh-${VERSION}-Windows64.zip gmsh-stable-Windows64.zip && ln -s gmsh-${VERSION}-Windows64-sdk.zip gmsh-stable-Windows64-sdk.zip"
    - ssh geuzaine@gmsh.info "cd .wwwgmsh/bin/macOS && rm -f gmsh-stable-MacOSX* && ln -s gmsh-${VERSION}-MacOSX.dmg gmsh-stable-MacOSX.dmg && ln -s gmsh-${VERSION}-MacOSX-sdk.tgz gmsh-stable-MacOSX-sdk.tgz"
    - ssh geuzaine@gmsh.info "cd .wwwgmsh/bin/macOS && rm -f gmsh-stable-MacOSARM* && ln -s gmsh-${VERSION}-MacOSARM.dmg gmsh-stable-MacOSARM.dmg && ln -s gmsh-${VERSION}-MacOSARM-sdk.tgz gmsh-stable-MacOSARM-sdk.tgz"
    - ssh geuzaine@gmsh.info "cd .wwwgmsh/src && rm -f gmsh-stable-source.tgz && ln -s gmsh-${VERSION}-source.tgz gmsh-stable-source.tgz"
  tags:
    - linux64
    - docker
  only:
    - /^gmsh_.*$/
