# Template file for 'dmd'
pkgname=dmd
version=2.080.0
revision=1
create_wrksrc=yes
makedepends="dmd-bootstrap"
depends="libphobos>=${version} gcc"
short_desc="The Digital Mars D compiler"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
license="BSL-1.0"
homepage="http://www.digitalmars.com/d/2.0/"
distfiles="
 https://github.com/dlang/dmd/archive/v${version}.tar.gz>dmd-${version}.tar.gz
 https://github.com/dlang/druntime/archive/v${version}.tar.gz>druntime-${version}.tar.gz
 https://github.com/dlang/phobos/archive/v${version}.tar.gz>phobos-${version}.tar.gz
 http://downloads.dlang.org/releases/2.x/${version}/dmd.${version}.linux.tar.xz"
checksum="bef32e69f73ec2fd8482a3649797d85e4740d2c2019b8e03b37524ba58acec92
 e8b856d0927f3600003dddb049f091a2a50c74ad35182ffd1f88d6faf9079d77
 ec24c2bf3705edcdb33879db4f4ee5bab7d226b77920a29f945bd0665c5b5be4
 0ce8cbde1116dfc0e3f6f3e6ead794a5e6cbefa9483cdd55493e6a3b7e093bc3"
conf_files="/etc/dmd.conf"
provides="d-compiler-${version}_${revision}"
conflicts="dmd-bootstrap"
nopie=yes

case "$XBPS_TARGET_MACHINE" in
	x86_64*) _archbits=64;;
	i686*) _archbits=32;;
	*) broken="unsupported arch upstream";;
esac

post_extract() {
	rm -rf dmd druntime phobos
	mv dmd-${version} dmd
	mv druntime-${version} druntime
	mv phobos-${version} phobos
}

pre_configure() {
	case "$XBPS_TARGET_MACHINE" in
		*-musl) patch -p0 < ${FILESDIR}/musl.patch
	esac
}

do_build() {
	local dmd

	cd dmd
	make ${makejobs} -f posix.mak MODEL=${_archbits} TARGET_CPU=X86 BUILD=release PIC=1
	dmd=${wrksrc}/dmd/generated/linux/release/$_archbits/dmd

	make ${makejobs} -C docs

	cd ../druntime
	make ${makejobs} -f posix.mak MODEL=${_archbits} DMD=$dmd BUILD=release PIC=1

	cd ../phobos
	make ${makejobs} -f posix.mak MODEL=${_archbits} DMD=$dmd BUILD=release PIC=1
}

do_install() {
	cd dmd
	vbin generated/linux/release/$_archbits/dmd
	vinstall ${FILESDIR}/dmd.conf 644 etc

	vman generated/docs/man/man1/dmd.1
	vman generated/docs/man/man5/dmd.conf.5
}

dmd-doc_package() {
	short_desc="Documentation and sample code for D programming language"
	replaces="dmd-docs>=0"
	license="Boost License"

	pkg_install() {
		cd dmd2

		vmkdir usr/share/doc/d
		vcopy samples usr/share/doc/d
		vcopy html usr/share/doc/d

		find ${PKGDESTDIR}/usr/share/doc/d -type f | xargs chmod 0644

		vlicense license.txt
	}
}

libphobos_package() {
	short_desc+=" - standard library"
	license="Boost License"
	conflicts="dmd-bootstrap"
	pkg_install() {
		vinstall ${wrksrc}/druntime/generated/linux/release/$_archbits/libdruntime.a 644 usr/lib libdruntime.a
		vinstall ${wrksrc}/phobos/generated/linux/release/$_archbits/libphobos2.a 644 usr/lib libphobos2.a

		vmkdir usr/include/d
		cp -r ${wrksrc}/phobos/{*.d,etc,std} ${PKGDESTDIR}/usr/include/d

		vmkdir usr/include/d/druntime
		cp -r ${wrksrc}/druntime/import ${PKGDESTDIR}/usr/include/d/druntime

		vlicense ${wrksrc}/druntime/LICENSE.txt
	}
}
