#!/usr/bin/make -f

export V=1

# sets DEBIAN_VERSION variable
include /usr/share/dpkg/pkg-info.mk

# Get the Debian version revision:
DEB_REVISION := $(word 2, $(subst -, ,$(DEB_VERSION)))

# sets DEB_HOST_* variables
include /usr/share/dpkg/architecture.mk

# 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.
SHLIB_PKGS := $(shell sed -nr '/extra/! s/^Package:[[:space:]]*(lib.*[0-9]+)[[:space:]]*$$/\1/p' debian/control)
EXTRA_PKGS := $(shell sed -nr 's/^Package:[[:space:]]*(.*extra[0-9]+)[[:space:]]*$$/\1/p' debian/control)
FLAVORS = standard extra static

# Enable as many features as possible, as long as the result is still GPLv2+ (a GPLv3+ variant is built as libavcodec-extra/libavfilter-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-libdavs2
#   --enable-libilbc                (see #675959 for the RFP bug)
#   --enable-libklvanc
#   --enable-libkvazaar
#   --enable-libmfx
#   --enable-libopenh264
#   --enable-libsrt
#   --enable-libtensorflow          (only in experimental at the moment)
#   --enable-libvmaf
#   --enable-libxavs2
#   --enable-libxavs
#   --enable-libzimg                (see #864888 for the RFP bug)
#   --enable-vapoursynth            (see #833942 for the RFP bug)
# The following flags are not added for various reasons:
#   --enable-librtmp                ffmpeg has better built-in RTMP support with listen mode.
#   --enable-mbedtls                Already using gnutls as TLS implementation
#   --enable-libmodplug             see #849840: replaced with libopenmpt
#   --enable-libv4l2                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                 This is considered an experimental API.
#   --enable-opencv                 Does not build with OpenCV >= 3.4
CONFIG := --prefix=/usr \
	--extra-version="$(DEB_REVISION)" \
	--toolchain=hardened \
	--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	--incdir=/usr/include/$(DEB_HOST_MULTIARCH) \
	--arch=$(DEB_HOST_ARCH_CPU) \
	--enable-gpl \
	--disable-stripping \
	--enable-avresample --disable-filter=resample \
	--enable-avisynth \
	--enable-gnutls \
	--enable-ladspa \
	--enable-libaom \
	--enable-libass \
	--enable-libbluray \
	--enable-libbs2b \
	--enable-libcaca \
	--enable-libcdio \
	--enable-libcodec2 \
	--enable-libflite \
	--enable-libfontconfig \
	--enable-libfreetype \
	--enable-libfribidi \
	--enable-libgme \
	--enable-libgsm \
	--enable-libjack \
	--enable-libmp3lame \
	--enable-libmysofa \
	--enable-libopenjpeg \
	--enable-libopenmpt \
	--enable-libopus \
	--enable-libpulse \
	--enable-librsvg \
	--enable-librubberband \
	--enable-libshine \
	--enable-libsnappy \
	--enable-libsoxr \
	--enable-libspeex \
	--enable-libssh \
	--enable-libtheora \
	--enable-libtwolame \
	--enable-libvidstab \
	--enable-libvorbis \
	--enable-libvpx \
	--enable-libwavpack \
	--enable-libwebp \
	--enable-libx265 \
	--enable-libxml2 \
	--enable-libxvid \
	--enable-libzmq \
	--enable-libzvbi \
	--enable-lv2 \
	--enable-omx \
	--enable-openal \
	--enable-opengl \
	--enable-sdl2

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

# With these enabled, resulting binaries are effectively licensed as GPLv3+.
CONFIG_extra = --enable-shared \
	--enable-version3 \
	--disable-doc \
	--disable-programs \
	--enable-liblensfun \
	--enable-libopencore_amrnb \
	--enable-libopencore_amrwb \
	--enable-libtesseract \
	--enable-libvo_amrwbenc

# The static libraries should not be built with PIC.
CONFIG_static = --disable-pic \
	--disable-doc \
	--disable-programs

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

# Respect CC/CXX from the environment, if they differ from the default.
# Don't set them if they equal the default, because that disables autodetection needed for cross-building.
ifneq ($(CC),cc)
	CONFIG += --cc=$(CC)
endif
ifneq ($(CXX),g++)
	CONFIG += --cxx=$(CXX)
endif

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

# ffmpeg is involved in build-dependency cycles with x264 and chromaprint, so disable them in stage one.
# Also disable frei0r, which build-depends on opencv.
ifneq ($(filter pkg.ffmpeg.stage1,$(DEB_BUILD_PROFILES)),)
	CONFIG += --disable-frei0r \
		--disable-chromaprint \
		--disable-libx264
else
	CONFIG += --enable-chromaprint \
		--enable-frei0r \
		--enable-libx264
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-shared \
		--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 some optimizations on mips*, because they are not always available on these architectures
ifneq (,$(filter mips%r6 mips%r6el, $(DEB_HOST_ARCH)))
	MIPS_R2_R6_CONFIG = --disable-mips32r2 \
		--disable-mips64r2
else
	MIPS_R2_R6_CONFIG = --disable-mips32r6 \
		--disable-mips64r6
endif

ifneq (,$(filter mips%, $(DEB_HOST_ARCH)))
	CONFIG += --disable-mipsdsp \
		--disable-mipsdspr2 \
		--disable-loongson2 \
		--disable-loongson3 \
		--disable-mmi \
		--disable-mips32r5 \
		$(MIPS_R2_R6_CONFIG) \
		--disable-msa
endif

# Disable mipsfpu on mips and mipsel, because it is not compatible with the FPXX ABI
ifneq (,$(filter $(DEB_HOST_ARCH),mips mipsel))
	CONFIG += --disable-mipsfpu
endif

# Disable some tests which fail when FLT_EVAL_METHOD != 0 (found on i386, m68k and s390x)
ifneq (,$(filter $(DEB_HOST_ARCH_CPU),i386 m68k s390x))
	CONFIG += --ignore-tests=hapenc-hap-none,hapenc-hapa-none,hapenc-hapq-none
endif

# Set cross-build prefix for compiler, pkg-config...
ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	CONFIG += --cross-prefix=$(DEB_HOST_GNU_TYPE)- \
		--target-os=$(DEB_HOST_ARCH_OS)
endif

# Use the default debhelper scripts, where possible.
%:
	dh $@

# Add configuration options:
override_dh_auto_configure:
	$(foreach flavor,$(FLAVORS),mkdir -p debian/$(flavor);)
	$(foreach flavor,$(FLAVORS),set -e; echo " *** $(flavor) ***"; cd debian/$(flavor); ../../configure $(CONFIG) $(CONFIG_$(flavor)) || (cat ffbuild/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 documentation
	# Create the minified CSS files.
	#  Due to https://github.com/nodejs/node/issues/14752 where nodejs will
	#  leave set O_NONBLOCK on stderr, we pipe stderr through cat which will
	#  create a temporary pipe so this doesn't break the rest of the build.
	#  See: #895154
	(lessc debian/missing-sources/ffmpeg-web/src/less/style.less | cleancss > debian/standard/doc/style.min.css) 2>&1 | cat >&2

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'.
	set -e && 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 libavdevice libavfilter libavformat libavresample libavutil libpostproc libswscale 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; \
			rm debian/"$$flavor"/libavfilter/allfilters.o; \
		fi; \
		if [ "$$flavor" = "standard" ]; then \
			$(MAKE) -C debian/standard tools/qt-faststart; \
		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-arch:
	export LD_LIBRARY_PATH="libavcodec:libavdevice:libavfilter:libavformat:libavresample:libavutil:libpostproc:libswresample:libswscale"; \
		dh_auto_test -a --sourcedirectory=debian/standard -- -k

# No tests for indep build.
override_dh_auto_test-indep:

override_dh_auto_install-arch:
	dh_auto_install -a --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 -a --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
	# Use the static libraries from the --disable-pic build
	install -m 644 debian/static/*/lib*.a debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)

override_dh_auto_install-indep:
	$(MAKE) -C debian/standard DESTDIR=$(CURDIR)/debian/tmp install-doc install-examples

override_dh_install:
	dh_install $(addprefix -p,$(EXTRA_PKGS)) --sourcedir=debian/tmp/extra
	dh_install --remaining-packages

override_dh_shlibdeps:
	$(RM) debian/shlibs.local
	set -e && for pkg in $(SHLIB_PKGS); do \
		lib=`echo $$pkg | sed 's/\([a-z]*\)\([0-9]*\)/\1/'`; \
		abi=`echo $$pkg | sed 's/\([a-z]*\)\([0-9]*\)/\2/'`; \
		ver=${DEB_VERSION}; \
		echo "$$lib $$abi $$pkg (= $$ver)" >> debian/shlibs.local; \
	done
	dh_shlibdeps

override_dh_missing:
	dh_missing --list-missing

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