name: TREXIO CI

on:
  push:
    branches:
      - master
    tags:
      # After vMajor.Minor.Patch _anything_ is allowed (without "/") !
      - v[0-9]+.[0-9]+.[0-9]+*
  pull_request:
    branches: [ master ]


jobs:

  get_commit_message:
    name: Get commit message
    runs-on: ubuntu-20.04
    outputs:
      message: ${{ steps.commit_message.outputs.message }}
    steps:
      - name: Checkout the repo
        uses: actions/checkout@e2f20e631ae6d7dd3b768f56a5d2af784dd54791
        # Gets the correct commit message for pull request
        with:
          ref: ${{ github.event.pull_request.head.sha }}
      - name: Get commit message
        id: commit_message
        run: |
          set -xe
          COMMIT_MSG=$(git log --no-merges -1 --oneline)
          echo "::set-output name=message::$COMMIT_MSG"

  trexio_ubuntu:
    name: x86 Ubuntu 20.04
    runs-on: ubuntu-20.04
    needs: get_commit_message

    steps:
      - uses: actions/checkout@e2f20e631ae6d7dd3b768f56a5d2af784dd54791

      - name: check versions
        run: |
          grep AC_INIT configure.ac | tr -d '[]' | awk -F, '{ print $2 }' > configure_v
          grep -w VERSION CMakeLists.txt | grep -v 'cmake_minimum_required' | awk '{ print $2 }' > cmake_v
          grep version python/pytrexio/_version.py | tr -d '"' | awk '{ print $3 }' > python_v
          grep version rust/trexio/Cargo.toml | grep -v features | tr -d '"' | awk '{ print $3 }' > rust_v
          grep version ocaml/trexio/dune-project | tr -d '()' | awk '{ print $2 }' > ocaml_v
          diff configure_v cmake_v 
          diff configure_v python_v
          diff configure_v rust_v
          diff configure_v ocaml_v

      - name: install dependencies
        run: |
          sudo add-apt-repository ppa:kelleyk/emacs
          sudo apt-get install libhdf5-dev emacs26

      - name: configure with autotools
        run: |
          ./autogen.sh
          ./configure --enable-silent-rules

      - name: compile TREXIO
        run: make -j2

      - name: check TREXIO
        run: make -j2 check

      - name: Archive test log file
        if: failure()
        uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2
        with:
          name: test-report-ubuntu
          path: test-suite.log

      - name: create virtual environment
        run: |
          python3 -m venv --clear pytrexio-venv
          source pytrexio-venv/bin/activate

      - name: install Python API
        run: make python-install

      - name: check Python API
        run: make python-test

      - name: build and move Python API distribution
        run: |
          make python-sdist
          cp python/dist/trexio-*.tar.gz .

      - name: publish Python API distribution as an artifact
        if: >-
          contains(needs.get_commit_message.outputs.message, '[wheel build]') ||
          github.event_name == 'release'
        uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2
        with:
          name: pytrexio-source
          path: ./trexio-*.tar.gz

      - name: maintainer clean
        run: make maintainer-clean

      - name: reconfigure with clang and AddressSanitizer
        run: |
          ./autogen.sh
          ./configure CC=clang-11 CFLAGS="-O2 -fsanitize=address -fno-omit-frame-pointer" LDFLAGS="-fsanitize=address" --enable-silent-rules

      - name: recompile TREXIO
        run: make -j2

      - name: recheck TREXIO for memory leaks
        run: make -j2 check

      - name: Archive test log file
        if: failure()
        uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2
        with:
          name: test-report-ubuntu-2
          path: test-suite.log

      - name: maintainer clean
        run: make maintainer-clean

  trexio_macos:
    name: x86 MacOS 12
    runs-on: macos-12

    steps:
      - uses: actions/checkout@e2f20e631ae6d7dd3b768f56a5d2af784dd54791

      - name: install dependencies
        run: |
          brew install emacs
          brew install hdf5
          brew install automake
          brew --prefix hdf5

      - name: configure with autotools
        run: |
          ./autogen.sh
          ./configure FC=gfortran-12 --enable-silent-rules

      - name: compile TREXIO
        run: make -j3

      - name: check TREXIO
        run: make -j3 check
      
      - name: compile Python API
        run: |
          export H5_CFLAGS="-I$(brew --prefix hdf5)/include"
          export H5_LDFLAGS="-L$(brew --prefix hdf5)/lib"
          make python-install
      
      - name: test Python API
        run: make python-test

      - name: Archive test log file
        if: failure()
        uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2
        with:
          name: test-report-macos
          path: test-suite.log

      - name: clean
        run: make clean
