# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>
# Contributor: Sarah Ottinger <schalaalexiazeal@gmail.com>

_realname=sccache
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.12.0
pkgrel=2
pkgdesc='Shared compilation cache (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/mozilla/sccache"
msys2_references=(
  'archlinux: sccache'
  'purl: pkg:cargo/sccache'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-openssl")
options=('!strip')
source=("https://github.com/mozilla/sccache/archive/v$pkgver/${_realname}-${pkgver}.tar.gz"
        "zstd-sys.tar.gz::https://crates.io/api/v1/crates/zstd-sys/2.0.10+zstd.1.5.6/download"
        "link-zstd-dynamically.patch")
sha256sums=('309edaf43f44088e55e99ce14eb9cfdee8f85acad290171ebfd29eb9e368def3'
            'c253a4914af5bafc8fa8c86ee400827e83cf6ec01195ec1f1ed8441bf00d65aa'
            '48f4900ceb02d3aaf9a1020f33d56629156e96759f456c0e7ca18bfcf910767b')

_env() {
  export OPENSSL_NO_VENDOR=1
  export PKG_CONFIG_ALL_DYNAMIC=1
  export ZSTD_SYS_USE_PKG_CONFIG=1
  export WINAPI_NO_BUNDLED_LIBRARIES=1
  export RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static}"
}

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

  patch -d ../zstd-sys-2.0.10+zstd.1.5.6 -i ../link-zstd-dynamically.patch
  cat >> Cargo.toml <<END

[patch.crates-io]
zstd-sys.path = "../zstd-sys-2.0.10+zstd.1.5.6"
END

  cargo update -p zstd-sys
  # fix CLANG* issue: https://github.com/mozilla/sccache/issues/2373
  cargo update -p blake3 --precise 1.8.2
  cargo fetch --locked
}

build() {
  cd "${_realname}-${pkgver}"

  _env
  cargo build --release --frozen
}

check() {
  cd "${_realname}-${pkgver}"

  _env
  cargo test --release --frozen
}

package() {
  cd "${_realname}-${pkgver}"

  _env
  cargo install \
    --frozen \
    --offline \
    --no-track \
    --path . \
    --root "${pkgdir}${MINGW_PREFIX}"

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