# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=directxmath
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
# Version from CMakeLists.txt
pkgver=3.20.b
_tag=apr2025
pkgrel=1
pkgdesc="DirectXMath is an all inline SIMD C++ linear algebra library for use in games and graphics apps (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://go.microsoft.com/fwlink/?LinkID=615560'
msys2_changelog_url='https://github.com/microsoft/DirectXMath/blob/main/CHANGELOG.md'
msys2_repository_url="https://github.com/microsoft/DirectXMath"
msys2_references=(
  'aur: mingw-w64-directxmath'
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://github.com/microsoft/DirectXMath/archive/${_tag}/${_realname}-${_tag}.tar.gz"
        '001-path-adjustment.patch')
sha256sums=('8ac7f060d7fcc971d77c5a9f3ff98548ecf119c144a7c2dd9d21f4b66ba94cd4'
            '749352c566989b0bd4ad39b5185afc236b581e6b51aaec786ba6b9dfb5fc8e39')

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

prepare() {
    cd "${srcdir}"/DirectXMath-${_tag}

    # Upstream changed the path for installed files to be located directly in
    # ${prefix}/include/ instead of ${prefix}/include/directxmath/. See
    # <https://github.com/microsoft/DirectXMath/commit/78922ff9e7aea7384643b5734eec6fa6b163575b>.
    # That causes conflicts with the headers-git package which also has a file
    # at ${prefix}/include/directxmath.h. The patch reverts that so that the
    # old location is kept and the file location conflict is avoided.
    apply_patch_with_msg \
      001-path-adjustment.patch
}

build() {
  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=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -S "${_realname}-${_tag}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

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