1{ version, sha256 }:
2{ lib, stdenv, fetchurl, cmake, ninja, llvm_14, curl, tzdata
3, libconfig, lit, gdb, unzip, darwin, bash
4, callPackage, makeWrapper, runCommand, targetPackages
5, ldcBootstrap ? callPackage ./bootstrap.nix { }
6}:
7
8let
9 pathConfig = runCommand "ldc-lib-paths" {} ''
10 mkdir $out
11 echo ${tzdata}/share/zoneinfo/ > $out/TZDatabaseDirFile
12 echo ${curl.out}/lib/libcurl${stdenv.hostPlatform.extensions.sharedLibrary} > $out/LibcurlPathFile
13 '';
14
15in
16
17stdenv.mkDerivation rec {
18 pname = "ldc";
19 inherit version;
20
21 src = fetchurl {
22 url = "https://github.com/ldc-developers/ldc/releases/download/v${version}/ldc-${version}-src.tar.gz";
23 inherit sha256;
24 };
25
26 # https://issues.dlang.org/show_bug.cgi?id=19553
27 hardeningDisable = [ "fortify" ];
28
29 postUnpack = ''
30 patchShebangs .
31 ''
32 + ''
33 rm ldc-${version}-src/tests/dmd/fail_compilation/mixin_gc.d
34 rm ldc-${version}-src/tests/dmd/runnable/xtest46_gc.d
35 rm ldc-${version}-src/tests/dmd/runnable/testptrref_gc.d
36
37 # test depends on current year
38 rm ldc-${version}-src/tests/dmd/compilable/ddocYear.d
39 ''
40 + lib.optionalString stdenv.hostPlatform.isDarwin ''
41 # https://github.com/NixOS/nixpkgs/issues/34817
42 rm -r ldc-${version}-src/tests/plugins/addFuncEntryCall
43 '';
44
45 postPatch = ''
46 # Setting SHELL=$SHELL when dmd testsuite is run doesn't work on Linux somehow
47 substituteInPlace tests/dmd/Makefile --replace "SHELL=/bin/bash" "SHELL=${bash}/bin/bash"
48 ''
49 + lib.optionalString stdenv.hostPlatform.isLinux ''
50 substituteInPlace runtime/phobos/std/socket.d --replace "assert(ih.addrList[0] == 0x7F_00_00_01);" ""
51 ''
52 + lib.optionalString stdenv.hostPlatform.isDarwin ''
53 substituteInPlace runtime/phobos/std/socket.d --replace "foreach (name; names)" "names = []; foreach (name; names)"
54 '';
55
56 nativeBuildInputs = [
57 cmake ldcBootstrap lit lit.python llvm_14.dev makeWrapper ninja unzip
58 ]
59 ++ lib.optionals stdenv.hostPlatform.isDarwin [
60 darwin.apple_sdk.frameworks.Foundation
61 ]
62 ++ lib.optionals (!stdenv.hostPlatform.isDarwin) [
63 # https://github.com/NixOS/nixpkgs/pull/36378#issuecomment-385034818
64 gdb
65 ];
66
67 buildInputs = [ curl tzdata ];
68
69 cmakeFlags = [
70 "-DD_FLAGS=-d-version=TZDatabaseDir;-d-version=LibcurlPath;-J${pathConfig}"
71 "-DCMAKE_BUILD_TYPE=Release"
72 ];
73
74 postConfigure = ''
75 export DMD=$PWD/bin/ldmd2
76 '';
77
78 makeFlags = [ "DMD=$DMD" ];
79
80 fixNames = lib.optionalString stdenv.hostPlatform.isDarwin ''
81 fixDarwinDylibNames() {
82 local flags=()
83
84 for fn in "$@"; do
85 flags+=(-change "$(basename "$fn")" "$fn")
86 done
87
88 for fn in "$@"; do
89 if [ -L "$fn" ]; then continue; fi
90 echo "$fn: fixing dylib"
91 install_name_tool -id "$fn" "''${flags[@]}" "$fn"
92 done
93 }
94
95 fixDarwinDylibNames $(find "$(pwd)/lib" -name "*.dylib")
96 export DYLD_LIBRARY_PATH=$(pwd)/lib
97 '';
98
99 # https://github.com/ldc-developers/ldc/issues/2497#issuecomment-459633746
100 additionalExceptions = lib.optionalString stdenv.hostPlatform.isDarwin
101 "|druntime-test-shared";
102
103 checkPhase = ''
104 # Build default lib test runners
105 ninja -j$NIX_BUILD_CORES all-test-runners
106
107 ${fixNames}
108
109 # Run dmd testsuite
110 export DMD_TESTSUITE_MAKE_ARGS="-j$NIX_BUILD_CORES DMD=$DMD"
111 ctest -V -R "dmd-testsuite"
112
113 # Build and run LDC D unittests.
114 ctest --output-on-failure -R "ldc2-unittest"
115
116 # Run LIT testsuite.
117 ctest -V -R "lit-tests"
118
119 # Run default lib unittests
120 ctest -j$NIX_BUILD_CORES --output-on-failure -E "ldc2-unittest|lit-tests|dmd-testsuite${additionalExceptions}"
121 '';
122
123 postInstall = ''
124 wrapProgram $out/bin/ldc2 \
125 --prefix PATH ":" "${targetPackages.stdenv.cc}/bin" \
126 --set-default CC "${targetPackages.stdenv.cc}/bin/cc"
127 '';
128
129 meta = with lib; {
130 description = "The LLVM-based D compiler";
131 homepage = "https://github.com/ldc-developers/ldc";
132 # from https://github.com/ldc-developers/ldc/blob/master/LICENSE
133 license = with licenses; [ bsd3 boost mit ncsa gpl2Plus ];
134 maintainers = with maintainers; [ ThomasMader lionello ];
135 platforms = [ "x86_64-linux" "i686-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
136 };
137}