pkgname=shaderc
pkgver=2025.4
pkgdesc='Collection of tools, libraries and tests for shader compilation'
homepage='https://github.com/google/shaderc'
arch=('x86_64')
license=('Apache-2.0')
depends=('glibc' 'gcc' 'glslang' 'spirv-tools')
makedepends=('cmake' 'ninja' 'python' 'spirv-headers')
_patches=("shaderc-2020.4-fix-build.patch")
_patch_sums=("95cbd65997146c3ba018c81d49f90a7713f99f5bc86b6c30d6c83ca9bb139309df763d325b75c9b1d02ca60ed3bddd003eb08e64e3856867bc15d58c620341ed")
sources=("${pkgname}-${pkgver}.tar.gz"
  "${_patches[@]}"
)
urls=("https://github.com/google/${pkgname}/archive/v${pkgver}.tar.gz"
  "${_patches[@]}"
)
sha512sums=("78a1dac0bd80c447193680066970343413cf504314d0f9a0af772181cf2e673b22b0d467a06cefa9743f451e1c0c9445ade82d3ee68eae927a4c784ec2ca15e3"
  "${_patch_sums[@]}"
)

src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  for _patch in ${_patches[@]}; do
    patch -p1 -i ${filedir}/${_patch}
  done

  sed '/examples/d;/third_party/d' -i CMakeLists.txt
  sed -i \
        -e "s|\$<TARGET_FILE:spirv-dis>|/usr/bin/spirv-dis|" \
        glslc/test/CMakeLists.txt

  # Disable git versioning
  sed -i -e '/build-version/d' glslc/CMakeLists.txt

  # Manually create build-version.inc as we disabled git versioning
  local spirv_tools_ver
  if ! spirv_tools_ver="$(leaf query spirv-tools | awk '{print $3}')"; then
    leaf_error "leaf query spirv-tools failed. Please check spirv-tools installation."
  fi  
  local glslang_ver
  if ! glslang_ver="$(leaf query glslang | awk '{print $3}')"; then
    leaf_error "leaf query glslang failed. Please check glslang installation."
  fi
  cat <<- EOF > glslc/src/build-version.inc
"${pkgver}\\n"
"${spirv_tools_ver}\\n"
"${glslang_ver}\\n"
EOF
}

src_build() {
  local cmake_args=(
    -DCMAKE_BUILD_TYPE=Release
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCMAKE_CXX_FLAGS="$CXXFLAGS -ffat-lto-objects"
    -DSHADERC_SKIP_TESTS=ON
    -DSHADERC_ENABLE_WERROR_COMPILE="false"
  )
  cmake -B build "${cmake_args[@]}"
  cmake --build build
}

src_install() {
  DESTDIR="$pkgdir" cmake --install build
  #find "${pkgdir}" --type f --name "*.a" -delete
  #rm "${pkgdir}/usr/lib/pkgconfig/shaderc_static.pc"
}

# vim:ft=sh syn=sh et sw=2:
