# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Shiz <hi@shiz.me>
# Maintainer: Shiz <hi@shiz.me>
pkgname=julia
pkgver=0.5.2
# Keep in sync with deps/libuv.version.
_libuv_ver=8d5131b6c1595920dd30644cd1435b4f344b46c8
_llvm_ver=3.9
pkgrel=3
pkgdesc="A high-level, high-performance dynamic language for technical computing"
url="http://julialang.org"
# x86: no libunwind package
arch=""
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
	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
	find-syslibs
	0001-hardened.patch
	0002-verbose-build.patch
	0003-no-clean-docs.patch
	0004-remove-sysctl.h.patch
	0005-fix-rpath-issues.patch
	0008-ldconfig-compat.patch
	fix-or-disable-broken-tests.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 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_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="70b3e23c7297d86268c2d32b0c19976dc6f9512bd41c5e9f621b1b9a082ed25ee85cf5f11f3e5acfc623b3a13fa88d245eb2609bad01935d86d90b502d068353  julia-0.5.2.tar.gz
c53513a5aea84405bf302b084a23f24f54148aac90a2bd666219ce14879723baab959942934d0d801a4572fffd07e60a7d574ade8d7eb57b6da8216063c20a48  libuv-8d5131b6c1595920dd30644cd1435b4f344b46c8.tar.gz
0283c7b0ff7dd99ae79f6ddac63513ce7d58ba31de506a3fee07af9b8882ddc275d0f9cb002381ba1e304bcacf252612fa16b21b85667422477e6b945b725899  find-syslibs
15968d95b09ed0f2315e08a5fd14e798db7fd1faebd1327ceaf7886248b61d2e61f64e726d98cd70cbc2349c66fc3a707298a2481c6abf98c699826f3f2f4ccb  0001-hardened.patch
6530709d5cd58970b5868a6a536727df6839b3aedfe81c67d4e5b37cf840803a33027eed338b47816810b9ae1119470acd097806e90a41cd59f8d1fcb0843663  0002-verbose-build.patch
66add07137c3baafba86aea1469ee2cf669f3fa4c01195c04c7a3f9e1b4a85dee020bef315919efa6261b675049bc251aa9fc1878ae7f7929baed5084eb697d7  0003-no-clean-docs.patch
6c111ccdd12af5b00fdf0b7cc8fba11980fceccf7c246b40e791cd1be8e39cd75b8c290c65ebd9b4add60fc242261e17e94807736fce6a96733e9bf18182b489  0004-remove-sysctl.h.patch
9875b3cdc49bcc2812ea52b2e25836ba22058dbad94287c9aa7c4e4ee48b7819655588a08d49cb9a3c73431ff6969b3b7eb140fc96dc5b0d64777c3027e06538  0005-fix-rpath-issues.patch
a66526aee8745875b254f0d27db78b10e76cd8489f4601c77b82674a31ee7a0cf417af8b24a520e069a464303ad7f0077b75543e197eaba48a75b960c9f81a05  0008-ldconfig-compat.patch
1d128441bb3e15defa2b23c8d68255fe15320060ac7203ebf7d3573e9e8c0120ebf49b2256f022fdddcc816691c6271f35a27d36b03cbe203734b9f902417ca9  fix-or-disable-broken-tests.patch"