#!/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

CXXFLAGS += -fno-strict-aliasing -Wno-error=unused-but-set-variable
CFLAGS += -fno-strict-aliasing -Wno-error=unused-but-set-variable
export CFLAGS
export CXXFLAGS

# 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))

DEB_SCONS_OPTIONS = library=shared soname=on snapshot=on shlibtype=hidden arch=$(v8arch) os=$(v8os)
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
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
