---
name: Linux build and test
on:
  push:
    branches:
      - 'master'
      - 'stable/*'
  pull_request:
    branches:
      - '**'
  release:
    types:
      - created

jobs:
  test:
    name: Run tests
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-20.04, ubuntu-22.04]
        build_static: [true, false]
        flags: [ADD_CXXFLAGS=-fvisibility=hidden]
        download_requirements: [sudo apt install -y -qq gfortran liblapack-dev libmetis-dev libnauty2-dev]
        include:
          - os: macos-13
            build_static: false
            flags: CC=clang OSX=13
            download_requirements: brew install metis bash
          - os: macos-13
            build_static: false
            flags: CC=gcc-13 CXX=g++-13 OSX=13 ADD_CXXFLAGS=-Wl,-ld_classic
            download_requirements: brew install metis bash
          - os: macos-14
            arch: arm64
            build_static: false
            flags: CC=gcc-13 CXX=g++-13 OSX=14 ADD_CXXFLAGS=-Wl,-ld_classic
            download_requirements: brew install metis bash
    steps:
      - name: Checkout source
        uses: actions/checkout@v4
        with:
          path: ${{ github.event.repository.name }}
      - name: Install required packages from package manager
        run: ${{ matrix.download_requirements }}
      - name: Checkout coinbrew
        uses: actions/checkout@v4
        with:
          repository: coin-or/coinbrew
          path: coinbrew
      - name: Build project
        run: |
          export ${{ matrix.flags }}
          ADD_ARGS=()
          ADD_ARGS+=( --skip='ThirdParty/Metis ThirdParty/Mumps ThirdParty/Blas ThirdParty/Lapack' )
          ADD_BUILD_ARGS=()
          ADD_BUILD_ARGS+=( --tests main --enable-relocatable )
          ADD_BUILD_ARGS+=( --verbosity 2 )
          [[ ${{ matrix.build_static }} == "true" ]] && \
          ADD_BUILD_ARGS+=( --static --with-lapack='-llapack -lblas -lgfortran -lquadmath -lm' )
          bash coinbrew/coinbrew fetch ${{ github.event.repository.name }} --skip-update \
          "${ADD_ARGS[@]}"
          bash coinbrew/coinbrew build ${{ github.event.repository.name }} \
          "${ADD_ARGS[@]}" "${ADD_BUILD_ARGS[@]}" \
          ADD_CXXFLAGS="${ADD_CXXFLAGS}" CC=${CC} CXX=${CXX}
          [[ ${CC} ]] && CC="${CC}" || CC=""
          echo "CC=${CC}" >> $GITHUB_ENV
      - name: Archive dist contents
        run: |
          cp ${{ github.event.repository.name }}/README.md dist/
          cp ${{ github.event.repository.name }}/LICENSE dist/
          tar -czvf release.tar.gz -C dist .
      - name: Checkout package name generation script
        uses: actions/checkout@v4
        with:
          repository: coin-or-tools/platform-analysis-tools
          path: tools
          ref: 0.0.2
      - name: Retrieve platform info
        run: |
          python3 -m venv venv
          source venv/bin/activate
          pip install -r tools/requirements.txt
          [[ ${{ matrix.build_static }} == "true" ]] && buildtype=static || buildtype=
          platform_str=`python3 tools/hsf_get_platform.py -b $buildtype`
          echo "platform_string=${platform_str}" >> $GITHUB_ENV
      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ github.event.repository.name }}-${{ env.platform_string }}.tar.gz
          path: release.tar.gz
          if-no-files-found: error
      - name: Upload package to release
        if: ${{ github.event_name == 'release'}}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./release.tar.gz
          asset_name: ${{ github.event.repository.name }}-${{ github.event.release.tag_name }}-${{ env.platform_string }}.tar.gz
          asset_content_type: application/gzip