pkgname=mingw-w64-libxml2
pkgver=2.15.4
pkgrel=1
arch=('any')
pkgdesc='XML C parser and toolkit (mingw-w64)'
url='https://gitlab.gnome.org/GNOME/libxml2/-/wikis/home'
depends=('mingw-w64-crt' 'mingw-w64-libiconv' 'mingw-w64-zlib' 'mingw-w64-xz')
makedepends=('git' 'mingw-w64-configure')
options=('!buildflags' '!strip' 'staticlibs')
license=(MIT)
# use GitHub mirror https://github.com/GNOME/libxml2.git if https://gitlab.gnome.org/GNOME/libxml2.git is unavailable
source=(
  "git+https://github.com/GNOME/libxml2.git#tag=v$pkgver"
#  "git+https://gitlab.gnome.org/GNOME/libxml2.git#tag=v$pkgver"
)
b2sums=('ef8e27efaffb141925019219ce3fba969d41d52007688e02ba24e8e3f8b521209d6ac975ac91e8350ea44baca9b3bb44fe2b1297003e897c79c6bb1dec4149e3')

_architectures="i686-w64-mingw32 x86_64-w64-mingw32"

prepare () {
  cd libxml2

  # avoid `ld.lld: error: unknown argument '--enable-auto-image-base'` when using LLVM-based toolchain
  [[ $pkgname =~ .*-clang-.* ]] && \
    find . -name 'libtool.m4' -exec sed -i -e 's|$wl--enable-auto-image-base||g' {} \+

  # disable doc & examples
  sed -i "s| doc example | |g" Makefile.am
  autoreconf -vfi
}

build() {
  cd libxml2

  for _arch in ${_architectures}; do
    mkdir -p build-${_arch} && pushd build-${_arch}
    ${_arch}-configure --without-python
    make
    popd
  done
}

package() {
  cd libxml2
  install -Dm644 Copyright -t "$pkgdir/usr/share/licenses/$pkgname"

  for _arch in ${_architectures}; do
    cd "${srcdir}/libxml2/build-${_arch}"
    make install DESTDIR="${pkgdir}"
    rm "${pkgdir}"/usr/${_arch}/bin/*.exe
    ${_arch}-strip --strip-unneeded "${pkgdir}"/usr/${_arch}/bin/*.dll
    ${_arch}-strip --strip-debug "${pkgdir}"/usr/${_arch}/lib/*.a
  done
}

