1{ stdenv, fetchurl, cmake, llvm, curl, tzdata
2, python, libconfig, lit, gdb, unzip, darwin, bash
3, callPackage, makeWrapper, targetPackages
4, bootstrapVersion ? false
5, version ? "1.14.0"
6, ldcSha256 ? "147vlzzzjx2n6zyz9wj54gj046i1mw5p5wixwzi5wkllgxghyy9c"
7}:
8
9let
10 bootstrapLdc = if !bootstrapVersion then
11 # LDC 0.17.x is the last version which doesn't need a working D compiler to
12 # build so we use that version to bootstrap the actual build.
13 callPackage ./default.nix {
14 bootstrapVersion = true;
15 version = "0.17.6";
16 ldcSha256 = "0qf5kbxddgmg3kqzi0kf4bgv8vdrnv16y07hcpm0cwv9mc3qr2w6";
17 }
18 else
19 "";
20
21in
22
23stdenv.mkDerivation rec {
24 name = "ldc-${version}";
25
26 enableParallelBuilding = true;
27
28 src = fetchurl {
29 url = "https://github.com/ldc-developers/ldc/releases/download/v${version}/ldc-${version}-src.tar.gz";
30 sha256 = ldcSha256;
31 };
32
33 # https://issues.dlang.org/show_bug.cgi?id=19553
34 hardeningDisable = [ "fortify" ];
35
36 postUnpack = ''
37 patchShebangs .
38 ''
39
40 + stdenv.lib.optionalString (!bootstrapVersion && stdenv.hostPlatform.isDarwin) ''
41 # https://github.com/NixOS/nixpkgs/issues/34817
42 rm -r ldc-${version}-src/tests/plugins/addFuncEntryCall
43 ''
44
45 + stdenv.lib.optionalString (!bootstrapVersion) ''
46 echo ${tzdata}/share/zoneinfo/ > ldc-${version}-src/TZDatabaseDirFile
47
48 echo ${curl.out}/lib/libcurl${stdenv.hostPlatform.extensions.sharedLibrary} > ldc-${version}-src/LibcurlPathFile
49 '';
50
51 postPatch = ''
52 # Setting SHELL=$SHELL when dmd testsuite is run doesn't work on Linux somehow
53 substituteInPlace tests/d2/dmd-testsuite/Makefile --replace "SHELL=/bin/bash" "SHELL=${bash}/bin/bash"
54 ''
55
56 + stdenv.lib.optionalString (!bootstrapVersion && stdenv.hostPlatform.isLinux) ''
57 substituteInPlace runtime/phobos/std/socket.d --replace "assert(ih.addrList[0] == 0x7F_00_00_01);" ""
58 ''
59
60 + stdenv.lib.optionalString (!bootstrapVersion && stdenv.hostPlatform.isDarwin) ''
61 substituteInPlace runtime/phobos/std/socket.d --replace "foreach (name; names)" "names = []; foreach (name; names)"
62 ''
63
64 + stdenv.lib.optionalString (bootstrapVersion && stdenv.hostPlatform.isDarwin) ''
65 # Was not able to compile on darwin due to "__inline_isnanl"
66 # being undefined.
67 # TODO Remove with version > 0.17.6
68 substituteInPlace dmd2/root/port.c --replace __inline_isnanl __inline_isnan
69 '';
70
71 nativeBuildInputs = [ cmake makeWrapper llvm unzip ]
72
73 ++ stdenv.lib.optional (!bootstrapVersion) [
74 bootstrapLdc python lit
75 ]
76
77 ++ stdenv.lib.optional (!bootstrapVersion && !stdenv.hostPlatform.isDarwin) [
78 # https://github.com/NixOS/nixpkgs/pull/36378#issuecomment-385034818
79 gdb
80 ]
81
82 ++ stdenv.lib.optional (bootstrapVersion) [
83 libconfig
84 ]
85
86 ++ stdenv.lib.optional stdenv.hostPlatform.isDarwin (with darwin.apple_sdk.frameworks; [
87 Foundation
88 ]);
89
90
91 buildInputs = [ curl tzdata ];
92
93 cmakeFlagsString = stdenv.lib.optionalString (!bootstrapVersion) ''
94 "-DD_FLAGS=-d-version=TZDatabaseDir;-d-version=LibcurlPath;-J$PWD"
95 "-DCMAKE_BUILD_TYPE=Release"
96 '';
97
98 preConfigure = stdenv.lib.optionalString (!bootstrapVersion) ''
99 cmakeFlagsArray=(
100 ${cmakeFlagsString}
101 )
102 '';
103
104 postConfigure = ''
105 export DMD=$PWD/bin/ldmd2
106 '';
107
108 makeFlags = [ "DMD=$DMD" ];
109
110 fixNames = if stdenv.hostPlatform.isDarwin then ''
111 fixDarwinDylibNames() {
112 local flags=()
113
114 for fn in "$@"; do
115 flags+=(-change "$(basename "$fn")" "$fn")
116 done
117
118 for fn in "$@"; do
119 if [ -L "$fn" ]; then continue; fi
120 echo "$fn: fixing dylib"
121 install_name_tool -id "$fn" "''${flags[@]}" "$fn"
122 done
123 }
124
125 fixDarwinDylibNames $(find "$(pwd)/lib" -name "*.dylib")
126 ''
127 else
128 "";
129
130 # https://github.com/ldc-developers/ldc/issues/2497#issuecomment-459633746
131 additionalExceptions = if stdenv.hostPlatform.isDarwin then
132 "|druntime-test-shared"
133 else
134 "";
135
136 doCheck = !bootstrapVersion;
137
138 checkPhase = stdenv.lib.optionalString doCheck ''
139 # Build default lib test runners
140 make -j$NIX_BUILD_CORES all-test-runners
141
142 ${fixNames}
143
144 # Run dmd testsuite
145 export DMD_TESTSUITE_MAKE_ARGS="-j$NIX_BUILD_CORES DMD=$DMD CC=$CXX"
146 ctest -V -R "dmd-testsuite"
147
148 # Build and run LDC D unittests.
149 ctest --output-on-failure -R "ldc2-unittest"
150
151 # Run LIT testsuite.
152 ctest -V -R "lit-tests"
153
154 # Run default lib unittests
155 ctest -j$NIX_BUILD_CORES --output-on-failure -E "ldc2-unittest|lit-tests|dmd-testsuite${additionalExceptions}"
156 '';
157
158 postInstall = ''
159 wrapProgram $out/bin/ldc2 \
160 --prefix PATH ":" "${targetPackages.stdenv.cc}/bin" \
161 --set-default CC "${targetPackages.stdenv.cc}/bin/cc"
162 '';
163
164 meta = with stdenv.lib; {
165 description = "The LLVM-based D compiler";
166 homepage = https://github.com/ldc-developers/ldc;
167 # from https://github.com/ldc-developers/ldc/blob/master/LICENSE
168 license = with licenses; [ bsd3 boost mit ncsa gpl2Plus ];
169 maintainers = with maintainers; [ ThomasMader ];
170 platforms = [ "x86_64-linux" "i686-linux" "x86_64-darwin" ];
171 };
172}
173