#! /usr/bin/make -f

#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk

CFLAGS += -Wall -g

# ioctlsort is compiled with *_FOR_BUILD flags
export CFLAGS_FOR_BUILD = $(CFLAGS)
export CPPFLAGS_FOR_BUILD = $(CPPFLAGS)
export LDFLAGS_FOR_BUILD = $(LDFLAGS)

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  MAKEFLAGS += -j$(NUMJOBS)
endif

#extra_build_targets += build-udeb-stamp

arch64_map = i386=x86_64 powerpc=powerpc64 sparc=sparc64 s390=s390x
ifneq (,$(filter $(DEB_HOST_ARCH)=%, $(arch64_map)))
  HOST64 = $(strip $(patsubst $(DEB_HOST_ARCH)=%, %, \
	     $(filter $(DEB_HOST_ARCH)=%, $(arch64_map))))-linux-gnu
  CC64 = gcc -m64
  extra_build_targets += build64-stamp
endif

ifeq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
  CONFIG_OPTS = --build=$(DEB_BUILD_GNU_TYPE)
else
  CONFIG_OPTS = --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
endif

ifeq ($(DEB_HOST_ARCH),ppc64el)
  CONFIG_OPTS += --disable-gcc-Werror
endif

all: build

build: build-arch build-indep

build-arch: build-stamp $(extra_build_targets)
build-indep: build-stamp $(extra_build_targets)

configure:
	dh_autoreconf

%-stamp: %/Makefile
	$(MAKE) -C $*

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	$*/src/strace -V
ifneq (,$(findstring $(DEB_HOST_ARCH), mips64el mipsel))
	echo "WARNING: Making tests non-fatal because of known test failures on $(DEB_HOST_ARCH)."
	$(MAKE) -C $* check VERBOSE=1 || true
else
	$(MAKE) -C $* check VERBOSE=1
endif
endif
	touch $@

build/Makefile: configure
	mkdir -p $(@D)
	cd $(@D); sh ../configure --enable-mpers=check --prefix=/usr $(CONFIG_OPTS)

build-udeb/Makefile: configure
	mkdir -p $(@D)
	cd $(@D); sh ../configure --enable-mpers=check --prefix=/usr \
		--without-libunwind --without-libiberty $(CONFIG_OPTS)

build64/Makefile: configure
	mkdir -p $(@D)
	cd $(@D); CC="$(CC64)" sh ../configure --enable-mpers=check --prefix=/usr --build=$(DEB_BUILD_GNU_TYPE) --host=$(HOST64)

clean:
	dh_testdir
	dh_testroot
	rm -rf build build-udeb build64 doc/strace64.1 debian/strace64.substvars
	dh_autoreconf_clean
	dh_clean configure

binary: binary-indep binary-arch

binary-indep:

binary-arch: build
	test -f build-stamp || make $(MFLAGS) -f debian/rules build

	echo "Truncating to 128KB files larger than 50MB; these are typically sparse files of only zeroes but most are 41GB in size and add up to 3TB, which causes troubles down the road when sparse files are not handled efficiently."
	find -type f -size +50M -exec ls -lh {} +
	find -type f -size +50M -exec truncate --size=128KB {} +

	# prepare 64bit executable and manpage, if it has been built
	test -f build64-stamp && ( mv build64/src/strace build64/src/strace64 ; \
		mv build64/doc/strace.1 build64/doc/strace64.1 ) || true

	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_installdocs -a
	dh_installman -a
	dh_installexamples -a
	dh_installchangelogs -a
	dh_install -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a
