name: Continuous Integration

on:
  push:
  pull_request:

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release
  version: osslsigncode-2.13

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - id: ubuntu-24.04
            triplet: x64-linux
            compiler: gcc
            os: ubuntu-24.04
            generator: Unix Makefiles
            vcpkg_root:
          - id: ubuntu-22.04
            triplet: x64-linux
            compiler: gcc
            os: ubuntu-22.04
            generator: Unix Makefiles
            vcpkg_root:
          - id: macOS
            triplet: arm64-osx
            compiler: clang
            os: macOS-latest
            generator: Unix Makefiles
            vcpkg_root: /usr/local/share/vcpkg
            cache: /Users/runner/.cache/vcpkg/archives
          - id: windows-x64-vs
            triplet: x64-windows
            compiler: vs
            arch: x64
            os: windows-latest
            generator: Ninja
            vcpkg_root: C:/vcpkg
            cache: C:/Users/runneradmin/AppData/Local/vcpkg/archives
          - id: windows-x86-vs
            triplet: x86-windows
            compiler: vs
            arch: x86
            os: windows-latest
            generator: Ninja
            vcpkg_root: C:/vcpkg
            cache: C:/Users/runneradmin/AppData/Local/vcpkg/archives
          - id: windows-x64-static-vs
            triplet: x64-windows-static
            compiler: vs
            arch: x64
            os: windows-latest
            generator: Ninja
            vcpkg_root: C:/vcpkg
            cache: C:/Users/runneradmin/AppData/Local/vcpkg/archives
          - id: windows-x64-mingw
            triplet: x64-windows
            compiler: mingw
            os: windows-latest
            generator: Ninja
            vcpkg_root: C:/vcpkg
            cache: C:/Users/runneradmin/AppData/Local/vcpkg/archives

    runs-on: ${{matrix.os}}

    env:
      VCPKG_ROOT: ${{matrix.vcpkg_root}}

    steps:
    - uses: actions/checkout@v4

    - name: Cache the vcpkg archives
      if: matrix.cache != ''
      uses: actions/cache@v4
      with:
        path: ${{matrix.cache}}
        key: ${{matrix.id}}-${{hashFiles('vcpkg.json')}}
        restore-keys: |
          ${{matrix.id}}-${{hashFiles('vcpkg.json')}}
          ${{matrix.id}}-

    - name: Configure Visual Studio
      if: matrix.compiler == 'vs'
      uses: ilammy/msvc-dev-cmd@v1
      with:
        arch: ${{matrix.arch}}

    - name: Install MSYS2
      if: matrix.compiler == 'mingw'
      uses: msys2/setup-msys2@v2
      with:
        update: true
        install: mingw-w64-x86_64-ninja

    - name: Put MSYS2_MinGW64 on PATH
      if: matrix.compiler == 'mingw'
      run: echo "D:/a/_temp/msys64/mingw64/bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

    - name: Set up Python (macOS)
      if: runner.os == 'macOS'
      uses: actions/setup-python@v4
      with:
          python-version: '3.13'
          update-environment: false
          architecture: 'arm64'

    - name: Set up Python virtual environment (Linux/macOS)
      if: runner.os != 'Windows'
      run: |
        python -m venv --system-site-packages --copies venv

    - name: Set up Python virtual environment (Windows)
      if: runner.os == 'Windows'
      run: |
        python.exe -m venv --system-site-packages --copies venv

    - name: Install Xcode (macOS)
      if: runner.os == 'macOS'
      uses: maxim-lobanov/setup-xcode@v1
      with:
        xcode-version: latest-stable

    - name: Setup the oldest supported version of cmake (macOS)
      if: runner.os == 'macOS'
      uses: jwlawson/actions-setup-cmake@v2.0

    - name: Install python3 cryptography module (Linux)
      if: runner.os == 'Linux'
      run: |
        source venv/bin/activate
        python -m pip install --upgrade pip
        python -m pip install --upgrade cryptography
        python -c "import sys; print(sys.executable)"
        python --version
        python -c "import cryptography; print(f'Python3 cryptography version {cryptography.__version__}')"

    - name: Install python3 cryptography module (macOS)
      if: runner.os == 'macOS'
      run: |
        source venv/bin/activate
        python -m pip install --upgrade pip
        ARCHFLAGS="-arch arm64" python -m pip install --upgrade cryptography
        python -c "import sys; print(sys.executable)"
        python --version
        python -c "import cryptography; print(f'Python3 cryptography version {cryptography.__version__}')"

    - name: Install python3 cryptography module (Windows)
      if: runner.os == 'Windows'
      run: |
        .\venv\Scripts\Activate.ps1
        python.exe -m ensurepip
        python.exe -m pip install --upgrade pip
        python.exe -m pip install cryptography
        python.exe -c "import sys; print(sys.executable)"
        python.exe --version
        python.exe -c "import cryptography; print(f'Python3 cryptography version {cryptography.__version__}')"

    - name: Configure CMake (Linux/macOS)
      if: runner.os != 'Windows'
      run: |
        source venv/bin/activate
        cmake \
          -G "${{matrix.generator}}" \
          -S "${{github.workspace}}" \
          -B "${{github.workspace}}/build" \
          -DCMAKE_OSX_ARCHITECTURES=arm64 \
          -DCMAKE_BUILD_TYPE="${{env.BUILD_TYPE}}" \
          -DCMAKE_INSTALL_PREFIX="${{github.workspace}}/dist"

    - name: Configure CMake (Windows)
      if: runner.os == 'Windows'
      run: |
        .\venv\Scripts\Activate.ps1
        cmake `
          -G "${{matrix.generator}}" `
          -S "${{github.workspace}}" `
          -B "${{github.workspace}}/build" `
          -DCMAKE_BUILD_TYPE="${{env.BUILD_TYPE}}" `
          -DCMAKE_INSTALL_PREFIX="${{github.workspace}}/dist"

    - name: Build
      run: cmake
        --build ${{github.workspace}}/build
        --config ${{env.BUILD_TYPE}}

    - name: List files (Linux/macOS)
      if: runner.os != 'Windows'
      run: find .. -ls

    - name: List files (Windows)
      if: runner.os == 'Windows'
      run: Get-ChildItem -Recurse -Name ..

    - name: Test (Linux/macOS)
      if: runner.os != 'Windows'
      working-directory: ${{github.workspace}}/build
      run: |
        source ../venv/bin/activate
        ctest -C ${{env.BUILD_TYPE}}

    - name: Test (Windows)
      if: runner.os == 'Windows'
      working-directory: ${{github.workspace}}/build
      run: |
        ..\venv\Scripts\Activate.ps1
        ctest -C ${{env.BUILD_TYPE}}

    - name: Upload the errors
      uses: actions/upload-artifact@v4
      if: failure()
      with:
        name: errors-${{matrix.id}}
        path: |
          ${{github.workspace}}/build/Testing/Temporary/LastTest.log
          ${{github.workspace}}/build/Testing/conf/makecerts.log
          ${{github.workspace}}/build/Testing/logs/server.log
          ${{github.workspace}}/build/Testing/logs/port.log

    - name: Install
      run: cmake --install ${{github.workspace}}/build

    - name: Upload the executables
      uses: actions/upload-artifact@v4
      with:
        name: ${{env.version}}-${{matrix.id}}
        path: ${{github.workspace}}/dist
