#!/usr/bin/make -f

# This enables verbose mode.
#export DH_VERBOSE=1
export V=1

# Determine the debian directory, which is the directory of this makefile.
# The path of this makefile is the first word in the MAKEFILE_LIST.
DEBIAN_DIR = $(dir $(firstword $(MAKEFILE_LIST)))

# Get the Debian version revision:
DEB_VERSION  := $(shell dpkg-parsechangelog -l$(DEBIAN_DIR)changelog -SVersion)
DEB_REVISION := $(word 2, $(subst -, ,$(DEB_VERSION)))

# Get the architecture triplet:
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Get the host architecture/OS:
DEB_HOST_ARCH_OS   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

# Ubuntu ld adds -Bsymbolic-functions by default, but that prevents FFmpeg from building.
export DEB_LDFLAGS_MAINT_STRIP=-Wl,-Bsymbolic-functions

# Package name for the extra flavor.
EXTRA_PKG := $(shell sed -nr 's/^Package:[[:space:]]*(.*extra[0-9]+)[[:space:]]*$$/\1/p' debian/control)
FLAVORS = standard extra

# Enable as many features as possible, as long as the result is still GPLv2+ (a GPLv3+ variant is built as libavcodec-extra flavor).
# The following flags (and build-dependencies) are not added, because they would require a libavformat-extra flavor:
#   --enable-libsmbclient           (libsmbclient-dev [!hurd-i386 !m68k !sparc64])
# The following flags are not added, because the necessary libraries are not in Debian:
#   --enable-decklink
#   --enable-libcelt                (see #676592: removed from Debian as abandoned upstream, replaced by opus)
#   --enable-libdcadec
#   --enable-libilbc                (see #675959 for the RFP bug)
#   --enable-libkvazaar
#   --enable-libmfx
#   --enable-libnut
#   --enable-libopenh264
#   --enable-libstagefright-h264
#   --enable-libutvideo
#   --enable-libvidstab             (see #709193 for the RFP bug)
#   --enable-libxavs
# The following flags are not added for various reasons:
#   * --enable-libquvi: The wrapper wasn't updated for recent versions (due to license concerns) and thus might produce problems, e.g. for mpv.
#   * --enable-libv4l2 [!hurd-any]: This is only needed for very old devices and may cause problems for others.
#                                   Should anyone need it, using LD_PRELOAD pointing on libv4l2 has the same effect.
#   * --enable-opencl [!hurd-any]:  This is considered an experimental API.
# For the shared libraries there are several ways to build:
#  * Without further option: This will have no compatibility with Libav, but some sonames clash. Therefore no good choice.
#  * With --enable-incompatible-libav-abi, packages build against Libav would work with these libraries, but probably not the other way around.
#    This could be used together with Breaks:, Replaces: against the corresponding Libav libraries.
#  * With --enable-raise-major, the resulting binaries should be co-installable with the Libav ones and thus no conflicts would be necessary.
#    But this could break versioned configure checks.
#  * With --build-suffix="-ffmpeg" the library names are changed, so there can't be a conflict with Libav.
CONFIG := --prefix=/usr \
	--extra-version="$(DEB_REVISION)" \
	--build-suffix="-ffmpeg" \
	--toolchain=hardened \
	--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	--incdir=/usr/include/$(DEB_HOST_MULTIARCH) \
	--cc=$(CC) \
	--cxx=$(CXX) \
	--enable-gpl \
	--enable-shared \
	--disable-stripping \
	--disable-decoder=libopenjpeg \
	--disable-decoder=libschroedinger \
	--enable-avresample \
	--enable-avisynth \
	--enable-gnutls \
	--enable-ladspa \
	--enable-libass \
	--enable-libbluray \
	--enable-libbs2b \
	--enable-libcaca \
	--enable-libcdio \
	--enable-libflite \
	--enable-libfontconfig \
	--enable-libfreetype \
	--enable-libfribidi \
	--enable-libgme \
	--enable-libgsm \
	--enable-libmodplug \
	--enable-libmp3lame \
	--enable-libopenjpeg \
	--enable-libopus \
	--enable-libpulse \
	--enable-librtmp \
	--enable-libschroedinger \
	--enable-libshine \
	--enable-libsnappy \
	--enable-libsoxr \
	--enable-libspeex \
	--enable-libssh \
	--enable-libtheora \
	--enable-libtwolame \
	--enable-libvorbis \
	--enable-libvpx \
	--enable-libwavpack \
	--enable-libwebp \
	--enable-libx265 \
	--enable-libxvid \
	--enable-libzvbi \
	--enable-openal \
	--enable-opengl \
	--enable-x11grab

# The standard configuration only uses the shared CONFIG.
CONFIG_standard =

# With these enabled, resulting binaries are effectively licensed as GPLv3+.
CONFIG_extra = --enable-version3 \
	--disable-doc \
	--disable-programs \
	--disable-avdevice \
	--disable-avfilter \
	--disable-avformat \
	--disable-avresample \
	--disable-postproc \
	--disable-swscale \
	--enable-libopencore_amrnb \
	--enable-libopencore_amrwb \
	--enable-libvo_aacenc \
	--enable-libvo_amrwbenc

# Disable optimizations if requested.
ifneq (,$(filter $(DEB_BUILD_OPTIONS),noopt))
	CONFIG += --disable-optimizations
endif

# Some libraries are build only on linux.
ifeq ($(DEB_HOST_ARCH_OS),linux)
	CONFIG += --enable-libdc1394 \
		--enable-libiec61883
endif

# Some libraries are not build on hurd.
ifneq ($(DEB_HOST_ARCH_OS),hurd)
	CONFIG += --enable-libzmq
endif

# ffmpeg is involed in build-dependency cycles with opencv and x264, so disable them in stage one.
# Also disable frei0r, which build-depends on opencv.
ifneq ($(filter stage1,$(DEB_BUILD_PROFILES)),)
	CONFIG += --disable-libx264 --disable-libopencv --disable-frei0r
else
	CONFIG += --enable-frei0r

	# Some libraries are unusable (#792921) on sparc64.
	ifeq (,$(filter $(DEB_HOST_ARCH),sparc64))
		CONFIG += --enable-libx264
	endif

	# OpenCV is not available on some architectures.
	ifeq (,$(filter $(DEB_HOST_ARCH),sh4 sparc64))
		CONFIG += --enable-libopencv
	endif
endif

# Disable i686 optimizations on i386, because they are not always available on this architecture.
ifneq (,$(filter $(DEB_HOST_ARCH),i386 hurd-i386))
	CONFIG += --disable-i686
endif

# Disable altivec optimizations on powerpc, because they are not always available on this architecture.
ifeq ($(DEB_HOST_ARCH),powerpc)
	CONFIG += --disable-altivec
	# Build an altivec flavor of the libraries on powerpc.
	# This works around the problem that runtime cpu detection on powerpc currently does not work,
	# because, if altivec is enabled, all files are build with '-maltivec' so that the compiler inserts altivec instructions, wherever it likes.
	CONFIG_altivec = --enable-altivec \
		--disable-doc \
		--disable-programs
	CONFIG_altivec-extra = $(CONFIG_altivec) $(CONFIG_extra)
	FLAVORS += altivec altivec-extra
endif

# Disable assembly optimizations on x32, because they don't work (yet).
ifneq (,$(filter $(DEB_HOST_ARCH),x32))
	CONFIG += --disable-asm
endif

# Disable optimizations on mips(el) and some on mips64(el), because they are not always available on these architectures.
ifneq (,$(filter $(DEB_HOST_ARCH),mips mipsel mips64 mips64el))
	CONFIG += --disable-mipsdspr1 \
	--disable-mipsdspr2 \
	--disable-loongson3
endif
ifneq (,$(filter $(DEB_HOST_ARCH),mips mipsel))
	CONFIG += --disable-mipsfpu
endif

# Use the default debhelper scripts, where possible.
# Enable parallel building.
%:
	dh $@ --parallel

# Add configuration options:
override_dh_auto_configure:
	$(info DEB_BUILD_OPTIONS = $(DEB_BUILD_OPTIONS))
	$(foreach flavor,$(FLAVORS),mkdir -p debian/$(flavor);)
	$(foreach flavor,$(FLAVORS), echo " *** $(flavor) ***"; cd debian/$(flavor); ../../configure $(CONFIG) $(CONFIG_$(flavor)) || (cat config.log && exit 1); cd ../.. ;)

# Remove the subdirectories generated for the flavors.
override_dh_auto_clean:
	$(foreach flavor,$(FLAVORS),[ ! -d debian/$(flavor) ] || rm -r debian/$(flavor);)

# Create doxygen documentation:
override_dh_auto_build-indep:
	dh_auto_build -i --sourcedirectory=debian/standard -- apidoc
	# Create the minified CSS files.
	lessc debian/missing-sources/ffmpeg-web/src/less/style.less | cleancss > debian/standard/doc/style.min.css

override_dh_auto_build-arch:
	# Copy built object files to avoid building them again for the extra flavor.
	# Build qt-faststart here, to make it possible to build with 'nocheck'.
	for flavor in $(FLAVORS); do \
		echo " *** $$flavor ***"; \
		if echo "$$flavor" | grep -q "extra"; then \
			subdir=`[ "$$flavor" = "extra" ] && echo "debian/standard/" || echo "debian/altivec/"`; \
			for dir in `cd ./$$subdir; find libavcodec libavutil libswresample -type d`; do \
				mkdir -p debian/"$$flavor"/"$$dir"; \
				echo "$$subdir$$dir"/*.o | grep -q '*' || cp "$$subdir$$dir"/*.o debian/"$$flavor"/"$$dir"; \
			done; \
			rm debian/"$$flavor"/libavcodec/allcodecs.o; \
		fi; \
		if [ "$$flavor" = "standard" ]; then \
			cd debian/standard; make tools/qt-faststart; cd ../..; \
		fi; \
		dh_auto_build -a --sourcedirectory=debian/"$$flavor" || (cat debian/"$$flavor"/config.log && exit 1); \
	done

# Set the library path for the dynamic linker, because the tests otherwise don't find the libraries.
override_dh_auto_test:
	export LD_LIBRARY_PATH="libavcodec:libavdevice:libavfilter:libavformat:libavresample:libavutil:libpostproc:libswresample:libswscale"; dh_auto_test --sourcedirectory=debian/standard -- -k

override_dh_auto_install:
	dh_auto_install --sourcedirectory=debian/standard
ifeq ($(DEB_HOST_ARCH),powerpc)
	install -d debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/altivec
	install -m 644 debian/altivec/*/*.so.* debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/altivec
endif
	dh_auto_install --sourcedirectory=debian/extra --destdir=debian/tmp/extra
ifeq ($(DEB_HOST_ARCH),powerpc)
	install -d debian/tmp/extra/usr/lib/$(DEB_HOST_MULTIARCH)/altivec
	install -m 644 debian/altivec-extra/*/*.so.* debian/tmp/extra/usr/lib/$(DEB_HOST_MULTIARCH)/altivec
endif
	# Create symbolic links from the standard library lib*.so symlinks, pkg-config files and static libraries to the suffixed ones.
	for lib in `find debian/ -name lib*-ffmpeg.so; find debian/ -name lib*.pc; find debian/ -name lib*.a`; do \
		dir=`dirname $$lib`; \
		base=`basename $$lib`; \
		link=`echo $$base | sed 's/-ffmpeg//'`; \
		ln -sf $$base $$link; \
		mv $$link $$dir; \
	done

override_dh_install:
	dh_install -N $(EXTRA_PKG)
	dh_install -p $(EXTRA_PKG) --sourcedir=debian/tmp/extra

# Don't compress the example source code files.
override_dh_compress:
	dh_compress -Xexamples

# Move the debug symbols to the debug package.
override_dh_strip:
	dh_strip --dbg-package=ffmpeg-dbg

# Download the latest upstream tarball.
get-orig-source:
	cd $(DEBIAN_DIR)../ && \
	uscan --rename --force-download --destdir $(CURDIR)
