# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Shiz <hi@shiz.me>
# Maintainer: Shiz <hi@shiz.me>
pkgname=julia
pkgver=0.6.2
# Keep in sync with deps/libuv.version.
_libuv_ver=d8ab1c6a33e77bf155facb54215dd8798e13825d
_llvm_ver=3.9
pkgrel=2
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
	libssh2-dev
	libunwind-dev
	llvm$_llvm_ver-dev
	mbedtls-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
	"
# FIXME: -tests should be noarch, but for unknown reason it fails on builder
# (arch-specific binaries are found)...
subpackages="$pkgname-tests $pkgname-doc $pkgname-dev $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
	UnicodeData.txt-9.0.0::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
	makefile-dont-rebuild-docs.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-9.0.0 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
		NO_GIT=1
		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_MBEDTLS=1
		USE_SYSTEM_LIBSSH2=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 debug is not built here, it is built during make install.
	make release debug
}

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

	rm -Rf "$pkgdir"/usr/share/julia/site
}

tests() {
	pkgdesc="$pkgdesc (tests)"
	replaces="$pkgname"  # for backward compatibility

	mkdir -p "$subpkgdir"/usr/share/julia
	mv "$pkgdir"/usr/share/julia/test "$subpkgdir"/usr/share/julia
}

# Note: Julia builds somehow special debug binaries, so we need to bypass
# default_dbg and just copy them as-is.
dbg() {
	pkgdesc="$pkgdesc (debug build)"
	depends="$pkgname=$pkgver-r$pkgrel"
	replaces="$pkgname-debug"

	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="679050463ed5825d34c477bd44cd91cfecc7d1735a2f52cc70b509afd9883cb60902861643266ecac5ba6cbb03f8fcdd12fc2e87dc41b9ed0eef813fa40610f1  julia-0.6.2.tar.gz
272e3cc7b1290ef19cc941c3b3e6dd39dba7dcb26f0aea8e667c48c56288aa9266020504e0cc90074f02881521b3352079416f564c7eb24ab444326a8f04ca64  libuv-d8ab1c6a33e77bf155facb54215dd8798e13825d.tar.gz
43eaf66d9cb3748012b2dfd77da1b41f667c5c7602a56bea8186b796b215bde82d555d79ab053378c2222521396354dcce5cf23a78fa3b1456062c47771c8433  UnicodeData.txt-9.0.0
0283c7b0ff7dd99ae79f6ddac63513ce7d58ba31de506a3fee07af9b8882ddc275d0f9cb002381ba1e304bcacf252612fa16b21b85667422477e6b945b725899  find-syslibs
f812b05b7712975eaef0bf071dd2c1cd0b21c68b18d6a513878bb8fe877bfcadf9f4b9dd8cd27b6809378de5b250a010301e7a90169677d155ad2ba377e8b428  0001-hardened.patch
52bbaff51c7d0299e875294b43e23773bcdbe1bc7d02b670814d051bb4f1224bba507182944dc739adb07acd10d3d7f5a0ac7c270c28d3a760d36d90520a2f16  0002-verbose-build.patch
04ab09428c299ca1cc7f162d2344763d9b9136c209a6718df62689f421b4925af20a7b739513965dcbdcbf191577b15d48ece08d63c6db21f8742c02d56a2ea0  0003-no-clean-docs.patch
e1257c42b819bbdcdca74c7b912c1e0e43d6ba70c5e8625d6141bb2dcc49abbab5a8690046bf6cc14cb058b972a74cb60eea6162a48f280035f69154cd25d657  0005-fix-rpath-issues.patch
251b3b8f9d5292afbff5816f48f6595b61da95322f5fa137260cde5b0e578aa08c2b8fc28d8031cace306403637b21f80d30edca79978ae5493b170a0db7143c  0008-ldconfig-compat.patch
e989a1f39a68de3378cedd6d009ae07af1888eb7a39b2f18c2441c06b75305f0cff90d8bef28bb2d0d84422435713060ffe07f244994a2941d591c66ac1cb131  fix-or-disable-broken-tests.patch
8d4608dd90735ffdbdb04e1dc27fc9702624e45969ab379401d5b605cccf78dea9a79d713995358b552532ae938212ee0f032e5ce7e6c28edb30c49c02b58e7b  test-disable-tests-using-fake_pty.patch
87267dd0402d8e70dd511bd79c7edfc3d1b5db7dbbdc9618832adb73ab7d34c6eed2c347fe74bf93757cc6b4f573ed7918d733b3bbf3653eab06f65a74428ff4  test-libgit2-skip-SSL_CERT_FILE.patch
fc5eef406d84b60fdf1c837aa1f5bd0e7a205c311853d0ae8b2b237e2eb750d7da2152632d866d372174918a3953746620f2bf130596ab2b027ca389a6816edd  test-repl-disable-22176-20482.patch
98a25c49465771e72c1c0d114602c6937dbddac3e81255b310a0309083b4b365c35eb37f7a7c6128b3369dd1a071cc5c16c18aa9628dd355defd0fcb0f289706  makefile-dont-rebuild-docs.patch"
