1{ stdenv, fetchFromGitHub
2, makeWrapper, unzip, which
3, curl, tzdata, gdb
4# Versions 2.070.2 and up require a working dmd compiler to build:
5, bootstrapDmd }:
6
7stdenv.mkDerivation rec {
8 name = "dmd-${version}";
9 version = "2.075.1";
10
11 srcs = [
12 (fetchFromGitHub {
13 owner = "dlang";
14 repo = "dmd";
15 rev = "v${version}";
16 sha256 = "0kq6r8rcghvzk5jcphg89l85rg734s29bssd2rcw3fygx0k9a9k5";
17 })
18 (fetchFromGitHub {
19 owner = "dlang";
20 repo = "druntime";
21 rev = "v${version}";
22 sha256 = "0idn2v1lmp7hl637g3i7pdfj9mjk4sclkz4cm77nl8873k2fhk8j";
23 })
24 (fetchFromGitHub {
25 owner = "dlang";
26 repo = "phobos";
27 rev = "v${version}";
28 sha256 = "1a7q5fd15yspgs5plxgx54jyrcwgzlyw3rahmz04jd2s5h56dj04";
29 })
30 ];
31
32 sourceRoot = ".";
33
34 postUnpack = ''
35 mv dmd-v${version}-src dmd
36 mv druntime-v${version}-src druntime
37 mv phobos-v${version}-src phobos
38
39 # Remove cppa test for now because it doesn't work.
40 rm dmd/test/runnable/cppa.d
41 rm dmd/test/runnable/extra-files/cppb.cpp
42 '';
43
44 # Compile with PIC to prevent colliding modules with binutils 2.28.
45 # https://issues.dlang.org/show_bug.cgi?id=17375
46 usePIC = "-fPIC";
47
48 postPatch = ''
49 # Ugly hack so the dlopen call has a chance to succeed.
50 # https://issues.dlang.org/show_bug.cgi?id=15391
51 substituteInPlace phobos/std/net/curl.d \
52 --replace libcurl.so ${curl.out}/lib/libcurl.so
53
54 # Ugly hack to fix the hardcoded path to zoneinfo in the source file.
55 # https://issues.dlang.org/show_bug.cgi?id=15391
56 substituteInPlace phobos/std/datetime/timezone.d \
57 --replace /usr/share/zoneinfo/ ${tzdata}/share/zoneinfo/
58
59 substituteInPlace druntime/test/common.mak \
60 --replace "DFLAGS:=" "DFLAGS:=${usePIC} "
61
62 # phobos uses curl, so we need to patch the path to the lib.
63 substituteInPlace phobos/posix.mak \
64 --replace "-soname=libcurl.so.4" "-soname=${curl.out}/lib/libcurl.so.4"
65
66 # Use proper C++ compiler
67 substituteInPlace dmd/posix.mak \
68 --replace g++ $CXX
69 ''
70
71 + stdenv.lib.optionalString stdenv.hostPlatform.isDarwin ''
72 substituteInPlace dmd/posix.mak \
73 --replace MACOSX_DEPLOYMENT_TARGET MACOSX_DEPLOYMENT_TARGET_
74 '';
75
76 nativeBuildInputs = [ bootstrapDmd makeWrapper unzip which gdb ];
77 buildInputs = [ curl tzdata ];
78
79 # Buid and install are based on http://wiki.dlang.org/Building_DMD
80 buildPhase = ''
81 cd dmd
82 make -j$NIX_BUILD_CORES -f posix.mak INSTALL_DIR=$out
83 ${
84 let bits = builtins.toString stdenv.hostPlatform.parsed.cpu.bits;
85 osname = if stdenv.hostPlatform.isDarwin then "osx" else stdenv.hostPlatform.parsed.kernel.name; in
86 "export DMD=$PWD/generated/${osname}/release/${bits}/dmd"
87 }
88 cd ../druntime
89 make -j$NIX_BUILD_CORES -f posix.mak PIC=${usePIC} INSTALL_DIR=$out DMD=$DMD
90 cd ../phobos
91 make -j$NIX_BUILD_CORES -f posix.mak PIC=${usePIC} INSTALL_DIR=$out DMD=$DMD
92 cd ..
93 '';
94
95 # disable check phase because some tests are not working with sandboxing
96 doCheck = false;
97
98 checkPhase = ''
99 cd dmd
100 ${
101 let bits = builtins.toString stdenv.hostPlatform.parsed.cpu.bits;
102 osname = if stdenv.hostPlatform.isDarwin then "osx" else stdenv.hostPlatform.parsed.kernel.name; in
103 "export DMD=$PWD/generated/${osname}/release/${bits}/dmd"
104 }
105 make -j$NIX_BUILD_CORES -C test -f Makefile PIC=${usePIC} DMD=$DMD BUILD=release SHARED=0
106 cd ../druntime
107 make -j$NIX_BUILD_CORES -f posix.mak unittest PIC=${usePIC} DMD=$DMD BUILD=release
108 cd ../phobos
109 make -j$NIX_BUILD_CORES -f posix.mak unittest PIC=${usePIC} DMD=$DMD BUILD=release
110 cd ..
111 '';
112
113 installPhase = ''
114 cd dmd
115 mkdir $out
116 mkdir $out/bin
117 ${
118 let bits = builtins.toString stdenv.hostPlatform.parsed.cpu.bits;
119 osname = if stdenv.hostPlatform.isDarwin then "osx" else stdenv.hostPlatform.parsed.kernel.name; in
120 "cp $PWD/generated/${osname}/release/${bits}/dmd $out/bin"
121 }
122
123 mkdir -p $out/share/man/man1
124 mkdir -p $out/share/man/man5
125 cp -r docs/man/man1/* $out/share/man/man1/
126 cp -r docs/man/man5/* $out/share/man/man5/
127
128 cd ../druntime
129 mkdir $out/include
130 mkdir $out/include/d2
131 cp -r import/* $out/include/d2
132
133 cd ../phobos
134 mkdir $out/lib
135 ${
136 let bits = builtins.toString stdenv.hostPlatform.parsed.cpu.bits;
137 osname = if stdenv.hostPlatform.isDarwin then "osx" else stdenv.hostPlatform.parsed.kernel.name;
138 extension = if stdenv.hostPlatform.isDarwin then "a" else "{a,so}"; in
139 "cp generated/${osname}/release/${bits}/libphobos2.${extension} $out/lib"
140 }
141
142 cp -r std $out/include/d2
143 cp -r etc $out/include/d2
144
145 wrapProgram $out/bin/dmd \
146 --prefix PATH ":" "${stdenv.cc}/bin" \
147 --set CC "$""{CC:-$CC""}"
148
149 cd $out/bin
150 tee dmd.conf << EOF
151 [Environment]
152 DFLAGS=-I$out/include/d2 -L-L$out/lib ${stdenv.lib.optionalString (!stdenv.cc.isClang) "-L--export-dynamic"} -fPIC
153 EOF
154 '';
155
156 meta = with stdenv.lib; {
157 description = "Official reference compiler for the D language";
158 homepage = http://dlang.org/;
159 # Everything is now Boost licensed, even the backend.
160 # https://github.com/dlang/dmd/pull/6680
161 license = licenses.boost;
162 platforms = platforms.unix;
163 };
164}