# Contributor: Kreijstal <rainb@tfwno.gf>

_realname=ollama
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgdesc='Create, run and share large language models (LLMs) (mingw-w64)'
pkgver=0.13.5
pkgrel=1
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://ollama.com'
msys2_repository_url='https://github.com/ollama/ollama'
msys2_references=(
  'anitya: 377712'
  'archlinux: ollama'
  'cpe: cpe:2.3:a:ollama:ollama'
  'purl: pkg:golang/github.com/ollama/ollama'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-go"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-openblas"
             "${MINGW_PACKAGE_PREFIX}-shaderc"
             "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             git)
optdepends=("${MINGW_PACKAGE_PREFIX}-openblas: BLAS Backend"
            "${MINGW_PACKAGE_PREFIX}-vulkan-loader: Vulkan Backend")
source=(https://github.com/ollama/ollama/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        0001-no-static-linking.patch
        0002-fix-dynamic-libraries-path.patch
        0003-do-not-install-vulkan-dll.patch)
sha256sums=('6b6bc20a52c11341aa296eecce5ee6782f05815224a4196983b0aa2f1453c19f'
            '8dfbc740dab6b28e826cc038d321f836f034290aadf0f946e8230c2a4f57df26'
            '019f8025bec15a67ca32fd60a5170457fee4d88993469ba583f148f3cefd6bd0'
            '7f64f33b52a6c37bc6e609f09bf88df5bc0ba9db90bb077f4284588865227d14')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${pkgver}

  apply_patch_with_msg \
    0001-no-static-linking.patch \
    0002-fix-dynamic-libraries-path.patch \
    0003-do-not-install-vulkan-dll.patch

  if check_option "debug" "n"; then
    sed -i 's,var mode string = gin.DebugMode,var mode string = gin.ReleaseMode,g' server/routes.go
  fi
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  if [[ ${CARCH} == aarch64 ]]; then
    # See https://github.com/ggerganov/llama.cpp/blob/325afb37/cmake/arm64-windows-llvm.cmake#L12
    local _flags="-march=armv8.7-a -fvectorize -ffp-model=fast -fno-finite-math-only"
    CFLAGS+=" ${_flags}"
    CXXFLAGS+=" ${_flags}"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DGGML_WIN_VER="0xA00" \
    -DGGML_OPENMP=ON \
    -DGGML_BLAS=ON \
    -DGGML_BLAS_VENDOR=OpenBLAS \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  export CGO_CFLAGS="$CFLAGS" CGO_CPPFLAGS="$CPPFLAGS" CGO_CXXFLAGS="$CXXFLAGS" CGO_LDFLAGS="$LDFLAGS"
  export GOPROXY=direct
  export GOROOT=${MINGW_PREFIX}/lib/go
  export GOPATH=${MINGW_PREFIX}/

  go build -ldflags "${LDFLAGS} -X=github.com/ollama/ollama/version.Version=${pkgver}" .
}

check() {
  $srcdir/build-$MSYSTEM/ollama --version >/dev/null

  cd "build-${MSYSTEM}"
  go test .
}

package() {
  cd $srcdir/build-$MSYSTEM

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  cp lib/ollama/libggml-blas.dll "${pkgdir}"${MINGW_PREFIX}/lib/ollama

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/bin
  install ollama.exe -t"${pkgdir}"${MINGW_PREFIX}/bin

  # they are installed in lib/ollama, not sure why they get installed twice
  rm "${pkgdir}${MINGW_PREFIX}/bin/libggml-"*.dll

  install -Dm644 LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
