# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=triton
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
_ver_base=0.9
pkgver=0.9.r4320.34e1de35
_commit=34e1de35869eb3d7e571f21e9c2561dff126e040
pkgrel=1
pkgdesc="Triton is a dynamic binary analysis library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/JonathanSalwan/Triton/"
license=('spdx:Apache-2.0')
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-capstone"
         #"${MINGW_PACKAGE_PREFIX}-bitwuzla"
         #"${MINGW_PACKAGE_PREFIX}-llvm-libs"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-z3"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-llvm-20"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-libffi"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "git")
source=("${_realname}"::"git+https://github.com/JonathanSalwan/Triton.git#commit=$_commit"
        002-cmake-install-dir.patch
        003-cmake-target-config.patch)
sha256sums=('83d18c072ac44b95d2e22b9a105489ee8c6e18a2c6f2fbeb8ce9b70b3d7c2c67'
            'b8da26b33d1cf3ff7d620987e45cbef2c32df0b68fe662b0779ffcc933b33ba9'
            '4282cdcc0ec16d1a07857855494ac2fb83b1bbc258323fe1c8b0da25b42c3985')

pkgver() {
  cd "${srcdir}/Triton"
  printf "%s.r%s.%s" "${_ver_base}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/Triton"
  patch -p1 -i "${srcdir}"/002-cmake-install-dir.patch
  patch -p1 -i "${srcdir}"/003-cmake-target-config.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_PREFIX_PATH=${MINGW_PREFIX}/opt/llvm-20 \
    -DBUILD_SHARED_LIBS=ON \
    -DBOOST_INTERFACE=ON \
    -DLLVM_INTERFACE=ON \
    -DZ3_INTERFACE=ON \
    -DBITWUZLA_INTERFACE=OFF \
    -DPYTHON_BINDINGS_AUTOCOMPLETE=OFF \
    "${_extra_config[@]}" \
    ../triton

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

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

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  # support relocation
  local MINGW_PREFIX_W=$(cygpath -m "${MINGW_PREFIX}")
  sed -i "s#${MINGW_PREFIX_W}#\${_IMPORT_PREFIX}#g" "${pkgdir}${MINGW_PREFIX}"/lib/cmake/triton/tritonTargets.cmake
  sed -i "s#${MINGW_PREFIX_W}#\${PACKAGE_PREFIX_DIR}#g" "${pkgdir}${MINGW_PREFIX}"/lib/cmake/triton/tritonConfig.cmake
  sed -i "s#${MINGW_PREFIX}#\${PACKAGE_PREFIX_DIR}#g" "${pkgdir}${MINGW_PREFIX}"/lib/cmake/triton/tritonConfig.cmake

  # License
  install -Dm644 "${srcdir}/Triton/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
