#!/usr/bin/make -f

# let debhelper be verbose
#export DH_VERBOSE=1

include /usr/share/dpkg/architecture.mk
export DEB_HOST_MULTIARCH

ifeq ($(DEB_HOST_ARCH),armel)
DEB_MAKE_ENVVARS := ARCH=arm CONFIG_AEABI=y
endif
ifeq ($(DEB_HOST_ARCH),armhf)
DEB_MAKE_ENVVARS := ARCH=arm CONFIG_AEABI=y CPU_ARCH=armv6 CPU_TUNE=arm1176jzf-s CONFIG_KLIBC_THUMB=n
endif
ifeq ($(DEB_HOST_ARCH),ia64)
DEB_MAKE_ENVVARS := ARCH=ia64
endif
ifeq ($(DEB_HOST_ARCH),i386)
DEB_MAKE_ENVVARS := ARCH=i386
endif
ifeq ($(DEB_HOST_ARCH),sparc)
DEB_MAKE_ENVVARS := ARCH=sparc
endif
ifeq ($(DEB_HOST_ARCH),sparc64)
DEB_MAKE_ENVVARS := ARCH=sparc64
endif
ifeq ($(DEB_HOST_ARCH),s390)
DEB_MAKE_ENVVARS := ARCH=s390
endif
ifeq ($(DEB_HOST_ARCH),s390x)
DEB_MAKE_ENVVARS := ARCH=s390x
endif
ifneq (,$(filter powerpc%,$(DEB_HOST_ARCH)))
DEB_MAKE_ENVVARS := ARCH=ppc
endif
ifneq (,$(filter mips mipsel,$(DEB_HOST_ARCH)))
DEB_MAKE_ENVVARS := ARCH=mips
endif
ifneq (,$(filter mips64 mips64el,$(DEB_HOST_ARCH)))
DEB_MAKE_ENVVARS := ARCH=mips64
endif
ifeq ($(DEB_HOST_ARCH),sh4)
DEB_MAKE_ENVVARS := ARCH=sh
endif
ifneq (,$(filter ppc64%,$(DEB_HOST_ARCH)))
DEB_MAKE_ENVVARS := ARCH=ppc64
endif
ifeq ($(DEB_HOST_ARCH),riscv64)
DEB_MAKE_ENVVARS := ARCH=riscv64
endif

DEB_MAKE_ENVVARS := INSTALLROOT=debian/tmp $(DEB_MAKE_ENVVARS)

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
    DEB_MAKE_ENVVARS += CROSS_COMPILE=$(DEB_HOST_GNU_TYPE)-
endif


# Enable this to get verbose build information
DEB_MAKE_ENVVARS += KBUILD_VERBOSE=1

# Defer stripping of debug symbols to debhelper
DEB_MAKE_ENVVARS += CONFIG_DEBUG_INFO=y

%:
	dh $@ --link-doc=libklibc

override_dh_auto_build:
	rm -rf linux
	mkdir -p linux/include
	for x in /usr/include/linux /usr/include/asm-generic \
		/usr/include/$(DEB_HOST_MULTIARCH)/asm; do \
		ln -s $${x} linux/include || exit; \
	done
	$(MAKE) all $(DEB_MAKE_ENVVARS)

override_dh_auto_clean:
	rm -rf linux test-*.log
	MAKEFLAGS="$(DEB_MAKE_ENVVARS)" dh_auto_clean

override_dh_auto_install:
	$(MAKE) install $(DEB_MAKE_ENVVARS)
# Replace kernel UAPI header copies with links
	for x in /usr/include/linux /usr/include/asm-generic \
		/usr/include/$(DEB_HOST_MULTIARCH)/asm; do \
		rm -rf debian/tmp/usr/lib/klibc/include/$$(basename $${x}) \
		&& ln -s $${x} debian/tmp/usr/lib/klibc/include/ \
		|| exit; \
	done

define run_test_program
usr/klibc/tests/$(1) > test-$(1).log
! grep -qw ERROR test-$(1).log
$(if $(2),grep -qF -- $(2) test-$(1).log)
$(if $(3),grep -qF -- $(3) test-$(1).log)
endef

comma=,

override_dh_auto_test:
	$(MAKE) test $(DEB_MAKE_ENVVARS)
	$(call run_test_program,microhello)
	$(call run_test_program,minihello)
	$(call run_test_program,hello)
	$(call run_test_program,environ,'Verifying envp == environ... ok')
	$(call run_test_program,fcntl)
	$(call run_test_program,malloctest)
	$(call run_test_program,malloctest2)
	$(call run_test_program,opentest,"Line 1 = $$(head -1 /etc/passwd)")
	$(call run_test_program,pipetest)
	$(call run_test_program,select)
	$(call run_test_program,setjmptest,"calling longjmp with 256... setjmp returned 256")
	$(call run_test_program,sigint,"Signal received OK")
	$(call run_test_program,socket)
	$(call run_test_program,sscanf)
	$(call run_test_program,stdio,"Hello$(comma) World!","Hello again - and some more - and some more")
	$(call run_test_program,strlcpycat)
	$(call run_test_program,vfork)
	usr/dash/static/sh -c "exit"
	usr/dash/static/sh -c "usr/utils/static/true; exit"

override_dh_fixperms:
	dh_fixperms -X.so

override dh_strip:
	dh_strip -pklibc-utils -plibklibc
