#!/usr/bin/make -f

# This needs to run before inclusion of CDBS snippets
debian/control:: debian/control.in
DEB_PHONY_RULES += debian/control.in
debian/control.in::
	sed -e 's/__LIBPKGNAME__/$(LIBPKGNAME)/g' <debian/control.in.in >debian/control.in

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

SONAME = $(DEB_UPSTREAM_VERSION)
LIBPKGNAME = libv8-$(SONAME)

# suppress checking binary files, to not upset dpkg-source
DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^(benchmarks/v8-logo\.png|test/mjsunit/unicode-test\.js|debian/(changelog|copyright(|_hints|_newhints)))$

DEB_DH_MAKESHLIBS_ARGS = -- -c4

# map HOST ARCH AND OS to v8 options
v8arch := $(or $(v8arch),$(if $(filter i386,$(DEB_HOST_ARCH)),ia32))
v8arch := $(or $(v8arch),$(if $(filter amd64,$(DEB_HOST_ARCH)),x64))
v8arch := $(or $(v8arch),$(if $(filter armel,$(DEB_HOST_ARCH)),arm))
v8arch := $(or $(v8arch),$(if $(filter armhf,$(DEB_HOST_ARCH)),arm))
v8arch := $(or $(v8arch),$(if $(filter mipsel,$(DEB_HOST_ARCH)),mips))
v8arch := $(or $(v8arch),$(DEB_HOST_ARCH))
v8os := $(or $(v8os),$(if $(filter linux,$(DEB_HOST_ARCH_OS)),linux))
v8os := $(or $(v8os),$(if $(filter kfreebsd,$(DEB_HOST_ARCH_OS)),freebsd))
v8os := $(or $(v8os),$(DEB_HOST_ARCH_OS))

# force aligned access
v8armunaligned :=
# armel and armhf arches need flags to work around those issues :
# -fno-tree-sink: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39604
# -Wno-psabi: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42748
v8armeabi := soft
# armhf is hardfloat
ifeq (armhf, $(DEB_HOST_ARCH))
v8armeabi := hard
CCFLAGS += -fno-tree-sink
CXXFLAGS += -Wno-psabi
endif

ifeq (armel, $(DEB_HOST_ARCH))
v8armunaligned := unalignedaccesses=off
CCFLAGS += -fno-tree-sink
CXXFLAGS += -Wno-psabi
# Disable thumb-interworking because v8 supports it only on >= armv5t.
# http://code.google.com/p/v8/issues/detail?id=590
CCFLAGS += -mno-thumb-interwork
endif

export CCFLAGS
export CXXFLAGS

DEB_SCONS_OPTIONS = library=shared soname=on snapshot=on shlibtype=hidden arch=$(v8arch) os=$(v8os) armeabi=$(v8armeabi) $(v8armunaligned)
DEB_SCONS_BUILD_TARGET = library

# Scons does not always add -soname param, force it. This prevents gold linker from erroring.
DEB_SCONS_ENVVARS = SONAME=libv8.so.$(SONAME)

# regression tests need to build shell for mjsunit -- will be deprecated in 3.5 branch in favor of d8. shell becoming a simple v8 cli.
DEB_SCONS_CHECK_TARGET = sample sample=shell

DEB_SCONS_INSTALL_OPTIONS += DESTDIR="$(cdbs_curdestdir)"

DEB_DH_INSTALL_ARGS_$(LIBPKGNAME) = usr/lib/libv8.so.$(SONAME)

clean::
	rm -f tools/*.pyc libv8-$(SONAME).so
	rm -rf obj
	rm -f test/*/*.pyc
	rm -f shell

# actually run regression tests
#  * relax regression tests when targeted experimental suite
#  * run only javascript tests, cctests are for development purposes
exp-relax-check := $(if $(shell dpkg-parsechangelog | grep -Fx 'Distribution: experimental'),-)
common-post-build-impl::
	$(exp-relax-check)$(if $(filter nocheck,$(DEB_BUILD_OPTIONS)),,LD_PRELOAD=$(CURDIR)/libv8-$(SONAME).so $(CURDIR)/tools/test.py --progress=verbose --snapshot --no-build --timeout=120 mjsunit message arch=$(v8arch))

common-install-impl::
	cd debian/tmp/usr/lib/ ; \
	mv libv8-$(SONAME)*.so libv8.so.$(SONAME) ; \
	ln -s -T libv8.so.$(SONAME) libv8.so
