name: Build

on:
    push: {paths: [src/**, .github/workflows/build.yml]}
    pull_request: {paths: [src/**, .github/workflows/build.yml]}

jobs:

    unix:
        runs-on: ${{ matrix.os }}
        strategy:
            fail-fast: false
            matrix:
                name: [linux-clang, linux-clang-openssl, linux-gcc]
                include:
                    - name: linux-clang
                      os: ubuntu-latest
                      compiler: clang
                      flags: -Werror
                      configureopts: --enable-asan
                    - name: linux-clang-openssl
                      os: ubuntu-latest
                      compiler: clang
                      flags: -Werror -DOPENSSL_NO_DEPRECATED
                      configureopts: --with-crypto-impl=openssl
                    - name: linux-gcc
                      os: ubuntu-latest
                      compiler: gcc
                      flags: -D_FORTIFY_SOURCE=3
        steps:
            - name: Checkout repository
              uses: actions/checkout@v1
            - name: Linux setup
              if: startsWith(matrix.os, 'ubuntu')
              run: |
                sudo apt-get update -qq
                sudo apt-get install -y bison gettext keyutils ldap-utils libcmocka-dev libldap2-dev libkeyutils-dev libsasl2-dev libssl-dev python3-kdcproxy python3-pip slapd tcsh yasm softhsm2 opensc
                pip3 install pyrad
            - name: Build
              env:
                CC: ${{ matrix.compiler }}
                FLAGS: ${{ matrix.flags }}
                CONFIGURE_OPTS:  ${{ matrix.configureopts }}
              run: |
                # For the ksu tests, allow homedir access from other users.
                umask 022
                chmod a+rx $HOME
                chmod -R a+rX src
                cd src
                autoreconf
                ./configure --enable-maintainer-mode --with-ldap $CONFIGURE_OPTS --prefix=$HOME/inst
                make CPPFLAGS="$FLAGS"
                make check
                make install
                (cd clients/ksu && make check-ksu)
            - name: Display skipped tests
              run: cat src/skiptests
            - name: Check for files unexpectedly not removed by make distclean
              run: |
                cd src
                make distclean
                rm -rf autom4te.cache configure include/autoconf.h.in
                if [ -n "$(git ls-files -o)" ]; then
                  echo "Files not removed by make distclean:"
                  git ls-files -o
                  exit 1
                fi

    windows:
        runs-on: windows-2025
        env:
            KRB_INSTALL_DIR: C:\kfw
            OPENSSL_DIR: C:\Program Files\OpenSSL
            OPENSSL_VERSION: 3
        steps:
            - name: Checkout repository
              uses: actions/checkout@v1
            - name: Setup
              run: |
                mkdir "$env:KRB_INSTALL_DIR"
                copy "$env:OPENSSL_DIR\lib\VC\x64\MD\libcrypto.lib" "$env:OPENSSL_DIR\lib"
            - uses: ilammy/msvc-dev-cmd@v1
              with:
                arch: x64
            - name: Build
              working-directory: src
              run: |
                $env:Path += ';' + $env:WindowsSdkVerBinPath + '\\x86'
                nmake -f Makefile.in prep-windows
                nmake
            - name: Populate install dir
              working-directory: src
              run: |
                nmake install
            - name: Build installer
              working-directory: src\windows\installer\wix
              run: |
                $env:Path += ';' + $env:wix + 'bin'
                nmake
