#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie

# This needs to run before inclusion of CDBS snippets
debian/control:: debian/control.in debian/watch
DEB_PHONY_RULES += debian/control.in debian/watch
debian/control.in::
	sed $(foreach re,ABI ARCHS BRANCH,-e 's/__$(re)__/$($(re))/g') \
		< debian/control.in.in > debian/control.in

debian/watch::
	sed $(foreach re,BRANCH,-e 's/__$(re)__/$($(re))/g') \
		< debian/watch.in > debian/watch

include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/autotools.mk

# source equivalent of nodejs -e 'console.log(process.versions.modules)'
ABI = $(shell awk '/^\#define NODE_MODULE_VERSION/ {print $$3}' src/node_version.h)

# archs
ARCHS := amd64 arm64 armhf i386 kfreebsd-i386 kfreebsd-amd64 mips mips64el mips64r6el mipsel ppc64 ppc64el s390x

# branch
BRANCH := $(word 1, $(subst .,$(space), $(DEB_UPSTREAM_VERSION)))

# Build-depend unversioned on debhelper
#  TODO: Drop when adopted in cdbs
CDBS_BUILD_DEPENDS_rules_debhelper_v9 = debhelper (>=9.20160114)

# Do not install licensecheck please
CDBS_BUILD_DEPENDS_rules_utils_copyright-check =

# Needed by upstream build
#  * recent gyp needed to build using flock
bdeps = pkg-config, bash-completion, curl, procps, libkvm-dev [kfreebsd-any], gyp (>= 0.1~svn1773)
bdeps +=, openssl (>= 1.1.1~)
bdeps +=, zlib1g-dev
bdeps +=, libicu-dev
bdeps +=, libc-ares-dev (>= 1.14~)
bdeps +=, libnghttp2-dev (>= 1.34.0~)
# bdeps +=, libhttp-parser-dev (>= 2.8~)
bdeps +=, node-acorn
# Needed for tests
bdeps +=, ca-certificates

# Needed by upstream build and binary development package
deps = python
deps +=, libssl-dev (>= 1.1.1~)
deps +=, libuv1-dev (>= 1.20.0~)

CDBS_BUILD_DEPENDS +=, $(bdeps), $(deps)
CDBS_DEPENDS_nodejs-dev = $(deps)
CDBS_DEPENDS_libnode-dev = libuv1-dev

CDBS_RECOMMENDS_nodejs = ca-certificates
CDBS_BREAKS_libnode-dev = nodejs-dev (<< 10.0.0~dfsg1-3), libnode64-dev
CDBS_REPLACES_libnode-dev = nodejs-dev (<< 10.0.0~dfsg1-3), libnode64-dev
LANG=C
export LANG
DEB_CONFIGURE_NORMAL_ARGS =
DEB_CONFIGURE_EXTRA_FLAGS = \
--without-npm \
--without-snapshot \
--shared \
--shared-zlib \
--shared-cares \
--shared-nghttp2 \
--with-intl=system-icu \
--prefix=/usr \
--openssl-use-def-ca-store \
--arch-triplet=$(DEB_HOST_MULTIARCH) \
--node-relative-path="lib/$(DEB_HOST_MULTIARCH)/nodejs:share/nodejs:lib/nodejs" \
--shared-libuv

DEB_CONFIGURE_EXTRA_FLAGS += --shared-openssl

# map HOST ARCH AND OS, and if unknown let upstream guess

# ia32 x64 x32 arm arm64 mips mipsel mips64el x87 ppc ppc64
destCpu =
destCpu := $(or $(destCpu),$(if $(filter i386,$(DEB_HOST_ARCH)),ia32))
destCpu := $(or $(destCpu),$(if $(filter x32,$(DEB_HOST_ARCH)),x32))
destCpu := $(or $(destCpu),$(if $(filter kfreebsd-i386,$(DEB_HOST_ARCH)),ia32))
destCpu := $(or $(destCpu),$(if $(filter hurd-i386,$(DEB_HOST_ARCH)),ia32))
destCpu := $(or $(destCpu),$(if $(filter amd64,$(DEB_HOST_ARCH)),x64))
destCpu := $(or $(destCpu),$(if $(filter kfreebsd-amd64,$(DEB_HOST_ARCH)),x64))
destCpu := $(or $(destCpu),$(if $(filter armel,$(DEB_HOST_ARCH)),unsupported))
destCpu := $(or $(destCpu),$(if $(filter armhf,$(DEB_HOST_ARCH)),arm))
destCpu := $(or $(destCpu),$(if $(filter aarch64,$(DEB_HOST_ARCH)),arm64))
destCpu := $(or $(destCpu),$(if $(filter mipsel,$(DEB_HOST_ARCH)),mipsel))
destCpu := $(or $(destCpu),$(if $(filter mips64el,$(DEB_HOST_ARCH)),mips64el))
destCpu := $(or $(destCpu),$(if $(filter mips64r6el,$(DEB_HOST_ARCH)),mips64el))
destCpu := $(or $(destCpu),$(if $(filter mips,$(DEB_HOST_ARCH)),mips))
destCpu := $(or $(destCpu),$(if $(filter powerpc,$(DEB_HOST_ARCH)),unsupported))
destCpu := $(or $(destCpu),$(if $(filter ppc64,$(DEB_HOST_ARCH)),ppc64))
destCpu := $(or $(destCpu),$(if $(filter s390x,$(DEB_HOST_ARCH)),s390x))

# solaris freebsd openbsd linux
destOs =
destOs := $(or $(destOs),$(if $(filter linux,$(DEB_HOST_ARCH_OS)),linux))
destOs := $(or $(destOs),$(if $(filter kfreebsd,$(DEB_HOST_ARCH_OS)),freebsd))

ifneq (, $(destOs))
DEB_CONFIGURE_EXTRA_FLAGS += --dest-os=$(destOs)
endif
ifeq (unsupported, $(destCpu))
$(error "nodejs cannot run on $(DEB_HOST_ARCH), please consult maintainers")
endif
ifneq (, $(destCpu))
DEB_CONFIGURE_EXTRA_FLAGS += --dest-cpu=$(destCpu)
endif

ifeq (freebsd, $(destOs))
LDFLAGS += -lbsd
endif

# mips and fpu
# https://github.com/paul99/v8m-rb/issues/192
# https://bugs.debian.org/800410
# mips and big binaries
# http://gcc.gnu.org/wiki/DebugFission
# https://bugs.debian.org/829139

# use r2, which is the supported variant for these architectures
# https://lists.debian.org/debian-mips/2016/07/msg00000.html
ifeq (mipsel, $(DEB_HOST_ARCH))
DEB_CONFIGURE_EXTRA_FLAGS += --with-mips-arch-variant=r2 --with-mips-fpu-mode=fpxx
LDFLAGS += -latomic
CFLAGS += -gsplit-dwarf
CPPFLAGS += -gsplit-dwarf
endif
ifeq (mips, $(DEB_HOST_ARCH))
DEB_CONFIGURE_EXTRA_FLAGS += --with-mips-arch-variant=r2 --with-mips-fpu-mode=fpxx
LDFLAGS += -latomic
CFLAGS += -gsplit-dwarf
CPPFLAGS += -gsplit-dwarf
endif

# mips32r1 or greater, detected at runtime
ifeq (mips64el, $(DEB_HOST_ARCH))
DEB_CONFIGURE_EXTRA_FLAGS += --with-mips-arch-variant=rx --with-mips-fpu-mode=fp64
endif
ifeq (mips64r6el, $(DEB_HOST_ARCH))
DEB_CONFIGURE_EXTRA_FLAGS += --with-mips-arch-variant=rx --with-mips-fpu-mode=fp64
endif

ifeq (armel, $(DEB_HOST_ARCH))
DEB_CONFIGURE_EXTRA_FLAGS += --with-arm-float-abi=softfp
ifeq ($(shell dpkg-vendor --is ubuntu && echo true),true)
# Ubuntu targets armv7+ with VFP and thumb2 support by default for armel
DEB_CONFIGURE_EXTRA_FLAGS += --with-arm-fpu=vfpv3
else
# debian defaults
# v8 does not support < armv5
CXXFLAGS += -march=armv5t
DEB_CONFIGURE_EXTRA_FLAGS += --with-arm-fpu=vfp
endif
endif

ifeq (armhf, $(DEB_HOST_ARCH))
DEB_CONFIGURE_EXTRA_FLAGS += --with-arm-float-abi=hard
ifeq ($(shell dpkg-vendor --derives-from raspbian && echo true),true)
# enable vfp, disable armv7
DEB_CONFIGURE_EXTRA_FLAGS += --with-arm-fpu=vfp
else
# enable armv7 vfpv3
DEB_CONFIGURE_EXTRA_FLAGS += --with-arm-fpu=vfpv3
endif
endif

DEB_DESTDIR = $(CURDIR)/debian/tmp
DEB_MAKE_CLEAN_TARGET = clean
ifeq ($(DEB_PARALLEL_JOBS),)
JOBS := 1
else ifeq ($(SCHROOT_USER),"buildd")
JOBS := 1
else
JOBS := $(DEB_PARALLEL_JOBS)
endif
export JOBS
DEB_MAKE_BUILD_TARGET = build

#  relax regression tests when targeted experimental suite
export HOME = $(CURDIR)/tmp
export FLAKY_TESTS = dontcare
export TEST_CI_ARGS = --arch=$(destCpu) --timeout=3000
export CI_JS_SUITES = message parallel sequential
export NODE_TEST_DIR = $(CURDIR)/tmp
# needed for tests to avoid "ee key too small" errors
export OPENSSL_CONF = ./deps/openssl/openssl/apps/openssl.cnf
# keep in mind ./debian/rules check is also invoked by adt-run, see debian/tests/
exp-relax-check := $(if $(shell dpkg-parsechangelog | grep -x 'Distribution: \(experimental\|UNRELEASED\)'),-i)
DEB_MAKE_CHECK_TARGET = $(exp-relax-check) test-ci-js

# build -fPIC
CFLAGS+=-fPIC
CPPFLAGS+=-fPIC
# hardening gyp
CFLAGS+=-g
CPPFLAGS+=-g
CXXFLAGS+=$(CPPFLAGS)
export CPPFLAGS
export CXXFLAGS
export CFLAGS
export LDFLAGS

# disable DNS requests during tests
export RES_OPTIONS = attempts:0

# properly clean files from build, test, python
clean::
	rm -f icu_config.gypi
	rm -rf test/addons/doc-*
	rm -f test/addons/.buildstamp
	rm -f test/addons/.docbuildstamp
	rm -rf test/addons/*/build
	rm -f config.gypi
	rm -f config.mk
	rm -f test/fixtures/hello.txt
	rm -rf $(NODE_TEST_DIR)
	rm -f node_modules
	rm -rf tools/node_modules
	find . -name "*.pyc" -delete
	rm -rf out
	rm -rf deps/acorn/
	rm -f debian/libnode$(ABI).install
	rm -f debian/libnode-dev.links

# remove *.json files from documentation
binary-install/nodejs-doc::
	find debian/nodejs-doc/usr/share/doc/nodejs-doc -name *.json -delete

pre-build::
	ln -sTf /usr/lib/nodejs node_modules
	mkdir -p $(NODE_TEST_DIR)
	mkdir -p deps/acorn/dist/
	cp -rf /usr/lib/nodejs/acorn/dist/* deps/acorn/dist/

common-install-arch::
	sed 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' debian/libnode.install > debian/libnode$(ABI).install
	sed -e 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' -e 's/@ABI@/$(ABI)/g' debian/libnode-dev.links.in > debian/libnode-dev.links

build/nodejs-doc::
	mkdir -p ./tools/node_modules/eslint/node_modules
	ln -sf -t ./tools/node_modules/eslint/node_modules/ /usr/lib/nodejs/js-yaml/
	make doc-only

check:
	-ln -s /usr/bin/node node
	mkdir -p $(NODE_TEST_DIR)
	make $(DEB_MAKE_CHECK_TARGET)
	rm -f node
	rm -rf $(NODE_TEST_DIR)
