name: CI

on:
  push:
  pull_request:

jobs:
  cppcheck:
    runs-on: ubuntu-20.04
    name: cppcheck
    steps:
      - uses: actions/checkout@v2
        name: checkout repo
      - name: Setup Dependencies
        run: |
          sudo apt-get update -y -qq
          sudo apt-get install cppcheck
      - name: cppcheck
        run: |
          cppcheck --std=c89 \
            --enable=all \
            --inconclusive \
            --suppress=variableScope \
            --suppress=missingInclude \
            --suppress=unusedFunction:src/fuzz/*.c \
            --quiet \
            --error-exitcode=1 \
            --template='{file}:{line} {id} {severity} {message}' \
            .

  clang-static:
    runs-on: ubuntu-20.04
    name: clang static check
    steps:
      - uses: actions/checkout@v2
        name: checkout repo
      - name: make analyze
        run: |
          ./autogen.sh
          ./configure
          make -C src analyze

  build-linux:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-20.04, ubuntu-22.04]
        platform: [x32, x64]
        compiler: [gcc, clang]
    steps:
      - uses: actions/checkout@v2
      - name: Setup Dependencies
        run: |
          sudo apt-get update -y -qq
          sudo apt-get install valgrind
      - name: build
        run: |
          ./autogen.sh
          ./configure
          make
      - name: test
        run: make -e check

      - name: clean
        run: make clean

      - name: build pedantic
        run: make
        env:
          CFLAGS: '-Wall -Wextra -Werror -pedantic -ansi -g -O1'
      - name: test valgrind
        run: make -e check
        env:
          VALGRIND: 'valgrind --gen-suppressions=no --leak-check=full --show-leak-kinds=all --read-var-info=yes --error-exitcode=1 --track-origins=yes --suppressions=/home/runner/work/libinjection/libinjection/src/alpine.supp'

  build-macos:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-12, macos-11]
        compiler: [gcc, clang]
    steps:
      - uses: actions/checkout@v2

      - name: Cache brew deps
        uses: actions/cache@v2
        with:
          # Paths to cache:
          # /usr/local/Homebrew - installation folder of Homebrew
          # /usr/local/Cellar - installation folder of Homebrew formulae
          # /usr/local/Frameworks, /usr/local/bin, /usr/local/opt - contain (links to) binaries installed by Homebrew formulae
          path: |
            /usr/local/Homebrew
            /usr/local/Cellar
            /usr/local/Frameworks
            /usr/local/bin
            /usr/local/opt
          key: macos-build-cache-${{ hashFiles('./scripts/builder_setup.sh') }}
      - name: build
        run: |
          brew install automake autoconf libtool
          ./autogen.sh
          ./configure
          make
      - name: test
        run: make -e check
      - name: clean
        run: make clean
      - name: build pedantic
        run: make all
        env:
          CFLAGS: '-Wall -Wextra -Werror -pedantic -ansi -g -O1'
