# Maintainer:  Matheus Catarino <matheus-catarino@hotmail.com>

_realname=zig
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.15.1
pkgrel=2
pkgdesc='A general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software (mingw-w64)'
arch=('any')
mingw_arch=('clang64' 'clangarm64')
url='https://ziglang.org'
msys2_repository_url='https://github.com/ziglang/zig/'
msys2_documentation_url='https://github.com/ziglang/zig?tab=readme-ov-file#readme'
msys2_references=(
  'archlinux: zig'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-clang-libs"
         "${MINGW_PACKAGE_PREFIX}-llvm-libs"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-lld")
source=("https://github.com/ziglang/zig/archive/${pkgver}/zig-${pkgver}.tar.gz"
        "001-support-llvm-21.patch")
sha256sums=('f7d587087e551a206ad2e1c1a10e8124f5ecaec1f1efc69b0de6df89af80245c'
            '391f97e14d8ecdf039a52e2cef16fc39b8551edeeb76980d2299e0f89bb56cb3')

prepare(){
  cd ${_realname}-${pkgver}
  # https://github.com/ziglang/zig/pull/25055
  patch -p1 -i "${srcdir}"/001-support-llvm-21.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 -Wno-dev \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_C_COMPILER=clang \
    -DCMAKE_CXX_COMPILER=clang++ \
    -DZIG_HOST_TARGET_TRIPLE=${CARCH}-windows-gnu \
    -DZIG_TARGET_TRIPLE=${CARCH}-windows-gnu \
    -DZIG_SHARED_LLVM=ON \
    -DZIG_STATIC_ZLIB=OFF \
    -DZIG_STATIC_ZSTD=OFF \
    -DZIG_SYSTEM_LIBCXX=c++ \
    -DZIG_TARGET_MCPU=baseline \
    -DZIG_USE_LLVM_CONFIG=ON \
    "${_extra_config[@]}" \
    -S ${_realname}-${pkgver} \
    -B build-${MSYSTEM}

  cmake --build build-${MSYSTEM}
}

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

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