1{ lib
2, stdenv
3, fetchFromGitHub
4, nix-update-script
5
6, cmake
7, mimalloc
8, ninja
9, tbb
10, zlib
11, zstd
12
13, buildPackages
14, clangStdenv
15, gccStdenv
16, hello
17, mold
18, mold-wrapped
19, runCommandCC
20, testers
21, useMoldLinker
22}:
23
24stdenv.mkDerivation rec {
25 pname = "mold";
26 version = "2.3.3";
27
28 src = fetchFromGitHub {
29 owner = "rui314";
30 repo = "mold";
31 rev = "v${version}";
32 hash = "sha256-YXFfjJp4dSxzEyAtrEi/ONQZKD7QAU/MZ62l4QCcbwE=";
33 };
34
35 nativeBuildInputs = [
36 cmake
37 ninja
38 ];
39
40 buildInputs = [
41 tbb
42 zlib
43 zstd
44 ] ++ lib.optionals (!stdenv.isDarwin) [
45 mimalloc
46 ];
47
48 cmakeFlags = [
49 "-DMOLD_USE_SYSTEM_MIMALLOC:BOOL=ON"
50 "-DMOLD_USE_SYSTEM_TBB:BOOL=ON"
51 ];
52
53 env.NIX_CFLAGS_COMPILE = toString (lib.optionals stdenv.isDarwin [
54 "-faligned-allocation"
55 ]);
56
57 passthru = {
58 updateScript = nix-update-script { };
59 tests =
60 let
61 helloTest = name: helloMold:
62 let
63 command = "$READELF -p .comment ${lib.getExe helloMold}";
64 emulator = stdenv.hostPlatform.emulator buildPackages;
65 in
66 runCommandCC "mold-${name}-test" { passthru = { inherit helloMold; }; }
67 ''
68 echo "Testing running the 'hello' binary which should be linked with 'mold'" >&2
69 ${emulator} ${lib.getExe helloMold}
70
71 echo "Checking for mold in the '.comment' section" >&2
72 if output=$(${command} 2>&1); then
73 if grep -Fw -- "mold" - <<< "$output"; then
74 touch $out
75 else
76 echo "No mention of 'mold' detected in the '.comment' section" >&2
77 echo "The command was:" >&2
78 echo "${command}" >&2
79 echo "The output was:" >&2
80 echo "$output" >&2
81 exit 1
82 fi
83 else
84 echo -n "${command}" >&2
85 echo " returned a non-zero exit code." >&2
86 echo "$output" >&2
87 exit 1
88 fi
89 ''
90 ;
91 in
92 {
93 version = testers.testVersion { package = mold; };
94 } // lib.optionalAttrs stdenv.isLinux {
95 adapter-gcc = helloTest "adapter-gcc" (hello.override (old: { stdenv = useMoldLinker gccStdenv; }));
96 adapter-llvm = helloTest "adapter-llvm" (hello.override (old: { stdenv = useMoldLinker clangStdenv; }));
97 wrapped = helloTest "wrapped" (hello.overrideAttrs (previousAttrs: {
98 nativeBuildInputs = (previousAttrs.nativeBuildInputs or [ ]) ++ [ mold-wrapped ];
99 NIX_CFLAGS_LINK = toString (previousAttrs.NIX_CFLAGS_LINK or "") + " -fuse-ld=mold";
100 }));
101 };
102 };
103
104 meta = with lib; {
105 description = "A faster drop-in replacement for existing Unix linkers (unwrapped)";
106 longDescription = ''
107 mold is a faster drop-in replacement for existing Unix linkers. It is
108 several times faster than the LLVM lld linker. mold is designed to
109 increase developer productivity by reducing build time, especially in
110 rapid debug-edit-rebuild cycles.
111 '';
112 homepage = "https://github.com/rui314/mold";
113 changelog = "https://github.com/rui314/mold/releases/tag/v${version}";
114 license = licenses.mit;
115 platforms = platforms.unix;
116 mainProgram = "mold";
117 maintainers = with maintainers; [ azahi nitsky paveloom ];
118 };
119}