Skip to content

Instantly share code, notes, and snippets.

@rjmcguire
Last active February 6, 2017 15:29
Show Gist options
  • Save rjmcguire/14f7f1e83678af1349b66e13eb512076 to your computer and use it in GitHub Desktop.
Save rjmcguire/14f7f1e83678af1349b66e13eb512076 to your computer and use it in GitHub Desktop.
// dmd -O -inline -unittest -w -m64 -defaultlib=libphobos2.so archlinux-dmd.d
module archlinux_dmd;
import archlinux_dmd_config;
import std.stdio : writefln, readf;
import std.exception;
import std.file;
enum CURRENT_SYMLINK=DMD_DIR~"/current";
import std.string;
import std.conv;
import std.regex;
import std.algorithm;
import std.array;
//current=$(basename `readlink $DMD_DIR/current`)
struct SemVer {
int major,minor,patch;
string pre_release, build;
static auto fromMatch(Captures!(string, ulong) match) {
SemVer ret;
with (ret) {
major = match["major"].to!int;
minor = match["minor"].to!int;
patch = match["patch"].to!int;
pre_release = match["pre_release"];
build = match["build"];
}
return ret;
}
int opCmp(inout typeof(this) s) const {
if (major == s.major) {
if (minor == s.minor) {
return patch - s.patch;
}
return minor - s.minor;
}
return major - s.major;
}
string toString() const {
import std.format;
return "%d.0%d.%d%-(-%s%)%-(+%s%)".format(
major,
minor,
patch,
pre_release!="" ? [pre_release] : null,
build!="" ? [build] : null);
}
}
unittest {
assert(SemVer(0,1,2)<SemVer(0,1,3));
assert(SemVer(0,1,2)<=SemVer(0,1,3));
assert(SemVer(1,1,2)>SemVer(0,1,3));
assert(SemVer(1,1,2)>=SemVer(0,1,3));
assert(SemVer(1,1,2)<=SemVer(1,1,2));
assert(SemVer(1,1,2)>=SemVer(1,1,2));
assert(SemVer(1,1,2)==SemVer(1,1,2));
}
auto sortSemVer(R)(R items) {
auto r = regex(`\b(?P<major>[0-9]+)\.(?P<minor>[0-9]+)\.(?P<patch>[0-9]+)(-(?P<pre_release>.*))?(\+(?P<build>.*))?`);
SemVer[] versions;
foreach (item; items) {
auto match = item.matchFirst(r);
if (match.empty) {
continue;
}
versions ~= SemVer.fromMatch(match);
}
string[] ret;
foreach (item; sort(versions).array.reverse) {
ret ~= item.to!string;
}
return ret;
}
auto list_options() {
import std.file;
import std.path;
import std.algorithm;
auto options = std.file.dirEntries(DMD_DIR, SpanMode.shallow)
.filter!(a => a.isDir)
.map!(a => std.path.baseName(a.name))
.sortSemVer;
foreach (i, d; options) {
if (d == "current") {
continue;
}
auto current = baseName(readLink(CURRENT_SYMLINK));
if (current == d) {
writefln(" [%d] %s (default)", i, d);
} else {
writefln(" [%d] %s", i, d);
}
}
return options;
}
auto set_option() {
import std.process;
import std.path;
auto userid = executeShell("id -u");
if (userid.output.strip.to!int != 0) {
writefln("must be root to select default dmd");
return;
}
auto options = list_options();
writefln("select default dmd:");
int v;
try {
auto r = readf("%d", &v);
enforce!Exception(v < options.length);
} catch (Exception e) {
writefln("expected number between 0 and %d", options.length - 1);
return;
}
if (CURRENT_SYMLINK.exists) {
remove(CURRENT_SYMLINK);
}
symlink(buildPath([DMD_DIR, options[v]]) , CURRENT_SYMLINK);
writefln("default dmd: %s", baseName(readLink(CURRENT_SYMLINK)));
}
void main(string[] args) {
if (args.length <= 1) {
list_options();
return;
}
switch (args[1]) {
case "status":
goto case;
case "list":
list_options();
return;
case "set":
set_option();
return;
default:
writefln("Usage: %s [list]", args[0]);
return;
}
}
# Maintainer: Rory McGuire <[email protected]>
# Contributor: Mihails Strasunse <[email protected]>
# Contributor: Sven-Hendrik Haase <[email protected]>
# Contributor: Chris Brannon <[email protected]>
# Contributor: Andrea Scarpino <[email protected]>
# Contributor: Anders Bergh <[email protected]>
# Contributor: Alexander Fehr <pizzapunk gmail com>
pkgname=('dmd' 'libphobos-devel' 'libphobos' 'dmd-devel-common')
pkgdesc="D programming language compiler and standard library"
groups=('dlang' 'dlang-dmd')
pkgbase=dmd
dlangdir='/usr/lib/dlang/dmd'
pkgver=2.071.2
pkgrel=1
epoch=1
arch=('i686' 'x86_64')
url="http://www.dlang.org"
source=("http://downloads.dlang.org/releases/2.x/2.071.2/dmd.2.071.2.linux.tar.xz"
"archlinux-dmd.d")
sha256sums=('bab3343083af3aa7a88ac669af9a92f5e7aee1a19882a0a5f21538d90d3aa48c'
'20767406e25c46b45875948e0ec8a6540017d69605f55c0ca8a2073ad5345933')
[[ $CARCH == "x86_64" ]] && _archbits="64"
[[ $CARCH == "i686" ]] && _archbits="32"
build() {
echo 'building in ' `pwd`
echo 'module archlinux_dmd_config; enum DMD_DIR=`' $dlangdir '`;' > archlinux_dmd_config.d;
$srcdir/dmd2/linux/bin$_archbits/dmd \
-O -inline -unittest -m64 \
-L-L$srcdir/dmd2/linux/lib$_archbits/ -defaultlib=libphobos2.so \
archlinux-dmd.d
}
package_dmd() {
pkgdesc="The D programming language reference compiler"
depends=('gcc', 'dmd-devel-common')
provides=("d-compiler=$pkgver")
license=('custom')
# binaries
for file in ddemangle dman dmd dumpobj dustmite obj2asm rdmd; do
install -Dm755 $srcdir/dmd2/linux/bin$_archbits/$file $pkgdir$dlangdir/$pkgver/bin/$file
done;
# configs
install -Dm755 $srcdir/dmd2/linux/bin$_archbits/dmd.conf $pkgdir$dlangdir/$pkgver/dmd.conf
# licences
install -Dm644 $srcdir/dmd2/license.txt $pkgdir/usr/share/licenses/$pkgname/LICENSE
install -Dm644 $srcdir/dmd2/src/dmd/backendlicense.txt $pkgdir/usr/share/licenses/$pkgname/LICENSE-backend
install -Dm644 $srcdir/dmd2/src/dmd/boostlicense.txt $pkgdir/usr/share/licenses/$pkgname/LICENSE-frontend
# manuals
mkdir -p $pkgdir/usr/share/man/man1
mkdir -p $pkgdir/usr/share/man/man5
cp -r $srcdir/dmd2/man/man1/* $pkgdir/usr/share/man/man1/
cp -r $srcdir/dmd2/man/man5/* $pkgdir/usr/share/man/man5/
find $pkgdir/usr -type f | xargs chmod 0644
chmod 755 $pkgdir$dlangdir/$pkgver/bin/*
}
package_dmd-devel-common() {
pkgdesc="Common files for dmd build Environments"
license=("CC")
backup=('etc/dmd.conf')
makedepends=('dmd' 'gcc' 'libphobos')
depends=('dmd')
mkdir -p $pkgdir$dlangdir/
ln -s ./2.071.2 $pkgdir$dlangdir/current
mkdir $pkgdir/etc
ln -s ..$dlangdir/current/dmd.conf $pkgdir/etc/dmd.conf
mkdir -p $pkgdir/usr/bin
install -Dm755 $srcdir/archlinux-dmd $pkgdir/usr/bin/
chmod 755 $pkgdir/usr/bin/*
}
package_libphobos-devel() {
pkgdesc="The Phobos standard library for D programming language. Modules and static library."
options=("staticlibs")
provides=("d-runtime=$pkgver" "d-stdlib=$pkgver")
license=("custom")
for file in libphobos2.a libphobos2.so libphobos2.so.0.71 libphobos2.so.0.71.2; do
install -Dm644 $srcdir/dmd2/linux/lib$_archbits/$file $pkgdir$dlangdir/$pkgver/lib/$file
done;
mkdir -p $pkgdir$dlangdir/$pkgver/include
cp -r $srcdir/dmd2/src/phobos/{*.d,etc,std} $pkgdir$dlangdir/$pkgver/include
cp -r $srcdir/dmd2/src/druntime/import/* $pkgdir$dlangdir/$pkgver/include
find $pkgdir/usr -type f | xargs chmod 0644
install -Dm644 $srcdir/dmd2/src/druntime/LICENSE $pkgdir/usr/share/licenses/$pkgname/LICENSE-druntime
install -Dm644 $srcdir/dmd2/src/phobos/LICENSE_1_0.txt $pkgdir/usr/share/licenses/$pkgname/LICENSE-phobos
}
package_libphobos() {
pkgdesc="The Phobos standard library for D programming language."
depends=("curl")
license=("custom")
for file in libphobos2.so.0.71 libphobos2.so.0.71.2; do
install -Dm644 $srcdir/dmd2/linux/lib$_archbits/$file $pkgdir/usr/lib$_archbits/$file
done;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment