name: Build on Windows

on:
  push:
    branches:
    - '*'
    paths:
    - .github/workflows/build-windows.yml
    - build/CMakeLists.txt
    - build/cmake_modules/**
    - daemon/**
    - i18n/**
    - libi2pd/**
    - libi2pd_client/**
    - Win32/**
    - Makefile
    - Makefile.mingw
    tags:
    - '*'
  pull_request:
    branches:
    - '*'

defaults:
  run:
    shell: msys2 {0}

jobs:
  build:
    name: ${{ matrix.arch }}
    runs-on: windows-latest

    strategy:
      fail-fast: false
      matrix:
        include: [
          { msystem: UCRT64, arch: ucrt-x86_64, arch_short: x64-ucrt, compiler: gcc },
          { msystem: CLANG64, arch: clang-x86_64, arch_short: x64-clang, compiler: clang },
          { msystem: MINGW64, arch: x86_64, arch_short: x64, compiler: gcc },
          # { msystem: MINGW32, arch: i686, arch_short: x86, compiler: gcc }
        ]

    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Setup MSYS2
      uses: msys2/setup-msys2@v2
      with:
        msystem: ${{ matrix.msystem }}
        install: base-devel git mingw-w64-${{ matrix.arch }}-${{ matrix.compiler }} mingw-w64-${{ matrix.arch }}-boost mingw-w64-${{ matrix.arch }}-openssl mingw-w64-${{ matrix.arch }}-miniupnpc
        update: true

    - name: Install additional clang packages
      if: ${{ matrix.msystem == 'CLANG64' }}
      run: pacman --noconfirm -S mingw-w64-${{ matrix.arch }}-gcc-compat

    - name: Build application
      run: |
        mkdir -p obj/Win32 obj/libi2pd obj/libi2pd_client obj/daemon
        make USE_UPNP=yes DEBUG=no USE_GIT_VERSION=yes -j3

    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: i2pd-${{ matrix.arch_short }}.exe
        path: i2pd.exe

  build-cmake:
    name: CMake ${{ matrix.arch }}
    runs-on: windows-latest

    strategy:
      fail-fast: false
      matrix:
        include: [
          { msystem: UCRT64, arch: ucrt-x86_64, arch_short: x64-ucrt, compiler: gcc },
          { msystem: CLANG64, arch: clang-x86_64, arch_short: x64-clang, compiler: clang },
          { msystem: MINGW64, arch: x86_64, arch_short: x64, compiler: gcc },
          # { msystem: MINGW32, arch: i686, arch_short: x86, compiler: gcc }
        ]

    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Setup MSYS2
      uses: msys2/setup-msys2@v2
      with:
        msystem: ${{ matrix.msystem }}
        install: base-devel git mingw-w64-${{ matrix.arch }}-cmake mingw-w64-${{ matrix.arch }}-ninja mingw-w64-${{ matrix.arch }}-${{ matrix.compiler }} mingw-w64-${{ matrix.arch }}-boost mingw-w64-${{ matrix.arch }}-openssl mingw-w64-${{ matrix.arch }}-miniupnpc
        update: true

    - name: Build application
      run: |
        cd build
        cmake -DWITH_GIT_VERSION=ON -DWITH_STATIC=ON -DWITH_UPNP=ON -DCMAKE_BUILD_TYPE=Release .
        cmake --build . -- -j3

    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: i2pd-cmake-${{ matrix.arch_short }}.exe
        path: build/i2pd.exe

  build-xp:
    if: false
    name: XP
    runs-on: windows-latest

    strategy:
        fail-fast: false

    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Setup MSYS2
      uses: msys2/setup-msys2@v2
      with:
        msystem: MINGW32
        install: base-devel git mingw-w64-i686-gcc mingw-w64-i686-boost mingw-w64-i686-openssl mingw-w64-i686-miniupnpc
        cache: true
        update: true

    - name: Clone MinGW packages repository and revert boost to 1.85.0
      run: |
        git clone https://github.com/msys2/MINGW-packages
        cd MINGW-packages
        git checkout 4cbb366edf2f268ac3146174b40ce38604646fc5 mingw-w64-boost
        cd mingw-w64-boost
        sed -i 's/boostorg.jfrog.io\/artifactory\/main/archives.boost.io/' PKGBUILD

    # headers
    - name: Get headers package version
      id:  version-headers
      run: |
        echo "version=$(pacman -Si mingw-w64-i686-headers-git | grep -Po '^Version\s*: \K.+')" >> $GITHUB_OUTPUT
    - name: Cache headers package
      uses: actions/cache@v4
      id: cache-headers
      with:
        path: MINGW-packages/mingw-w64-headers-git/*.zst
        key: winxp-headers-${{ steps.version-headers.outputs.version }}
    - name: Build WinXP-capable headers package
      if: steps.cache-headers.outputs.cache-hit != 'true'
      run: |
        cd MINGW-packages/mingw-w64-headers-git
        sed -i 's/0x601/0x501/' PKGBUILD
        MINGW_ARCH=mingw32 makepkg-mingw -sCLf --noconfirm --nocheck
    - name: Install headers package
      run: pacman --noconfirm -U MINGW-packages/mingw-w64-headers-git/mingw-w64-i686-*-any.pkg.tar.zst

    # CRT
    - name: Get crt package version
      id:  version-crt
      run: |
        echo "version=$(pacman -Si mingw-w64-i686-crt-git | grep -Po '^Version\s*: \K.+')" >> $GITHUB_OUTPUT
    - name: Cache crt package
      uses: actions/cache@v4
      id: cache-crt
      with:
        path: MINGW-packages/mingw-w64-crt-git/*.zst
        key: winxp-crt-${{ steps.version-crt.outputs.version }}
    - name: Build WinXP-capable crt package
      if: steps.cache-crt.outputs.cache-hit != 'true'
      run: |
        cd MINGW-packages/mingw-w64-crt-git
        MINGW_ARCH=mingw32 makepkg-mingw -sCLf --noconfirm --nocheck
    - name: Install crt package
      run: pacman --noconfirm -U MINGW-packages/mingw-w64-crt-git/mingw-w64-i686-*-any.pkg.tar.zst

    # winpthreads
    - name: Get winpthreads package version
      id:  version-winpthreads
      run: |
        echo "version=$(pacman -Si mingw-w64-i686-winpthreads-git | grep -Po '^Version\s*: \K.+')" >> $GITHUB_OUTPUT
    - name: Cache winpthreads package
      uses: actions/cache@v4
      id: cache-winpthreads
      with:
        path: MINGW-packages/mingw-w64-winpthreads/*.zst
        key: winxp-winpthreads-${{ steps.version-winpthreads.outputs.version }}
    - name: Build WinXP-capable winpthreads package
      if: steps.cache-winpthreads.outputs.cache-hit != 'true'
      run: |
        cd MINGW-packages/mingw-w64-winpthreads
        MINGW_ARCH=mingw32 makepkg-mingw -sCLf --noconfirm --nocheck
    - name: Install winpthreads package
      run: pacman --noconfirm -U MINGW-packages/mingw-w64-winpthreads/mingw-w64-i686-*-any.pkg.tar.zst

    # OpenSSL
    - name: Get openssl package version
      id:  version-openssl
      run: |
        echo "version=$(pacman -Si mingw-w64-i686-openssl | grep -Po '^Version\s*: \K.+')" >> $GITHUB_OUTPUT
    - name: Cache openssl package
      uses: actions/cache@v4
      id: cache-openssl
      with:
        path: MINGW-packages/mingw-w64-openssl/*.zst
        key: winxp-openssl-${{ steps.version-openssl.outputs.version }}
    - name: Build WinXP-capable openssl package
      if: steps.cache-openssl.outputs.cache-hit != 'true'
      run: |
        cd MINGW-packages/mingw-w64-openssl
        curl https://openssl-library.org/source/pubkeys.asc | gpg --import
        MINGW_ARCH=mingw32 makepkg-mingw -sCLf --noconfirm --nocheck
    - name: Install openssl package
      run: pacman --noconfirm -U MINGW-packages/mingw-w64-openssl/mingw-w64-i686-*-any.pkg.tar.zst

    # Boost
    #- name: Get boost package version
    #  id:  version-boost
    #  run: |
    #    echo "version=$(pacman -Si mingw-w64-i686-boost | grep -Po '^Version\s*: \K.+')" >> $GITHUB_OUTPUT
    - name: Cache boost package
      uses: actions/cache@v4
      id: cache-boost
      with:
        path: MINGW-packages/mingw-w64-boost/*.zst
        key: winxp-boost-1.85.0+crt-${{ steps.version-headers.outputs.version }}+ossl-${{ steps.version-openssl.outputs.version }}
    - name: Build WinXP-capable boost package
      if: steps.cache-boost.outputs.cache-hit != 'true'
      run: |
        cd MINGW-packages/mingw-w64-boost
        MINGW_ARCH=mingw32 makepkg-mingw -sCLf --noconfirm --nocheck
    - name: Remove boost packages
      run: pacman --noconfirm -R mingw-w64-i686-boost mingw-w64-i686-boost-libs
    - name: Install boost package
      run: pacman --noconfirm -U MINGW-packages/mingw-w64-boost/mingw-w64-i686-*-any.pkg.tar.zst

    # Building i2pd
    - name: Build application
      run: |
        mkdir -p obj/Win32 obj/libi2pd obj/libi2pd_client obj/daemon
        make USE_UPNP=yes DEBUG=no USE_GIT_VERSION=yes USE_WINXP_FLAGS=yes -j3
    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: i2pd-xp.exe
        path: i2pd.exe
