1{ stdenv, lib, fetchFromGitHub, fetchpatch
2, makeWrapper, unzip, which, writeTextFile
3, curl, tzdata, gdb, darwin, git
4, callPackage, targetPackages, ldc
5, version ? "2.084.1"
6, dmdSha256 ? "10ll5072rkv3ln7i5l88h2f9mzda567kw2jwh6466vm6ylzl4jms"
7, druntimeSha256 ? "0i0g2cnzh097pmvb86gvyj79canaxppw33hp7ylqnd11q4kqc8pb"
8, phobosSha256 ? "1hxpismj9gy5n1bc9kl9ykgd4lfmkq9i8xgrq09j0fybfwn9j1gc"
9}:
10
11let
12
13 dmdConfFile = writeTextFile {
14 name = "dmd.conf";
15 text = (lib.generators.toINI {} {
16 "Environment" = {
17 DFLAGS = ''-I@out@/include/dmd -L-L@out@/lib -fPIC ${stdenv.lib.optionalString (!targetPackages.stdenv.cc.isClang) "-L--export-dynamic"}'';
18 };
19 });
20 };
21
22in
23
24stdenv.mkDerivation rec {
25 name = "dmd-${version}";
26 inherit version;
27
28 enableParallelBuilding = true;
29
30 srcs = [
31 (fetchFromGitHub {
32 owner = "dlang";
33 repo = "dmd";
34 rev = "v${version}";
35 sha256 = dmdSha256;
36 name = "dmd";
37 })
38 (fetchFromGitHub {
39 owner = "dlang";
40 repo = "druntime";
41 rev = "v${version}";
42 sha256 = druntimeSha256;
43 name = "druntime";
44 })
45 (fetchFromGitHub {
46 owner = "dlang";
47 repo = "phobos";
48 rev = "v${version}";
49 sha256 = phobosSha256;
50 name = "phobos";
51 })
52 ];
53
54 patches = [
55 (fetchpatch {
56 name = "fix-loader-import.patch";
57 url = "https://github.com/dlang/dmd/commit/e7790436c4af1910b8c079dac9bb69627d7dea4b.patch";
58 sha256 = "0w69hajx8agywc7m2hph5m27g2yclz8ml0gjjyjk9k6ii3jv45kx";
59 })
60 ];
61
62 patchFlags = [ "--directory=dmd" "-p1" ];
63
64 sourceRoot = ".";
65
66 # https://issues.dlang.org/show_bug.cgi?id=19553
67 hardeningDisable = [ "fortify" ];
68
69 postUnpack = ''
70 patchShebangs .
71 '';
72
73 postPatch = stdenv.lib.optionalString stdenv.hostPlatform.isLinux ''
74 substituteInPlace phobos/std/socket.d --replace "assert(ih.addrList[0] == 0x7F_00_00_01);" ""
75 ''
76
77 + stdenv.lib.optionalString stdenv.hostPlatform.isDarwin ''
78 substituteInPlace phobos/std/socket.d --replace "foreach (name; names)" "names = []; foreach (name; names)"
79 '';
80
81 nativeBuildInputs = [ ldc makeWrapper unzip which gdb git ]
82
83 ++ stdenv.lib.optional stdenv.hostPlatform.isDarwin (with darwin.apple_sdk.frameworks; [
84 Foundation
85 ]);
86
87 buildInputs = [ curl tzdata ];
88
89 bits = builtins.toString stdenv.hostPlatform.parsed.cpu.bits;
90 osname = if stdenv.hostPlatform.isDarwin then
91 "osx"
92 else
93 stdenv.hostPlatform.parsed.kernel.name;
94 top = "$(echo $NIX_BUILD_TOP)";
95 pathToDmd = "${top}/dmd/generated/${osname}/release/${bits}/dmd";
96
97 # Buid and install are based on http://wiki.dlang.org/Building_DMD
98 buildPhase = ''
99 cd dmd
100 make -j$NIX_BUILD_CORES -f posix.mak INSTALL_DIR=$out BUILD=release ENABLE_RELEASE=1 PIC=1 HOST_DMD=ldmd2
101 cd ../druntime
102 make -j$NIX_BUILD_CORES -f posix.mak BUILD=release ENABLE_RELEASE=1 PIC=1 INSTALL_DIR=$out DMD=${pathToDmd}
103 cd ../phobos
104 echo ${tzdata}/share/zoneinfo/ > TZDatabaseDirFile
105 echo ${curl.out}/lib/libcurl${stdenv.hostPlatform.extensions.sharedLibrary} > LibcurlPathFile
106 make -j$NIX_BUILD_CORES -f posix.mak BUILD=release ENABLE_RELEASE=1 PIC=1 INSTALL_DIR=$out DMD=${pathToDmd} DFLAGS="-version=TZDatabaseDir -version=LibcurlPath -J$(pwd)"
107 cd ..
108 '';
109
110 doCheck = true;
111
112 checkPhase = ''
113 cd dmd
114 # https://github.com/NixOS/nixpkgs/pull/55998#issuecomment-465871846
115 #make -j$NIX_BUILD_CORES -C test -f Makefile PIC=1 CC=$CXX DMD=${pathToDmd} BUILD=release SHELL=$SHELL
116 cd ../druntime
117 make -j$NIX_BUILD_CORES -f posix.mak unittest PIC=1 DMD=${pathToDmd} BUILD=release
118 cd ../phobos
119 make -j$NIX_BUILD_CORES -f posix.mak unittest BUILD=release ENABLE_RELEASE=1 PIC=1 DMD=${pathToDmd} DFLAGS="-version=TZDatabaseDir -version=LibcurlPath -J$(pwd)"
120 cd ..
121 '';
122
123 dontStrip = true;
124
125 installPhase = ''
126 cd dmd
127 mkdir $out
128 mkdir $out/bin
129 cp ${pathToDmd} $out/bin
130
131 mkdir -p $out/share/man/man1
132 mkdir -p $out/share/man/man5
133 cp -r docs/man/man1/* $out/share/man/man1/
134 cp -r docs/man/man5/* $out/share/man/man5/
135
136 cd ../druntime
137 mkdir $out/include
138 mkdir $out/include/dmd
139 cp -r import/* $out/include/dmd
140
141 cd ../phobos
142 mkdir $out/lib
143 cp generated/${osname}/release/${bits}/libphobos2.* $out/lib
144
145 cp -r std $out/include/dmd
146 cp -r etc $out/include/dmd
147
148 wrapProgram $out/bin/dmd \
149 --prefix PATH ":" "${targetPackages.stdenv.cc}/bin" \
150 --set-default CC "${targetPackages.stdenv.cc}/bin/cc"
151
152 substitute ${dmdConfFile} "$out/bin/dmd.conf" --subst-var out
153 '';
154
155 meta = with stdenv.lib; {
156 description = "Official reference compiler for the D language";
157 homepage = http://dlang.org/;
158 # Everything is now Boost licensed, even the backend.
159 # https://github.com/dlang/dmd/pull/6680
160 license = licenses.boost;
161 maintainers = with maintainers; [ ThomasMader ];
162 platforms = [ "x86_64-linux" "i686-linux" "x86_64-darwin" ];
163 };
164}
165