#!/usr/bin/make -f

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

# recent OpenSSL needed because... FIXME (git commit c232e17)
ssl-minver = 1.0.0g

# Needed by upstream build
#  * recent gyp needed to build using flock
bdeps = binutils, openssl (>= $(ssl-minver)), pkg-config
bdeps +=, bash-completion, curl, procps, zlib1g-dev, libicu-dev
bdeps +=, libkvm-dev [kfreebsd-any], gyp (>= 0.1~svn1654)

# Needed by upstream build and binary development package
#  * recent libv8 because... FIXME (git commit eb490ef)
deps = python, libssl-dev (>= $(ssl-minver))
deps +=, libuv1-dev, ca-certificates

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

# suppress checking binary files, to not upset dpkg-source
DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^(deps/v8/benchmarks/v8-logo\.png|test/fixtures/person\.jpg.*|src/res/node\.ico|debian/(changelog|copyright(|_hints|_newhints)))$

DEB_CONFIGURE_NORMAL_ARGS =
DEB_CONFIGURE_EXTRA_FLAGS = --without-npm --shared-openssl --shared-zlib --shared-libuv --with-intl=system-icu --prefix=/usr

# 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)),arm))
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 mips,$(DEB_HOST_ARCH)),mips))

# 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
ifneq (, $(destCpu))
DEB_CONFIGURE_EXTRA_FLAGS += --dest-cpu=$(destCpu)
endif

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

# all mips arches with fp32 because of https://github.com/paul99/v8m-rb/issues/192

# use loongson because it is a subset of mips32r1
ifeq (mipsel, $(DEB_HOST_ARCH))
DEB_CONFIGURE_EXTRA_FLAGS += --with-mips-arch-variant=loongson --with-mips-fpu-mode=fp32
endif
ifeq (mips, $(DEB_HOST_ARCH))
DEB_CONFIGURE_EXTRA_FLAGS += --with-mips-arch-variant=loongson --with-mips-fpu-mode=fp32
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=fp32
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 vfpv2, disable armv7
DEB_CONFIGURE_EXTRA_FLAGS += --with-arm-fpu=vfpv2
else
# enable armv7 vfpv3
DEB_CONFIGURE_EXTRA_FLAGS += --with-arm-fpu=vfpv3
endif
endif

DEB_DESTDIR = $(CURDIR)/debian/tmp
DEB_MAKE_CLEAN_TARGET = clean

#  relax regression tests when targeted experimental suite
exp-relax-check := $(if $(shell dpkg-parsechangelog | grep -x 'Distribution: \(experimental\|UNRELEASED\)'),-i)
DEB_MAKE_CHECK_TARGET = $(exp-relax-check)

# hardening gyp
CXXFLAGS+=$(CPPFLAGS)
export CXXFLAGS
export LDFLAGS

# 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 -f config.gypi
	rm -f config.mk
	rm -f test/fixtures/hello.txt
	rm -rf test/tmp
	find . -name "*.pyc" -delete
	rm -rf out

binary-post-install/nodejs::
	mv debian/nodejs/usr/bin/node debian/nodejs/usr/bin/nodejs
	mv debian/nodejs/usr/share/man/man1/node.1 debian/nodejs/usr/share/man/man1/nodejs.1

# remove *.json files from documentation
# remove all <script> tags from html files, js files are not installed anyway
# TODO: when doc will be generated from markdown, patch doc/template.html instead
binary-install/nodejs::
	find debian/nodejs/usr/share/doc/nodejs -name *.json -delete

build/nodejs::
	make doc
