#!/usr/bin/make -f

# https://wiki.ubuntu.com/ToolChain/LTO
# can't do clang build with ubuntu lto defaults
# my CI won't consistently build the package from source with LTO turned on because sqlite takes 20+ minutes
# Would it yield better performance to have LTO *on* and use slightly more ""normal"" sqlite?
# this is muddied by my suspicion that they are actually using sqlcipher - but that is also in the repos.
export DEB_BUILD_MAINT_OPTIONS=optimize=-lto

# examples have this file in them
export DH_ALWAYS_EXCLUDE=.gitignore

# telegram bot api uses different versioning to TDlib
export API_VERSION=$(shell grep 'TelegramBotApi VERSION' CMakeLists.txt|grep -o -P '\d\.\d')
export LIB_VERSION=$(shell head -n1 debian/changelog|grep -oP '\(.+\)'|grep -oP '[^()]+')
#export DEB_REVISION=$(shell echo $(LIB_VERSION) | sed -E 's,.+\+([0-9]+([~+][a-z0-9.]+)?),\1,')
export DEB_REVISION=$(shell echo $(LIB_VERSION) | sed -E 's,([.0-9]+)-([0-9]+),\2,')

# never quite got clang working
#export CXXFLAGS=-stdlib=libc++
#export CC=/usr/bin/clang-10
#export CXX=/usr/bin/clang++-10
	# -DCMAKE_AR=/usr/bin/llvm-ar-10 -DCMAKE_NM=/usr/bin/llvm-nm-10
	# -DCMAKE_OBJDUMP=/usr/bin/llvm-objdump-10 -DCMAKE_RANLIB=/usr/bin/llvm-ranlib-10

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- -DTD_ENABLE_LTO=ON -DTD_ENABLE_JNI=ON

override_dh_auto_build:
	dh_auto_build
	$(MAKE) -f */td/Makefile tg_cli tdjson tdjson_static
	$(MAKE) -f */td/tddb/Makefile binlog_dump

override_dh_gencontrol:
	dh_gencontrol
#	dh_gencontrol -ptelegram-bot-api -- -v$(API_VERSION)+$(DEB_REVISION)
	dh_gencontrol -ptelegram-bot-api -- -v$(API_VERSION)-$(DEB_REVISION)
