name: Build and test

on: [push, pull_request]

env:
  UBSAN_OPTIONS: print_stacktrace=1

jobs:
  standard-build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        config:
          - "--enable-debug --enable-cuckoo --enable-magic --enable-macho --enable-pb-tests --enable-address-sanitizer --enable-undefined-behaviour-sanitizer"
          - "--enable-debug --enable-profiling --enable-cuckoo --enable-magic --enable-macho --enable-pb-tests --enable-address-sanitizer --enable-undefined-behaviour-sanitizer"
    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            autoconf \
            automake \
            gcc-multilib \
            libtool \
            libjansson-dev \
            libmagic-dev \
            libssl-dev  \
            protobuf-compiler \
            protobuf-c-compiler \
            libprotobuf-c-dev
      - name: Print versions
        run: |
          bison --version
          gcc --version
      - name: Prepare for building
        run: |
          ./bootstrap.sh
          ./configure ${{ matrix.config }}
      - name: Build
        run: make clean && make
      - name: Run tests
        run: make check
      - name: Print tests errors
        if: ${{ failure() }}
        run: cat test-suite.log

  mingw-build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        config:
          - "--host=x86_64-w64-mingw32"
          - "--host=i686-w64-mingw32"
    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            autoconf \
            automake \
            libtool \
            libssl-dev  \
            gcc-mingw-w64
      - name: Prepare for building
        run: |
          ./bootstrap.sh
          ./configure ${{ matrix.config }}
      - name: Build
        run: make clean && make

  macosx-build:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: |
          brew install \
            autoconf \
            automake \
            bison \
            flex \
            libtool \
            pkg-config \
            jansson \
            libmagic \
            openssl@3 \
            protobuf \
            protobuf-c
      - name: Print versions
        run: |
          bison --version
          gcc --version
      - name: Prepare for building
        run: |
          ./bootstrap.sh
          ./configure --enable-debug --enable-cuckoo --enable-magic --enable-macho --enable-pb-tests
      - name: Build
        run: make clean && make
      - name: Run tests
        run: make check
      - name: Print tests errors
        if: ${{ failure() }}
        run: cat test-suite.log

  bigendian:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup multiarch/qemu-user-static
        run: |
          docker run --rm --privileged multiarch/qemu-user-static:register --reset
      - name: ubuntu-core:s390x-focal
        uses: docker://multiarch/ubuntu-core:s390x-focal
        with:
          args: >
            bash -c
            "
            apt-get update -y &&
            DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata &&
            apt-get install -y automake libtool make gcc pkg-config bison git &&
            cd /github/workspace &&
            ./bootstrap.sh &&
            ./configure --disable-proc-scan --enable-macho &&
            make &&
            make check &&
            cat test-suite.log
            "
