#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1

include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk

# The upstream Makefile promotes several warnings to errors by default
# (-Werror=discarded-qualifiers, -Werror=incompatible-pointer-types,
# -Werror=implicit-function-declaration, -Werror=int-conversion).
# Since glibc 2.43 strchr() is const-correct in C23 mode, and src/cbortojson.c
# defines _GNU_SOURCE, so its line 343 (strchr() result assigned to a non-const
# pointer) fails with -Werror=discarded-qualifiers on every architecture.
# Use the switch provided by the upstream Makefile to stop turning warnings
# into errors; warnings themselves are still emitted.
export DISABLE_WERROR = 1

TARGET_DIR = $(CURDIR)/debian/tmp/usr
TARGET_LIBDIR = $(TARGET_DIR)/lib/$(DEB_HOST_MULTIARCH)

%:
	dh $@

override_dh_auto_clean:
	$(MAKE) clean
	rm -f .config

override_dh_auto_configure:

override_dh_auto_build:
	$(MAKE) CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)"
	$(MAKE) docs

override_dh_auto_test:
ifeq (,$(filter, nocheck,$(DEB_BUILD_OPTIONS)))
	@echo 'blhc: ignore-line-regexp: .*(qt|Qt|tst).*'
	$(MAKE) check
endif

override_dh_auto_install:
	$(MAKE) install prefix=$(TARGET_DIR) libdir=$(TARGET_LIBDIR)

execute_after_dh_auto_build:
	help2man --version-string=$(DEB_VERSION_UPSTREAM) --name="tinycbor Concise Binary Object Representation (CBOR) library" -N -o cbordump.1 bin/cbordump
	help2man --version-string=$(DEB_VERSION_UPSTREAM) --name="tinycbor Concise Binary Object Representation (CBOR) library" -N -o tinycbor-json2cbor.1 bin/json2cbor

override_dh_installdocs:
ifeq (,$(filter, nodoc,$(DEB_BUILD_OPTIONS)))
	dh_installdocs
	dh_link -plibtinycbor-dev /usr/share/javascript/jquery/jquery.js usr/share/doc/libtinycbor-dev/html/jquery.js
endif
