#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

# see FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

ifneq (,$(filter $(DEB_HOST_ARCH), armel mips mipsel powerpc))
  export DEB_LDFLAGS_MAINT_APPEND = -latomic
endif

WITH_ASAN  = -DWLCS_BUILD_ASAN=ON
WITH_UBSAN = -DWLCS_BUILD_UBSAN=ON
WITH_TSAN  = -DWLCS_BUILD_TSAN=ON

ifeq ($(DEB_DISTRIBUTION),xenial)
  ifneq ($(filter arm64 ppc64el, $(DEB_HOST_ARCH)),)
    # arm64 on 16.04 does not have tsan
    WITH_TSAN = -DWLCS_BUILD_TSAN=OFF
  endif
endif

ifneq ($(filter armel armhf i386 mipsel mips64el s390x alpha hppa ia64 loong64 m68k powerpc riscv64 sh4 sparc64 x32, $(DEB_HOST_ARCH)),)
  WITH_TSAN = -DWLCS_BUILD_TSAN=OFF
endif

ifneq ($(filter mipsel mips64el alpha hppa ia64 loong64 m68k powerpc riscv64 sh4, $(DEB_HOST_ARCH)),)
  WITH_UBSAN = -DWLCS_BUILD_UBSAN=OFF
endif

ifneq ($(filter mipsel mips64el s390x alpha hppa ia64 loong64 m68k riscv64 sh4 sparc64, $(DEB_HOST_ARCH)),)
  WITH_ASAN = -DWLCS_BUILD_ASAN=OFF
endif

COMMON_CONFIGURE_OPTIONS =\
  $(WITH_ASAN) \
  $(WITH_UBSAN) \
  $(WITH_TSAN) \
  -DCMAKE_INSTALL_LIBEXECDIR="lib/${DEB_HOST_MULTIARCH}"

override_dh_auto_configure:
	dh_auto_configure -- $(COMMON_CONFIGURE_OPTIONS)

%:
	dh $@

get-orig-source:
	uscan --noconf --force-download --rename --download-current-version --destdir=..
