nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ stdenv, lib, fetchFromGitHub
2, makeWrapper, unzip, which, writeTextFile
3, curl, tzdata, gdb, Foundation, git, callPackage
4, targetPackages, fetchpatch, bash
5, HOST_DMD? "${callPackage ./bootstrap.nix { }}/bin/dmd"
6, version? "2.097.2"
7, dmdSha256? "16ldkk32y7ln82n7g2ym5d1xf3vly3i31hf8600cpvimf6yhr6kb"
8, druntimeSha256? "1sayg6ia85jln8g28vb4m124c27lgbkd6xzg9gblss8ardb8dsp1"
9, phobosSha256? "0czg13h65b6qwhk9ibya21z3iv3fpk3rsjr3zbcrpc2spqjknfw5"
10}:
11
12let
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 ${lib.optionalString (!targetPackages.stdenv.cc.isClang) "-L--export-dynamic"}'';
18 };
19 });
20 };
21
22 bits = builtins.toString stdenv.hostPlatform.parsed.cpu.bits;
23in
24
25stdenv.mkDerivation rec {
26 pname = "dmd";
27 inherit version;
28
29 enableParallelBuilding = true;
30
31 srcs = [
32 (fetchFromGitHub {
33 owner = "dlang";
34 repo = "dmd";
35 rev = "v${version}";
36 sha256 = dmdSha256;
37 name = "dmd";
38 })
39 (fetchFromGitHub {
40 owner = "dlang";
41 repo = "druntime";
42 rev = "v${version}";
43 sha256 = druntimeSha256;
44 name = "druntime";
45 })
46 (fetchFromGitHub {
47 owner = "dlang";
48 repo = "phobos";
49 rev = "v${version}";
50 sha256 = phobosSha256;
51 name = "phobos";
52 })
53 ];
54
55 sourceRoot = ".";
56
57 # https://issues.dlang.org/show_bug.cgi?id=19553
58 hardeningDisable = [ "fortify" ];
59
60 # Not using patches option to make it easy to patch, for example, dmd and
61 # Phobos at same time if that's required
62 patchPhase =
63 lib.optionalString (builtins.compareVersions version "2.092.1" <= 0) ''
64 patch -p1 -F3 --directory=druntime -i ${(fetchpatch {
65 url = "https://github.com/dlang/druntime/commit/438990def7e377ca1f87b6d28246673bb38022ab.patch";
66 sha256 = "0nxzkrd1rzj44l83j7jj90yz2cv01na8vn9d116ijnm85jl007b4";
67 })}
68
69 '' + postPatch;
70
71 postPatch =
72 ''
73 patchShebangs .
74
75 '' + lib.optionalString (version == "2.092.1") ''
76 rm dmd/test/dshell/test6952.d
77 '' + lib.optionalString (builtins.compareVersions "2.092.1" version < 0) ''
78 substituteInPlace dmd/test/dshell/test6952.d --replace "/usr/bin/env bash" "${bash}/bin/bash"
79
80 '' + ''
81 rm dmd/test/runnable/gdb1.d
82 rm dmd/test/runnable/gdb10311.d
83 rm dmd/test/runnable/gdb14225.d
84 rm dmd/test/runnable/gdb14276.d
85 rm dmd/test/runnable/gdb14313.d
86 rm dmd/test/runnable/gdb14330.d
87 rm dmd/test/runnable/gdb15729.sh
88 rm dmd/test/runnable/gdb4149.d
89 rm dmd/test/runnable/gdb4181.d
90
91 '' + lib.optionalString stdenv.isLinux ''
92 substituteInPlace phobos/std/socket.d --replace "assert(ih.addrList[0] == 0x7F_00_00_01);" ""
93 '' + lib.optionalString stdenv.isDarwin ''
94 substituteInPlace phobos/std/socket.d --replace "foreach (name; names)" "names = []; foreach (name; names)"
95 '';
96
97 nativeBuildInputs = [ makeWrapper unzip which git ];
98
99 buildInputs = [ gdb curl tzdata ]
100 ++ lib.optional stdenv.isDarwin [ Foundation gdb ];
101
102
103 osname = if stdenv.isDarwin then
104 "osx"
105 else
106 stdenv.hostPlatform.parsed.kernel.name;
107 top = "$NIX_BUILD_TOP";
108 pathToDmd = "${top}/dmd/generated/${osname}/release/${bits}/dmd";
109
110 # Build and install are based on http://wiki.dlang.org/Building_DMD
111 buildPhase = ''
112 cd dmd
113 make -j$NIX_BUILD_CORES -f posix.mak INSTALL_DIR=$out BUILD=release ENABLE_RELEASE=1 PIC=1 HOST_DMD=${HOST_DMD}
114 cd ../druntime
115 make -j$NIX_BUILD_CORES -f posix.mak BUILD=release ENABLE_RELEASE=1 PIC=1 INSTALL_DIR=$out DMD=${pathToDmd}
116 cd ../phobos
117 echo ${tzdata}/share/zoneinfo/ > TZDatabaseDirFile
118 echo ${curl.out}/lib/libcurl${stdenv.hostPlatform.extensions.sharedLibrary} > LibcurlPathFile
119 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)"
120 cd ..
121 '';
122
123 doCheck = true;
124
125 # many tests are disbled because they are failing
126
127 # NOTE: Purity check is disabled for checkPhase because it doesn't fare well
128 # with the DMD linker. See https://github.com/NixOS/nixpkgs/issues/97420
129 checkPhase = ''
130 cd dmd
131 NIX_ENFORCE_PURITY= \
132 make -j$NIX_BUILD_CORES -C test -f Makefile PIC=1 CC=$CXX DMD=${pathToDmd} BUILD=release SHELL=$SHELL
133
134 cd ../druntime
135 NIX_ENFORCE_PURITY= \
136 make -j$NIX_BUILD_CORES -f posix.mak unittest PIC=1 DMD=${pathToDmd} BUILD=release
137
138 cd ../phobos
139 NIX_ENFORCE_PURITY= \
140 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)"
141
142 cd ..
143 '';
144
145 installPhase = ''
146 cd dmd
147 mkdir $out
148 mkdir $out/bin
149 cp ${pathToDmd} $out/bin
150
151 mkdir -p $out/share/man/man1
152 mkdir -p $out/share/man/man5
153 cp -r docs/man/man1/* $out/share/man/man1/
154 cp -r docs/man/man5/* $out/share/man/man5/
155
156 cd ../druntime
157 mkdir $out/include
158 mkdir $out/include/dmd
159 cp -r import/* $out/include/dmd
160
161 cd ../phobos
162 mkdir $out/lib
163 cp generated/${osname}/release/${bits}/libphobos2.* $out/lib
164
165 cp -r std $out/include/dmd
166 cp -r etc $out/include/dmd
167
168 wrapProgram $out/bin/dmd \
169 --prefix PATH ":" "${targetPackages.stdenv.cc}/bin" \
170 --set-default CC "${targetPackages.stdenv.cc}/bin/cc"
171
172 substitute ${dmdConfFile} "$out/bin/dmd.conf" --subst-var out
173 '';
174
175 meta = with lib; {
176 broken = stdenv.isDarwin;
177 description = "Official reference compiler for the D language";
178 homepage = "https://dlang.org/";
179 # Everything is now Boost licensed, even the backend.
180 # https://github.com/dlang/dmd/pull/6680
181 license = licenses.boost;
182 maintainers = with maintainers; [ ThomasMader lionello ];
183 platforms = [ "x86_64-linux" "i686-linux" "x86_64-darwin" ];
184 };
185}