# Template file for 'x265'
pkgname=x265
version=3.6
revision=2
build_wrksrc=source
build_style=cmake
configure_args="-DENABLE_PIC=1 -DEXTRA_LINK_FLAGS='-L.'
 -DEXTRA_LIB='x265_main10.a;x265_main12.a' -DLINKED_10BIT=TRUE -DLINKED_12BIT=TRUE"
hostmakedepends="git"
short_desc="Open Source H.265/HEVC video encoder"
maintainer="Orphaned <orphan@voidlinux.org>"
license="GPL-2.0-or-later"
homepage="http://x265.org/"
distfiles="https://bitbucket.org/multicoreware/x265_git/get/${version}.tar.gz"
checksum=206329b9599c78d06969a1b7b7bb939f7c99a459ab283b2e93f76854bd34ca7b

build_options="altivec assembly"

desc_option_altivec="Enable AltiVec support on ppc64 (POWER8 and higher only)"
desc_option_assembly="Enable assembly support on x86/ARM"

case "$XBPS_TARGET_MACHINE" in
	ppc64le*) build_options_default="altivec" ;;
	# on ARM, enabling it currently fails with undefined reference to
	# x265::setupAssemblyPrimitives(x265::EncoderPrimitives&, int)
	x86_64*|i686*) build_options_default="assembly" ;;
	*) ;;
esac

if [ "$build_option_assembly" ]; then
	case "$XBPS_TARGET_MACHINE" in
		x86_64*|i686*)
			# see https://bitbucket.org/multicoreware/x265_git/issues/559
			configure_args+=" -DCMAKE_ASM_NASM_FLAGS=-w-macro-params-legacy"
			hostmakedepends+=" nasm"
			;;
		*) ;;
	esac
fi

case "$XBPS_TARGET_MACHINE" in
	ppc64*)
		# altivec does not build without power8 being enabled
		configure_args+=" -DENABLE_ALTIVEC=$(vopt_if altivec ON OFF)
		 -DCPU_POWER8=$(vopt_if altivec ON OFF)"
		;;
	*) configure_args+=" -DENABLE_ASSEMBLY=$(vopt_if assembly ON OFF)" ;;
esac

pre_configure() {
	if [ "$CROSS_BUILD" ]; then
		vsed -i CMakeLists.txt -e "s; -mcpu=native;;"
	fi
}

post_configure() {
	# We need to build the library multiple times in order to support multiple bit depths
	# The libraries are linked together using -DDEXTRA_LIB=...

	configure_args="${configure_args/-DEXTRA_LIB=.*/}"
	configure_args="${configure_args/-DLINKED_10BIT=.*/}"
	configure_args="${configure_args/-DLINKED_12BIT=.*/}"

	# source/common/x86/intrapred16.asm:20642: error: invalid combination of opcode and operands
	case "$XBPS_TARGET_MACHINE" in
		i686*) configure_args="${configure_args/-DENABLE_ASSEMBLY=.*/} -DENABLE_ASSEMBLY=OFF" ;;
		*) ;;
	esac

	(
		cmake_builddir="build-10" \
		configure_args="${configure_args} -DHIGH_BIT_DEPTH=TRUE -DENABLE_CLI=FALSE -DEXPORT_C_API=FALSE -DENABLE_SHARED=FALSE" \
		do_configure
	)

	(
		cmake_builddir="build-12" \
		configure_args="${configure_args} -DHIGH_BIT_DEPTH=TRUE -DMAIN12=TRUE -DENABLE_CLI=FALSE -DEXPORT_C_API=FALSE -DENABLE_SHARED=FALSE" \
		do_configure
	)
}

pre_build() {
	(
		cd build-10
		export NINJA_STATUS="[1/3][%f/%t] "
		ninja ${makejobs} ${make_build_args} ${make_build_target}
	)

	(
		cd build-12
		export NINJA_STATUS="[2/3][%f/%t] "
		ninja ${makejobs} ${make_build_args} ${make_build_target}
	)

	ln -sf ../build-10/libx265.a build/libx265_main10.a
	ln -sf ../build-12/libx265.a build/libx265_main12.a
}

x265-devel_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/pkgconfig
		vmove "usr/lib/*.so"
		vmove "usr/lib/*.a"
	}
}
