Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{ version
2, dmdSha256
3, druntimeSha256
4, phobosSha256
5}:
6
7{ stdenv
8, lib
9, fetchFromGitHub
10, makeWrapper
11, which
12, writeTextFile
13, curl
14, tzdata
15, gdb
16, Foundation
17, callPackage
18, targetPackages
19, fetchpatch
20, bash
21, installShellFiles
22, git
23, unzip
24, HOST_DMD ? "${callPackage ./bootstrap.nix { }}/bin/dmd"
25}:
26
27let
28 dmdConfFile = writeTextFile {
29 name = "dmd.conf";
30 text = (lib.generators.toINI { } {
31 Environment = {
32 DFLAGS = ''-I@out@/include/dmd -L-L@out@/lib -fPIC ${lib.optionalString (!targetPackages.stdenv.cc.isClang) "-L--export-dynamic"}'';
33 };
34 });
35 };
36
37 bits = builtins.toString stdenv.hostPlatform.parsed.cpu.bits;
38 osname =
39 if stdenv.isDarwin then
40 "osx"
41 else
42 stdenv.hostPlatform.parsed.kernel.name;
43
44 pathToDmd = "\${NIX_BUILD_TOP}/dmd/generated/${osname}/release/${bits}/dmd";
45in
46
47stdenv.mkDerivation rec {
48 pname = "dmd";
49 inherit version;
50
51 enableParallelBuilding = true;
52
53 srcs = [
54 (fetchFromGitHub {
55 owner = "dlang";
56 repo = "dmd";
57 rev = "v${version}";
58 sha256 = dmdSha256;
59 name = "dmd";
60 })
61 (fetchFromGitHub {
62 owner = "dlang";
63 repo = "druntime";
64 rev = "v${version}";
65 sha256 = druntimeSha256;
66 name = "druntime";
67 })
68 (fetchFromGitHub {
69 owner = "dlang";
70 repo = "phobos";
71 rev = "v${version}";
72 sha256 = phobosSha256;
73 name = "phobos";
74 })
75 ];
76
77 sourceRoot = ".";
78
79 # https://issues.dlang.org/show_bug.cgi?id=19553
80 hardeningDisable = [ "fortify" ];
81
82 patches = lib.optionals (lib.versionOlder version "2.088.0") [
83 # Migrates D1-style operator overloads in DMD source, to allow building with
84 # a newer DMD
85 (fetchpatch {
86 url = "https://github.com/dlang/dmd/commit/c4d33e5eb46c123761ac501e8c52f33850483a8a.patch";
87 stripLen = 1;
88 extraPrefix = "dmd/";
89 sha256 = "sha256-N21mAPfaTo+zGCip4njejasraV5IsWVqlGR5eOdFZZE=";
90 })
91 ] ++ lib.optionals (lib.versionOlder version "2.092.2") [
92 # Fixes C++ tests that compiled on older C++ but not on the current one
93 (fetchpatch {
94 url = "https://github.com/dlang/druntime/commit/438990def7e377ca1f87b6d28246673bb38022ab.patch";
95 stripLen = 1;
96 extraPrefix = "druntime/";
97 sha256 = "sha256-/pPKK7ZK9E/mBrxm2MZyBNhYExE8p9jz8JqBdZSE6uY=";
98 })
99 ];
100
101 postPatch = ''
102 patchShebangs dmd/test/{runnable,fail_compilation,compilable,tools}{,/extra-files}/*.sh
103
104 rm dmd/test/runnable/gdb1.d
105 rm dmd/test/runnable/gdb10311.d
106 rm dmd/test/runnable/gdb14225.d
107 rm dmd/test/runnable/gdb14276.d
108 rm dmd/test/runnable/gdb14313.d
109 rm dmd/test/runnable/gdb14330.d
110 rm dmd/test/runnable/gdb15729.sh
111 rm dmd/test/runnable/gdb4149.d
112 rm dmd/test/runnable/gdb4181.d
113
114 # Disable tests that rely on objdump whitespace until fixed upstream:
115 # https://issues.dlang.org/show_bug.cgi?id=23317
116 rm dmd/test/runnable/cdvecfill.sh
117 rm dmd/test/compilable/cdcmp.d
118
119 # Grep'd string changed with gdb 12
120 # https://issues.dlang.org/show_bug.cgi?id=23198
121 substituteInPlace druntime/test/exceptions/Makefile \
122 --replace 'in D main (' 'in _Dmain ('
123
124 # We're using gnused on all platforms
125 substituteInPlace druntime/test/coverage/Makefile \
126 --replace 'freebsd osx' 'none'
127 ''
128
129 + lib.optionalString (lib.versionOlder version "2.091.0") ''
130 # This one has tested against a hardcoded year, then against a current year on
131 # and off again. It just isn't worth it to patch all the historical versions
132 # of it, so just remove it until the most recent change.
133 rm dmd/test/compilable/ddocYear.d
134 '' + lib.optionalString (lib.versionAtLeast version "2.089.0" && lib.versionOlder version "2.092.2") ''
135 rm dmd/test/dshell/test6952.d
136 '' + lib.optionalString (lib.versionAtLeast version "2.092.2") ''
137 substituteInPlace dmd/test/dshell/test6952.d --replace "/usr/bin/env bash" "${bash}/bin/bash"
138 ''
139
140 + lib.optionalString stdenv.isLinux ''
141 substituteInPlace phobos/std/socket.d --replace "assert(ih.addrList[0] == 0x7F_00_00_01);" ""
142 '' + lib.optionalString stdenv.isDarwin ''
143 substituteInPlace phobos/std/socket.d --replace "foreach (name; names)" "names = []; foreach (name; names)"
144 '';
145
146 nativeBuildInputs = [
147 makeWrapper
148 which
149 installShellFiles
150 ] ++ lib.optionals (lib.versionOlder version "2.088.0") [
151 git
152 ];
153
154 buildInputs = [
155 curl
156 tzdata
157 ] ++ lib.optionals stdenv.isDarwin [
158 Foundation
159 ];
160
161 nativeCheckInputs = [
162 gdb
163 ] ++ lib.optionals (lib.versionOlder version "2.089.0") [
164 unzip
165 ];
166
167 buildFlags = [
168 "BUILD=release"
169 "ENABLE_RELEASE=1"
170 "PIC=1"
171 ];
172
173 # Build and install are based on http://wiki.dlang.org/Building_DMD
174 buildPhase = ''
175 runHook preBuild
176
177 export buildJobs=$NIX_BUILD_CORES
178 if [ -z $enableParallelBuilding ]; then
179 buildJobs=1
180 fi
181
182 make -C dmd -f posix.mak $buildFlags -j$buildJobs HOST_DMD=${HOST_DMD}
183 make -C druntime -f posix.mak $buildFlags -j$buildJobs DMD=${pathToDmd}
184 echo ${tzdata}/share/zoneinfo/ > TZDatabaseDirFile
185 echo ${lib.getLib curl}/lib/libcurl${stdenv.hostPlatform.extensions.sharedLibrary} > LibcurlPathFile
186 make -C phobos -f posix.mak $buildFlags -j$buildJobs DMD=${pathToDmd} DFLAGS="-version=TZDatabaseDir -version=LibcurlPath -J$PWD"
187
188 runHook postBuild
189 '';
190
191 doCheck = true;
192
193 checkFlags = buildFlags;
194
195 # many tests are disbled because they are failing
196
197 # NOTE: Purity check is disabled for checkPhase because it doesn't fare well
198 # with the DMD linker. See https://github.com/NixOS/nixpkgs/issues/97420
199 checkPhase = ''
200 runHook preCheck
201
202 export checkJobs=$NIX_BUILD_CORES
203 if [ -z $enableParallelChecking ]; then
204 checkJobs=1
205 fi
206
207 NIX_ENFORCE_PURITY= \
208 make -C dmd/test $checkFlags CC=$CXX SHELL=$SHELL -j$checkJobs N=$checkJobs
209
210 NIX_ENFORCE_PURITY= \
211 make -C druntime -f posix.mak unittest $checkFlags -j$checkJobs
212
213 NIX_ENFORCE_PURITY= \
214 make -C phobos -f posix.mak unittest $checkFlags -j$checkJobs DFLAGS="-version=TZDatabaseDir -version=LibcurlPath -J$PWD"
215
216 runHook postCheck
217 '';
218
219 installPhase = ''
220 runHook preInstall
221
222 install -Dm755 ${pathToDmd} $out/bin/dmd
223
224 installManPage dmd/docs/man/man*/*
225
226 mkdir -p $out/include/dmd
227 cp -r {druntime/import/*,phobos/{std,etc}} $out/include/dmd/
228
229 mkdir $out/lib
230 cp phobos/generated/${osname}/release/${bits}/libphobos2.* $out/lib/
231
232 wrapProgram $out/bin/dmd \
233 --prefix PATH ":" "${targetPackages.stdenv.cc}/bin" \
234 --set-default CC "${targetPackages.stdenv.cc}/bin/cc"
235
236 substitute ${dmdConfFile} "$out/bin/dmd.conf" --subst-var out
237
238 runHook postInstall
239 '';
240
241 meta = with lib; {
242 description = "Official reference compiler for the D language";
243 homepage = "https://dlang.org/";
244 # Everything is now Boost licensed, even the backend.
245 # https://github.com/dlang/dmd/pull/6680
246 license = licenses.boost;
247 maintainers = with maintainers; [ ThomasMader lionello dukc ];
248 platforms = [ "x86_64-linux" "i686-linux" "x86_64-darwin" ];
249 };
250}