#!/usr/bin/make -f

TARGET_BUILD	:= build-llvm
# Force the version of gcc. Some archs are still using gcc 4.6
GCC_VERSION     := $(shell dpkg-query -W -f '$${Version}' g++ | sed -rne 's,^([0-9]+:)?([0-9]+\.[0-9]+).*$$,\2,p')
LLVM_VERSION	:= 3.4
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=$(shell 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)

OCAML_STDLIB_DIR    ?= $(shell ocamlc -where)

export CC=gcc-$(GCC_VERSION)
export CXX=g++-$(GCC_VERSION)

LDFLAGS_EXTRA=
CXXFLAGS_EXTRA=
CONFIGURE_EXTRA=

confargs := \
	--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

ifeq ($(shell dpkg --compare-versions $(shell dpkg-query -W -f '$${Version}' g++-$(GCC_VERSION)) ge 4.8-20121128-1~ ; echo $$?),0)
	control_vars = '-Vdep:devlibs=libstdc++-$(GCC_VERSION)-dev, libgcc-$(GCC_VERSION)-dev, libobjc-$(GCC_VERSION)-dev'
else ifeq ($(shell dpkg --compare-versions $(shell dpkg-query -W -f '$${Version}' g++-$(GCC_VERSION)) ge 4.7.2-10~ ; echo $$?),0)
	control_vars = '-Vdep:devlibs=libstdc++6-$(GCC_VERSION)-dev, libgcc-$(GCC_VERSION)-dev, libobjc-$(GCC_VERSION)-dev'
else
	control_vars = '-Vdep:devlibs=libstdc++6-$(GCC_VERSION)-dev'
endif

BINUTILS_GOLD_ARCHS := amd64 armhf i386 powerpc powerpcspe ppc64 sparc sparc64 x32
ifeq ($(shell dpkg --compare-versions $(shell dpkg-query -W -f '$${Version}' binutils) ge 2.23.1-1~exp3 ; echo $$?),0)
ifneq (,$(findstring $(DEB_HOST_ARCH),$(BINUTILS_GOLD_ARCHS)))
# -fused-ld=gold enables the gold linker (but is not supported by all archs / distro)
	LDFLAGS_EXTRA += -fuse-ld=gold
endif
endif

# Enable polly (or not)
POLLY_ENABLE=no
ifeq ($(shell dpkg --compare-versions $(shell dpkg-query -W -f '$${Version}' libisl-dev) ge 0.12.1; echo $$?),0)
ifeq ($(shell dpkg --compare-versions $(shell dpkg-query -W -f '$${Version}' libcloog-isl-dev) ge 0.18.1; echo $$?),0)
POLLY_ENABLE=yes
endif
endif

ifneq (,$(filter codecoverage,$(DEB_BUILD_OPTIONS)))
# no-op on Ubuntu to avoid pulling lcov into main:
#	CODECOVERAGE=yes
# for -fvisibility-inlines-hidden see http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20130729/183016.html
#	CXXFLAGS_EXTRA += -fprofile-arcs -ftest-coverage
#	LDFLAGS_EXTRA += -coverage -lgcov
#	confargs += llvm_cv_cxx_visibility_inlines_hidden=no
endif

ifneq (,$(filter scan-build,$(DEB_BUILD_OPTIONS)))
# enable the build using scan-build
# The package are installed through the variable declarations:
# OTHERMIRROR="deb http://llvm.org/apt/unstable/ llvm-toolchain main"
# EXTRAPACKAGES="clang-3.4"
	PRE_PROCESS=scan-build
	PRE_PROCESS_CONF=scan-build
# no need to run tests in this case
	DEB_BUILD_OPTIONS+=" nocheck"
	CONFIGURE_EXTRA += --enable-assertions
endif

ifneq (,$(filter coverity,$(DEB_BUILD_OPTIONS)))
# enable the build using coverity
# pbuilder contains BINDMOUNTS="/opt/cov-analysis/"
# And we have some pbuilder hooks to configure and pack the result
# Where the binaries are installed on the jenkins instance
	PRE_PROCESS=PATH=$$PATH:/opt/cov-analysis/bin/ cov-build --dir cov-int
# We don't want to check the temporary files produced by the configure
	PRE_PROCESS_CONF=
	COVERITY_ENABLE=1
	CONFIGURE_EXTRA += --enable-assertions
# no need to run tests in this case
	DEB_BUILD_OPTIONS+=" nocheck"
else
	COVERITY_ENABLE=0
endif


LLDB_ENABLE=yes
LLDB_DISABLE_ARCHS := arm64 hurd-i386 mips mipsel ia64
# hurd has threading issues
# mips* fails with "undefined references to `__atomic_load_8'"
ifeq (,$(filter-out $(LLDB_DISABLE_ARCHS), $(DEB_HOST_ARCH)))
# Disable LLDB for this arch.
        LLDB_ENABLE=no
endif


%:
	dh $@ --with ocaml


debian/%: debian/%.in
	sed -e 's|@DEB_HOST_MULTIARCH@|$(DEB_HOST_MULTIARCH)|g' -e "s|@OCAML_STDLIB_DIR@|$(OCAML_STDLIB_DIR)|g" -e "s|@LLVM_VERSION@|$(LLVM_VERSION)|g" $< > $@

# Override this two targets. They are trying to manage the .in conversion for me
override_dh_ocamlinit:
override_dh_ocamlclean:

override_dh_auto_configure: debian/lldb-$(LLVM_VERSION).install debian/lldb-$(LLVM_VERSION).links debian/libllvm$(LLVM_VERSION).install debian/llvm-$(LLVM_VERSION)-dev.links debian/libllvm-$(LLVM_VERSION)-ocaml-dev.install debian/libllvm-$(LLVM_VERSION)-ocaml-dev.dirs debian/llvm-$(LLVM_VERSION)-runtime.binfmt debian/llvm-$(LLVM_VERSION)-runtime.prerm debian/libclang1-$(LLVM_VERSION).links debian/libclang-$(LLVM_VERSION)-dev.links debian/libclang1-$(LLVM_VERSION).install debian/pollycc
	echo "Using gcc: "
	$(CC) -v
	mkdir -p $(TARGET_BUILD)
	mkdir -p clang/include/clang/Debian
	sed -e "s|@DEB_PATCHSETVERSION@|$(DEBIAN_REVISION)|" \
		debian/debian_path.h > clang/include/clang/Debian/debian_path.h

# Remove some old symlinks
	cd tools/ && \
	if test -h clang; then \
		rm clang; \
	fi; \
    ln -s ../clang .; \
	readlink clang

	if test $(POLLY_ENABLE) = yes; then \
	cd tools/ && \
    if test -h polly; then \
        rm polly; \
    fi; \
    ln -s ../polly .; \
	fi

	if test $(LLDB_ENABLE) = yes; then \
	    cd tools/ && \
	    if test -h lldb; then \
		   rm lldb; \
	    fi; \
		ln -s ../lldb .; \
	fi

	cd projects/ && \
	if test -h compiler-rt; then \
		rm compiler-rt; \
	fi; \
	ln -s ../compiler-rt .; \
	readlink compiler-rt

	# Configure coverity (we need the compilers)
	-(if test $(COVERITY_ENABLE) -eq 1; then \
		export PATH=$$PATH:/opt/cov-analysis/bin/; \
		cov-configure --compiler clang --comptype gcc; \
		cov-configure --compiler gcc-4.8 --comptype gcc; \
		cov-configure --compiler g++-4.8 --comptype gcc; \
	fi)

	# Due to bug upstream, no symlink here
	cp -R -H clang-tools-extra tools/clang/tools/extra
# Start the actual configure
	cd $(TARGET_BUILD) && $(PRE_PROCESS_CONF) \
	  ../configure $(confargs) \
	--prefix=/usr/lib/llvm-$(LLVM_VERSION) \
	--bindir=\$${prefix}/bin/ \
	--disable-assertions \
	--enable-shared \
	--enable-optimized \
	--with-optimize-option=' $(opt_flags)' \
	--enable-pic \
	--enable-libffi \
	--with-ocaml-libdir=/usr/lib/ocaml/llvm-$(LLVM_VERSION) \
	--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
	--with-binutils-include=/usr/include \
	--with-cloog --with-isl \
	--with-bug-report-url=http://bugs.debian.org/ \
	--enable-shared \
	$(CONFIGURE_EXTRA) \
	CLANG_VENDOR=$(VENDOR) || { cat config.log tools/polly/config.log; exit 1; }
#	cd $(TARGET_BUILD) && cmake ../ -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_VERBOSE_MAKEFILE=ON
#	mkdir build-compiler-rt && cd build-compiler-rt && cmake ../compiler-rt


override_dh_auto_build:
	$(PRE_PROCESS) $(MAKE) $(NJOBS) -C $(TARGET_BUILD) VERBOSE=1 CLANG_VENDOR=$(VENDOR) CXXFLAGS="$(CXXFLAGS_EXTRA) -std=c++0x"  LDFLAGS="$(LDFLAGS_EXTRA)" REQUIRES_RTTI=1 DEBUGMAKE=1

# We run the build with the Asserts, new directory, make a symlink to make
# sure the rest still work
	cd $(TARGET_BUILD) \
	if test -d Release+Asserts -a ! -d Release; then \
		ln -s Release+Asserts Release; \
	fi


override_dh_prep: build_doc
	dh_prep


build_doc:
	cd $(CURDIR)/docs && make -f Makefile.sphinx && make -f Makefile.sphinx man
	cd $(CURDIR)/tools/clang/docs && make -f Makefile.sphinx

# Rename manpages
	d=$(CURDIR)/docs/_build/man/; \
	cd $$d; \
	for f in *.1; do \
		echo "$$f"|grep $(LLVM_VERSION) || mv $$f `echo $$f|sed "s|\.1|-$(LLVM_VERSION).1|"`; \
	done

# Remove the copy of jquery. See bug #701087
	for d in $(CURDIR)/clang/docs/_build/html/_static/ $(CURDIR)/docs/_build/html/_static/; do \
		cd $$d && rm -f jquery.js && ln -s /usr/share/javascript/jquery/jquery.js; \
		cd $$d && rm -f underscore.js && ln -s /usr/share/javascript/underscore/underscore.js; \
	done

	help2man --version-string=$(LLVM_VERSION) clang/tools/scan-view/scan-view > debian/man/scan-view.1
	help2man --version-string=$(LLVM_VERSION) clang/tools/clang-format/clang-format-diff.py > debian/man/clang-format-diff-$(LLVM_VERSION).1

	for f in clang clang-tblgen clang-check; do \
		help2man --version-string=$(LLVM_VERSION) $(TARGET_BUILD)/Release/bin/$$f > debian/man/$$f.1; \
	done

	for f in llvm-dwarfdump llvm-mc llvm-mcmarkup llvm-objdump llvm-rtdyld llvm-size llvm-tblgen macho-dump lldb clang-format clang-modernize; do \
		help2man --version-string=$(LLVM_VERSION) $(TARGET_BUILD)/Release/bin/$$f > debian/man/$$f-$(LLVM_VERSION).1; \
	done


override_dh_auto_install:
	$(MAKE) -C $(TARGET_BUILD) VERBOSE=1 install DESTDIR=$(CURDIR)/debian/tmp/

	chrpath -d $(TARGET_BUILD)/Release/bin/clang
	chrpath -d `find $(CURDIR)/debian/tmp/usr/lib/llvm-$(LLVM_VERSION)/bin/ -type f -executable`

	mv $(CURDIR)/debian/tmp/usr/lib/llvm-$(LLVM_VERSION)/lib/libLLVM-$(LLVM_VERSION)*.so $(CURDIR)/debian/tmp/usr/lib/llvm-$(LLVM_VERSION)/lib/libLLVM-$(LLVM_VERSION).so.$(SONAME_EXT)

	if test $(LLDB_ENABLE) = yes; then \
		mv $(CURDIR)/debian/tmp/usr/lib/llvm-$(LLVM_VERSION)/lib/liblldb.so $(CURDIR)/debian/tmp/usr/lib/llvm-$(LLVM_VERSION)/lib/liblldb.so.$(SONAME_EXT); \
	fi

	cd debian/tmp/usr/lib/llvm-$(LLVM_VERSION)/lib/ && \
	if test ! -f libclang-$(LLVM_VERSION).so.$(SONAME_EXT); then \
		mv libclang.so libclang-$(LLVM_VERSION).so.$(SONAME_EXT) && \
		ln -s libclang-$(LLVM_VERSION).so.$(SONAME_EXT) libclang.so.$(SONAME_EXT); \
	fi

#	cd $(TARGET_BUILD)/tools/clang/runtime/compiler-rt/ && rm -rf $$(find . -iname "SubDir.lib*" -o -iname .dir) && rm -rf $$(find -empty)

	mkdir -p $(CURDIR)/debian/clang-$(LLVM_VERSION)/usr/bin/
	cp compiler-rt/lib/asan/scripts/asan_symbolize.py $(CURDIR)/debian/clang-$(LLVM_VERSION)/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
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/llvm-$(LLVM_VERSION)/lib/clang/$(LLVM_VERSION)/lib/


# Rename binaries
	mkdir -p $(CURDIR)/debian/tmp/usr/bin/
	cd $(CURDIR)/debian/tmp/usr/bin/; rm -f *; \
	for f in ../lib/llvm-$(LLVM_VERSION)/bin/*; do \
		ln -s $$f `basename $$f`-$(LLVM_VERSION); \
		echo "Link $$f to `basename $$f`-$(LLVM_VERSION)"; \
	done

# Rename some stuff with the version name
	# WILL DO when we can install several clang versions together
	#mv $(CURDIR)/$(TARGET_BUILD)/tools/clang/docs/tools/clang.1 $(CURDIR)/$(TARGET_BUILD)/tools/clang/docs/tools/clang-$(LLVM_VERSION).1

	cp -f $(CURDIR)/utils/vim/llvm.vim $(CURDIR)/utils/vim/llvm-$(LLVM_VERSION).vim

	cp -f $(CURDIR)/utils/vim/tablegen.vim $(CURDIR)/utils/vim/tablegen-$(LLVM_VERSION).vim

	cp -f $(CURDIR)/clang/tools/clang-format/clang-format-diff.py $(CURDIR)/clang/tools/clang-format/clang-format-diff-$(LLVM_VERSION)

	cp -f $(CURDIR)/clang/tools/clang-format/clang-format.py clang/tools/clang-format/clang-format-$(LLVM_VERSION).py

# Remove some license files
	rm -f $(CURDIR)/debian/tmp/usr/lib/llvm-$(LLVM_VERSION)/include/llvm/Support/LICENSE.TXT \
	$(CURDIR)/debian/llvm-$(LLVM_VERSION)-dev/usr/lib/llvm-$(LLVM_VERSION)/build/autoconf/LICENSE.TXT

# Managed in lldb-X.Y.links.in
	rm -f $(CURDIR)/$(TARGET_BUILD)/Release/lib/python*/site-packages/lldb/_lldb.so


override_dh_installman:
	dh_installman
# Make sure that lli manpage is only in llvm-3.2-runtime (See #697117)
	rm -f $(CURDIR)/debian/llvm-$(LLVM_VERSION)/usr/share/man/man1/lli*


.PHONY: override_dh_strip
override_dh_strip:
	dh_strip -p libclang$(SONAME_EXT)-$(LLVM_VERSION) --dbg-package=libclang$(SONAME_EXT)-$(LLVM_VERSION)-dbg
	dh_strip -p libllvm$(LLVM_VERSION) --dbg-package=libllvm$(LLVM_VERSION)-dbg
	dh_strip -a


override_dh_shlibdeps:
	LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(CURDIR)/debian/tmp/usr/lib/llvm-$(LLVM_VERSION)/lib/ dh_shlibdeps


override_dh_installdeb:
# Managed by the package
	dh_installdeb -a

	rm -f $(CURDIR)/debian/lldb-$(LLVM_VERSION)/usr/lib/python*/dist-packages/lldb/__init__.pyc
	rm -f $(CURDIR)/debian/clang-$(LLVM_VERSION)-examples/usr/share/doc/clang-$(LLVM_VERSION)-examples/examples/*Make*
	rm -f $(CURDIR)//debian/llvm-$(LLVM_VERSION)-dev/usr/lib/llvm-$(LLVM_VERSION)/build/autoconf/LICENSE.TXT



ifeq (,$(filter nocheck, $(DEB_BUILD_OPTIONS)))
# List of the archs we know we have 100 % tests working
ARCH_LLVM_TEST_OK := amd64 i386
override_dh_auto_test:

# LLVM tests
ifneq (,$(findstring $(DEB_HOST_ARCH),$(ARCH_LLVM_TEST_OK)))
# Fail the build if the test fails
	LD_LIBRARY_PATH=$(CURDIR)/$(TARGET_BUILD)/Release/lib/ $(MAKE) -C $(TARGET_BUILD) check
else
	LD_LIBRARY_PATH=$(CURDIR)/$(TARGET_BUILD)/Release/lib/ $(MAKE) -C $(TARGET_BUILD) check || true
endif

# clang tests
# setup the env
	cd $(TARGET_BUILD)/Release/lib/ && ln -s libclang.so libclang.so.1
	cd $(TARGET_BUILD)/ && $(MAKE) -C tools/clang/ test || true
	rm -f $(TARGET_BUILD)/Release/lib/libclang.so.1

# lldb tests
ifeq (,$(filter $(DEB_HOST_ARCH), $(LLDB_DISABLE_ARCHS) armhf))
	if test $(LLDB_ENABLE) = yes; then \
	cd $(TARGET_BUILD)/Release/lib/python2.7/site-packages/lldb && if test ! -h _lldb.so -o ! -f _lldb.so; then ln -s ../../../liblldb.so _lldb.so; fi; \
	if test ! -h libLLVM-$(LLVM_VERSION).so.$(SONAME_EXT) -o ! -f _lldb.so; then ln -s ../../../libLLVM-$(LLVM_VERSION).so libLLVM-$(LLVM_VERSION).so.$(SONAME_EXT); fi; \
	fi
	cd $(TARGET_BUILD)/ && $(MAKE) -C tools/lldb/test || true
	if test -d $(TARGET_BUILD)/tools/lldb/test/lldb-test-traces/; then \
		cat $(TARGET_BUILD)/tools/lldb/test/lldb-test-traces/*; \
	fi
endif

# Managed by debian build system
	rm -f $(CURDIR)/$(TARGET_BUILD)/Release/lib/python*/site-packages/lldb/_lldb.so
# polly tests
	cd $(TARGET_BUILD)/ && $(MAKE) -C tools/polly/test/ check-polly || true

# The compression of the code coverage report is done in the
# hook B21GetCoverageResults on the server
	if test "$(CODECOVERAGE)" = "yes"; then \
		REPORT=reports/llvm-toolchain.info; \
		mkdir -p reports/; \
		lcov --directory $(TARGET_BUILD)/ --capture --ignore-errors source --output-file $$REPORT; \
		lcov --remove $$REPORT "/usr*" -o $$REPORT; \
		genhtml -o reports/coverage --show-details --highlight --legend $$REPORT; \
	fi
else
override_dh_auto_test:
endif


override_dh_gencontrol:
	dh_gencontrol -- $(control_vars)


override_dh_auto_clean:
	rm -rf $(TARGET_BUILD) tools/clang/include/clang/Debian/debian_path.h docs/_build/ clang/docs/_build tools/clang/docs/_html/
# QA tools
	rm -rf cov-int/ reports/
	rm -f `ls debian/*.in|sed -e "s|.in$$||g"`
	find utils -name '*.pyc' | xargs -r rm -f
	# Use -I because a test has a space in its name
	find lldb/test -iname '*.pyc' | xargs -I{} -r rm -f {}
	find test -name '*.pyc' -o -name '*.cm[ix]' | xargs -r rm -f
	find test/Bindings -name '*.o' | xargs -r rm -f
	rm -f tools/clang tools/polly tools/lldb projects/compiler-rt
	rm -rf tools/clang/tools/extra clang/tools/extra/
	rm -f $(CURDIR)/utils/vim/llvm-$(LLVM_VERSION).vim $(CURDIR)/utils/vim/tablegen-$(LLVM_VERSION).vim
	rm -f $(CURDIR)/clang/tools/clang-format/clang-format-diff-$(LLVM_VERSION)
	rm -f $(CURDIR)/clang/tools/clang-format/clang-format-$(LLVM_VERSION).py
