# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Andrzej Giniewicz <gginiu@gmail.com>
# Contributor: Johan Förberg <johan@forberg.se>
# PGO Version: Laio O. Seman <laio@ieee.org>

_optimize_znver4=${_optimize_znver4-}

pkgname=zstd
pkgver=1.5.7
pkgrel=4
pkgdesc='Zstandard - Fast real-time compression algorithm'
url='https://facebook.github.io/zstd/'
arch=(x86_64)
license=(
  BSD-3-Clause
  GPL-2.0-only
)
depends=(
  glibc
  libgcc
  libstdc++
  zlib
  xz
  lz4
)
makedepends=(
  cmake
  gtest
  ninja
)
provides=(libzstd.so)
source=(https://github.com/facebook/zstd/releases/download/v${pkgver}/zstd-${pkgver}.tar.zst{,.sig}
        https://github.com/facebook/zstd/commit/6af3842118ea5325480b403213b2a9fbed3d3d74.patch)
sha256sums=('5b331d961d6989dc21bb03397fc7a2a4d86bc65a14adc5ffbbce050354e30fd2'
            'SKIP'
            '7c07be222d45718b81fb16f97e611adfeb24efa0712ca77fb1e08f4c67803ec3')
b2sums=('20df0493d9d960b8fc98f7cac7630f84dae6226bd906364ab9c9ce7dfb2be44a303fd077e4264979f3ee92efd59072d63c4bb4ae7b65c52f6ce0153513088837'
        'SKIP'
        '02e472b01ead30ebc703f64d35ccbfe1db8a9eae6b15a3690e6e5092c6b8cdfaaca585b31b965d94eea7da04cebd6bcca7f5c8bbf9e2c2e84b4acd7f3ce54e5d')
validpgpkeys=(4EF4AC63455FC9F4545D9B7DEF8FE99528B52FFD)
options=('debug')
prepare() {
  cd ${pkgname}-${pkgver}
  # avoid error on tests without static libs, we use LD_LIBRARY_PATH
  sed '/build static library to build tests/d' -i build/cmake/CMakeLists.txt
  sed 's/libzstd_static/libzstd_shared/g' -i build/cmake/tests/CMakeLists.txt
  # fix manpages
  patch -Np1 -i ../6af3842118ea5325480b403213b2a9fbed3d3d74.patch

  # PGO Patch
  echo '
	# Apply PGO-specific flags
	if(PGO_GENERATE)
      set_target_properties(zstd PROPERTIES COMPILE_FLAGS "-fprofile-generate")
      message(STATUS "PGO GENERATE")
	    target_link_libraries(zstd gcov)
	endif()

	if(PGO_USE)
      set_target_properties(zstd PROPERTIES COMPILE_FLAGS "-fprofile-use -Werror=missing-profile -Wno-error=coverage-mismatch")
	endif()
  ' >> build/cmake/programs/CMakeLists.txt
}

build() {
  cd ${pkgname}-${pkgver}

  # https://github.com/facebook/zstd/issues/3819
  # XXH64_update can be vectorized with AVX-512, but this is disabled by default
  # due to Skylake's high latency for AVX-512 instructions. Re-enable this for
  # Znver4, since unlike Intel u-archs, it doesn't suffer from high AVX-512 latency.
  xxh64_vectorize=''

  if [ -n "$_optimize_znver4" ]; then
    xxh64_vectorize="-DXXH_ENABLE_AUTOVECTORIZE=ON"
  fi

  export CFLAGS+=" -ffat-lto-objects ${xxh64_vectorize}"
  export CXXFLAGS+=" -ffat-lto-objects ${xxh64_vectorize}"

  # First build pass: Generate profile
  cmake -S build/cmake -B build -G Ninja \
    -DCMAKE_BUILD_TYPE=None \
      -DCMAKE_INSTALL_PREFIX=/usr \
      -DZSTD_ZLIB_SUPPORT=ON \
      -DZSTD_LZMA_SUPPORT=ON \
      -DZSTD_LZ4_SUPPORT=ON \
      -DZSTD_BUILD_CONTRIB=ON \
      -DZSTD_BUILD_STATIC=OFF \
      -DZSTD_BUILD_TESTS=ON \
      -DZSTD_PROGRAMS_LINK_SHARED=ON \
      -DPGO_GENERATE=ON \
      -DPGO_USE=OFF
  cmake --build build

  build/programs/zstd -b19i1
  build/programs/zstd -b16i1
  build/programs/zstd -b9i2
  build/programs/zstd -b
  build/programs/zstd -b7i2
  build/programs/zstd -b5

  # clean files
  cmake --build build --target clean

  # Second build pass: Use profile
  cmake -S build/cmake -B build -G Ninja \
    -DCMAKE_BUILD_TYPE=None \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DZSTD_ZLIB_SUPPORT=ON \
    -DZSTD_LZMA_SUPPORT=ON \
    -DZSTD_LZ4_SUPPORT=ON \
    -DZSTD_BUILD_CONTRIB=ON \
    -DZSTD_BUILD_STATIC=OFF \
    -DZSTD_BUILD_TESTS=ON \
    -DZSTD_PROGRAMS_LINK_SHARED=ON \
    -DPGO_GENERATE=OFF \
    -DPGO_USE=ON

  cmake --build build
}


check() {
  cd ${pkgname}-${pkgver}
  LD_LIBRARY_PATH="$(pwd)/build/lib" \
    ctest -VV --test-dir build
}

package() {
  cd ${pkgname}-${pkgver}
  DESTDIR="${pkgdir}" cmake --install build
  install -Dm 644 LICENSE -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

# vim: ts=2 sw=2 et:
