name: C/C++ CI

on:
  push:
    paths-ignore:
      - 'docker-compose/**'
      - 'docs/**'
      - 'README.md'

  pull_request:

jobs:
  build-alpine-static:

    timeout-minutes: 240
    runs-on: ubuntu-22.04

    strategy:
      matrix:
        config:
          - {arch: x86_64, branch: latest-stable, flags: "-ffunction-sections -Wno-error=inline"}
          - {arch: aarch64, branch: latest-stable, flags: "-ffunction-sections -Wno-error=inline -mfix-cortex-a53-835769 -mfix-cortex-a53-843419"}
          - {arch: riscv64, branch: edge, flags: "-ffunction-sections -Wno-error=inline"}

    steps:
    - name: Setup Alpine Linux
      uses: jirutka/setup-alpine@v1
      with:
        arch: ${{ matrix.config.arch }}
        branch: ${{ matrix.config.branch }}

    - name: Install dependencies
      shell: alpine.sh --root {0}
      run: |
        apk add git cmake gcc g++ make linux-headers

    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Build libcurl
      shell: alpine.sh {0}
      run: |
        cd external/src/curl
        cmake . -DCMAKE_C_FLAGS="-Os -flto=auto ${{ matrix.config.flags }}" -DBUILD_CURL_EXE=OFF -DBUILD_SHARED_LIBS=OFF -DCURL_DISABLE_INSTALL=ON -DCURL_ENABLE_EXPORT_TARGET=OFF -DCURL_DISABLE_HEADERS_API=ON -DCURL_DISABLE_BINDLOCAL=ON -DBUILD_LIBCURL_DOCS=OFF -DBUILD_MISC_DOCS=OFF -DENABLE_CURL_MANUAL=OFF -DCURL_ZLIB=OFF -DCURL_DISABLE_ALTSVC=ON -DCURL_DISABLE_COOKIES=ON -DCURL_DISABLE_DOH=ON -DCURL_DISABLE_GETOPTIONS=ON -DCURL_DISABLE_HSTS=ON -DCURL_DISABLE_LIBCURL_OPTION=ON -DCURL_DISABLE_MIME=ON -DCURL_DISABLE_NETRC=ON -DCURL_DISABLE_NTLM=ON -DCURL_DISABLE_PARSEDATE=ON -DCURL_DISABLE_PROGRESS_METER=ON -DCURL_DISABLE_SHUFFLE_DNS=ON -DCURL_DISABLE_SOCKETPAIR=ON -DCURL_DISABLE_VERBOSE_STRINGS=ON -DHTTP_ONLY=ON -DCURL_ENABLE_SSL=OFF -DUSE_LIBIDN2=OFF -DCURL_USE_LIBPSL=OFF -DCURL_USE_LIBSSH2=OFF -DENABLE_UNIX_SOCKETS=OFF -DCURL_DISABLE_TESTS=ON -DUSE_NGHTTP2=OFF -DBUILD_EXAMPLES=OFF
        make -j$(nproc)
        cd lib && mkdir .libs && cp libcurl.a .libs

    - name: Build libuv
      shell: alpine.sh {0}
      run: |
        cd external/src/libuv
        mkdir build
        cd build
        cmake .. -DCMAKE_C_FLAGS='-Os -flto=auto ${{ matrix.config.flags }}' -DBUILD_TESTING=OFF -DLIBUV_BUILD_SHARED=OFF
        make -j$(nproc)

    - name: Build libzmq
      shell: alpine.sh {0}
      run: |
        cd external/src/libzmq
        mkdir build
        cd build
        cmake .. -DCMAKE_C_FLAGS='-Os -flto=auto ${{ matrix.config.flags }}' -DCMAKE_CXX_FLAGS='-Os -flto=auto ${{ matrix.config.flags }}' -DWITH_LIBSODIUM=OFF -DWITH_LIBBSD=OFF -DBUILD_TESTS=OFF -DWITH_DOCS=OFF -DENABLE_DRAFTS=OFF -DBUILD_SHARED=OFF
        make -j$(nproc)

    - name: Build p2pool
      shell: alpine.sh {0}
      run: |
        mkdir build
        cd build
        cmake ..  -DCMAKE_C_FLAGS='${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections' -DCMAKE_CXX_FLAGS='${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections' -DSTATIC_BINARY=ON -DARCH_ID=${{ matrix.config.arch }}
        make -j$(nproc) p2pool

    - name: Run RandomX tests
      shell: alpine.sh {0}
      run: |
        cd build
        ./p2pool --test
        make -j$(nproc) randomx-tests
        external/src/RandomX/randomx-tests

    - name: Build tests
      shell: alpine.sh {0}
      run: |
        cd tests
        mkdir build
        cd build
        cmake ..  -DCMAKE_C_FLAGS='${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections' -DCMAKE_CXX_FLAGS='${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections' -DSTATIC_LIBS=ON -DARCH_ID=${{ matrix.config.arch }}
        make -j$(nproc)

    - name: Run tests
      shell: alpine.sh {0}
      run: |
        cd tests/build
        gunzip *.gz
        ./p2pool_tests

    - name: List directory
      run: |
        cd build
        ls -la
        file p2pool

    - name: Archive binary
      uses: actions/upload-artifact@v4
      with:
        name: p2pool-alpine-static-${{ matrix.config.arch }}
        path: build/p2pool

  build-ubuntu:

    timeout-minutes: 60
    runs-on: ${{ matrix.config.os }}

    strategy:
      matrix:
        config:
          - {os: ubuntu-20.04, c: gcc-8, cpp: g++-8, flags: ""}
          - {os: ubuntu-20.04, c: gcc-11, cpp: g++-11, flags: ""}
          - {os: ubuntu-22.04, c: gcc-12, cpp: g++-12, flags: ""}

    steps:
    - name: Install dependencies
      run: |
        sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
        sudo apt update
        sudo apt install -y git build-essential cmake libuv1-dev libzmq3-dev libsodium-dev libpgm-dev libnorm-dev libgss-dev libcurl4-openssl-dev libidn2-0-dev ${{ matrix.config.c }} ${{ matrix.config.cpp }}

    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Build p2pool
      run: |
        mkdir build
        cd build
        cmake .. -DCMAKE_C_COMPILER=${{ matrix.config.c }} -DCMAKE_CXX_COMPILER=${{ matrix.config.cpp }} -DCMAKE_C_FLAGS='${{ matrix.config.flags }}' -DCMAKE_CXX_FLAGS='${{ matrix.config.flags }}'
        make -j$(nproc) p2pool

    - name: Run RandomX tests
      run: |
        cd build
        ./p2pool --test
        make -j$(nproc) randomx-tests
        external/src/RandomX/randomx-tests

    - name: Build tests
      run: |
        cd tests
        mkdir build
        cd build
        cmake .. -DCMAKE_C_COMPILER=${{ matrix.config.c }} -DCMAKE_CXX_COMPILER=${{ matrix.config.cpp }} -DCMAKE_C_FLAGS='${{ matrix.config.flags }}' -DCMAKE_CXX_FLAGS='${{ matrix.config.flags }}'
        make -j$(nproc)

    - name: Run tests
      run: |
        cd tests/build
        gunzip *.gz
        ./p2pool_tests

    - name: Archive binary
      uses: actions/upload-artifact@v4
      with:
        name: p2pool-${{ matrix.config.os }}-${{ matrix.config.c }}
        path: build/p2pool

  build-ubuntu-static-libs:

    timeout-minutes: 60
    runs-on: ubuntu-22.04

    strategy:
      matrix:
        config:
          - {flags: "-fuse-linker-plugin -ffunction-sections -Wno-error=inline"}

    steps:
    - name: Install dependencies
      run: |
        sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
        sudo apt update
        sudo apt install -y git build-essential cmake libgss-dev gcc-12 g++-12
        sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 100 --slave /usr/bin/g++ g++ /usr/bin/g++-12 --slave /usr/bin/gcov gcov /usr/bin/gcov-12

    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Build libcurl
      run: |
        cd external/src/curl
        cmake . -DCMAKE_C_FLAGS="-Os -flto=auto ${{ matrix.config.flags }}" -DBUILD_CURL_EXE=OFF -DBUILD_SHARED_LIBS=OFF -DCURL_DISABLE_INSTALL=ON -DCURL_ENABLE_EXPORT_TARGET=OFF -DCURL_DISABLE_HEADERS_API=ON -DCURL_DISABLE_BINDLOCAL=ON -DBUILD_LIBCURL_DOCS=OFF -DBUILD_MISC_DOCS=OFF -DENABLE_CURL_MANUAL=OFF -DCURL_ZLIB=OFF -DCURL_DISABLE_ALTSVC=ON -DCURL_DISABLE_COOKIES=ON -DCURL_DISABLE_DOH=ON -DCURL_DISABLE_GETOPTIONS=ON -DCURL_DISABLE_HSTS=ON -DCURL_DISABLE_LIBCURL_OPTION=ON -DCURL_DISABLE_MIME=ON -DCURL_DISABLE_NETRC=ON -DCURL_DISABLE_NTLM=ON -DCURL_DISABLE_PARSEDATE=ON -DCURL_DISABLE_PROGRESS_METER=ON -DCURL_DISABLE_SHUFFLE_DNS=ON -DCURL_DISABLE_SOCKETPAIR=ON -DCURL_DISABLE_VERBOSE_STRINGS=ON -DHTTP_ONLY=ON -DCURL_ENABLE_SSL=OFF -DUSE_LIBIDN2=OFF -DCURL_USE_LIBPSL=OFF -DCURL_USE_LIBSSH2=OFF -DENABLE_UNIX_SOCKETS=OFF -DCURL_DISABLE_TESTS=ON -DUSE_NGHTTP2=OFF -DBUILD_EXAMPLES=OFF
        make -j$(nproc)
        cd lib && mkdir .libs && cp libcurl.a .libs

    - name: Build libuv
      run: |
        cd external/src/libuv
        mkdir build
        cd build
        cmake .. -DCMAKE_C_FLAGS='-Os -flto=auto ${{ matrix.config.flags }}' -DBUILD_TESTING=OFF -DLIBUV_BUILD_SHARED=OFF
        make -j$(nproc)

    - name: Build libzmq
      run: |
        cd external/src/libzmq
        mkdir build
        cd build
        cmake .. -DCMAKE_C_FLAGS='-Os -flto=auto ${{ matrix.config.flags }}' -DCMAKE_CXX_FLAGS='-Os -flto=auto ${{ matrix.config.flags }}' -DWITH_LIBSODIUM=OFF -DWITH_LIBBSD=OFF -DBUILD_TESTS=OFF -DWITH_DOCS=OFF -DENABLE_DRAFTS=OFF -DBUILD_SHARED=OFF
        make -j$(nproc)

    - name: Build p2pool
      run: |
        mkdir build
        cd build
        cmake ..  -DCMAKE_C_FLAGS='${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections' -DCMAKE_CXX_FLAGS='${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections' -DSTATIC_LIBS=ON
        make -j$(nproc) p2pool

    - name: Run RandomX tests
      run: |
        cd build
        ./p2pool --test
        make -j$(nproc) randomx-tests
        external/src/RandomX/randomx-tests

    - name: Build tests
      run: |
        cd tests
        mkdir build
        cd build
        cmake ..  -DCMAKE_C_FLAGS='${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections' -DCMAKE_CXX_FLAGS='${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections' -DSTATIC_LIBS=ON
        make -j$(nproc)

    - name: Run tests
      run: |
        cd tests/build
        gunzip *.gz
        ./p2pool_tests

    - name: Archive binary
      uses: actions/upload-artifact@v4
      with:
        name: p2pool-ubuntu-static-libs
        path: build/p2pool

  build-ubuntu-aarch64:

    timeout-minutes: 60
    runs-on: ${{ matrix.config.os }}

    strategy:
      matrix:
        config:
          - {os: ubuntu-20.04, flags: "-fuse-linker-plugin -ffunction-sections -mfix-cortex-a53-835769 -mfix-cortex-a53-843419"}
          - {os: ubuntu-22.04, flags: "-fuse-linker-plugin -ffunction-sections -mfix-cortex-a53-835769 -mfix-cortex-a53-843419"}

    steps:
    - name: Install dependencies
      run: |
        sudo apt update
        sudo apt install -y git build-essential cmake gcc-aarch64-linux-gnu g++-aarch64-linux-gnu binutils-aarch64-linux-gnu qemu-user

    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Build libcurl
      run: |
        cd external/src/curl
        cmake . -DCMAKE_TOOLCHAIN_FILE=../../../cmake/aarch64_toolchain.cmake -DCMAKE_C_FLAGS="-Os -flto=auto ${{ matrix.config.flags }}" -DBUILD_CURL_EXE=OFF -DBUILD_SHARED_LIBS=OFF -DCURL_DISABLE_INSTALL=ON -DCURL_ENABLE_EXPORT_TARGET=OFF -DCURL_DISABLE_HEADERS_API=ON -DCURL_DISABLE_BINDLOCAL=ON -DBUILD_LIBCURL_DOCS=OFF -DBUILD_MISC_DOCS=OFF -DENABLE_CURL_MANUAL=OFF -DCURL_ZLIB=OFF -DCURL_DISABLE_ALTSVC=ON -DCURL_DISABLE_COOKIES=ON -DCURL_DISABLE_DOH=ON -DCURL_DISABLE_GETOPTIONS=ON -DCURL_DISABLE_HSTS=ON -DCURL_DISABLE_LIBCURL_OPTION=ON -DCURL_DISABLE_MIME=ON -DCURL_DISABLE_NETRC=ON -DCURL_DISABLE_NTLM=ON -DCURL_DISABLE_PARSEDATE=ON -DCURL_DISABLE_PROGRESS_METER=ON -DCURL_DISABLE_SHUFFLE_DNS=ON -DCURL_DISABLE_SOCKETPAIR=ON -DCURL_DISABLE_VERBOSE_STRINGS=ON -DHTTP_ONLY=ON -DCURL_ENABLE_SSL=OFF -DUSE_LIBIDN2=OFF -DCURL_USE_LIBPSL=OFF -DCURL_USE_LIBSSH2=OFF -DENABLE_UNIX_SOCKETS=OFF -DCURL_DISABLE_TESTS=ON -DUSE_NGHTTP2=OFF -DBUILD_EXAMPLES=OFF
        make -j$(nproc)
        cd lib && mkdir .libs && cp libcurl.a .libs

    - name: Build libuv
      run: |
        cd external/src/libuv
        mkdir build
        cd build
        cmake .. -DCMAKE_TOOLCHAIN_FILE=../../../cmake/aarch64_toolchain.cmake -DCMAKE_C_FLAGS='-Os -flto=auto ${{ matrix.config.flags }}' -DBUILD_TESTING=OFF -DLIBUV_BUILD_SHARED=OFF
        make -j$(nproc)

    - name: Build libzmq
      run: |
        cd external/src/libzmq
        mkdir build
        cd build
        cmake .. -DCMAKE_TOOLCHAIN_FILE=../../../cmake/aarch64_toolchain.cmake -DCMAKE_C_FLAGS='-Os -flto=auto ${{ matrix.config.flags }}' -DCMAKE_CXX_FLAGS='-Os -flto=auto ${{ matrix.config.flags }}' -DWITH_LIBSODIUM=OFF -DWITH_LIBBSD=OFF -DBUILD_TESTS=OFF -DWITH_DOCS=OFF -DENABLE_DRAFTS=OFF -DBUILD_SHARED=OFF
        make -j$(nproc)

    - name: Build p2pool
      run: |
        mkdir build
        cd build
        cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/aarch64_toolchain.cmake -DCMAKE_C_FLAGS='${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections' -DCMAKE_CXX_FLAGS='${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections' -DSTATIC_LIBS=ON -DARCH_ID=aarch64
        make -j$(nproc) p2pool

    - name: Run RandomX tests
      run: |
        cd build
        qemu-aarch64 -L /usr/aarch64-linux-gnu p2pool --test
        make -j$(nproc) randomx-tests
        qemu-aarch64 -L /usr/aarch64-linux-gnu external/src/RandomX/randomx-tests

    - name: Build tests
      run: |
        cd tests
        mkdir build
        cd build
        cmake .. -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ -DCMAKE_STRIP=/usr/bin/aarch64-linux-gnu-strip -DCMAKE_C_FLAGS='${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections' -DCMAKE_CXX_FLAGS='${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections' -DSTATIC_LIBS=ON -DARCH_ID=aarch64
        make -j$(nproc)

    - name: Run tests
      run: |
        cd tests/build
        gunzip *.gz
        qemu-aarch64 -L /usr/aarch64-linux-gnu ./p2pool_tests

    - name: Archive binary
      uses: actions/upload-artifact@v4
      with:
        name: p2pool-${{ matrix.config.os }}-aarch64
        path: build/p2pool

  build-windows-msys2:

    timeout-minutes: 60
    runs-on: windows-latest

    strategy:
      matrix:
        config:
          - {c: "gcc", cxx: "g++", flags: "-ffunction-sections -Wno-error=maybe-uninitialized -Wno-error=attributes"}
          - {c: "clang", cxx: "clang++", flags: "-fuse-ld=lld -Wno-unused-command-line-argument -Wno-nan-infinity-disabled"}

    defaults:
      run:
        shell: msys2 {0}

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Setup MSYS2
      uses: eine/setup-msys2@v2
      with:
        update: true
        install: mingw-w64-x86_64-toolchain mingw-w64-x86_64-clang mingw-w64-x86_64-lld mingw-w64-x86_64-cmake make git

    - name: Apply patch
      run: |
        cd /
        git apply --verbose --ignore-whitespace --directory=mingw64/include/c++/`ls mingw64/include/c++`/ /d/a/p2pool/p2pool/patches/msys2/typeinfo.patch

    - name: Build libcurl
      run: |
        cd external/src/curl
        cmake . -G "Unix Makefiles" -DCMAKE_C_COMPILER=${{ matrix.config.c }} -DCMAKE_C_FLAGS="-Os ${{ matrix.config.flags }}" -DCURL_TARGET_WINDOWS_VERSION=0x0600 -DBUILD_CURL_EXE=OFF -DBUILD_SHARED_LIBS=OFF -DCURL_DISABLE_INSTALL=ON -DCURL_ENABLE_EXPORT_TARGET=OFF -DCURL_DISABLE_HEADERS_API=ON -DCURL_DISABLE_BINDLOCAL=ON -DBUILD_LIBCURL_DOCS=OFF -DBUILD_MISC_DOCS=OFF -DENABLE_CURL_MANUAL=OFF -DCURL_ZLIB=OFF -DCURL_DISABLE_ALTSVC=ON -DCURL_DISABLE_COOKIES=ON -DCURL_DISABLE_DOH=ON -DCURL_DISABLE_GETOPTIONS=ON -DCURL_DISABLE_HSTS=ON -DCURL_DISABLE_LIBCURL_OPTION=ON -DCURL_DISABLE_MIME=ON -DCURL_DISABLE_NETRC=ON -DCURL_DISABLE_NTLM=ON -DCURL_DISABLE_PARSEDATE=ON -DCURL_DISABLE_PROGRESS_METER=ON -DCURL_DISABLE_SHUFFLE_DNS=ON -DCURL_DISABLE_SOCKETPAIR=ON -DCURL_DISABLE_VERBOSE_STRINGS=ON -DHTTP_ONLY=ON -DCURL_ENABLE_SSL=OFF -DUSE_LIBIDN2=OFF -DCURL_USE_LIBPSL=OFF -DCURL_USE_LIBSSH2=OFF -DENABLE_UNIX_SOCKETS=OFF -DCURL_DISABLE_TESTS=ON -DUSE_NGHTTP2=OFF -DBUILD_EXAMPLES=OFF
        make -j$(nproc)
        cd lib && mkdir .libs && cp libcurl.a .libs

    - name: Build libuv
      run: |
        cd external/src/libuv
        mkdir build
        cd build
        cmake .. -G "Unix Makefiles" -DCMAKE_C_COMPILER=${{ matrix.config.c }} -DCMAKE_C_FLAGS='-Os ${{ matrix.config.flags }}' -DBUILD_TESTING=OFF -DLIBUV_BUILD_SHARED=OFF
        make -j$(nproc)

    - name: Build libzmq
      run: |
        cd external/src/libzmq
        mkdir build
        cd build
        cmake .. -G "Unix Makefiles" -DCMAKE_C_COMPILER=${{ matrix.config.c }} -DCMAKE_CXX_COMPILER=${{ matrix.config.cxx }} -DCMAKE_C_FLAGS='-Os ${{ matrix.config.flags }}' -DCMAKE_CXX_FLAGS='-Os ${{ matrix.config.flags }}' -DWITH_LIBSODIUM=OFF -DWITH_LIBBSD=OFF -DBUILD_TESTS=OFF -DZMQ_HAVE_IPC=OFF -DWITH_DOCS=OFF -DENABLE_DRAFTS=OFF -DBUILD_SHARED=OFF
        make -j$(nproc)

    - name: Build p2pool
      run: |
        mkdir build
        cd build
        cmake .. -G "Unix Makefiles" -DCMAKE_C_COMPILER=${{ matrix.config.c }} -DCMAKE_CXX_COMPILER=${{ matrix.config.cxx }} -DCMAKE_C_FLAGS="${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections" -DSTATIC_LIBS=ON -DOPENSSL_NO_ASM=ON -DWITH_LTO=OFF
        make -j$(nproc) p2pool

    - name: Run RandomX tests
      run: |
        cd build
        ./p2pool.exe --test
        make -j$(nproc) randomx-tests
        external/src/RandomX/randomx-tests.exe

    - name: Build tests
      run: |
        cd tests
        mkdir build
        cd build
        cmake .. -G "Unix Makefiles" -DCMAKE_C_COMPILER=${{ matrix.config.c }} -DCMAKE_CXX_COMPILER=${{ matrix.config.cxx }} -DCMAKE_C_FLAGS="${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="${{ matrix.config.flags }} -Wl,-s -Wl,--gc-sections" -DSTATIC_LIBS=ON
        make -j$(nproc)

    - name: Run tests
      run: |
        cd tests/build
        gunzip *.gz
        ./p2pool_tests.exe

    - name: Archive binary
      uses: actions/upload-artifact@v4
      with:
        name: p2pool-msys2-${{ matrix.config.c }}.exe
        path: build/p2pool.exe

  build-windows-msbuild:

    timeout-minutes: 60
    runs-on: windows-${{ matrix.config.os }}

    strategy:
      matrix:
        config:
          - {vs: Visual Studio 17 2022, os: 2022, vspath: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise", rx: "ON", upnp: "ON", grpc: "ON", tls: "ON"}
          - {vs: Visual Studio 16 2019, os: 2019, vspath: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise", rx: "ON", upnp: "ON", grpc: "ON", tls: "ON"}
          - {vs: Visual Studio 16 2019, os: 2019, vspath: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise", rx: "ON", upnp: "ON", grpc: "OFF", tls: "ON"}
          - {vs: Visual Studio 16 2019, os: 2019, vspath: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise", rx: "OFF", upnp: "ON", grpc: "OFF", tls: "ON"}
          - {vs: Visual Studio 16 2019, os: 2019, vspath: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise", rx: "OFF", upnp: "OFF", grpc: "OFF", tls: "ON"}
          - {vs: Visual Studio 16 2019, os: 2019, vspath: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise", rx: "OFF", upnp: "OFF", grpc: "OFF", tls: "OFF"}

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Setup cmake
      uses: lukka/get-cmake@latest

    - name: Apply patch
      run: |
        git apply --verbose --ignore-whitespace --directory=external/src/grpc/third_party/boringssl-with-bazel patches/boringssl/win7.patch

    - name: Build p2pool
      run: |
        mkdir build
        cd build
        cmake .. -G "${{ matrix.config.vs }}" -DCMAKE_SYSTEM_VERSION="10.0" -DWITH_RANDOMX=${{ matrix.config.rx }} -DWITH_UPNP=${{ matrix.config.upnp }} -DWITH_GRPC=${{ matrix.config.grpc }} -DWITH_TLS=${{ matrix.config.tls }}
        & "${{ matrix.config.vspath }}\\MSBuild\\Current\\Bin\\amd64\\msbuild" -v:m /m /p:Configuration=Release p2pool.vcxproj

    - name: Check Windows 7 compatibility
      shell: cmd
      run: |
        call "${{ matrix.config.vspath }}\\VC\\Auxiliary\\Build\\vcvarsall.bat" amd64
        dumpbin /IMPORTS build/Release/p2pool.exe > 1.txt
        findstr /R /C:"^  *[0-9A-F][0-9A-F]* [a-zA-Z][a-zA-Z0-9]*$" 1.txt > 2.txt
        python tests/src/check_win7.py 2.txt

    - name: Run RandomX tests
      if: matrix.config.rx == 'ON'
      run: |
        build/Release/p2pool.exe --test
        cd build/external/src/RandomX
        & "${{ matrix.config.vspath }}\\MSBuild\\Current\\Bin\\amd64\\msbuild" -v:m /m /p:Configuration=Release randomx-tests.vcxproj
        Release/randomx-tests.exe

    - name: Build tests
      run: |
        cd tests
        mkdir build
        cd build
        cmake .. -G "${{ matrix.config.vs }}" -DCMAKE_SYSTEM_VERSION="10.0" -DDEV_WITH_ASAN=ON
        & "${{ matrix.config.vspath }}\\MSBuild\\Current\\Bin\\amd64\\msbuild" -v:m /m /p:Configuration=Debug p2pool_tests.vcxproj

    - name: Run tests
      shell: cmd
      run: |
        cd tests/build/Debug
        7z x *.gz
        call "${{ matrix.config.vspath }}\\VC\\Auxiliary\\Build\\vcvarsall.bat" amd64
        p2pool_tests.exe

    - name: Archive binary
      uses: actions/upload-artifact@v4
      with:
        name: p2pool-vs-${{ matrix.config.os }}-randomx-${{ matrix.config.rx }}-upnp-${{ matrix.config.upnp }}-grpc-${{ matrix.config.grpc }}-tls-${{ matrix.config.tls }}.exe
        path: build/Release/p2pool.exe

  build-macos:

    timeout-minutes: 60
    runs-on: macos-13

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Build libcurl
      run: |
        cd external/src/curl
        cmake . -DCMAKE_C_COMPILER="$(brew --prefix llvm@15)/bin/clang" -DCMAKE_AR="$(brew --prefix llvm@15)/bin/llvm-ar" -DCMAKE_RANLIB="$(brew --prefix llvm@15)/bin/llvm-ranlib" -DCMAKE_C_FLAGS="-Os -flto -target x86_64-apple-macos10.13 -Wno-overriding-t-option" -DBUILD_CURL_EXE=OFF -DBUILD_SHARED_LIBS=OFF -DCURL_DISABLE_INSTALL=ON -DCURL_ENABLE_EXPORT_TARGET=OFF -DCURL_DISABLE_HEADERS_API=ON -DCURL_DISABLE_BINDLOCAL=ON -DBUILD_LIBCURL_DOCS=OFF -DBUILD_MISC_DOCS=OFF -DENABLE_CURL_MANUAL=OFF -DCURL_ZLIB=OFF -DCURL_DISABLE_ALTSVC=ON -DCURL_DISABLE_COOKIES=ON -DCURL_DISABLE_DOH=ON -DCURL_DISABLE_GETOPTIONS=ON -DCURL_DISABLE_HSTS=ON -DCURL_DISABLE_LIBCURL_OPTION=ON -DCURL_DISABLE_MIME=ON -DCURL_DISABLE_NETRC=ON -DCURL_DISABLE_NTLM=ON -DCURL_DISABLE_PARSEDATE=ON -DCURL_DISABLE_PROGRESS_METER=ON -DCURL_DISABLE_SHUFFLE_DNS=ON -DCURL_DISABLE_SOCKETPAIR=ON -DCURL_DISABLE_VERBOSE_STRINGS=ON -DHTTP_ONLY=ON -DCURL_ENABLE_SSL=OFF -DUSE_LIBIDN2=OFF -DCURL_USE_LIBPSL=OFF -DCURL_USE_LIBSSH2=OFF -DENABLE_UNIX_SOCKETS=OFF -DCURL_DISABLE_TESTS=ON -DUSE_NGHTTP2=OFF -DBUILD_EXAMPLES=OFF
        make -j4
        cd lib && mkdir .libs && cp libcurl.a .libs

    - name: Build libuv
      run: |
        cd external/src/libuv
        mkdir build
        cd build
        cmake .. -DCMAKE_C_COMPILER="$(brew --prefix llvm@15)/bin/clang" -DCMAKE_AR="$(brew --prefix llvm@15)/bin/llvm-ar" -DCMAKE_RANLIB="$(brew --prefix llvm@15)/bin/llvm-ranlib" -DCMAKE_C_FLAGS='-Os -flto -target x86_64-apple-macos10.13 -Wno-overriding-t-option' -DBUILD_TESTING=OFF -DLIBUV_BUILD_SHARED=OFF
        make -j4

    - name: Build libzmq
      run: |
        cd external/src/libzmq
        mkdir build
        cd build
        cmake .. -DCMAKE_C_COMPILER="$(brew --prefix llvm@15)/bin/clang" -DCMAKE_CXX_COMPILER="$(brew --prefix llvm@15)/bin/clang++" -DCMAKE_AR="$(brew --prefix llvm@15)/bin/llvm-ar" -DCMAKE_RANLIB="$(brew --prefix llvm@15)/bin/llvm-ranlib" -DCMAKE_C_FLAGS='-Os -flto -target x86_64-apple-macos10.13 -Wno-overriding-t-option' -DCMAKE_CXX_FLAGS='-Os -flto -target x86_64-apple-macos10.13 -Wno-overriding-t-option' -DWITH_LIBSODIUM=OFF -DWITH_LIBBSD=OFF -DBUILD_TESTS=OFF -DWITH_DOCS=OFF -DENABLE_DRAFTS=OFF -DBUILD_SHARED=OFF
        make -j4

    - name: Build p2pool
      run: |
        mkdir build
        cd build
        cmake .. -DCMAKE_C_COMPILER="$(brew --prefix llvm@15)/bin/clang" -DCMAKE_CXX_COMPILER="$(brew --prefix llvm@15)/bin/clang++" -DCMAKE_AR="$(brew --prefix llvm@15)/bin/llvm-ar" -DCMAKE_RANLIB="$(brew --prefix llvm@15)/bin/llvm-ranlib" -DCMAKE_C_FLAGS='-flto -target x86_64-apple-macos10.13 -Wno-overriding-t-option' -DCMAKE_CXX_FLAGS='-flto -target x86_64-apple-macos10.13 -Wno-overriding-t-option' -DCMAKE_ASM_FLAGS='-target x86_64-apple-macos10.13' -DSTATIC_LIBS=ON
        make -j4 p2pool

    - name: Run RandomX tests
      run: |
        cd build
        ./p2pool --test
        make -j4 randomx-tests
        external/src/RandomX/randomx-tests

    - name: Build tests
      run: |
        cd tests
        mkdir build
        cd build
        cmake .. -DCMAKE_C_COMPILER="$(brew --prefix llvm@15)/bin/clang" -DCMAKE_CXX_COMPILER="$(brew --prefix llvm@15)/bin/clang++" -DCMAKE_AR="$(brew --prefix llvm@15)/bin/llvm-ar" -DCMAKE_RANLIB="$(brew --prefix llvm@15)/bin/llvm-ranlib" -DCMAKE_C_FLAGS='-flto -target x86_64-apple-macos10.13 -Wno-overriding-t-option' -DCMAKE_CXX_FLAGS='-flto -target x86_64-apple-macos10.13 -Wno-overriding-t-option' -DSTATIC_LIBS=ON
        make -j4

    - name: Run tests
      run: |
        cd tests/build
        gunzip *.gz
        ./p2pool_tests

    - name: Archive binary
      uses: actions/upload-artifact@v4
      with:
        name: p2pool-macos-13
        path: build/p2pool

  build-macos-aarch64:

    timeout-minutes: 30
    runs-on: macos-14

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Build libcurl
      run: |
        cd external/src/curl
        cmake . -DCMAKE_C_COMPILER="$(brew --prefix llvm@15)/bin/clang" -DCMAKE_AR="$(brew --prefix llvm@15)/bin/llvm-ar" -DCMAKE_RANLIB="$(brew --prefix llvm@15)/bin/llvm-ranlib" -DCMAKE_C_FLAGS="-Os -target arm64-apple-macos-11" -DBUILD_CURL_EXE=OFF -DBUILD_SHARED_LIBS=OFF -DCURL_DISABLE_INSTALL=ON -DCURL_ENABLE_EXPORT_TARGET=OFF -DCURL_DISABLE_HEADERS_API=ON -DCURL_DISABLE_BINDLOCAL=ON -DBUILD_LIBCURL_DOCS=OFF -DBUILD_MISC_DOCS=OFF -DENABLE_CURL_MANUAL=OFF -DCURL_ZLIB=OFF -DCURL_DISABLE_ALTSVC=ON -DCURL_DISABLE_COOKIES=ON -DCURL_DISABLE_DOH=ON -DCURL_DISABLE_GETOPTIONS=ON -DCURL_DISABLE_HSTS=ON -DCURL_DISABLE_LIBCURL_OPTION=ON -DCURL_DISABLE_MIME=ON -DCURL_DISABLE_NETRC=ON -DCURL_DISABLE_NTLM=ON -DCURL_DISABLE_PARSEDATE=ON -DCURL_DISABLE_PROGRESS_METER=ON -DCURL_DISABLE_SHUFFLE_DNS=ON -DCURL_DISABLE_SOCKETPAIR=ON -DCURL_DISABLE_VERBOSE_STRINGS=ON -DHTTP_ONLY=ON -DCURL_ENABLE_SSL=OFF -DUSE_LIBIDN2=OFF -DCURL_USE_LIBPSL=OFF -DCURL_USE_LIBSSH2=OFF -DENABLE_UNIX_SOCKETS=OFF -DCURL_DISABLE_TESTS=ON -DUSE_NGHTTP2=OFF -DBUILD_EXAMPLES=OFF
        make -j3
        cd lib && mkdir .libs && cp libcurl.a .libs

    - name: Build libuv
      run: |
        cd external/src/libuv
        mkdir build
        cd build
        cmake .. -DCMAKE_C_COMPILER="$(brew --prefix llvm@15)/bin/clang" -DCMAKE_AR="$(brew --prefix llvm@15)/bin/llvm-ar" -DCMAKE_RANLIB="$(brew --prefix llvm@15)/bin/llvm-ranlib" -DCMAKE_C_FLAGS='-Os -target arm64-apple-macos-11' -DBUILD_TESTING=OFF -DLIBUV_BUILD_SHARED=OFF
        make -j3

    - name: Build libzmq
      run: |
        cd external/src/libzmq
        mkdir build
        cd build
        cmake .. -DCMAKE_C_COMPILER="$(brew --prefix llvm@15)/bin/clang" -DCMAKE_CXX_COMPILER="$(brew --prefix llvm@15)/bin/clang++" -DCMAKE_AR="$(brew --prefix llvm@15)/bin/llvm-ar" -DCMAKE_RANLIB="$(brew --prefix llvm@15)/bin/llvm-ranlib" -DCMAKE_C_FLAGS='-Os -target arm64-apple-macos-11' -DCMAKE_CXX_FLAGS='-Os -target arm64-apple-macos-11' -DWITH_LIBSODIUM=OFF -DWITH_LIBBSD=OFF -DBUILD_TESTS=OFF -DWITH_DOCS=OFF -DENABLE_DRAFTS=OFF -DBUILD_SHARED=OFF
        make -j3

    - name: Build p2pool
      run: |
        mkdir build
        cd build
        cmake .. -DCMAKE_C_COMPILER="$(brew --prefix llvm@15)/bin/clang" -DCMAKE_CXX_COMPILER="$(brew --prefix llvm@15)/bin/clang++" -DCMAKE_AR="$(brew --prefix llvm@15)/bin/llvm-ar" -DCMAKE_RANLIB="$(brew --prefix llvm@15)/bin/llvm-ranlib" -DCMAKE_C_FLAGS="-target arm64-apple-macos-11" -DCMAKE_CXX_FLAGS="-target arm64-apple-macos-11" -DSTATIC_LIBS=ON -DWITH_LTO=OFF -DARCH_ID=aarch64
        make -j3 p2pool

    - name: Run RandomX tests
      run: |
        cd build
        ./p2pool --test
        make -j3 randomx-tests
        external/src/RandomX/randomx-tests

    - name: Build tests
      run: |
        cd tests
        mkdir build
        cd build
        cmake .. -DCMAKE_C_COMPILER="$(brew --prefix llvm@15)/bin/clang" -DCMAKE_CXX_COMPILER="$(brew --prefix llvm@15)/bin/clang++" -DCMAKE_AR="$(brew --prefix llvm@15)/bin/llvm-ar" -DCMAKE_RANLIB="$(brew --prefix llvm@15)/bin/llvm-ranlib" -DCMAKE_C_FLAGS="-target arm64-apple-macos-11" -DCMAKE_CXX_FLAGS="-target arm64-apple-macos-11" -DSTATIC_LIBS=ON -DWITH_LTO=OFF -DARCH_ID=aarch64
        make -j3

    - name: Run tests
      run: |
        cd tests/build
        gunzip *.gz
        ./p2pool_tests

    - name: Archive binary
      uses: actions/upload-artifact@v4
      with:
        name: p2pool-macos-11-aarch64
        path: build/p2pool

  build-freebsd:

    timeout-minutes: 60
    runs-on: ${{ matrix.os.host }}

    strategy:
      matrix:
        os:
          - name: freebsd
            architecture: x86-64
            version: '13.3'
            host: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Delete unused files
      run: |
        rm -rf .git
        rm -rf external/lib

    - name: Build p2pool
      uses: cross-platform-actions/action@v0.24.0
      with:
        operating_system: ${{ matrix.os.name }}
        architecture: ${{ matrix.os.architecture }}
        version: ${{ matrix.os.version }}
        memory: 12G
        cpu_count: 4
        shell: bash
        run: |
          sudo pkg install -y cmake
          cd external/src/curl
          cmake . -DCMAKE_C_FLAGS="-Os -flto" -DBUILD_CURL_EXE=OFF -DBUILD_SHARED_LIBS=OFF -DCURL_DISABLE_INSTALL=ON -DCURL_ENABLE_EXPORT_TARGET=OFF -DCURL_DISABLE_HEADERS_API=ON -DCURL_DISABLE_BINDLOCAL=ON -DBUILD_LIBCURL_DOCS=OFF -DBUILD_MISC_DOCS=OFF -DENABLE_CURL_MANUAL=OFF -DCURL_ZLIB=OFF -DCURL_DISABLE_ALTSVC=ON -DCURL_DISABLE_COOKIES=ON -DCURL_DISABLE_DOH=ON -DCURL_DISABLE_GETOPTIONS=ON -DCURL_DISABLE_HSTS=ON -DCURL_DISABLE_LIBCURL_OPTION=ON -DCURL_DISABLE_MIME=ON -DCURL_DISABLE_NETRC=ON -DCURL_DISABLE_NTLM=ON -DCURL_DISABLE_PARSEDATE=ON -DCURL_DISABLE_PROGRESS_METER=ON -DCURL_DISABLE_SHUFFLE_DNS=ON -DCURL_DISABLE_SOCKETPAIR=ON -DCURL_DISABLE_VERBOSE_STRINGS=ON -DHTTP_ONLY=ON -DCURL_ENABLE_SSL=OFF -DUSE_LIBIDN2=OFF -DCURL_USE_LIBPSL=OFF -DCURL_USE_LIBSSH2=OFF -DENABLE_UNIX_SOCKETS=OFF -DCURL_DISABLE_TESTS=ON -DUSE_NGHTTP2=OFF -DBUILD_EXAMPLES=OFF
          make -j4
          cd lib && mkdir .libs && cp libcurl.a .libs
          cd ../../libuv
          mkdir build
          cd build
          cmake .. -DCMAKE_C_FLAGS='-Os -flto' -DBUILD_TESTING=OFF -DLIBUV_BUILD_SHARED=OFF
          make -j4
          cd ../../libzmq
          mkdir build
          cd build
          cmake .. -DCMAKE_C_FLAGS='-Os -flto' -DCMAKE_CXX_FLAGS='-Os -flto' -DWITH_LIBSODIUM=OFF -DWITH_LIBBSD=OFF -DBUILD_TESTS=OFF -DWITH_DOCS=OFF -DENABLE_DRAFTS=OFF -DBUILD_SHARED=OFF
          make -j4
          cd ../../../..
          mkdir build
          cd build
          cmake .. -DSTATIC_LIBS=ON
          make -j4 p2pool
          ./p2pool --test
          make -j4 randomx-tests
          external/src/RandomX/randomx-tests
          cd ../tests
          mkdir build
          cd build
          cmake .. -DSTATIC_LIBS=ON
          make -j4 p2pool_tests
          gunzip *.gz
          ./p2pool_tests

    - name: Archive binary
      uses: actions/upload-artifact@v4
      with:
        name: p2pool-${{ matrix.os.name }}-${{ matrix.os.version }}
        path: build/p2pool

  build-openbsd:

    timeout-minutes: 60
    runs-on: ${{ matrix.os.host }}

    strategy:
      matrix:
        os:
          - name: openbsd
            architecture: x86-64
            version: '7.4'
            host: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Delete unused files
      run: |
        rm -rf .git
        rm -rf external/lib

    - name: Build p2pool
      uses: cross-platform-actions/action@v0.24.0
      with:
        operating_system: ${{ matrix.os.name }}
        architecture: ${{ matrix.os.architecture }}
        version: ${{ matrix.os.version }}
        memory: 12G
        cpu_count: 4
        shell: bash
        run: |
          sudo pkg_add -I cmake libuv zeromq curl
          mkdir build && cd build
          cmake ..
          make -j4 p2pool
          cd ../tests
          mkdir build && cd build
          cmake ..
          make -j4 p2pool_tests
          gunzip *.gz
          ./p2pool_tests

    - name: Archive binary
      uses: actions/upload-artifact@v4
      with:
        name: p2pool-${{ matrix.os.name }}-${{ matrix.os.version }}
        path: build/p2pool
