#!/usr/bin/make -f

TARGET_BUILD	:= build-llvm
DEB_INST		:= $(CURDIR)/debian/tmp/
#GCC_VERSION     := 4.8
# The 5|6| in the regexp is a crappy workaround. g++ 5.2 in Debian is not providing a g++-5.2 binary (only g++-5)
# accomodate that by hardcoding the 5 detection
GCC_VERSION     := $(shell dpkg-query -W -f '$${Version}' g++ | sed -rne 's,^([0-9]+:)?(5|6|[0-9]+\.[0-9]+|[0-9]+).*$$,\2,p')
LLVM_VERSION	:= 3.9
LLVM_VERSION_FULL := $(LLVM_VERSION).0
SONAME_EXT      := 1
# Manage the case when the version is 1:3.5~svn213052-1~exp1 or 1:3.4.2-1
DEBIAN_REVISION := $(shell dpkg-parsechangelog |  sed -rne "s,^Version: 1:([0-9.]+)(~|-)(.*),\3,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)

LDFLAGS_EXTRA =
CXXFLAGS_EXTRA = -std=c++0x -gsplit-dwarf
CONFIGURE_EXTRA =
CMAKE_EXTRA =

ifneq (,$(filter $(DEB_HOST_ARCH),powerpc powerpcspe))
LDFLAGS_EXTRA += -latomic
endif

ifeq ($(shell dpkg --compare-versions $(shell dpkg-query -W -f '$${Version}' g++-$(GCC_VERSION)) lt 4.7.2-10~ ; echo $$?),0)
# Too old version of gcc. Force 4.9
	GCC_VERSION := 4.9
endif

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

opt_flags = -g -O2
ifneq (,$(findstring $(DEB_HOST_ARCH),armel))
  opt_flags += -marm
  # 3.8 fails to build, disable the compiler_rt builtins
  # See http://lists.llvm.org/pipermail/llvm-dev/2016-May/099761.html
  CMAKE_EXTRA += -DCOMPILER_RT_BUILD_BUILTINS=OFF
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 powerpcspe ppc64 ppc64el sparc sparc64 x32
ifeq ($(shell dpkg --compare-versions $(shell dpkg-query -W -f '$${Version}' binutils) ge 2.23.1-1~exp3 ; echo $$?),0)
ifneq (,$(filter $(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
	CXXFLAGS_EXTRA += -Wl,-fuse-ld=gold
	CMAKE_EXTRA += -DLLVM_BINUTILS_INCDIR=/usr/include/
endif
endif

# Enable polly (or not)
POLLY_ENABLE=yes
ifeq (,$(filter-out $(DEB_HOST_ARCH), powerpc s390x))
  POLLY_ENABLE=no
endif

ifneq (,$(filter codecoverage,$(DEB_BUILD_OPTIONS)))
# enable the code coverage
	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
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.5"
	PRE_PROCESS=scan-build-$(LLVM_VERSION) -analyzer-config stable-report-filename=true -enable-checker optin.performance.Padding
	PRE_PROCESS_CONF=scan-build-$(LLVM_VERSION)
# no need to run tests in this case
	DEB_BUILD_OPTIONS+=" nocheck"
	CONFIGURE_EXTRA += --enable-assertions
	CMAKE_EXTRA += -DLLVM_ENABLE_ASSERTIONS=ON
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
	CMAKE_EXTRA += -DLLVM_ENABLE_ASSERTIONS=ON
# 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 ia64 ppc64el powerpc s390x sparc64 mips64el ppc64
# hurd has threading issues
ifeq (,$(filter-out $(LLDB_DISABLE_ARCHS), $(DEB_HOST_ARCH)))
# Disable LLDB for this arch.
		LLDB_ENABLE=no
endif

LLD_ENABLE=NO

%:
	dh $@ --with ocaml



preconfigure:
	for f in debian/*.in; do \
		f2=$$(echo $$f | sed 's/\.in$$//;s/X\.Y/$(LLVM_VERSION)/'); \
		echo "$$f => $$f2"; \
		sed -e 's|@DEB_HOST_MULTIARCH@|$(DEB_HOST_MULTIARCH)|g' \
			-e "s|@OCAML_STDLIB_DIR@|$(OCAML_STDLIB_DIR)|g" \
			-e "s|@LLVM_VERSION_FULL@|$(LLVM_VERSION_FULL)|g" \
			-e "s|@LLVM_VERSION@|$(LLVM_VERSION)|g" $$f > $$f2; \
	done

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

override_dh_auto_configure: preconfigure
	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 "$(LLD_ENABLE)" = yes; then \
	cd tools/ && \
		if test -h lld; then \
			rm lld; \
		fi; \
		ln -s ../lld .; \
	readlink lld; \
	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 clang; \
		cov-configure --compiler gcc-4.8 --comptype gcc; \
		cov-configure --compiler g++-4.8 --comptype gcc; \
		cov-configure --compiler gcc-4.9 --comptype gcc; \
		cov-configure --compiler g++-4.9 --comptype gcc; \
		cov-configure --compiler gcc-5 --comptype gcc; \
		cov-configure --compiler g++-5 --comptype gcc; \
	fi)

	# Due to bug upstream, no symlink here
	rm -fr tools/clang/tools/extra
	cp -R -H clang-tools-extra tools/clang/tools/extra

	# if cmake is installed in /tmp/cmake/ uses it
	# Used to build llvm on old ubuntu (precise) on the llvm.org/apt/ ci
	CMAKE_BIN=cmake; \
	if test -f /tmp/cmake/bin/cmake; then \
		CMAKE_BIN=/tmp/cmake/bin/cmake; \
	fi; \
	echo "Using cmake: $$CMAKE_BIN"; \
	cd $(TARGET_BUILD) && \
	$(PRE_PROCESS_CONF) $$CMAKE_BIN ../ \
	-DCMAKE_INSTALL_PREFIX=/usr/lib/llvm-$(LLVM_VERSION) \
	-DCMAKE_VERBOSE_MAKEFILE=ON \
	-DCMAKE_BUILD_TYPE=RelWithDebInfo \
	-DCMAKE_CXX_FLAGS='$(CXXFLAGS_EXTRA)' \
	-DLLVM_LINK_LLVM_DYLIB=ON \
	-DLLVM_INSTALL_UTILS=ON \
	-DLLVM_VERSION_SUFFIX= \
	-DLLVM_ENABLE_SPHINX=ON \
	-DSPHINX_WARNINGS_AS_ERRORS=OFF \
	-DLLVM_BUILD_LLVM_DYLIB=ON \
	-DLLVM_ENABLE_RTTI=ON \
	-DLLVM_ENABLE_FFI=ON \
	$(CMAKE_EXTRA) \
	-DLIBCLANG_LIBRARY_VERSION=$(SONAME_EXT)

override_dh_auto_build:
	$(PRE_PROCESS) $(MAKE) $(NJOBS) -C $(TARGET_BUILD) VERBOSE=1 CLANG_VENDOR=$(VENDOR) CXXFLAGS="$(CXXFLAGS_EXTRA)"  LDFLAGS="$(LDFLAGS_EXTRA)" REQUIRES_RTTI=1 DEBUGMAKE=1
	cd $(TARGET_BUILD) \
	CFLAGS=`dpkg-buildflags --get CFLAGS`; \
	CFLAGS="$$CFLAGS `dpkg-buildflags --get CPPFLAGS`"; \
	echo $$CFLAGS; \
	bin/clang++ -c $$CFLAGS -std=c++11 ../lib/Fuzzer/*.cpp -IFuzzer; \
	ar ruv libFuzzer.a Fuzzer*.o

override_dh_prep: build_doc
	dh_prep

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

# Continue if failing, Ubuntu precise cannot generate manpages as sphinx is too old
	-(cd $(TARGET_BUILD) && make $(NJOBS) docs-llvm-html docs-clang-html docs-clang-tools-html docs-polly-html docs-polly-man docs-clang-tools-man docs-clang-man docs-llvm-man)

# Rename manpages
	d=$(CURDIR)/docs/_build/man/; \
	if test -d $$d; then \
		cd $$d; \
		for f in *.1; do \
			echo "$$f"|grep $(LLVM_VERSION) || mv $$f `echo $$f|sed "s|\.1|-$(LLVM_VERSION).1|"`; \
		done; \
	else \
		echo "could not find $$d"; \
	fi
# the clang doc generation only generates clang manpage
# When it will do more, we should move that in the loop above
	cd $(CURDIR)/clang/docs/_build/man/; mv clang.1 clang-$(LLVM_VERSION).1

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

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

	CMDS="llvm-dwarfdump llvm-mc llvm-mcmarkup llvm-objdump llvm-rtdyld llvm-size llvm-ranlib lldb lldb-mi clang-format clang clang-tblgen clang-check clang-tidy clang-apply-replacements clang-rename clang-query pp-trace sancov lli modularize clang-include-fixer find-all-symbols"; \
	for f in $$CMDS; do \
		echo "Generating manpage of $$f"; \
		LD_LIBRARY_PATH=$(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/:/usr/lib/*/libfakeroot help2man --version-string=$(LLVM_VERSION) $(TARGET_BUILD)/bin/$$f > debian/man/$$f-$(LLVM_VERSION).1; \
	done

override_dh_auto_install:
	# Clean up temporary files to make sure the install works
	rm -rf $(find $(TARGET_BUILD) -wholename '*CMakeFiles*' -not -name CMakeLists.txt -a -name "*.dir" -type d)
	# install/fast enables a make install without recompiling temporary files
	$(MAKE) -C $(TARGET_BUILD) VERBOSE=1 install/fast DESTDIR=$(DEB_INST)/
	# Not used on Linux.
	rm -f $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/bin/argdumper
	rm -f $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/share/clang/clang-format-bbedit.applescript

	cp $(TARGET_BUILD)/bin/clang-query $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/bin

	# Only run on executable, not script
	chrpath -d `find $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/bin/ -type f -executable -exec file -i '{}' \; | grep 'x-executable; charset=binary'|cut -d: -f1`

	cd debian/tmp/usr/lib/llvm-$(LLVM_VERSION)/lib/ && rm -f libclang.so.$(SONAME_EXT) libclang-$(LLVM_VERSION).so; \
	ln -s libclang-$(LLVM_VERSION).so.$(SONAME_EXT) libclang.so.$(SONAME_EXT)

# Remove artefact (where compiler-rt is built)
#	if test -d $(TARGET_BUILD)/tools/clang/runtime/compiler-rt/clang_linux; then \
#	cd $(TARGET_BUILD)/tools/clang/runtime/compiler-rt/clang_linux && rm -rf $$(find . -mindepth 2 -maxdepth 2 -type d) && rm -rf $$(find -empty) && rm -rf */.dir; \
#	fi

	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-$(LLVM_VERSION)

	mkdir -p $(CURDIR)/debian/libfuzzer-$(LLVM_VERSION)-dev/usr/lib/llvm-$(LLVM_VERSION)/lib/
	cp -v $(TARGET_BUILD)/libFuzzer.a $(CURDIR)/debian/libfuzzer-$(LLVM_VERSION)-dev/usr/lib/llvm-$(LLVM_VERSION)/lib/

# Create this fake directory to make the install libclang-common-dev happy
# under the unsupported archs of compiler-rt
	mkdir -p $(DEB_INST)/usr/lib/clang/$(LLVM_VERSION)/lib
	mkdir -p $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/clang/$(LLVM_VERSION_FULL)/lib/
	mkdir -p $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/clang/$(LLVM_VERSION)/lib/clang_linux/
	mkdir -p $(TARGET_BUILD)/tools/clang/runtime/compiler-rt/clang_linux/
	mkdir -p $(TARGET_BUILD)/tools/clang/runtime/compiler-rt/clang_linux/
# On some archs, the sanatizers are not built. As we explicitly includes some txt files, create
# a fake txt to make sure it doesn't fail
	echo "The *.txt files, if available, contain helper to override some of the errors messages." > $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/clang/$(LLVM_VERSION_FULL)/README.txt
	echo "Please visit https://github.com/google/sanitizers/wiki/AddressSanitizer for help" >> $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/clang/$(LLVM_VERSION_FULL)/README.txt

# idem for the lldb python binding
	mkdir -p $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/python2.7/site-packages/lldb/

# Remove things that CMake  install but which aren't packaged yet,
# or are packaged from the source or build tree.
	mv $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/bin/clang-$(LLVM_VERSION) \
	   $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/bin/clang

# Don't think it is used
	rm -f $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/libPolly*a
# Probably useless
	rm -f $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/python2.7/site-packages/six.py

# Rename binaries
	mkdir -p $(DEB_INST)/usr/bin/
	cd $(DEB_INST)/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
	cp $(CURDIR)/clang/tools/scan-build/man/scan-build.1 $(CURDIR)/clang/tools/scan-build/man/scan-build-$(LLVM_VERSION).1

	# copy the vim files (except that tablegen does not exist for indent
	VIM_DIRS="ftdetect ftplugin syntax indent"; \
	for dir in $$VIM_DIRS; do \
		cp -f $(CURDIR)/utils/vim/$$dir/llvm.vim $(CURDIR)/utils/vim/$$dir/llvm-$(LLVM_VERSION).vim; \
		if test -f $(CURDIR)/utils/vim/$$dir/tablegen.vim; then \
			cp -f $(CURDIR)/utils/vim/$$dir/tablegen.vim $(CURDIR)/utils/vim/$$dir/tablegen-$(LLVM_VERSION).vim; \
		fi; \
	done
	cp -f $(CURDIR)/utils/vim/vimrc $(CURDIR)/utils/vim/llvm-$(LLVM_VERSION)-vimrc

	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

	rm -rf clang/tools/scan-build-$(LLVM_VERSION)
	cp -fR $(CURDIR)/clang/tools/scan-build clang/tools/scan-build-$(LLVM_VERSION)

	rm -rf clang/tools/scan-build-py-$(LLVM_VERSION)
	cp -fR $(CURDIR)/clang/tools/scan-build-py clang/tools/scan-build-py-$(LLVM_VERSION)

	rm -rf clang/tools/scan-view-$(LLVM_VERSION)
	cp -fR $(CURDIR)/clang/tools/scan-view clang/tools/scan-view-$(LLVM_VERSION)

# Remove some license files
	rm -f $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/include/llvm/Support/LICENSE.TXT

# Change CMake module dir
	sed -i 's|LLVM_CMAKE_DIR "/usr/lib/llvm-$(LLVM_VERSION)/share/llvm/cmake"|LLVM_CMAKE_DIR "/usr/share/llvm-$(LLVM_VERSION)/cmake"|' $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/cmake/llvm/LLVMConfig.cmake

# Disable CMake's package validation checks for target files that we may remove.
	sed -i '/_IMPORT_CHECK_TARGETS \(Polly\|sancov\)/ {s|^|#|}' $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/cmake/llvm/LLVMExports-*.cmake

# Managed in python-lldb-X.Y.links.in
	rm -f $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/python*/site-packages/lldb/_lldb.so

# Manage the polly files. Sometimes, we build them. Sometimes not.
	if test "$(POLLY_ENABLE)" = yes; then \
		mkdir -p $(CURDIR)/debian/libclang-$(LLVM_VERSION)-dev/usr/lib/llvm-$(LLVM_VERSION)/lib/ $(CURDIR)/debian/libclang-common-$(LLVM_VERSION)-dev/usr/lib/llvm-$(LLVM_VERSION)/include/polly/; \
		mv -f $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/libpolly* \
		$(CURDIR)/debian/libclang-$(LLVM_VERSION)-dev/usr/lib/llvm-$(LLVM_VERSION)/lib/; \
		rm -rf $(CURDIR)/debian/libclang-common-$(LLVM_VERSION)-dev/usr/lib/llvm-$(LLVM_VERSION)/include/polly; \
		mv -f $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/include/polly/ \
		$(CURDIR)/debian/libclang-common-$(LLVM_VERSION)-dev/usr/lib/llvm-$(LLVM_VERSION)/include/; \
	fi

	mkdir -p $(CURDIR)/debian/usr/share/doc/llvm-$(LLVM_VERSION)-doc/ $(CURDIR)/debian/usr/share/doc/clang-$(LLVM_VERSION)-doc/
	cp -R $(TARGET_BUILD)/docs/html $(CURDIR)/debian/usr/share/doc/llvm-$(LLVM_VERSION)-doc/
	cp -R $(TARGET_BUILD)/tools/clang/docs/html $(CURDIR)/debian/usr/share/doc/clang-$(LLVM_VERSION)-doc/
	cp -R $(TARGET_BUILD)/tools/clang/tools/extra/docs/html $(CURDIR)/debian/usr/share/doc/clang-$(LLVM_VERSION)-doc/clang-extra

# Delete the target build directory to save some space on the build systems
# All the files have been installed in $(CURDIR)/debian/tmp/ already
	rm -rf $(TARGET_BUILD)


override_dh_shlibdeps:
	LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/ dh_shlibdeps

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*


override_dh_strip:
	: # running out of diskspace on the buildds
	find $(TARGET_BUILD) -name '*.o' -o -name '*.a' | xargs -r rm -f
	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 -p liblldb-$(LLVM_VERSION) --dbg-package=liblldb-$(LLVM_VERSION)-dbg
ifeq (${LLD_ENABLE},yes)
	dh_strip -p liblld-$(LLVM_VERSION) --dbg-package=liblld-$(LLVM_VERSION)-dbg
endif
	dh_strip -a


override_dh_install:
#	cp $(TARGET_BUILD)/lib/libLLVM-$(LLVM_VERSION).so $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/libLLVM-$(LLVM_VERSION).so.$(SONAME_EXT)
	dh_install --fail-missing

override_dh_installdeb:
# Managed by the package
	dh_installdeb -a

	rm -f $(CURDIR)/debian/tmp/usr/lib/llvm-$(LLVM_VERSION)/lib/python*/site-packages/lldb/__init__.pyc $(CURDIR)/debian/python-lldb-$(LLVM_VERSION)/usr/lib/llvm-$(LLVM_VERSION)/lib/python*/site-packages/lldb/__init__.pyc
	rm -f $(CURDIR)/debian/clang-$(LLVM_VERSION)-examples/usr/share/doc/clang-$(LLVM_VERSION)-examples/examples/*Make*

# Remove auto generated python pyc
	find $(CURDIR)/debian/llvm-$(LLVM_VERSION)-tools/usr/lib/llvm-$(LLVM_VERSION)/ -name '*.pyc' | xargs -r rm -f


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

override_dh_auto_test:

# LLVM tests (CMake)
ifneq (,$(findstring $(DEB_HOST_ARCH),$(ARCH_LLVM_TEST_OK)))
	$(MAKE) $(NJOBS) -C $(TARGET_BUILD) check-llvm
else
	$(MAKE) $(NJOBS) -C $(TARGET_BUILD) check-llvm || true
endif

# Clang tests (CMake)
	$(MAKE) $(NJOBS) -C $(TARGET_BUILD) check-clang || true

# LLD tests (CMake)
ifeq (${LLD_ENABLE},yes)
	$(MAKE) $(NJOBS) -C $(TARGET_BUILD) check-lld || true
endif

# LLDB tests (CMake)
ifeq (,$(filter $(DEB_HOST_ARCH), $(LLDB_DISABLE_ARCHS) armhf armel))
ifneq (,$(filter codecoverageDISABLED,$(DEB_BUILD_OPTIONS)))
	LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(CURDIR)/$(TARGET_BUILD)/lib/ $(MAKE) $(NJOBS) -C $(TARGET_BUILD) check-lldb || true
endif
endif

# Polly tests (CMake)
ifeq (${POLLY_ENABLE},yes)
	$(MAKE) $(NJOBS) -C $(TARGET_BUILD) check-polly || true
endif

# Managed by debian build system
	rm -f $(CURDIR)/$(TARGET_BUILD)/lib/python*/site-packages/lldb/_lldb.so

# polly tests
	if test "$(POLLY_ENABLE)" = yes; then \
		cd $(TARGET_BUILD)/ && LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/lib/ $(MAKE) -C tools/polly/test/ check-polly || true; \
    fi

# 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
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/lld 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


.PHONY: override_dh_strip preconfigure
