#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
#export DH_OPTIONS=-v

# Enable BINDNOW. PIE is handled through hardening.patch for now.
export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# Upstream build system uses CFLAGS only
CFLAGS = $(CPPFLAGS) $(CXXFLAGS)

# Avoid unneeded library dependencies
LDFLAGS += -Wl,--as-needed

JAVA_HOME = /usr/lib/jvm/default-java/
DEB_UPSTREAM_VERSION = $(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^+]+).*-.*,\1,p')

# Target for stripping / not stripping
BUILD_TARGET = astyle
ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
    BUILD_TARGET = astyled
endif

%:
	dh $@ --parallel

override_dh_auto_build:
	dh_auto_build -Smakefile -- -C build/gcc \
		$(BUILD_TARGET) shared java \
		CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"

	[ "$(BUILD_TARGET)" = "astyle" ] || mv build/gcc/bin/$(BUILD_TARGET) build/gcc/bin/astyle

override_dh_auto_clean:
	dh_auto_clean
	$(MAKE) -C build/gcc clean
	[ ! -d build/gcc/obj ] || rmdir build/gcc/obj
	[ ! -d bin ] || rmdir bin
