#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

# generate documentation unless nodoc requested
DOCS_stem = README ANONYMITY_NETWORKS LEVIN_PROTOCOL
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
DOCS = $(foreach d,$(DOCS_stem),$d.html $d.txt)
endif

# reduce debugging information on 32bit archs
# to avoid some FTBFS (memory axhaustion of build environment?)
ifeq ($(DEB_HOST_ARCH_BITS),32)
export DEB_CXXFLAGS_MAINT_APPEND = -g1
endif

# explicitly link with libatomic
# to work around gcc bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81358>
ifneq (,$(filter $(DEB_HOST_ARCH), armel m68k mips mipsel powerpc powerpcspe sh4))
export DEB_LDFLAGS_MAINT_APPEND = -latomic
endif

DH_OPTIONS = -O--buildsystem=cmake

# detect if build targets experimental suite (or is a draft)
DEB_SUITE_EXP = $(filter experimental% UNRELEASED,$(DEB_DISTRIBUTION))

#testsuite needs writable $HOME
FAKEHOME = $(CURDIR)/debian/fakehome

# skip tests requiring network
GTEST_FILTER = address_from_url.*:dns_resolver.*
TEST_EXCLUDE = functional_tests_rpc

# skip slow tests
TEST_EXCLUDE += core_tests

# skip tests possibly broken
TEST_EXCLUDE += $(if $(DEB_SUITE_EXP),,cnv4-jit hash-slow-4)

override_dh_auto_configure:
	dh_auto_configure -- -DARCH=default -DNO_AES=ON -DBUILD_TESTS=ON -DPYTHON_EXECUTABLE=/usr/bin/python3

override_dh_auto_build: $(DOCS)
	dh_auto_build

override_dh_auto_test:
	HOME="${FAKEHOME}" \
	GTEST_FILTER="$(GTEST_FILTER)" \
		dh_auto_test -- ARGS+="--output-on-failure -E '$(subst $() ,|,$(TEST_EXCLUDE))'" \
		$(if $(DEB_SUITE_EXP),|| true)

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -rf "$(FAKEHOME)"
	dh_auto_clean

override_dh_installdocs:
	dh_installdocs --all -- $(DOCS)

override_dh_clean:
	dh_clean -- $(DOCS)

%.html: %.md
	pandoc --from gfm-raw_html --to html --standalone --output $@ $<

%.txt: %.md
	pandoc --from gfm-raw_html --to plain --output $@ $<

%:
	dh $@ $(DH_OPTIONS:-O%=%)
