name: Linux

on:
   push:
      branches: [ RC_1_2 RC_2_0 master ]
   pull_request:

concurrency:
   group: ${{ github.ref }}-${{ github.workflow }}-${{ github.event_name }}
   cancel-in-progress: true

jobs:

   pre-commit:
      # TODO: matrix across python version and os
      runs-on: ubuntu-latest
      steps:
      - uses: actions/checkout@v2
        with:
           submodules: recursive
      - uses: actions/setup-python@v2
        with:
           python-version: 3.7
      - uses: pre-commit/action@v2.0.0

   build:
      name: build
      runs-on: ubuntu-20.04
      continue-on-error: true

      strategy:
         matrix:
            config: [ asio-debugging=on picker-debugging=on, extensions=off logging=off streaming=off super-seeding=off share-mode=off predictive-pieces=off dht=off alert-msg=off encryption=off mutable-torrents=off deprecated-functions=off, crypto=gcrypt, mmap-disk-io=off ]

      steps:
      - name: checkout
        uses: actions/checkout@v2
        with:
           submodules: true

      - name: update package lists
        continue-on-error: true
        run: |
          sudo apt update

      - uses: Chocobo1/setup-ccache-action@v1
        with:
          update_packager_index: false
          override_cache_key: ccache-linux-build-${{ github.base_ref }}-${{ matrix.config }}
          ccache_options: |
            max_size=5G

      - name: install boost
        run: |
          sudo apt install libboost-tools-dev libboost-dev libboost-system-dev
          echo "using gcc ;" >>~/user-config.jam

      - name: install gcrypt
        if: ${{ contains(matrix.config, 'crypto=gcrypt') }}
        run: sudo apt install libgcrypt20-dev

      - name: build library
        run: |
          b2 ${{ matrix.config }} cxxstd=14

      - name: build examples
        run: |
          cd examples
          b2 ${{ matrix.config }}

      - name: build tools
        run: |
          cd tools
          b2 ${{ matrix.config }} warnings-as-errors=on



   fuzzers:
      name: Fuzzers
      runs-on: ubuntu-20.04
      continue-on-error: true

      steps:
      - name: checkout
        uses: actions/checkout@v2
        with:
           submodules: true

      - name: update package lists
        continue-on-error: true
        run: |
          sudo apt update

      - uses: Chocobo1/setup-ccache-action@v1
        with:
          update_packager_index: false
          override_cache_key: ccache-linux-fuzzers-${{ github.base_ref }}
          ccache_options: |
            max_size=500M

      - name: install clang-9
        continue-on-error: true
        run: |
          sudo apt install clang-9

      - name: install boost
        run: |
          sudo apt install libboost-tools-dev libboost-dev libboost-system-dev
          echo "using clang : 9 : clang++-9 ;" >>~/user-config.jam

      - name: build fuzzers
        run: |
          cd fuzzers
          b2 clang cxxstd=14 warnings-as-errors=on



   check_headers:
      name: check headers
      runs-on: ubuntu-20.04

      steps:
      - name: checkout
        uses: actions/checkout@v2
        with:
           submodules: true

      - name: update package lists
        continue-on-error: true
        run: |
          sudo apt update

      - uses: Chocobo1/setup-ccache-action@v1
        with:
          update_packager_index: false
          override_cache_key: ccache-linux-check-headers-${{ github.base_ref }}
          ccache_options: |
            max_size=500M

      - name: install boost
        run: |
          sudo apt install libboost-tools-dev libboost-dev libboost-system-dev
          echo "using gcc ;" >>~/user-config.jam

      - name: compile header files individually
        run: |
          b2 check-headers cxxstd=14 warnings-as-errors=on



   clang_tidy:
      name: clang-tidy
      runs-on: ubuntu-20.04

      steps:
      - name: checkout
        uses: actions/checkout@v2
        with:
           submodules: true

      - name: update package lists
        continue-on-error: true
        run: |
          sudo apt update

      - uses: Chocobo1/setup-ccache-action@v1
        with:
          update_packager_index: false
          override_cache_key: ccache-linux-clang-tidy-${{ github.base_ref }}
          ccache_options: |
            max_size=500M

      - name: install clang-tidy
        run: sudo apt install clang-tidy libc++-dev

      - name: install boost
        run: |
          sudo apt install libboost-tools-dev libboost-dev
          echo "using clang_tidy : : clang-tidy \"-checks=-clang-analyzer-core.*,-clang-analyzer-unix.*\" : <cxxflags>-std=c++14 <cxxflags>-I/usr/local/clang-7.0.0/include/c++/v1 <cxxflags>-stdlib=libc++ <linkflags>-stdlib=libc++ ;" >> ~/user-config.jam;

      - name: analyze
        run: |
          b2 -a clang_tidy



   test:
      name: Tests
      runs-on: ubuntu-20.04
      continue-on-error: true

      strategy:
         matrix:
            include:
               - config: address-sanitizer=norecover undefined-sanitizer=norecover crypto=openssl
               - config: toolset=clang logging=off address-sanitizer=norecover undefined-sanitizer=norecover
               - config: thread-sanitizer=norecover crypto=openssl release debug-symbols=on
               - config: crypto=gnutls
               - config: deprecated-functions=off

      steps:
      - name: checkout
        uses: actions/checkout@v2
        with:
           submodules: true

      - name: update package lists
        continue-on-error: true
        run: |
          sudo apt update

      - uses: Chocobo1/setup-ccache-action@v1
        with:
          update_packager_index: false
          override_cache_key: ccache-linux-tests-${{ matrix.config }}-${{ github.base_ref }}
          ccache_options: |
            max_size=5G

      - name: install gnutls
        if: ${{ contains(matrix.config, 'crypto=gnutls') }}
        run: |
          sudo apt install libgnutls28-dev

      - name: install clang-10
        continue-on-error: true
        run: |
          sudo apt install clang-10

      - name: install boost
        run: |
          sudo apt install libboost-tools-dev libboost-dev libboost-system-dev
          echo "using gcc ;" >>~/user-config.jam
          echo "using clang : 10 : clang++-10 ;" >>~/user-config.jam

      - name: build and run tests
        run: |
          cd test
          b2 ${{ matrix.config }} -l500 warnings-as-errors=on debug-iterators=on invariant-checks=full asserts=on deterministic-tests

      - name: run tests (flaky)
        uses: nick-invision/retry@v2
        with:
          timeout_minutes: 30
          retry_wait_seconds: 4
          max_attempts: 3
          command: (cd test; b2 ${{ matrix.config }} -l500 warnings-as-errors=on debug-iterators=on invariant-checks=full asserts=on)



   sim:
      name: Simulations
      runs-on: ubuntu-20.04

      steps:
      - name: checkout
        uses: actions/checkout@v2
        with:
           submodules: true

      - name: update package lists
        continue-on-error: true
        run: |
          sudo apt update

      - uses: Chocobo1/setup-ccache-action@v1
        with:
          update_packager_index: false
          override_cache_key: ccache-linux-simulations-${{ github.base_ref }}
          ccache_options: |
            max_size=5G

      - name: install boost
        run: |
          sudo apt install libboost-tools-dev libboost-dev libboost-system-dev
          echo "using gcc ;" >>~/user-config.jam

      - name: build and run simulations
        run: |
          cd simulation
          b2 debug-iterators=on invariant-checks=full asserts=on picker-debugging=on

      - name: build simulations no deprecated
        run: |
          cd simulation
          b2 testing.execute=off deprecated-functions=off


   dist:
      name: build dist
      runs-on: ${{ matrix.os }}

      strategy:
         matrix:
            os: [ubuntu-20.04 ]

      steps:
      - name: checkout
        uses: actions/checkout@v2
        with:
           submodules: true

      - name: update package lists
        continue-on-error: true
        run: |
          sudo apt update

      - uses: Chocobo1/setup-ccache-action@v1
        with:
          update_packager_index: false
          override_cache_key: ccache-linux-dist-${{ matrix.os }}-${{ github.base_ref }}
          ccache_options: |
            max_size=15G

      - name: install dependencies
        run: |
          sudo apt install libboost-tools-dev libboost-python-dev libboost-dev libboost-system-dev
          sudo apt install python-docutils python-pygments python-pil gsfonts inkscape icoutils graphviz hunspell imagemagick python3-setuptools
          python3 -m pip install aafigure
          echo "using gcc ;" >>~/user-config.jam

      - name: build tarball
        run: AAFIGURE=~/.local/bin/aafigure RST2HTML=rst2html make dist

      - uses: actions/upload-artifact@v2
        with:
          name: tarball
          path: libtorrent-rasterbar-*.tar.gz

      - name: test-tarball (b2 install)
        run: |
          tar xvzf libtorrent-rasterbar-*.tar.gz
          cd libtorrent-rasterbar-*/
          b2 install cxxstd=14 --prefix=test-install-root
          cat test-install-root/lib/pkgconfig/libtorrent-rasterbar.pc

      - name: test-tarball (b2 tests)
        run: |
          cd libtorrent-rasterbar-*/test
          b2 testing.execute=off
          b2 test_torrent_info

      - name: test-tarball (python bindings)
        run: |
          cd libtorrent-rasterbar-*/
          python3 bindings/python/setup.py build
