#!/usr/bin/make -f

include /usr/share/dpkg/default.mk
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

# Only run the tests on amd64 and i386 for now
ifeq (,$(filter $(DEB_HOST_ARCH),amd64 i386))
export DEB_BUILD_OPTIONS += nocheck
endif

# Ensure pcre_h.jl and errno_h.jl are sorted reproducibly
export LC_ALL=C.UTF-8
# HOME is needed to avoid "mkdir(): Permission denied" while building docs.
# Some tests also need a HOME directory.
export HOME=/tmp
# TODO: document the usage of custom build against MKL.
CUSTOM_MKL ?= 0

LLVM_VER = 3.9

# NOTES:
# 1. Some of these flags come from upstream's Make.inc .
# 2. To enable USE_BLAS64=0 (ILP64), we need to set INTERFACE64=1 for OpenBLAS.
# 3. Julia is tightly coupled with a specific libuv1 version. we cannot use on provided in archive.
COMMON_FLAGS = \
	prefix=/usr \
	sysconfdir=/etc \
	DESTDIR=debian/tmp/ \
	LLVM_CONFIG=/usr/bin/llvm-config-$(LLVM_VER) \
	LLVM_VER=$(LLVM_VER) \
	MULTIARCH=$(DEB_HOST_MULTIARCH) \
	MULTIARCH_INSTALL=1 \
	NO_GIT=1 \
	TAGGED_RELEASE_BANNER='$(DEB_VENDOR) version $(DEB_VERSION)' \
	USE_BLAS64=0  \
	USE_LLVM_SHLIB=1 \
	USE_SYSTEM_ARPACK=0 \
	USE_SYSTEM_BLAS=1 \
	USE_SYSTEM_CURL=1 \
	USE_SYSTEM_DSFMT=1 \
	USE_SYSTEM_FFTW=1 \
	USE_SYSTEM_GMP=1 \
	USE_SYSTEM_LAPACK=1 \
	USE_SYSTEM_LIBGIT2=1 \
	USE_SYSTEM_LIBSSH2=1 \
	USE_SYSTEM_LIBUNWIND=1 \
	USE_SYSTEM_LIBUV=0 \
	USE_SYSTEM_LLVM=1 \
	USE_SYSTEM_MBEDTLS=1 \
	USE_SYSTEM_MPFR=1 \
	USE_SYSTEM_OPENSPECFUN=1 \
	USE_SYSTEM_PATCHELF=1 \
	USE_SYSTEM_PCRE=1 \
	USE_SYSTEM_SUITESPARSE=1 \
	USE_SYSTEM_UTF8PROC=1 \
	VERBOSE=1

# Use libopenlibm on architectures that have it
ifneq (,$(filter $(DEB_HOST_ARCH),amd64 kfreebsd-amd64 x32))
COMMON_FLAGS += MARCH=x86-64 USE_SYSTEM_OPENLIBM=1 USE_SYSTEM_LIBM=0
else ifneq (,$(filter $(DEB_HOST_ARCH),i386 hurd-i386 kfreebsd-i386))
COMMON_FLAGS += MARCH=pentium4 USE_SYSTEM_OPENLIBM=1 USE_SYSTEM_LIBM=0
else ifneq (,$(filter $(DEB_HOST_ARCH),arm64 armhf mips mips64el mipsel powerpc ppc64 ppc64el))
COMMON_FLAGS += USE_SYSTEM_OPENLIBM=1 USE_SYSTEM_LIBM=0
else
# Use libm elsewhere
COMMON_FLAGS += USE_SYSTEM_OPENLIBM=0 USE_SYSTEM_LIBM=1
endif

# Use libopenblas on architectures that have it
ifneq (,$(filter $(DEB_HOST_ARCH),amd64 arm64 armhf i386 mips64el powerpc ppc64 ppc64el))
COMMON_FLAGS += LIBBLAS=-lopenblas LIBBLASNAME=libopenblas \
                LIBLAPACK=-lopenblas LIBLAPACKNAME=libopenblas
else
# Use libblas and liblapack elsewhere
COMMON_FLAGS += LIBBLAS=-lblas LIBBLASNAME=libblas \
                LIBLAPACK=-llapack LIBLAPACKNAME=liblapack
endif

# Set number of parallel workers for tests
ifneq (,$(filter parallel=1,$(DEB_BUILD_OPTIONS)))
TESTS_ENV += JULIA_CPU_CORES=2
else ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
TESTS_ENV += JULIA_CPU_CORES=$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
TESTS_ENV += JULIA_CPU_CORES=2
endif
# Restart workers exceeding maximum resident memory size (requires JULIA_CPU_CORES >= 2)
TESTS_ENV += JULIA_TEST_MAXRSS_MB=500

ifeq (1,$(CUSTOM_MKL))
COMMON_FLAGS += USE_INTEL_MKL=1 USE_BLAS64=1 \
                LIBBLAS=-lmkl_rt LIBBLASNAME=libmkl_rt \
                LIBLAPACK=-lmkl_rt LIBLAPACKNAME=libmkl_rt \
				MKLROOT=/usr MKLLIB=/usr/lib/$(DEB_HOST_MULTIARCH)
endif


%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure
	# patch embedded source to prevent privacy-breach-generic
	patch -p1 < debian/embedded/privacy-breach.patch

override_dh_auto_build:
	dh_auto_build -- $(COMMON_FLAGS)

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	env $(TESTS_ENV) make -C test $(COMMON_FLAGS)
endif

override_dh_auto_clean:
	make $(COMMON_FLAGS) distcleanall
	make -f debian/shlibdeps.mk $(COMMON_FLAGS) clean

override_dh_auto_install:
	make $(COMMON_FLAGS) install
	rm -rf usr # Otherwise dh_install does not see debian/tmp/usr
	find debian -type f -name '*.so.debug' -delete

override_dh_link-arch:
	# Create *.so symlinks for dlopen'd libraries in private libdir.
	make -f debian/shlibdeps.mk $(COMMON_FLAGS)
	dh_link

override_dh_shlibdeps:
	# Generate dependencies for dlopen'd libraries using dummy executable.
	# Suppress useless dependency warnings caused by unused library symbols.
	dh_shlibdeps -- --warnings=1 debian/shlibdeps

override_dh_compress:
	dh_compress --exclude=examples/

override_dh_install-indep:
	dh_install --exclude=build_h.jl --exclude=build.h

# Don't strip sys.so and libjulia.so.* as suggested upstream.
# https://github.com/JuliaLang/julia/issues/23115#issuecomment-320715030
# 1. stripping sys.so would cause "backtrace" test failure.
# 2. not stripping libjulia.so* would improve the results of the backtrace.
override_dh_strip:
	dh_strip  -X"sys.so" -X"libjulia.so.$(shell dpkg-parsechangelog -SVersion | cut -d- -f1)"

override_dh_makeshlibs:
	dh_makeshlibs --no-scripts -Xlibarpack.so

# FIXME: Remove get-orig-source target when we are about to import Julia 0.6.4 or Julia 0.7.0
# If we need any embedded source, put them to debian/embedded/ .
# Reasons:
# 1. <policy> deprecate get-orig-source target.
# 2. then we can simply download source with Uscan.
# 3. shipping embedded code in debian/embedded/ is more flexible than in origtargz.
get-orig-source:
	rm -rf julia-$(DEB_VERSION_UPSTREAM)
	git clone https://github.com/JuliaLang/julia.git julia-$(DEB_VERSION_UPSTREAM)
	# Checkout right commit and generate base/version_git.jl
	cd julia-$(DEB_VERSION_UPSTREAM) && \
		git checkout "v`echo $(DEB_VERSION_UPSTREAM) | sed 's/\+dfsg\d*//' | sed 's/~/-/'`" -- && \
		make -C base version_git.jl.phony
	# Manually add an embedded copy of libuv
	git clone https://github.com/JuliaLang/libuv julia-$(DEB_VERSION_UPSTREAM)/deps/libuv
	cd julia-$(DEB_VERSION_UPSTREAM)/deps/libuv && \
		git checkout "`sed -n '/LIBUV_SHA1/s/.*\(\<[0-9a-f]\{40\}\>\).*/\1/p' ../libuv.version`" --
	# Pack reproducible orig tarball
	find julia-$(DEB_VERSION_UPSTREAM) -name '.git*' -prune -or -type f -print0 | LC_ALL=C sort -z | \
		tar -caf julia_$(DEB_VERSION_UPSTREAM).orig.tar.xz \
		--format=ustar --null --files-from=- \
		--owner=root --group=root --mode='a=rX,u+w' \
		--mtime=@`cd julia-$(DEB_VERSION_UPSTREAM) && git log -n1 --format=%ct`
	rm -rf julia-$(DEB_VERSION_UPSTREAM)
