#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export V=VERBOSE
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/default.mk

ifeq (,$(filter nolto,$(DEB_BUILD_OPTIONS)))
export DEB_LDFLAGS_MAINT_APPEND+=-flto
export DEB_CFLAGS_MAINT_APPEND+=-flto
endif

sampledir:=$(CURDIR)/debian/$(DEB_SOURCE)-examples/usr/share/doc/$(DEB_SOURCE)/examples

# HMMER only builds a static library.  Build a shared library from it as
# well, so that other packages (e.g. python3-pyhmmer) can link against the
# Debian-packaged HMMER code instead of shipping their own copy.
HMMER_VERSION   = 3.4.0
HMMER_SOVERSION = 3
MULTIARCH      := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

CFLAGS   := $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS  := $(shell dpkg-buildflags --get LDFLAGS)

# The static library must not consist of LTO bytecode only: it has to stay
# usable with tools that cannot read LTO objects and with non-GCC compilers.
NOLTO_CFLAGS := $(filter-out -flto -flto=%,$(CFLAGS))

EASEL_LIBS := $(shell pkg-config --libs easel)

# The programs and the test drivers link against the libhmmer.so built in the
# source tree, which is not on the default library search path yet.
export LD_LIBRARY_PATH = $(CURDIR)/src

%:
	dh $@

shared-libs:
	cd src && $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared \
		-Wl,-soname,libhmmer.so.$(HMMER_SOVERSION) \
		-o libhmmer.so.$(HMMER_VERSION) \
		-Wl,--whole-archive libhmmer.a -Wl,--no-whole-archive \
		$(EASEL_LIBS) -ldivsufsort -lm
	ln -sf libhmmer.so.$(HMMER_VERSION) src/libhmmer.so.$(HMMER_SOVERSION)
	ln -sf libhmmer.so.$(HMMER_SOVERSION) src/libhmmer.so

override_dh_auto_build:
	# Build the static library first, then the shared library from it, and
	# only then the programs, so that the programs link against the shared
	# library.
	$(MAKE) -C src libhmmer.a PIC_CFLAGS=-fPIC CFLAGS="$(NOLTO_CFLAGS)"
	$(MAKE) -f debian/rules shared-libs
	dh_auto_build -- PIC_CFLAGS=-fPIC

ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU)

ifeq (amd64,$(ARCH))
    DEB_CONFIGURE_EXTRA_FLAGS := --enable-threads --enable-sse
 else ifeq (powerpc,$(ARCH))
    DEB_CONFIGURE_EXTRA_FLAGS := --enable-threads --enable-vmx
 else ifeq (ppc64,$(ARCH))
    DEB_CONFIGURE_EXTRA_FLAGS := --enable-threads --enable-vmx
 else ifeq (arm64,$(ARCH))
    DEB_CONFIGURE_EXTRA_FLAGS := --enable-threads --enable-neon
 else
    DEB_CONFIGURE_EXTRA_FLAGS := --enable-threads
endif

override_dh_autoreconf:
	AUTOHEADER=true dh_autoreconf

override_dh_auto_configure:
	CPPFLAGS="$(shell dpkg-buildflags --get CPPFLAGS)" dh_auto_configure -- $(DEB_CONFIGURE_EXTRA_FLAGS)

override_dh_auto_install:
	dh_auto_install
	install -d $(CURDIR)/debian/tmp/usr/lib/$(MULTIARCH)
	install -m644 src/libhmmer.a $(CURDIR)/debian/tmp/usr/lib/$(MULTIARCH)/
	install -m755 src/libhmmer.so.$(HMMER_VERSION) $(CURDIR)/debian/tmp/usr/lib/$(MULTIARCH)/
	ln -sf libhmmer.so.$(HMMER_VERSION) $(CURDIR)/debian/tmp/usr/lib/$(MULTIARCH)/libhmmer.so.$(HMMER_SOVERSION)
	ln -sf libhmmer.so.$(HMMER_SOVERSION) $(CURDIR)/debian/tmp/usr/lib/$(MULTIARCH)/libhmmer.so
	install -d $(CURDIR)/debian/tmp/usr/include/hmmer
	install -m644 src/*.h $(CURDIR)/debian/tmp/usr/include/hmmer/
	for d in src/impl_*; do \
	    install -d $(CURDIR)/debian/tmp/usr/include/hmmer/$$(basename $$d); \
	    install -m644 $$d/*.h $(CURDIR)/debian/tmp/usr/include/hmmer/$$(basename $$d)/; \
	done
	install -d $(CURDIR)/debian/tmp/usr/lib/$(MULTIARCH)/pkgconfig
	sed -e 's/@VERSION@/$(HMMER_VERSION)/' \
	    -e 's|@LIBDIR@|/usr/lib/$(MULTIARCH)|' \
	    debian/hmmer.pc.in \
	    > $(CURDIR)/debian/tmp/usr/lib/$(MULTIARCH)/pkgconfig/hmmer.pc
	d-shlibmove --commit --multiarch --devunversioned --exclude-la --include-a \
	    --override 's/libeasel0-dev/libeasel-dev/' \
	    --movedev debian/tmp/usr/include/hmmer usr/include \
	    --movedev debian/tmp/usr/lib/$(MULTIARCH)/pkgconfig/hmmer.pc usr/lib/$(MULTIARCH)/pkgconfig \
	    debian/tmp/usr/lib/$(MULTIARCH)/libhmmer.so

override_dh_auto_test:
	# The upstream test suite is driven by the sqc harness and needs the
	# formats test data from the Easel source tree, which libeasel-dev does
	# not install.  Run the self-contained HMMER unit test drivers instead.
	for t in src/hmmer_utest src/build_utest src/p7_hmm_utest \
	         src/p7_hmmfile_utest src/impl/decoding_utest \
	         src/impl/fwdback_utest src/impl/msvfilter_utest \
	         src/impl/io_utest src/impl/vitfilter_utest; do \
	    echo "Running $$t"; ./$$t || exit 1; \
	done

execute_after_dh_auto_build:
	$(MAKE) tests $(DEB_BUILD_OPTION_PARALLEL:%=-j %)
	# Build the user guide (the generated PDF is not part of the upstream
	# tarball).
	$(MAKE) -C documentation/userguide Userguide.pdf

execute_after_dh_auto_clean:
	rm -f testsuite/esltmp*
	rm -f tutorial/MADE1.hmm.h3?
	rm -f config.log config.status configure
	find . -name Makefile -delete

override_dh_compress:
	dh_compress -Xtutorial/

execute_after_dh_auto_install:
	mkdir -p $(CURDIR)/debian/$(DEB_SOURCE)/usr/lib/${DEB_HOST_MULTIARCH}/hmmer/
	cp src/hmmc2 $(CURDIR)/debian/$(DEB_SOURCE)/usr/lib/${DEB_HOST_MULTIARCH}/hmmer/

execute_after_dh_installexamples:
	mkdir -p $(sampledir)/src/impl
	find src/ -maxdepth 1 -name "*_utest" -exec cp \{\} $(sampledir)/src/ \;
	find src/impl_sse -name "*_utest" -exec cp \{\} $(sampledir)/src/impl \;
	cp src/itest_brute $(sampledir)/src
	cp src/hmmpress.itest.pl $(sampledir)/src
	cp -aR testsuite $(sampledir)
	find $(sampledir) -name 'Makefile' | xargs sed -i "s^$(CURDIR)^.^g"

execute_after_dh_fixperms:
	find debian -name .dropbox.attr -delete
	find debian -name "*.fa" -executable -exec chmod -x \{\} \;
	find $(sampledir) -name "*.man" -exec chmod -x \{\} \;
