#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=+lto
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-z,noexecstack

# disable hardening-pie on x32
# fixes compilation problem: relocation R_X86_64_PC32 against symbol `cpucycles'
# can not be used when making a shared object
ifeq ($(DEB_BUILD_ARCH), x32)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie optimize=+lto
endif

# disable hardening-pie on alpha
# fixes compilation problem: gp-relative relocation against dynamic symbol cpucycles
ifeq ($(DEB_BUILD_ARCH), alpha)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie optimize=+lto
endif


%:
	dh $@

# The debian/compiler directory contains gcc and clang wrappers.
# It adds Debian specifific options distributed in
# $CC, $CFLAGS, $CPPFLAGS and $LDFLAGS
export PATH := $(shell pwd)/debian/compiler:$(PATH)

# Detects the correct ABINAME from the DEB_BUILD_ARCH
# instead of run-time detection. Fixes FTBR.
ABINAME=$(DEB_BUILD_ARCH)
ifeq ($(ABINAME), x32)
ABINAME=amd64
endif

# Print debug information
.PHONY: debugprint
debugprint:
	@echo "=== debug: ==="
	@echo "CFLAGS: ${CFLAGS}"
	@echo "CPPFLAGS: ${CPPFLAGS}"
	@echo "LDFLAGS: ${LDFLAGS}"
	@echo "=============="
	@echo "dpkg-architecture:"
	@dpkg-architecture
	@echo "=============="

override_dh_auto_configure: debugprint
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	head -v $(shell find . -name cpucycles.3)
	(! command -v pandoc) || autogen/man
	head -v $(shell find . -name cpucycles.3)
endif
	./configure --host=$(ABINAME) --prefix=/usr

override_dh_auto_clean:
	rm -rf ./build

# disable auto installation
override_dh_auto_install:

# run cpucycles-info
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	env "LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:./build/0/package/lib" ./build/0/package/bin/cpucycles-info
endif
