#!/usr/bin/make -f

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_CPU  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

configure_flags += \
  --prefix=/usr \
  --enable-pic \
  --enable-shared \
  --disable-install-bins \
  --disable-install-srcs \
  --size-limit=16384x16384 \
  --enable-postproc \
  --enable-multi-res-encoding \
  --enable-temporal-denoising \
  --enable-vp9-temporal-denoising \
  --enable-vp9-postproc

ifeq ($(DEB_HOST_ARCH_CPU),arm)
configure_flags_neon := $(configure_flags) --target=armv7-linux-gcc
BUILD_NEON=Yes
endif

ifeq ($(DEB_HOST_ARCH), armel)
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
configure_flags += --target=armv6-linux-gcc --enable-small
else
configure_flags += --target=generic-gnu
endif
else
ifeq ($(DEB_HOST_ARCH), armhf)
# now armhf is ARMv7, but ARMv7 in vpx means NEON, which is not mandatory on armhf
# thus we use ARMv6 and -marm (since no thumb2 on ARMv6) to ensure compatability
# with all ARMv7 cores we support.
CFLAGS += -marm
CXXFLAGS += -marm
configure_flags += --target=armv6-linux-gcc --enable-small
else
ifeq ($(DEB_HOST_ARCH), amd64)
configure_flags += --target=x86_64-linux-gcc
else
ifeq ($(DEB_HOST_ARCH), kfreebsd-amd64)
configure_flags += --target=x86_64-linux-gcc
else
ifeq ($(DEB_HOST_ARCH), i386)
configure_flags += --target=x86-linux-gcc
else
ifeq ($(DEB_HOST_ARCH), kfreebsd-i386)
configure_flags += --target=x86-linux-gcc
else
ifeq ($(DEB_HOST_ARCH), hurd-i386)
configure_flags += --target=x86-linux-gcc
else
ifeq ($(DEB_HOST_ARCH), mips)
configure_flags += --target=generic-gnu
# Disable MIPS assembly for now, it requires DSP R2
CFLAGS += -DLIBYUV_DISABLE_MIPS=1
CXXFLAGS += -DLIBYUV_DISABLE_MIPS=1
else
ifeq ($(DEB_HOST_ARCH), mipsel)
configure_flags += --target=generic-gnu
# Disable MIPS assembly for now, it requires DSP R2
CFLAGS += -DLIBYUV_DISABLE_MIPS=1
CXXFLAGS += -DLIBYUV_DISABLE_MIPS=1
else
configure_flags += --target=generic-gnu
endif
endif
endif
endif
endif
endif
endif
endif
endif

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CROSS=$(DEB_HOST_GNU_TYPE)-
export CROSS
endif

CFLAGS += -Wall $(shell dpkg-buildflags --get CPPFLAGS; dpkg-buildflags --get CFLAGS)
CXXFLAGS += -Wall $(shell dpkg-buildflags --get CPPFLAGS; dpkg-buildflags --get CXXFLAGS)
LDFLAGS += $(shell dpkg-buildflags --get LDFLAGS)

builddir := $(CURDIR)/builddir

configure: configure-stamp
configure-stamp:
	dh_testdir
	mkdir -p $(builddir); \
	cd $(builddir); \
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" $(CURDIR)/configure \
		$(configure_flags)
	touch $@
ifeq ($(BUILD_NEON), Yes)
	mkdir -p $(builddir)-neon; \
	cd $(builddir)-neon; \
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" $(CURDIR)/configure \
		$(configure_flags_neon)
	touch $@
endif

build-arch: build
build-indep: build
build: build-stamp

build-stamp: configure-stamp
	dh_testdir
	$(MAKE) -C $(builddir) verbose=yes
ifeq ($(BUILD_NEON), Yes)
	$(MAKE) -C $(builddir)-neon verbose=yes
endif
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f *-stamp
	rm -rf $(builddir)
	rm -rf $(builddir)-neon
	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) -C $(builddir) verbose=yes dist
ifeq ($(BUILD_NEON), Yes)
	$(MAKE) -C $(builddir)-neon verbose=yes dist
endif
	# don't use stripped library...
	cp -v $(builddir)/libvpx_g.a \
	  $(builddir)/vpx-vp8-*/lib/libvpx.a

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_install -i
	dh_installchangelogs -i CHANGELOG
	dh_installdocs -i -A README AUTHORS
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install -s
	mkdir -p debian/libvpx3/usr/lib/$(DEB_HOST_MULTIARCH)
	cp -a builddir/vpx-vp8-*/lib/libvpx.so.* debian/libvpx3/usr/lib/$(DEB_HOST_MULTIARCH)
ifeq ($(BUILD_NEON), Yes)
	mkdir -p debian/libvpx3/usr/lib/$(DEB_HOST_MULTIARCH)/vfp/neon
	cp -a builddir-neon/vpx-vp8-*/lib/libvpx.so.* debian/libvpx3/usr/lib/$(DEB_HOST_MULTIARCH)/vfp/neon
endif
	mkdir -p debian/libvpx-dev/usr/lib/$(DEB_HOST_MULTIARCH)
	cp -a builddir/vpx-vp8-*/lib/libvpx.so builddir/vpx-vp8-*/lib/libvpx.a debian/libvpx-dev/usr/lib/$(DEB_HOST_MULTIARCH)
	dh_installdocs -s -A README AUTHORS
	dh_installchangelogs -s CHANGELOG
	dh_link -s
	dh_strip -s --dbg-package=libvpx3-dbg
	# Make sure not to conflict with past SONAMEs:
	rm -rf debian/libvpx3-dbg/usr/lib/debug/usr/bin
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -plibvpx3 -V 'libvpx3 (>= 1.5.0)' -- -c4
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install clean
