# Contributor: Mathias LANG <pro.mathias.lang@gmail.com>
# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=dmd
pkgver=2.111.0
pkgrel=0
pkgdesc="D Programming Language reference compiler"
url="https://github.com/dlang/dmd"
# TODO: Enable on x86 once LDC-1.20.1 is out (and enabled on x86)
arch="x86_64"
license="BSL-1.0"
depends="llvm-libunwind-dev tzdata"
makedepends="chrpath ldc"
subpackages="$pkgname-doc"
source="$pkgname-$pkgver.tar.gz::https://github.com/dlang/dmd/archive/v$pkgver.tar.gz
	phobos-$pkgver.tar.gz::https://github.com/dlang/phobos/archive/v$pkgver.tar.gz

	dmd-install-config.conf
	10-dmd-musl.patch
	"
builddir="$srcdir"
options="!check" # todo

prepare() {
	# The Makefiles make some assumption about the directory structure
	ln -s "$srcdir/dmd-$pkgver/" "$srcdir/dmd"
	ln -s "$srcdir/phobos-$pkgver/" "$srcdir/phobos"

	default_prepare
}

build() {
	export HOST_DMD="ldmd2"

	local generated="$builddir"/dmd/generated
	mkdir -p "$generated"

	cd "$builddir"/dmd
	ldmd2 -ofgenerated/build -g compiler/src/build.d -release -O
	generated/build BUILD=release HOST_DMD="$HOST_DMD" CXX="c++" ENABLE_RELEASE=1 DFLAGS="$DFLAGS" dmd -v $MAKEFLAGS

	cd "$builddir"/dmd/druntime
	make -f posix.mak DMD="$generated"/linux/release/*/dmd BUILD=release ENABLE_RELEASE=1 PIC=1

	cd "$builddir"/phobos
	make -f posix.mak DMD="$generated"/linux/release/*/dmd BUILD=release ENABLE_RELEASE=1 PIC=1

	cd "$builddir"/dmd/compiler
	make -C docs DMD="$HOST_DMD"

	# Strip redundant rpath to avoid warnings in the builder
	chrpath -d "$builddir"/dmd/generated/linux/release/64/dmd
}

package() {
	mkdir -p "$pkgdir"/usr/lib/ "$pkgdir"/usr/include/dmd/phobos/

	install -Dm755 dmd/generated/linux/release/64/dmd -t "$pkgdir"/usr/bin/
	install -Dm644 dmd-install-config.conf "$pkgdir"/etc/dmd.conf

	install -Dm644 dmd/generated/docs/man/man1/dmd.1 -t "$pkgdir"/usr/share/man/man1/
	install -Dm644 dmd/generated/docs/man/man5/* -t "$pkgdir"/usr/share/man/man5/

	install -Dm755 phobos/generated/linux/release/64/*.so* -t "$pkgdir"/usr/lib/
	install -Dm755 phobos/generated/linux/release/64/*.a -t "$pkgdir"/usr/lib/

	cp -r phobos/etc phobos/std phobos/*.d "$pkgdir"/usr/include/dmd/phobos/
	cp -r dmd/druntime/import "$pkgdir"/usr/include/dmd/druntime
}

sha512sums="
424e10683ecaedd5fe84d21fecfc084507b45141616c4a1ff11dd15878f76904eea64d58ec19a53d38bbe85c42cdfcb6e59be43f0fb71cc53ccbe5a15ed17214  dmd-2.111.0.tar.gz
6fdf2d14d5e7ddee2756cf6b2da00be2744f49d23c14a3102ece7146907540024566c9456cecb3279d8d36ca1da0327fad0a4d291de7dc07d4f6a27fe93a1aff  phobos-2.111.0.tar.gz
123ec0f256a73030a5e5b4b87a7f2e0752320777b7fcd175a221807ec2917f5d6d88776c3448eab077eb7a2211dd4a3d64e3a556053b0f183eb058da437bc5da  dmd-install-config.conf
825c96fc50f91d21c1981be6aa01ba00cbb3514b2dff5457ccd7309a17ff4c9c1d47f769ee83a340186a7e76910baf931f9ea5f61ab098a94fc02a7d24f8c96e  10-dmd-musl.patch
"
