#!/usr/bin/make -f
#
# Copyright (C) 2004,2005,2006 Lemur Consulting Ltd
# Copyright (C) 2006-2021 Olly Betts
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

# For development releases, set DEV_SERIES to the release series (e.g. 1.3)
#DEV_SERIES := 1.3

# Codename we're building packages for.  For backported packages, put the
# codename (e.g. jessie or trusty) in debian/codename.  If that file doesn't
# exist, the default is sid.
#
# (Currently nothing in this file varies depending on the codename).
# CODENAME := $(shell cat debian/codename 2>/dev/null||echo sid)

# The soversion number needs to match that generated by libtool.  The following
# lines should read configure.ac to get the current interface number and
# subtract it from the interface age, to get the oldest interface number
# supported by the library. This should correspond to the soversion assigned
# to the library by libtool.  (This isn't documented, and is system dependent,
# so we need to check it doesn't break with future versions of libtool.)
libxapian_soversion:=$(shell \
    awk -F'[=:]' '($$1 == "LIBRARY_VERSION_INFO") {print $$2-$$4}' configure.ac\
    )
last_abi_change_version:=$(shell \
    awk 'BEGIN {v="1.4.0"} ($$0 ~ /^dnl [0-9][0-9]*:0:[0-9][0-9]* [0-9][0-9]*\./) { v = $$3 } END {print v}' configure.ac\
    )

ifneq ($(DEV_SERIES),)
libxapian_soversion:=-$(DEV_SERIES)-$(libxapian_soversion)
VERSION_SUFFIX:=-$(DEV_SERIES)
endif

# Ensure LFS is enabled.  Upstream's build system should do this, but this
# fails with some autoconf versions which fail to probe LFS support with a C++
# compiler (#1086249).
#
# Enable 64-bit time_t on i386 as well (sizeof(time_t) doesn't affect the ABI
# but time_t is used inside the library).
export DEB_BUILD_MAINT_OPTIONS = abi=+lfs,+time64

export DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

# For i386 and *-i386.
ifeq ($(patsubst %-i386,i386,$(DEB_HOST_ARCH)), i386)
    XAPIAN_BUILD_SSE := 1
    XAPIAN_EXCESS_PRECISION := 1
endif
ifeq ($(DEB_HOST_ARCH),m68k)
    XAPIAN_EXCESS_PRECISION := 1
endif

# Handle other DEB_BUILD_OPTIONS.  Note that dh_strip handles nostrip for us,
# dpkg-buildflags handles noopt, and dh_auto_* handles parallel=n.

# This explicit handling of nocheck can be dropped once we upgrade to compat
# level 13.
ifneq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
MAKE_CHECK := :
else
# Set AUTOMATED_TESTING to skip timed tests which can be flaky under uneven
# load.
MAKE_CHECK := $(MAKE) check VALGRIND= AUTOMATED_TESTING=1
endif

confflags :=
ifneq (,$(filter assertions,$(DEB_BUILD_OPTIONS)))
    confflags += --enable-assertions
endif

maint: \
	debian/control \
	debian/libxapian$(libxapian_soversion).install \
	debian/libxapian$(libxapian_soversion).shlibs \
	debian/libxapian-dev.install
	: # Dummy action so the "%" target isn't used.

maintclean: clean
	rm -f debian/control
	rm -f debian/libxapian$(libxapian_soversion).install
	rm -f debian/libxapian$(libxapian_soversion).shlibs
	rm -f debian/libxapian-dev.install
	rm -rf debian/libxapian$(libxapian_soversion)
	rm -rf debian/libxapian-dev
	rm -rf debian/xapian-doc
	rm -rf debian/xapian-tools
	rm -rf debian/tmp debian/tmp-sse2

debian/control: configure.ac debian/rules debian/control.in
	rm -f debian/control.tmp
	sed -e 's/@LIBXAPIAN_SOVERSION@/$(libxapian_soversion)/g' \
	     debian/control.in > debian/control.tmp
	mv debian/control.tmp debian/control

debian/libxapian$(libxapian_soversion).shlibs: configure.ac debian/rules
	rm -f $@.tmp
	echo 'libxapian $(libxapian_soversion) libxapian$(libxapian_soversion) (>= $(last_abi_change_version)~)' > $@.tmp
	mv $@.tmp $@

debian/libxapian$(libxapian_soversion).install: debian/rules debian/libxapianVERSION.install
	rm -f $@.tmp
	sed 's/@VERSION_SUFFIX@/$(VERSION_SUFFIX)/' \
	    debian/libxapianVERSION.install > $@.tmp
	mv $@.tmp $@

debian/libxapian-dev.install: debian/rules debian/libxapianVERSION-dev.install
	rm -f $@.tmp
	sed 's/@VERSION_SUFFIX@/$(VERSION_SUFFIX)/' \
	    debian/libxapianVERSION-dev.install > $@.tmp
	mv $@.tmp $@

%:
	dh $@

override_dh_auto_configure: debian/control debian/libxapian$(libxapian_soversion).shlibs
	dh_testdir

	# Configure in a subdirectory, for neatness.
ifdef XAPIAN_BUILD_SSE
	dh_auto_configure --builddir=build -- $(confflags) --disable-sse
	dh_auto_configure --builddir=build-sse2 -- $(confflags)
else
	dh_auto_configure --builddir=build -- $(confflags)
endif

override_dh_auto_build:
	$(MAKE) -C build
ifdef XAPIAN_BUILD_SSE
	$(MAKE) -C build-sse2
endif

override_dh_auto_test:
	# Excess precision causes some testcases to fail due to comparisons
	# between values that are spilled to memory and not.  For now we just
	# avoid running the testsuite on affected platforms (at least on i386
	# we only ran the tests for the SSE build prior to the debian/rules
	# modernisation in 1.4.18-2.)
ifndef XAPIAN_EXCESS_PRECISION
	$(MAKE_CHECK) -C build
endif
ifdef XAPIAN_BUILD_SSE
	$(MAKE_CHECK) -C build-sse2
endif

override_dh_auto_install: \
	 debian/libxapian$(libxapian_soversion).install \
	 debian/libxapian-dev.install

	# Install the files into debian/tmp
	$(MAKE) -C build DESTDIR=$(CURDIR)/debian/tmp install

ifdef XAPIAN_BUILD_SSE
	$(MAKE) -C build-sse2 DESTDIR=$(CURDIR)/debian/tmp-sse2 install
	mkdir -p $(CURDIR)/debian/libxapian$(libxapian_soversion)/usr/lib/$(DEB_HOST_MULTIARCH)/sse2
	mv $(CURDIR)/debian/tmp-sse2/usr/lib/$(DEB_HOST_MULTIARCH)/libxapian*.so.* $(CURDIR)/debian/libxapian$(libxapian_soversion)/usr/lib/$(DEB_HOST_MULTIARCH)/sse2
	rm -rf $(CURDIR)/debian/tmp-sse2
endif

	# Empty dependency_libs to placate luddite release goal.
	sed -i 's/^\(dependency_libs=\).*/\1/' $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libxapian$(VERSION_SUFFIX).la

	# Install the example source code
	mkdir -p debian/tmp/usr/share/doc/xapian-examples/examples
	cp examples/*.cc debian/tmp/usr/share/doc/xapian-examples/examples
	ln -s ../../../../lib/xapian-examples/examples debian/tmp/usr/share/doc/xapian-examples/examples/built

	# Fix up link to the internal sourcedocs to point to those on the
	# Xapian website since we don't build and package them.
	sed -i 's,sourcedoc/html/annotated.html,https://xapian.org/docs/&,' debian/tmp/usr/share/doc/xapian-core/internals.html

	# FIXME: Temporarily ignore these - we need to decide where to package
	# them (new arch all -common or bundle into the main lib with a
	# suitably versioned path?)
	rm -rf debian/tmp/usr/share/xapian-core/stopwords

override_dh_missing:
	# Compat level 13 defaults to --fail-missing, so this override can be
	# dropped once we upgrade to that.
	dh_missing --fail-missing
