jobs:
- job: PyPi_Linux_x86_64
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - script: |
      docker run -e BUILD_NUM=$(Build.BuildNumber) -e PYTHON_VERSIONS="$(PYTHON_VERSIONS)" -v $(Build.Repository.LocalPath):/boolector quay.io/pypa/manylinux2014_x86_64 /boolector/pypi/build.sh
    displayName: 'Run PyBoolector Build'

  - publish: result/dist
    artifact: dist-linux-x86_64

- job: PyPi_Linux_ppc64le
  pool:
    vmImage: 'ubuntu-latest'
  timeoutInMinutes: 180 # Builds in emulation are slow
  steps:
  - script: |
      sudo docker run --rm --privileged aptman/qus -s -- -p ppc64le
    displayName: 'Use Qemu to emulate a ppc64le environment'

  - script: |
      docker run -e BUILD_NUM=$(Build.BuildNumber) -e PYTHON_VERSIONS="$(PYTHON_VERSIONS)" -v $(Build.Repository.LocalPath):/boolector quay.io/pypa/manylinux2014_ppc64le /boolector/pypi/build.sh
    displayName: 'Run PyBoolector Build'

  - publish: result/dist
    artifact: dist-linux-ppc64le

- job: PyPi_MacOS
  pool:
    vmImage: 'macos-latest'
  strategy:
    matrix:
      production:
        needCython: true
        btorConfig: ''
        btorConfigEnv: ''
  steps:
  - script: echo "##vso[task.setvariable variable=ncpus]$(sysctl -n hw.logicalcpu)"
    displayName: 'Determine number of CPUs'

  - script: sudo pip3 install cython wheel setuptools
    displayName: 'Install Cython'

  - script: |
      brew remove boost
      ./contrib/setup-btor2tools.sh
      echo "Content of deps/install/lib"
      ls deps/install/lib
    displayName: 'Setup btor2tools dependency'
  - script: |
      ./contrib/setup-cadical.sh
      echo "Content of deps/install/lib"
      ls deps/install/lib
    displayName: 'Setup cadical dependency'
  - script: |
      ./contrib/setup-lingeling.sh
      echo "Content of deps/install/lib"
      ls deps/install/lib
      file deps/install/lib/liblgl.a
    displayName: 'Setup lingeling dependency'

  - script: $(btorConfigEnv) ./configure.sh -fPIC --universal $(btorConfig)
    displayName: 'Configure Boolector'

  - script: |
      make VERBOSE=1 -j $(ncpus)
      make VERBOSE=1 install
    workingDirectory: 'build'
    displayName: 'Build Boolector'

  - script: |
      export BUILD_NUM=$(Build.BuildNumber)
      echo "Build.Repository.LocalPath/deps/install/lib:"
      ls $(Build.Repository.LocalPath)/deps/install/lib
      export LIBRARY_PATH=$(Build.Repository.LocalPath)/deps/install/lib:$(Build.Repository.LocalPath)/build/lib
      echo "LIBRARY_PATH: $LIBRARY_PATH"
      export PACKAGE_BUILD=1
      cp -r src/api/python pypi/src
      mkdir -p pypi/src/utils
      cp src/*.h pypi/src
      cp COPYING pypi/LICENSE
      cp src/utils/*.h pypi/src/utils
      grep VERSION CMakeLists.txt | grep set | grep -v ARCHIVE | sed -e 's%^.*\(".*"\).*$%\1%' -e 's/"//g' > pypi/version.txt
      cd pypi
      sed -e 's/override//g' -e 's/noexcept/_NOEXCEPT/g' -e 's/\(BoolectorException (const.*\)/\1    virtual ~BoolectorException() _NOEXCEPT {}/' src/pyboolector_abort.cpp > src/pyboolector_abort.cpp.new
      if test $? -ne 0; then exit 1; fi
      cp src/pyboolector_abort.cpp.new src/pyboolector_abort.cpp
      python3 src/mkenums.py ./src/btortypes.h src/pyboolector_enums.pxd
      if test $? -ne 0; then exit 1; fi
      python3 setup.py sdist bdist_wheel
      if test $? -ne 0; then exit 1; fi
    displayName: 'Build PyPi package'

  - publish: pypi/dist
    artifact: dist-macos

- job: PyPi_Upload
  dependsOn:
  - PyPi_Linux_x86_64
  - PyPi_Linux_ppc64le
  - PyPi_MacOS
  pool:
    vmImage: 'ubuntu-latest'
  condition: and(succeeded(), eq(variables['Build.SourceBranchName'], 'master'))
  steps:
  - download: current
    displayName: Download all build artifacts

  - task: TwineAuthenticate@1
    inputs:
      pythonUploadServiceConnection: pypi-pyboolector

  - script: |
      sudo pip3 install twine
      python3 -m twine --version
      echo "Files to upload:"
      ls -l $(Agent.BuildDirectory)/dist-*/*.whl $(Agent.BuildDirectory)/dist-*/*.tar.gz
      echo "Calling twine"
      python3 -m twine upload -r pyboolector --config-file $(PYPIRC_PATH) $(Agent.BuildDirectory)/dist-*/*.whl $(Agent.BuildDirectory)/dist-*/*.tar.gz
      echo "Calling twine complete"
    displayName: 'Upload to PyPi'
