name: Build tests

on: [push, pull_request]

env:
  # This should be the minimum version required to run setools:
  SELINUX_USERSPACE_VERSION: 3.2

  # GitHub doesn't support building env
  # vars from others in this block.
  USERSPACE_SRC: /tmp/selinux-src
  #SEPOL_SRC: ${USERSPACE_SRC}/libsepol
  SEPOL_SRC: /tmp/selinux-src/libsepol
  #LIBSEPOLA: ${SEPOL_SRC}/src/libsepol.a
  LIBSEPOLA: /tmp/selinux-src/libsepol/src/libsepol.a
  #SELINUX_SRC: ${USERSPACE_SRC}/libselinux
  SELINUX_SRC: /tmp/selinux-src/libselinux
  #CHECKPOLICY_SRC: ${USERSPACE_SRC}/checkpolicy
  CHECKPOLICY_SRC: /tmp/selinux-src/checkpolicy

jobs:
  build:
    runs-on: ubuntu-20.04

    strategy:
      fail-fast: false

      matrix:
        build-opts:
          - {python: '3.9', tox: python3.9}
          - {python: '3.10', tox: python3.10}
          - {python: '3.11', tox: python3.11}
          - {python: '3.11', tox: pep8}
          - {python: '3.11', tox: lint}
          - {python: '3.11', tox: mypy}
          - {python: '3.11', tox: coverage}
          - {python: '3.11', tox: install}

    steps:
    - uses: actions/checkout@v3

    # This should be the minimum required Python version to build refpolicy.
    - name: Set up Python ${{ matrix.build-opts.python }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.build-opts.python }}

    - name: Install dependencies
      run: |
        sudo apt-get update -qq
        sudo apt-get install -qqy \
            bison \
            flex \
            gettext \
            libaudit-dev \
            libbz2-dev \
            libpcre3-dev

        sudo python -m pip install -U \
            tox

    - name: Cache SELinux userspace
      uses: actions/cache@v3
      id: cache-userspace
      with:
        path: ${{ env.USERSPACE_SRC }}
        key: ${{ runner.os }}-selinux-userspace-${{ env.SELINUX_USERSPACE_VERSION }}

    - name: Build SELinux userspace
      if: ${{ steps.cache-userspace.outputs.cache-hit != 'true' }}
      run: |
        # Download current SELinux userspace tools and libraries
        git clone https://github.com/SELinuxProject/selinux.git ${USERSPACE_SRC} -b ${SELINUX_USERSPACE_VERSION}

        # Compile SELinux userspace
        make -C ${SEPOL_SRC}
        make CFLAGS="-O2 -pipe -fPIC -Wall -I${SEPOL_SRC}/include" LDFLAGS="-L${SEPOL_SRC}/src" -C ${SELINUX_SRC}
        make CFLAGS="-O2 -pipe -fPIC -Wall -I${SEPOL_SRC}/include" -C ${CHECKPOLICY_SRC}

    - name: Run test
      run: |
        export LD_LIBRARY_PATH="${SEPOL_SRC}/src:${SELINUX_SRC}/src:${LD_LIBRARY_PATH}"
        tox -e ${{ matrix.build-opts.tox }}
