Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / development / compilers / dmd / generic.nix
blob0f6abb574a597a837fd8fec1a74331137d2239d9
1 { version
2 , dmdSha256
3 , phobosSha256
4 }:
6 { stdenv
7 , lib
8 , fetchFromGitHub
9 , removeReferencesTo
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 , dmdBootstrap ? callPackage ./bootstrap.nix { }
25 , dmd_bin ? "${dmdBootstrap}/bin"
28 let
29   dmdConfFile = writeTextFile {
30     name = "dmd.conf";
31     text = (lib.generators.toINI { } {
32       Environment = {
33         DFLAGS = ''-I@out@/include/dmd -L-L@out@/lib -fPIC ${lib.optionalString (!targetPackages.stdenv.cc.isClang) "-L--export-dynamic"}'';
34       };
35     });
36   };
38   bits = builtins.toString stdenv.hostPlatform.parsed.cpu.bits;
39   osname =
40     if stdenv.isDarwin then
41       "osx"
42     else
43       stdenv.hostPlatform.parsed.kernel.name;
45   pathToDmd = "\${NIX_BUILD_TOP}/dmd/generated/${osname}/release/${bits}/dmd";
48 stdenv.mkDerivation rec {
49   pname = "dmd";
50   inherit version;
52   enableParallelBuilding = true;
54   srcs = [
55     (fetchFromGitHub {
56       owner = "dlang";
57       repo = "dmd";
58       rev = "v${version}";
59       sha256 = dmdSha256;
60       name = "dmd";
61     })
62     (fetchFromGitHub {
63       owner = "dlang";
64       repo = "phobos";
65       rev = "v${version}";
66       sha256 = phobosSha256;
67       name = "phobos";
68     })
69   ];
71   sourceRoot = ".";
73   # https://issues.dlang.org/show_bug.cgi?id=19553
74   hardeningDisable = [ "fortify" ];
76   patches = lib.optionals (lib.versionOlder version "2.088.0") [
77     # Migrates D1-style operator overloads in DMD source, to allow building with
78     # a newer DMD
79     (fetchpatch {
80       url = "https://github.com/dlang/dmd/commit/c4d33e5eb46c123761ac501e8c52f33850483a8a.patch";
81       stripLen = 1;
82       extraPrefix = "dmd/";
83       sha256 = "sha256-N21mAPfaTo+zGCip4njejasraV5IsWVqlGR5eOdFZZE=";
84     })
85   ];
87   postPatch = ''
88     patchShebangs dmd/compiler/test/{runnable,fail_compilation,compilable,tools}{,/extra-files}/*.sh
90     rm dmd/compiler/test/runnable/gdb1.d
91     rm dmd/compiler/test/runnable/gdb10311.d
92     rm dmd/compiler/test/runnable/gdb14225.d
93     rm dmd/compiler/test/runnable/gdb14276.d
94     rm dmd/compiler/test/runnable/gdb14313.d
95     rm dmd/compiler/test/runnable/gdb14330.d
96     rm dmd/compiler/test/runnable/gdb15729.sh
97     rm dmd/compiler/test/runnable/gdb4149.d
98     rm dmd/compiler/test/runnable/gdb4181.d
100     # Disable tests that rely on objdump whitespace until fixed upstream:
101     #   https://issues.dlang.org/show_bug.cgi?id=23317
102     rm dmd/compiler/test/runnable/cdvecfill.sh
103     rm dmd/compiler/test/compilable/cdcmp.d
104   ''
106   + lib.optionalString (lib.versionOlder version "2.091.0") ''
107     # This one has tested against a hardcoded year, then against a current year on
108     # and off again. It just isn't worth it to patch all the historical versions
109     # of it, so just remove it until the most recent change.
110     rm dmd/compiler/test/compilable/ddocYear.d
111   '' + lib.optionalString (lib.versionAtLeast version "2.089.0" && lib.versionOlder version "2.092.2") ''
112     rm dmd/compiler/test/dshell/test6952.d
113   '' + lib.optionalString (lib.versionAtLeast version "2.092.2") ''
114     substituteInPlace dmd/compiler/test/dshell/test6952.d --replace "/usr/bin/env bash" "${bash}/bin/bash"
115   ''
117   + lib.optionalString stdenv.isLinux ''
118     substituteInPlace phobos/std/socket.d --replace "assert(ih.addrList[0] == 0x7F_00_00_01);" ""
119   '' + lib.optionalString stdenv.isDarwin ''
120     substituteInPlace phobos/std/socket.d --replace "foreach (name; names)" "names = []; foreach (name; names)"
121   '';
123   nativeBuildInputs = [
124     makeWrapper
125     which
126     installShellFiles
127   ] ++ lib.optionals (lib.versionOlder version "2.088.0") [
128     git
129   ];
131   buildInputs = [
132     curl
133     tzdata
134   ] ++ lib.optionals stdenv.isDarwin [
135     Foundation
136   ];
138   nativeCheckInputs = [
139     gdb
140   ] ++ lib.optionals (lib.versionOlder version "2.089.0") [
141     unzip
142   ];
144   buildFlags = [
145     "BUILD=release"
146     "ENABLE_RELEASE=1"
147     "PIC=1"
148   ];
150   # Build and install are based on http://wiki.dlang.org/Building_DMD
151   buildPhase = ''
152     runHook preBuild
154     export buildJobs=$NIX_BUILD_CORES
155     if [ -z $enableParallelBuilding ]; then
156       buildJobs=1
157     fi
159     ${dmd_bin}/rdmd dmd/compiler/src/build.d -j$buildJobs HOST_DMD=${dmd_bin}/dmd $buildFlags
160     make -C dmd/druntime -f posix.mak DMD=${pathToDmd} $buildFlags -j$buildJobs
161     echo ${tzdata}/share/zoneinfo/ > TZDatabaseDirFile
162     echo ${lib.getLib curl}/lib/libcurl${stdenv.hostPlatform.extensions.sharedLibrary} > LibcurlPathFile
163     make -C phobos -f posix.mak $buildFlags -j$buildJobs DMD=${pathToDmd} DFLAGS="-version=TZDatabaseDir -version=LibcurlPath -J$PWD"
165     runHook postBuild
166   '';
168   doCheck = true;
170   checkFlags = buildFlags;
172   # many tests are disbled because they are failing
174   # NOTE: Purity check is disabled for checkPhase because it doesn't fare well
175   # with the DMD linker. See https://github.com/NixOS/nixpkgs/issues/97420
176   checkPhase = ''
177     runHook preCheck
179     export checkJobs=$NIX_BUILD_CORES
180     if [ -z $enableParallelChecking ]; then
181       checkJobs=1
182     fi
184     NIX_ENFORCE_PURITY= \
185       make -C dmd/compiler/test $checkFlags CC=$CXX SHELL=$SHELL -j$checkJobs N=$checkJobs
187     NIX_ENFORCE_PURITY= \
188       make -C phobos -f posix.mak unittest $checkFlags -j$checkJobs DFLAGS="-version=TZDatabaseDir -version=LibcurlPath -J$PWD"
190     runHook postCheck
191   '';
193   installPhase = ''
194     runHook preInstall
196     install -Dm755 ${pathToDmd} $out/bin/dmd
198     installManPage dmd/docs/man/man*/*
200     mkdir -p $out/include/dmd
201     cp -r {dmd/druntime/import/*,phobos/{std,etc}} $out/include/dmd/
203     mkdir $out/lib
204     cp phobos/generated/${osname}/release/${bits}/libphobos2.* $out/lib/
206     wrapProgram $out/bin/dmd \
207       --prefix PATH ":" "${targetPackages.stdenv.cc}/bin" \
208       --set-default CC "${targetPackages.stdenv.cc}/bin/cc"
210     substitute ${dmdConfFile} "$out/bin/dmd.conf" --subst-var out
212     runHook postInstall
213   '';
215   preFixup = ''
216     find $out/bin -type f -exec ${removeReferencesTo}/bin/remove-references-to -t ${dmd_bin}/dmd '{}' +
217   '';
219   disallowedReferences = [ dmdBootstrap ];
221   meta = with lib; {
222     description = "Official reference compiler for the D language";
223     homepage = "https://dlang.org/";
224     # Everything is now Boost licensed, even the backend.
225     # https://github.com/dlang/dmd/pull/6680
226     license = licenses.boost;
227     maintainers = with maintainers; [ ThomasMader lionello dukc jtbx ];
228     platforms = [ "x86_64-linux" "i686-linux" "x86_64-darwin" ];
229   };