#=============================================================================
# Azure Pipeline settings
#=============================================================================

# Nightly build master for pypi upload
schedules:
- cron: "0 0 * * *"
  branches:
    include:
    - master
  always: true

# Auto cancel old PR builds
pr:
  autoCancel: true
# TODO : https://github.com/neuronsimulator/nrn/issues/1063
#  paths:
#    exclude:
#      - docs
#      - README.md

# Trigger build for certain branches only
trigger:
- master
- release/*

stages:
  - stage: BuildTestDeploy
    jobs:

    - job: 'ManyLinuxWheels'
      timeoutInMinutes: 45
      pool:
        vmImage: 'ubuntu-24.04'
      strategy:
        matrix:
          Python310:
            python.version: '3.10'
            python.nodot: '310'
          Python311:
            python.version: '3.11'
            python.nodot: '311'
          Python312:
            python.version: '3.12'
            python.nodot: '312'
          Python313:
            python.version: '3.13'
            python.nodot: '313'
          Python314:
            python.version: '3.14'
            python.nodot: '314'

      steps:

      # Secure files documentation:
      #   https://docs.microsoft.com/en-us/azure/devops/pipelines/library/secure-files?view=azure-devops
      #   NOTE: when uploading new secure files, access must be permitted from the Azure pipeline interface (check message there)
      - task: DownloadSecureFile@1
        name: mpt_headersSF
        displayName: 'Download mpt_headers secure file'
        inputs:
          secureFile: 'mpt_headears.2.21.tar.gz'

      - script: |
          python3 -m pip install tomli tomli-w
          python3 packaging/python/change_name.py ./pyproject.toml neuron
        condition: and(in(variables['Build.Reason'], 'Manual'), eq(variables['NRN_RELEASE_UPLOAD'], 'true'))
        displayName: 'Change name of package for release'

      # Note that mpt headers must be mounted in the docker imager under `/nrnwheel/mpt`
      # This path is checked by `packaging/python/build_wheels.bash` when run in the image.
      - script: |
          if [ "$BUILD_REASON" = "Manual" ] || [ "$BUILD_REASON" = "Schedule" ]; then
            export NRN_RX3D_OPT_LEVEL=1
          fi
          sudo mkdir -p /opt/nrnwheel/mpt
          sudo tar -zxf $(mpt_headersSF.secureFilePath) --directory /opt/nrnwheel/mpt
          packaging/python/build_wheels.bash linux $(python.nodot)
        displayName: 'Building ManyLinux Wheel'

      - script: |
          sudo apt update
          sudo apt install -y mpich openmpi-bin libopenmpi-dev libmpich-dev
          # mpich is fundamentally broken on Ubuntu 24.04 LTS, for more details see:
          # https://bugs.launchpad.net/ubuntu/+source/mpich/+bug/2072338
          # This is fixed in version 4.2.0-5.1, but this version has not been
          # backported, so as a workaround, we install that version manually
          source /etc/os-release
          if [[ "$ID" == "ubuntu" && "$VERSION_ID" == "24.04" ]]; then
            # mpich is fundamentally broken on Ubuntu 24.04 LTS, for more details see:
            # https://bugs.launchpad.net/ubuntu/+source/mpich/+bug/2072338
            # This is fixed in version 4.2.0-5.1, but this version has not been
            # backported, so as a workaround, we install that version manually
            sudo apt install -y mpich
            wget 'https://launchpad.net/ubuntu/+source/mpich/4.2.0-5.1/+build/28285882/+files/mpich_4.2.0-5.1_amd64.deb'
            wget 'https://launchpad.net/ubuntu/+source/mpich/4.2.0-5.1/+build/28285882/+files/libmpich12_4.2.0-5.1_amd64.deb'
            sudo dpkg --install mpich_4.2.0-5.1_amd64.deb libmpich12_4.2.0-5.1_amd64.deb
          fi
        displayName: 'Install Test System Dependencies'

      - template: ci/azure-wheel-test-upload.yml


    # Jobs to build OSX wheels natively
    - job: 'MacOSWheels'
      timeoutInMinutes: 60
      pool:
        vmImage: 'macOS-15'
      strategy:
        matrix:
          Python310:
            python.version: '3.10'
            python.nodot: '310'
            python.org.version: '3.10.11'
            python.installer.name: 'macos11.pkg'
          Python311:
            python.version: '3.11'
            python.nodot: '311'
            python.org.version: '3.11.7'
            python.installer.name: 'macos11.pkg'
          Python312:
            python.version: '3.12'
            python.nodot: '312'
            python.org.version: '3.12.0'
            python.installer.name: 'macos11.pkg'
          Python313:
            python.version: '3.13'
            python.nodot: '313'
            python.org.version: '3.13.0'
            python.installer.name: 'macos11.pkg'
          Python314:
            python.version: '3.14'
            python.nodot: '314'
            python.org.version: '3.14.0'
            python.installer.name: 'macos11.pkg'

      steps:

      - script: |
          installer=python-$(python.org.version)-$(python.installer.name)
          url=https://www.python.org/ftp/python/$(python.org.version)/$installer
          curl $url -o $installer
          sudo installer -pkg $installer -target /
        displayName: 'Install Python from python.org'

      - script: |
          brew install --cask xquartz
          brew install flex bison mpich
          brew unlink mpich && brew install openmpi
          cmake --version
          # the version of bash used by Azure (which is the default one
          # provided by Apple) is too old and does not support associative
          # arrays. This is because newer versions of bash have switched their
          # license from GNU GPL v2 to GNU GPL v3, and Apple refuses to add any
          # GNU GPL v3-licenced software in their stack, so we need to install
          # the homebrew one
          brew install bash
          bash --version
          # see https://github.com/BlueBrain/CoreNeuron/issues/817, uninstall libomp until we fix this
          # as we are building wheels, we shouldn't enable OpenMP here anyway
          brew uninstall --ignore-dependencies libomp || echo "libomp doesn't exist"
        displayName: 'Install OSX System Dependencies'

      # readline has been manually built with ncurses and MACOSX_DEPLOYMENT_TARGET=10.9 and stored as secure file on Azure.
      # See `packaging/python/Dockerfile` for build instructions.
      #
      # Secure files documentation:
      #   https://docs.microsoft.com/en-us/azure/devops/pipelines/library/secure-files?view=azure-devops
      #   NOTE: when uploading new secure files, access must be permitted from the Azure pipeline interface (check message there)
      - task: DownloadSecureFile@1
        name: readlineSF
        displayName: 'Download readline secure file'
        inputs:
          secureFile: 'readline7.0-ncurses6.4.tar.gz'

      - script: |
          python3 -m pip install tomli tomli-w
          python3 packaging/python/change_name.py ./pyproject.toml neuron
        condition: and(in(variables['Build.Reason'], 'Manual'), eq(variables['NRN_RELEASE_UPLOAD'], 'true'))
        displayName: 'Change name of package for release'

      - script: |
          export PATH=/usr/local/opt/flex/bin:/usr/local/opt/bison/bin:$PATH
          export SDKROOT=$(xcrun --sdk macosx --show-sdk-path)
          if [ "$BUILD_REASON" = "Manual" ] || [ "$BUILD_REASON" = "Schedule" ]; then
            export NRN_RX3D_OPT_LEVEL=1
          fi
          sudo mkdir -p /opt/nrnwheel/$(uname -m)
          sudo tar -zxf $(readlineSF.secureFilePath) --directory /opt/nrnwheel/$(uname -m)
          packaging/python/build_wheels.bash osx $(python.nodot)
        displayName: 'Build MacOS Wheel'

      - template: ci/azure-wheel-test-upload.yml


  - stage: Final
    jobs:
      - job: AzureDropURL
        pool:
          vmImage: 'ubuntu-22.04'
        condition: eq(variables['Build.Reason'], 'PullRequest')
        steps:
          - checkout: none
          - script: |
              export AZURE_DROP_URL=`curl -v 'https://dev.azure.com/neuronsimulator/nrn/_apis/build/builds/$(Build.BuildId)/artifacts?artifactName=drop' | jq -r '.resource.downloadUrl'`
              echo "Setting dropurl to $AZURE_DROP_URL"
              echo "##vso[task.setvariable variable=dropurl]$AZURE_DROP_URL"
            displayName: 'Resolve Azure drop URL'
              
          - task: GitHubComment@0
            continueOnError: true
            inputs:
              gitHubConnection: 'neuronsimulator'
              repositoryName: '$(Build.Repository.Name)'
              comment: |
                ✔️ $(system.pullRequest.sourceCommitId) -> [Azure artifacts URL]($(dropurl))
