#!/usr/bin/make -f
LLVM_VERSION = 2.9

deb_version		:= $(shell dpkg-parsechangelog | sed -ne "s/^Version: \(.*\)/\1/p")
upstream_version	:= $(shell echo $(deb_version) | sed -e "s/-[^-]*$$//")
major 			:= $(shell echo $(upstream_version) | sed -e "s/\([0-9]\+\.[0-9]\+\)[\.-].*/\1/g")

plugindir := $(shell gcc-4.5 -print-file-name=plugin | sed 's/4\.\([5-9]\)\../4.\1/')

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/simple-patchsys.mk
#	CXXFLAGS="-I$(shell /usr/lib/llvm-$(LLVM_VERSION)/bin/llvm-config --includedir)/llvm/Config -I$(plugindir)/include/ -fPIC" \
#	CFLAGS="-I$(shell /usr/lib/llvm-$(LLVM_VERSION)/bin/llvm-config --includedir)/llvm/Config -I$(plugindir)/include/ -fPIC" 

stamps/build: apply-patches
	# link with llvm
	LDFLAGS=-L/usr/lib/llvm-$(LLVM_VERSION)/lib $(MAKE) \
		VERBOSE=1 \
		LLVM_CONFIG=/usr/lib/llvm-$(LLVM_VERSION)/bin/llvm-config \
		-C $(DEB_SRCDIR)
	mkdir -p stamps
	touch $@

stamps/install:
	mkdir -p debian/dragonegg/$(plugindir)
	install -m755 $(DEB_SRCDIR)/dragonegg.so debian/dragonegg/$(plugindir)/
	mkdir -p stamps
	touch $@

stamps/install-llvm-gcc-4.5:
	mkdir -p debian/llvm-gcc-4.5/usr/bin
	sed 's,@plugindir@,$(plugindir),' debian/gcc-wrap.in > debian/gcc-wrap
	for i in cpp g++ gcc gccbug gcov ; do \
		install -m755 debian/gcc-wrap debian/llvm-gcc-4.5/usr/bin/llvm-$$i ; \
	done
	# Do the same for c++ as gcc but without the version (not provided by
	# gcc-defaults)
	sed 's,@plugindir@,$(plugindir),' debian/c++-wrap.in > debian/c++-wrap

build/dragonegg:: stamps/build
install/dragonegg:: stamps/install

install/llvm-gcc-4.5:: stamps/install-llvm-gcc-4.5

clean::
	$(MAKE) clean VERBOSE=1
	rm -f debian/gcc-wrap
	rm -rf stamps
