#!/usr/bin/make -f

TARGET_BUILD	:= build-clang
GCC_VERSION     := 4.7
LLVM_VERSION	:= 3.2
SONAME_EXT      := 1
DEBIAN_REVISION := $(shell dpkg-parsechangelog | sed -rne 's,^Version: .*-(.*),\1,p')
ifneq (,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
  NJOBS := -j $(subst parallel=,,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
endif
VENDOR=$(lsb_release -is)

DEB_HOST_MULTIARCH  = $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_HOST_GNU_TYPE   = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_ARCH_BITS  = $(shell dpkg-architecture -qDEB_HOST_ARCH_BITS)
DEB_HOST_ARCH       = $(shell dpkg-architecture -qDEB_HOST_ARCH)


confargs := \
        --with-c-include-dirs=/usr/include/$(DEB_HOST_MULTIARCH):/usr/include/$(DEB_HOST_GNU_TYPE):/usr/include \
	--with-cxx-include-root=/usr/include/c++/$(GCC_VERSION) \
	--with-cxx-include-arch=$(DEB_HOST_GNU_TYPE) \
	--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE)

ifeq ($(shell dpkg-architecture -qDEB_HOST_ARCH_BITS),64)
  confargs += --with-cxx-include-32bit-dir=32
else
  confargs += --with-cxx-include-64bit-dir=64
endif

opt_flags = -g -O2
ifneq (,$(findstring $(DEB_HOST_ARCH),armel))
  opt_flags += -marm
endif


%:
	dh $@

override_dh_auto_configure:
	xzcat /usr/src/llvm-$(LLVM_VERSION)/llvm-$(LLVM_VERSION).tar.xz|tar -x -f -
	mkdir -p tools/clang/include/clang/Debian
	mkdir -p $(TARGET_BUILD)
	sed -e "s|@DEB_HOST_MULTIARCH@|$(DEB_HOST_MULTIARCH)|" \
		-e "s|@DEB_HOST_GNU_TYPE@|$(DEB_HOST_GNU_TYPE)|" \
		-e "s|@DEB_PATCHSETVERSION@|$(DEBIAN_REVISION)|" \
		debian/debian_path.h > tools/clang/include/clang/Debian/debian_path.h

# Remove some old symlinks
	cd llvm-$(LLVM_VERSION)/tools/ && \
	if test -h clang; then \
		rm clang; \
	fi; \
    ln -s ../../tools/clang .; \
	readlink clang; \
	if test -h lldb; then \
		rm lldb; \
	fi; \
    ln -s ../../tools/lldb .; \


	cd llvm-$(LLVM_VERSION)/projects/ && \
	if test -h compiler-rt; then \
		rm compiler-rt; \
	fi; \
	ln -s ../../compiler-rt .; \
	readlink compiler-rt

# Start the actual configure
	cd $(TARGET_BUILD) && \
	  ../llvm-$(LLVM_VERSION)/configure $(confargs) \
	--prefix=/usr/ \
	--disable-assertions --enable-shared --enable-optimized \
	--with-optimize-option=' $(opt_flags)' --enable-pic --enable-libffi \
	CLANG_VENDOR=$(VENDOR)	|| { cat config.log; exit 1; }

override_dh_auto_build:
	$(MAKE) $(NJOBS) -C $(TARGET_BUILD) VERBOSE=1 ONLY_TOOLS="clang" CLANG_VENDOR=$(VENDOR) CXXFLAGS=-std=c++0x
# Only available in clang 3.3
#	cd $(CURDIR)/tools/clang/docs && make -f Makefile.sphinx
# && make -f Makefile.sphinx man

override_dh_auto_install:
	chrpath -d build-clang/Release/bin/clang build-clang/Release/bin/clang-check build-clang/Release/bin/clang-tblgen
	$(MAKE) -C $(TARGET_BUILD)  VERBOSE=1  ONLY_TOOLS="clang" install DESTDIR=$(CURDIR)/debian/tmp/
	cd debian/tmp/usr/lib/&& mv libclang.so libclang.so.$(SONAME_EXT) && ln -s libclang.so.$(SONAME_EXT) libclang.so
#	cd build-clang/tools/clang/runtime/compiler-rt/ && rm -rf $$(find . -iname "SubDir.lib*" -o -iname .dir) && rm -rf $$(find -empty)
	mkdir -p $(CURDIR)/debian/compiler-rt/usr/bin/
	cp compiler-rt/lib/asan/scripts/asan_symbolize.py $(CURDIR)/debian/compiler-rt/usr/bin/asan_symbolize
# Create this fake directory to make the install libclang-common-dev happy
# under the unsupported archs of compiler-rt
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/clang/$(LLVM_VERSION)/lib

.PHONY: override_dh_strip
override_dh_strip:
	dh_strip --dbg-package=libclang$(SONAME_EXT)-dbg

override_dh_auto_test:
	$(MAKE) -C $(TARGET_BUILD)  check || true

override_dh_auto_clean:
	rm -rf build-clang llvm-$(LLVM_VERSION) tools/clang/include/clang/Debian/debian_path.h
