#!/usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE=1

# To prevent bogus linkage.
LDFLAGS += -Wl,--as-needed

# avoid FTBFS with GCC 6 due to translator-asm-helpers.S not being PIC
# might be fixed with commit 8b6c9f4 in HHVM 3.14
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e no-pie),)
  CFLAGS  += -fno-pie
  LDFLAGS += -no-pie
endif

# used to store HHBC during tests
BUILDHOME = $(CURDIR)/debian/build

# Exported variable to make sure that we don't use cursor control in the tests.
export TRAVIS = bogus

# Contrary to its confusing name, this is actually the build's revision ID.
# hphp/util/generate-buildinfo.sh uses:
#   $COMPILER_ID -> git describe -> hg log -> date
# We want to avoid relying on git (because we may building from the .dsc) and
# date (because that would add variance to the build)
export COMPILER_ID = $(shell dpkg-parsechangelog -SVersion)

# same with the repo schema, which uniquely identifies a build; the script
# takes some variance into account, possibly from CMakeFiles; it also operates
# differently when under git, so make it just a hash of the Debian version.
export HHVM_REPO_SCHEMA = $(shell dpkg-parsechangelog -SVersion | sha1sum | cut -b-40)

%:
	dh $@ --parallel

override_dh_auto_build:
	# rerun flex/bison to regenerate their output for DFSG reasons
	./hphp/parser/make-lexer.sh
	./hphp/parser/make-parser.sh
	VERBOSE=1 dh_auto_build

override_dh_auto_configure:
	printenv
	echo $(LDFLAGS)
	dh_auto_configure -- \
		-DUSE_JSONC=ON \
		-DFREETYPE_INCLUDE_DIRS=/usr/include/freetype2/ \
		-DENABLE_MCROUTER=OFF \
		-DENABLE_ASYNC_MYSQL=OFF \
		-DMYSQL_UNIX_SOCK_ADDR=/dev/null \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	# HHVM by default uses ~/.hhvm.hhbc, but ~ isn't guranteed to exist.
	-mkdir $(BUILDHOME)
	HOME=$(BUILDHOME) hphp/hhvm/hhvm hphp/test/run -m jit quick
	HOME=$(BUILDHOME) hphp/hhvm/hhvm hphp/test/run -m interp quick
endif

override_dh_clean:
	dh_clean
	-rm -rf $(BUILDHOME)

override_dh_gencontrol:
	# see hphp/runtime/version.h, HHVM_VERSION_BRANCH
	hhvmapi=$(shell $(CURDIR)/debian/hhvm/usr/bin/hhvm --version | sed -ne 's/^HipHop VM \(.*\)\..*/\1/p') && \
		[ -n "$${hhvmapi}" ] && echo "hhvm:ApiVersion=hhvm-api-$${hhvmapi}" >> debian/hhvm.substvars
	dh_gencontrol

override_dh_strip:
	dh_strip --dbg-package=hhvm-dbg

override_dh_fixperms:
	dh_fixperms
	chown www-data:www-data \
		$(CURDIR)/debian/hhvm/var/cache/hhvm
