#!/usr/bin/make -f
#export DH_VERBOSE=1
#
# Uncomment to ignore all test failures (but the tests will run anyway)
#export DH_RUBY_IGNORE_TESTS=all
#
# Uncomment to ignore some test failures (but the tests will run anyway).
# Valid values:
#export DH_RUBY_IGNORE_TESTS=ruby1.8 ruby1.9.1 require-rubygems
#
# If you need to specify the .gemspec (eg there is more than one)
#export DH_RUBY_GEMSPEC=gem.gemspec

include /usr/share/dpkg/default.mk

V8_VERSION:=$(shell echo "$(DEB_VERSION_UPSTREAM)" | sed -e 's/\.[0-9]*$$//')
RUBY_VENDOR_DIR=$(shell ruby -r rbconfig -e "puts RbConfig::CONFIG['vendordir']")

### Lines below inspired from libv8-3.14 source package

# map HOST ARCH AND OS to v8 options
v8arch := $(or $(v8arch),$(if $(filter i386,$(DEB_HOST_ARCH)),ia32))
v8arch := $(or $(v8arch),$(if $(filter kfreebsd-i386,$(DEB_HOST_ARCH)),ia32))
v8arch := $(or $(v8arch),$(if $(filter hurd-i386,$(DEB_HOST_ARCH)),ia32))
v8arch := $(or $(v8arch),$(if $(filter amd64,$(DEB_HOST_ARCH)),x64))
v8arch := $(or $(v8arch),$(if $(filter kfreebsd-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)),mipsel))
v8arch := $(or $(v8arch),$(if $(filter mips,$(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))

GYPFLAGS += -Dhost_arch=$(v8arch) -DOS=$(v8os)

# build for loongson, which uses mips3, a sub-instruction-set of mips32r2
ifeq (mipsel, $(DEB_HOST_ARCH))
GYPFLAGS += -Dmips_arch_variant=loongson
endif

# build for loongson, which uses mips3, a sub-instruction-set of mips32r2
ifeq (mips, $(DEB_HOST_ARCH))
GYPFLAGS += -Dmips_arch_variant=loongson
endif

# 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
ifeq (armhf, $(DEB_HOST_ARCH))
CXXFLAGS += -fno-tree-sink
CXXFLAGS += -Wno-psabi
# enable armv7 vfpv3
GYPFLAGS += -Darmv7=1 -Darm_fpu=vfpv3 -Darm_neon=0 -Dv8_use_arm_eabi_hardfloat=true
endif

ifeq (armel, $(DEB_HOST_ARCH))
# hints can be found there:
# https://groups.google.com/forum/#!topic/v8-users/PIP1OgH0sOQ
# https://code.google.com/p/v8/issues/detail?id=914
# arm processors features are detected at runtime - and that can lead to
# a situation where compilation works but runtime crashes
CXXFLAGS += -fno-tree-sink
CXXFLAGS += -Wno-psabi
ifeq ($(shell dpkg-vendor --is ubuntu && echo true),true)
# Ubuntu targets armv7+ with VFP and thumb2 support by default for armel
GYPFLAGS += -Darmv7=1 -Darm_fpu=vfpv3 -Darm_neon=0 -Dv8_use_arm_eabi_hardfloat=false
else
DEB_MAKE_EXTRA_ARGS += vfp3=off
# Disable thumb-interworking because v8 supports it only on >= armv5t.
# http://code.google.com/p/v8/issues/detail?id=590
CXXFLAGS += -mno-thumb-interwork
# disable armv7, use softfloat
GYPFLAGS += -Darmv7=0 -Dv8_use_arm_eabi_hardfloat=false
endif
endif

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

export CXXFLAGS
export GYPFLAGS

### End copy for libv8-3.14's source package


%:
	dh $@ --buildsystem=ruby --with ruby

override_dh_auto_configure:
	# Download libv8 and the gyp build system
	mkdir -p vendor
	test -d vendor/v8/.git || git clone -b $(V8_VERSION) https://chromium.googlesource.com/v8/v8.git vendor/v8
	test -d vendor/gyp/.git || git clone https://chromium.googlesource.com/external/gyp.git vendor/gyp
	cd vendor/gyp && git checkout f7bc250 # As in upstream git repo, see
					      # https://github.com/cowboyd/libv8/tree/master/vendor
	dh_auto_configure

override_dh_auto_install:
	dh_auto_install
	# Manually install what the upstream Makefile doesn't install and
	# use some custom path (see debian/patches/use_fhs_paths.patch)
	mkdir -p debian/ruby-libv8/$(RUBY_VENDOR_DIR)/
	mkdir -p debian/ruby-libv8/usr/lib/ruby-libv8/
	cd ext && for f in libv8/*.rb libv8/.location.yml; do \
		install -m 644 -D $$f ../debian/ruby-libv8/$(RUBY_VENDOR_DIR)/$$f; \
	done
	cd vendor && for f in v8/include/*; do \
		install -m 644 -D $$f ../debian/ruby-libv8/usr/lib/ruby-libv8/$$f; \
	done
	cd vendor && for f in $$(find v8/out/ -name *.a); do \
		install -m 644 -D $$f ../debian/ruby-libv8/usr/lib/ruby-libv8/$$f; \
	done
	cd debian/ruby-libv8 && patch -p1 --no-backup-if-mismatch <$(CURDIR)/debian/patches/use_usr_lib_ruby-libv8.patch
