name: cmake-win64

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]
  workflow_dispatch:

env:
  ILOC: d:/a/local
  
jobs:
  build:
    name: cmake-win64
    runs-on: windows-latest
    steps:
      - uses: ilammy/setup-nasm@v1
      - name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )"
        uses: actions/checkout@v4
        with:
          submodules: recursive
      - run: git fetch --prune --unshallow --tags
      - name: Get the version
        id: get_version
        run: |
             $git_info=$(git describe --tags HEAD)
             echo "version=${git_info}" >> $env:GITHUB_OUTPUT
      - name: Setup Installation Location
        run: |
             mkdir ${{env.ILOC}}
      - name: Uninstall Perl
        run: |
             choco uninstall strawberryperl

      - name: Build and Install zlib-ng
        shell: cmd
        run: |
             git clone --depth 1 https://github.com/zlib-ng/zlib-ng.git
             cd zlib-ng
             cmake -Bbuild -DCMAKE_PREFIX_PATH=${{env.ILOC}} -DCMAKE_INSTALL_PREFIX=${{env.ILOC}} -DBUILD_SHARED_LIBS=OFF -DZLIB_COMPAT=ON -DZLIB_ENABLE_TESTS=OFF -DINSTALL_UTILS=OFF
             cmake --build build --config Release --target install
             cd ..

      - name: Build and Install libpng
        shell: cmd
        run: |
             curl -sSL https://download.sourceforge.net/libpng/lpng1643.zip -o lpng1643.zip
             unzip.exe -qq lpng1643.zip
             cd lpng1643
             cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=${{env.ILOC}} -DCMAKE_INSTALL_PREFIX=${{env.ILOC}} -DPNG_TESTS=OFF -DPNG_SHARED=OFF
             cmake --build build --config Release --target install
             cd ..

      - name: Build and Install giflib
        shell: cmd
        run: |
             git clone --depth 1 https://github.com/xbmc/giflib.git
             cd giflib
             cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=${{env.ILOC}} -DCMAKE_INSTALL_PREFIX=${{env.ILOC}}
             cmake --build build --config Release --target install
             cd ..
             
      - name: Build and Install libjpeg
        shell: cmd
        run: |
             git clone --depth 1 https://github.com/libjpeg-turbo/libjpeg-turbo.git
             cd libjpeg-turbo
             cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=${{env.ILOC}} -DCMAKE_INSTALL_PREFIX=${{env.ILOC}} -DWITH_TURBOJPEG=OFF
             cmake --build build --config Release --target install
             cd ..

      - name: Build and Install openjpeg
        shell: cmd
        run: |
             git clone --depth 1 https://github.com/uclouvain/openjpeg.git
             cd openjpeg
             cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=${{env.ILOC}} -DCMAKE_INSTALL_PREFIX=${{env.ILOC}}
             cmake --build build --config Release --target install
             cd ..

      - name: Build and Install webp
        shell: cmd
        run: |
             git clone --depth 1 https://chromium.googlesource.com/webm/libwebp
             cd libwebp
             cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=${{env.ILOC}} -DCMAKE_INSTALL_PREFIX=${{env.ILOC}} -DWEBP_BUILD_WEBP_JS=OFF -DWEBP_BUILD_ANIM_UTILS=OFF -DWEBP_BUILD_CWEBP=OFF -DWEBP_BUILD_DWEBP=OFF -DWEBP_BUILD_GIF2WEBP=OFF -DWEBP_BUILD_IMG2WEBP=OFF -DWEBP_BUILD_VWEBP=OFF -DWEBP_BUILD_WEBPMUX=OFF -DWEBP_BUILD_EXTRAS=OFF -DWEBP_BUILD_WEBP_JS=OFF
             cmake --build build --config Release --target install
             cd ..

      - name: Build and Install jbigkit
        shell: cmd
        run: |
             git clone --depth 1 https://github.com/zdenop/jbigkit.git
             cd jbigkit
             cmake -Bbuild -DCMAKE_PREFIX_PATH=${{env.ILOC}} -DCMAKE_INSTALL_PREFIX=${{env.ILOC}} -DBUILD_PROGRAMS=OFF -DBUILD_TOOLS=OFF -DCMAKE_WARN_DEPRECATED=OFF
             cmake --build build --config Release --target install
             cd ..
             
      - name: Build and Install zstd
        shell: cmd
        run: |
             git clone --depth 1 https://github.com/facebook/zstd.git
             cd zstd\build\cmake
             cmake -Bbuild -DCMAKE_PREFIX_PATH=${{env.ILOC}} -DCMAKE_INSTALL_PREFIX=${{env.ILOC}}
             cmake --build build --config Release --target install
             cd ..

      - name: Build and Install lzma
        shell: cmd
        run: |
             curl -sSL https://sourceforge.net/projects/lzmautils/files/xz-5.4.3.tar.zst/download -o xz-5.4.5.tar.zst
             zstd -d xz-5.4.5.tar.zst
             tar xf  xz-5.4.5.tar
             cd xz-5.4.5
             ; git clone --depth 1 https://github.com/tukaani-project/xz.git
             ; cd xz
             ; cmake -Bbuild -DCMAKE_PREFIX_PATH=${{env.ILOC}} -DCMAKE_INSTALL_PREFIX=${{env.ILOC}} -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=ON
             ; cmake --build build --config Release --target install
             ; cd ..
             
      - name: Build and Install libtiff
        shell: cmd
        run: |
             git clone --depth 1 https://gitlab.com/libtiff/libtiff.git
             cd libtiff
             cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=${{env.ILOC}} -DCMAKE_INSTALL_PREFIX=${{env.ILOC}} -Dtiff-tools=OFF -Dtiff-tests=OFF -Dtiff-contrib=OFF -Dtiff-docs=OFF
             cmake --build build --config Release --target install
             cd ..

      - name: Build and Install leptonica
        shell: cmd
        run: |
             cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=${{env.ILOC}} -DCMAKE_INSTALL_PREFIX=${{env.ILOC}} -DSW_BUILD=OFF -DBUILD_PROG=OFF -DBUILD_SHARED_LIBS=ON -DBUILD_PROG=ON
             cmake --build build --config Release --target install

      - name: Test leptonica
        shell: cmd
        run: |
          echo "Testing leptonica..."
          ${{env.ILOC}}/bin/fileinfo prog/test-rgb.png

      - name: Remove not needed tools
        shell: cmd
        run: >
             rm -Rf ${{env.ILOC}}/bin/*.exe
             
      - name: Upload Build Results
        uses: actions/upload-artifact@v4
        with:
          name: leptonica-${{ steps.get_version.outputs.version }}-VS2019_win64
          path: ${{env.ILOC}}
          retention-days: 5
