#!/usr/bin/make -f

# Use GCC 10 instead of earlier versions for better c++20 support
ifeq ($(shell $(CXX) --version | head -n1 | cut -d\  -f4 | xargs dpkg --compare-versions 10 lt || echo buggy-cxx-20),buggy-cxx-20)
export CC = gcc-10
export CXX = g++-10
endif
# Clang does not support -ffat-lto-objects and this breaks check_c_compiler_flag() tests in cmake
ifeq ($(CC),clang)
export DEB_CFLAGS_MAINT_STRIP = -ffat-lto-objects
export DEB_CPPFLAGS_MAINT_STRIP = -ffat-lto-objects
export DEB_CXXFLAGS_MAINT_STRIP = -ffat-lto-objects
export DEB_LDLAGS_MAINT_STRIP = -ffat-lto-objects
endif

VER_GLIBC := $(shell dpkg -s libc6 | grep ^Version: | cut -f2 -d' ' | cut -f1 -d '-')

%:
	dh $@ --with bash-completion

ifneq (,$(filter $(distrelease),bionic))
# Bionic's jemalloc does not ship pkgconfig files needed to detect it.
# TODO check performance with and without jemalloc on Bionic and maybe change
# detection to work without pkgconfig
override_dh_auto_configure:
   dh_auto_configure -- -DJEMALLOC_LIBRARIES=-ljemalloc
endif

# dwz does not support dwarf-5 binaries emitted by clang
# https://sourceware.org/bugzilla/show_bug.cgi?id=28985
override_dh_dwz:
ifneq ($(CC),clang)
	dh_dwz
endif

override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	make -C obj-* check
endif

override_dh_gencontrol:
	dh_gencontrol -- '-Vglibc:Depends=libc6 (>= '$(VER_GLIBC)')'

override_dh_installchangelogs:
	dh_installchangelogs -k ChangeLog
