# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Shiz <hi@shiz.me>
# Maintainer: Shiz <hi@shiz.me>
pkgname=julia
pkgver=0.6.0
# Keep in sync with deps/libuv.version.
_libuv_ver=52d72a52cc7ccd570929990f010ed16e2ec604c8
_llvm_ver=3.9
pkgrel=0
pkgdesc="A high-level, high-performance dynamic language for technical computing"
url="http://julialang.org"
# x86: no libunwind package
arch="x86_64"
license="MIT"
depends="arpack
	dsfmt
	fftw>=3.3
	gmp>=5.0
	libgit2>=0.21
	mpfr3
	openblas
	openlibm
	openspecfun>=0.4
	pcre2>=10.0
	suitesparse>=4.1
	"
depends_dev="arpack-dev
	curl-dev
	dsfmt-dev
	fftw-dev
	gmp-dev
	libgit2-dev
	libunwind-dev
	llvm$_llvm_ver-dev
	mpfr-dev
	openblas-dev
	openlibm-dev
	openspecfun-dev
	pcre2-dev
	suitesparse-dev
	utf8proc-dev>=2
	"
makedepends="$depends_dev
	bash
	cmake
	gfortran
	linux-headers
	libressl-dev
	paxmark
	perl
	"
subpackages="$pkgname-dev $pkgname-doc $pkgname-dbg"
source="https://github.com/JuliaLang/julia/releases/download/v$pkgver/$pkgname-$pkgver.tar.gz
	libuv-$_libuv_ver.tar.gz::https://api.github.com/repos/JuliaLang/libuv/tarball/$_libuv_ver
	http://www.unicode.org/Public/9.0.0/ucd/UnicodeData.txt
	find-syslibs
	0001-hardened.patch
	0002-verbose-build.patch
	0003-no-clean-docs.patch
	0005-fix-rpath-issues.patch
	0008-ldconfig-compat.patch
	fix-or-disable-broken-tests.patch
	test-disable-tests-using-fake_pty.patch
	test-libgit2-skip-SSL_CERT_FILE.patch
	test-repl-disable-22176-20482.patch
	"
builddir="$srcdir/$pkgname-$pkgver"
ldpath="/usr/lib/julia"

prepare() {
	cd "$builddir"

	# Julia needs patched libuv.
	mkdir deps/srccache
	cp "$srcdir"/libuv-$_libuv_ver.tar.gz deps/srccache

	# Prevent fetching of unicode data.
	cp "$srcdir"/UnicodeData.txt doc/UnicodeData.txt

	# Prevent fetching of bundled stuff in the build and package phase.
	cat > deps/tools/jldownload <<-'EOF'
		#!/bin/sh
		echo "!!! Downloading of $2 to $1 disabled !!!"
		echo "Abuild should not fetch any files in the build phase."
		echo "Add all the needed files to the APKBUILD's source=."
		exit 1
	EOF

	cat > Make.user <<-EOF
		prefix=/usr
		libexecdir=/usr/lib
		sysconfdir=/etc
		DESTDIR="$pkgdir"
		LIBBLAS=-lopenblas
		LIBBLASNAME=libopenblas
		LIBLAPACK=-lopenblas
		LIBLAPACKNAME=libopenblas
		LLVM_CONFIG=/usr/lib/llvm$_llvm_ver/bin/llvm-config
		LLVM_VER=$_llvm_ver
		USE_SYSTEM_LLVM=1
		USE_SYSTEM_LIBUNWIND=1
		USE_SYSTEM_PCRE=1
		USE_SYSTEM_LIBM=1
		USE_SYSTEM_OPENLIBM=1
		USE_SYSTEM_OPENSPECFUN=1
		USE_SYSTEM_DSFMT=1
		USE_SYSTEM_BLAS=1
		USE_SYSTEM_LAPACK=1
		USE_SYSTEM_FFTW=1
		USE_SYSTEM_GMP=1
		USE_SYSTEM_MPFR=1
		USE_SYSTEM_ARPACK=1
		USE_SYSTEM_SUITESPARSE=1
		USE_SYSTEM_LIBUV=0
		USE_SYSTEM_UTF8PROC=1
		USE_SYSTEM_CURL=1
		USE_SYSTEM_LIBGIT2=1
		USE_SYSTEM_PATCHELF=1
		USE_LLVM_SHLIB=1
		VERBOSE=1
	EOF

	# Pre-SSE2 CPU targets are not supported;
	# create a generic 32-bit x86 binary.
	if [ "$CARCH" = x86 ]; then
		echo "MARCH=i686" >> Make.user
		echo "JULIA_CPU_TARGET=pentium4" >> Make.user
	else
		echo "MARCH=${CARCH/_/-}" >> Make.user
	fi

	default_prepare
}

build() {
	cd "$builddir"

	# If we don't clean here, then make install will recompile everything,
	# don't know why...
	make clean && make release
}

check() {
	cd "$builddir"

	# TMPDIR=... is needed to prevent the tests from trying to write
	# into /tmp, which will fail.
	mkdir -p "$builddir"/tmp
	TMPDIR="$builddir"/tmp make test
}

package() {
	cd "$builddir"

	make install
	install -D "$srcdir"/find-syslibs "$pkgdir"/usr/share/julia/find-syslibs
	# re-apply lost PaX markings
	paxmark m "$pkgdir"/usr/bin/julia "$pkgdir"/usr/bin/julia-debug
}

dbg() {
	pkgdesc="$pkgdesc (debug build)"
	replaces="$pkgname-debug"
	default_dbg

	install -d "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/*-debug "$subpkgdir"/usr/bin/

	install -d "$subpkgdir"/usr/lib/julia
	mv "$pkgdir"/usr/lib/*-debug.* "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/julia/*-debug.* "$subpkgdir"/usr/lib/julia/
}

sha512sums="da21b35eb2c682c0fb8720974a76759c51fe993f10e6af5ca4864fc7f0bb6c90d880b706eb798476c7228bae2db19ff0825add6a2abde2961f16a93a3050cb69  julia-0.6.0.tar.gz
4bf9c8b16617691b70ea43c667ecc575d2a06b3d0c347b949d10360c012138585a13ecea5e18de81b1585a96cf7734b7cc3d6072490898f1d5531c702cf5afab  libuv-52d72a52cc7ccd570929990f010ed16e2ec604c8.tar.gz
43eaf66d9cb3748012b2dfd77da1b41f667c5c7602a56bea8186b796b215bde82d555d79ab053378c2222521396354dcce5cf23a78fa3b1456062c47771c8433  UnicodeData.txt
0283c7b0ff7dd99ae79f6ddac63513ce7d58ba31de506a3fee07af9b8882ddc275d0f9cb002381ba1e304bcacf252612fa16b21b85667422477e6b945b725899  find-syslibs
f812b05b7712975eaef0bf071dd2c1cd0b21c68b18d6a513878bb8fe877bfcadf9f4b9dd8cd27b6809378de5b250a010301e7a90169677d155ad2ba377e8b428  0001-hardened.patch
52bbaff51c7d0299e875294b43e23773bcdbe1bc7d02b670814d051bb4f1224bba507182944dc739adb07acd10d3d7f5a0ac7c270c28d3a760d36d90520a2f16  0002-verbose-build.patch
04ab09428c299ca1cc7f162d2344763d9b9136c209a6718df62689f421b4925af20a7b739513965dcbdcbf191577b15d48ece08d63c6db21f8742c02d56a2ea0  0003-no-clean-docs.patch
9875b3cdc49bcc2812ea52b2e25836ba22058dbad94287c9aa7c4e4ee48b7819655588a08d49cb9a3c73431ff6969b3b7eb140fc96dc5b0d64777c3027e06538  0005-fix-rpath-issues.patch
a66526aee8745875b254f0d27db78b10e76cd8489f4601c77b82674a31ee7a0cf417af8b24a520e069a464303ad7f0077b75543e197eaba48a75b960c9f81a05  0008-ldconfig-compat.patch
3f2a6cf439aaada90872b869972f17b506d2f39758f3e2407c3e5f6b28082e7797ed6f2d7e4bace56e02809fe94f879c2a34e22216aa19b08a452371b81e7462  fix-or-disable-broken-tests.patch
be3eea9d7506796a48171d244e61e8ee3f69ee659aa65ee738a7ec08499566a014bd6ee21ba39ec97e74d734a8b45b7ec1f5a630a96a31a6d4e17b274107b640  test-disable-tests-using-fake_pty.patch
a97a16e3b36c7c192c1133139c87b7b3a719cef645dd2435828b5b5468f4f9beb3bba0e02bb68935319dc150d0c4ccd4728c9eaedc9e2ddee2d823e881f36e2c  test-libgit2-skip-SSL_CERT_FILE.patch
fc5eef406d84b60fdf1c837aa1f5bd0e7a205c311853d0ae8b2b237e2eb750d7da2152632d866d372174918a3953746620f2bf130596ab2b027ca389a6816edd  test-repl-disable-22176-20482.patch"
