#!/usr/bin/make -f

OPTIONS = -DENABLE_PIC=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo

ifeq ($(DEB_HOST_ARCH),armel)
	OPTIONS += -DCROSS_COMPILE_ARM=ON
endif

ifneq (,$(filter $(DEB_HOST_ARCH),amd64))
	OPTIONS += -DENABLE_ASSEMBLY=ON
else
	OPTIONS += -DENABLE_ASSEMBLY=OFF
endif

ifeq ($(DEB_HOST_ARCH),powerpc)
	OPTIONS += -DENABLE_ALTIVEC=OFF -DCPU_POWER8=OFF
endif

# Doesn't build with libsvthevcenc-dev 1.4.1
# Missing EbTime.h include from SVT_HEVC
#
#ifeq ($(DEB_HOST_ARCH),amd64)
#	SVT_OPTIONS = -DENABLE_SVT_HEVC=1 -DSVT_HEVC_INCLUDE_DIR=/usr/include/svt-hevc
#endif

export NASMENV=-w-macro-params-legacy

%:
	dh $@ -Scmake+ninja --builddirectory=build/linux/8bit -Dsource --with quilt

override_dh_auto_configure:
	dh_auto_configure --builddirectory=build/linux/12bit -- \
	-DENABLE_CLI=OFF \
	-DENABLE_SHARED=OFF \
	-DEXPORT_C_API=OFF \
	-DHIGH_BIT_DEPTH=ON \
	-DMAIN12=ON \
	$(OPTIONS) $(SVT_OPTIONS)

	dh_auto_configure --builddirectory=build/linux/10bit -- \
	-DENABLE_CLI=OFF \
	-DENABLE_SHARED=OFF \
	-DEXPORT_C_API=OFF \
	-DHIGH_BIT_DEPTH=ON \
	$(OPTIONS) $(SVT_OPTIONS)

	dh_auto_configure -- \
	-DEXTRA_LIB="x265_main10.a;x265_main12.a" \
	-DEXTRA_LINK_FLAGS=-L. \
	-DLINKED_10BIT=ON \
	-DLINKED_12BIT=ON \
	-DLIB_INSTALL_DIR=lib/$(DEB_HOST_MULTIARCH) \
	$(OPTIONS) $(SVT_OPTIONS)

#	exit 1

override_dh_auto_build-arch:
	dh_auto_build --builddirectory=build/linux/12bit
	dh_auto_build --builddirectory=build/linux/10bit

	mv build/linux/10bit/libx265.a build/linux/8bit/libx265_main10.a
	mv build/linux/12bit/libx265.a build/linux/8bit/libx265_main12.a

	dh_auto_build

debian/arscript:
	sed 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/' debian/arscript.in > debian/arscript

override_dh_auto_install: debian/arscript
	dh_auto_install

	rm debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libx265.a
	ar -M <debian/arscript

	LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH):$LD_LIBRARY_PATH \
	help2man --no-discard-stderr $(CURDIR)/debian/tmp/usr/bin/x265 > debian/x265.1

override_dh_auto_build-indep:
	$(MAKE) -C doc/reST html

override_dh_installdocs-indep:
	dh_installdocs -plibx265-doc --doc-main-package=libx265-doc \
	-plibx265-dev

	dh_sphinxdoc

override_dh_installchangelogs:
	dh_installchangelogs doc/reST/releasenotes.rst

override_dh_clean:
	dh_clean debian/arscript

override_dh_auto_clean:
	dh_auto_clean --builddirectory=build/linux/10bit
	dh_auto_clean --builddirectory=build/linux/12bit
	dh_auto_clean

	$(RM) -r doc/reST/build

override_dh_auto_test:
