SUMMARY="Data compression utilities and libraries"
DESCRIPTION="LZ4 is a very fast lossless compression algorithm, providing \
compression speed at 400 MB/s per core, scalable with multi-core CPUs. It \
also features an extremely fast decoder, with speed in multiple GB/s per \
core, typically reaching RAM speed limits on multi-core systems."
HOMEPAGE="https://lz4.github.io/lz4/"
COPYRIGHT="2011-2019 Yann Collet"
LICENSE="BSD (2-clause)"
REVISION="1"
SOURCE_URI="https://github.com/lz4/lz4/archive/v$portVersion.tar.gz"
SOURCE_FILENAME="lz4-$portVersion.tar.gz"
CHECKSUM_SHA256="030644df4611007ff7dc962d981f390361e6c97a34e5cbc393ddfbe019ffe2c1"

ARCHITECTURES="all"
SECONDARY_ARCHITECTURES="x86_gcc2 x86"

libVersion="$portVersion"
libVersionCompat="$libVersion compat >= ${libVersion%%.*}"

PROVIDES="
	lz4$secondaryArchSuffix = $portVersion
	lib:liblz4$secondaryArchSuffix = $libVersionCompat
	"

if [ -z "$secondaryArchSuffix" ]; then
PROVIDES="$PROVIDES
	cmd:lz4 = $portVersion
	cmd:lz4c = $portVersion
	cmd:lz4cat = $portVersion
	cmd:unlz4 = $portVersion
	"
fi

REQUIRES="
	haiku$secondaryArchSuffix
	"

PROVIDES_devel="
	lz4${secondaryArchSuffix}_devel = $portVersion
	devel:liblz4$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES_devel="
	lz4$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	"
BUILD_PREREQUIRES="
	cmd:ar
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	"

TEST_REQUIRES="
	cmd:diff
	cmd:find
	cmd:python3
	"

PATCH()
{
	if [ "$effectiveTargetArchitecture" = x86_gcc2 ]; then
		sed -i \
			-e "s/-Wextra//" \
			-e "s/-Wswitch-enum//" \
			-e "s/-Wdeclaration-after-statement//" \
			-e "s/-Wstrict-aliasing=1//" \
			-e "s/-fvisibility=hidden//" \
			lib/Makefile \
			programs/Makefile \
			tests/Makefile \
			Makefile
	fi
}

BUILD()
{
	make -C lib $jobArgs
	make -C programs $jobArgs lz4 lz4c
}

INSTALL()
{
	make install LIBDIR="$libDir" INCLUDEDIR="$includeDir" BINDIR="$binDir"

	if [ -n "$secondaryArchSuffix" ]; then
		rm "$binDir"/lz4
		rm "$binDir"/lz4c
		rm "$binDir"/lz4cat
		rm "$binDir"/unlz4
		rmdir "$binDir"
	fi

	prepareInstalledDevelLibs liblz4
	fixPkgconfig

	packageEntries devel \
		"$developDir"
}

TEST()
{
	LIBRARY_PATH="$sourceDir/libs${LIBRARY_PATH:+:$LIBRARY_PATH}" \
	make check test
}
