1{ stdenv, targetPackages
2, fetchurl, fetchgit, fetchzip, file, python2, tzdata, ps
3, llvm, jemalloc, ncurses, darwin, rustPlatform, git, cmake, curl
4, which, libffi, gdb
5, version
6, forceBundledLLVM ? false
7, src
8, configureFlags ? []
9, patches
10, targets
11, targetPatches
12, targetToolchains
13, doCheck ? true
14, broken ? false
15}:
16
17let
18 inherit (stdenv.lib) optional optionalString;
19 inherit (darwin.apple_sdk.frameworks) Security;
20
21 llvmShared = llvm.override { enableSharedLibraries = true; };
22
23 target = builtins.replaceStrings [" "] [","] (builtins.toString targets);
24in
25
26stdenv.mkDerivation {
27 name = "rustc-${version}";
28 inherit version;
29
30 inherit src;
31
32 __darwinAllowLocalNetworking = true;
33
34 # rustc complains about modified source files otherwise
35 dontUpdateAutotoolsGnuConfigScripts = true;
36
37 # Running the default `strip -S` command on Darwin corrupts the
38 # .rlib files in "lib/".
39 #
40 # See https://github.com/NixOS/nixpkgs/pull/34227
41 stripDebugList = if stdenv.isDarwin then [ "bin" ] else null;
42
43 NIX_LDFLAGS = optionalString stdenv.isDarwin "-rpath ${llvmShared}/lib";
44
45 # Enable nightly features in stable compiles (used for
46 # bootstrapping, see https://github.com/rust-lang/rust/pull/37265).
47 # This loosens the hard restrictions on bootstrapping-compiler
48 # versions.
49 RUSTC_BOOTSTRAP = "1";
50
51 # Increase codegen units to introduce parallelism within the compiler.
52 RUSTFLAGS = "-Ccodegen-units=10";
53
54 # We need rust to build rust. If we don't provide it, configure will try to download it.
55 # Reference: https://github.com/rust-lang/rust/blob/master/src/bootstrap/configure.py
56 configureFlags = configureFlags
57 ++ [ "--enable-local-rust" "--local-rust-root=${rustPlatform.rust.rustc}" "--enable-rpath" ]
58 ++ [ "--enable-vendor" ]
59 # ++ [ "--jemalloc-root=${jemalloc}/lib"
60 ++ [ "--default-linker=${targetPackages.stdenv.cc}/bin/cc" ]
61 ++ optional (!forceBundledLLVM) [ "--enable-llvm-link-shared" ]
62 ++ optional (targets != []) "--target=${target}"
63 ++ optional (!forceBundledLLVM) "--llvm-root=${llvmShared}";
64
65 # The bootstrap.py will generated a Makefile that then executes the build.
66 # The BOOTSTRAP_ARGS used by this Makefile must include all flags to pass
67 # to the bootstrap builder.
68 postConfigure = ''
69 substituteInPlace Makefile --replace 'BOOTSTRAP_ARGS :=' 'BOOTSTRAP_ARGS := --jobs $(NIX_BUILD_CORES)'
70 '';
71
72 patches = patches ++ targetPatches;
73
74 # the rust build system complains that nix alters the checksums
75 dontFixLibtool = true;
76
77 passthru.target = target;
78
79 postPatch = ''
80 patchShebangs src/etc
81
82 # Fix dynamic linking against llvm
83 #${optionalString (!forceBundledLLVM) ''sed -i 's/, kind = \\"static\\"//g' src/etc/mklldeps.py''}
84
85 # Fix the configure script to not require curl as we won't use it
86 sed -i configure \
87 -e '/probe_need CFG_CURL curl/d'
88
89 # Fix the use of jemalloc prefixes which our jemalloc doesn't have
90 # TODO: reenable if we can figure out how to get our jemalloc to work
91 #[ -f src/liballoc_jemalloc/lib.rs ] && sed -i 's,je_,,g' src/liballoc_jemalloc/lib.rs
92 #[ -f src/liballoc/heap.rs ] && sed -i 's,je_,,g' src/liballoc/heap.rs # Remove for 1.4.0+
93
94 # Disable fragile tests.
95 rm -vr src/test/run-make/linker-output-non-utf8 || true
96 rm -vr src/test/run-make/issue-26092 || true
97
98 # Remove test targeted at LLVM 3.9 - https://github.com/rust-lang/rust/issues/36835
99 rm -vr src/test/run-pass/issue-36023.rs || true
100
101 # Disable test getting stuck on hydra - possible fix:
102 # https://reviews.llvm.org/rL281650
103 rm -vr src/test/run-pass/issue-36474.rs || true
104
105 # On Hydra: `TcpListener::bind(&addr)`: Address already in use (os error 98)'
106 sed '/^ *fn fast_rebind()/i#[ignore]' -i src/libstd/net/tcp.rs
107
108 # Useful debugging parameter
109 # export VERBOSE=1
110 '' + optionalString stdenv.isDarwin ''
111 # Disable all lldb tests.
112 # error: Can't run LLDB test because LLDB's python path is not set
113 rm -vr src/test/debuginfo/*
114 rm -v src/test/run-pass/backtrace-debuginfo.rs
115
116 # error: No such file or directory
117 rm -v src/test/run-pass/issue-45731.rs
118
119 # Disable tests that fail when sandboxing is enabled.
120 substituteInPlace src/libstd/sys/unix/ext/net.rs \
121 --replace '#[test]' '#[test] #[ignore]'
122 substituteInPlace src/test/run-pass/env-home-dir.rs \
123 --replace 'home_dir().is_some()' true
124 rm -v src/test/run-pass/fds-are-cloexec.rs # FIXME: pipes?
125 rm -v src/test/run-pass/sync-send-in-std.rs # FIXME: ???
126 '';
127
128 # rustc unfortunately need cmake for compiling llvm-rt but doesn't
129 # use it for the normal build. This disables cmake in Nix.
130 dontUseCmakeConfigure = true;
131
132 # ps is needed for one of the test cases
133 nativeBuildInputs =
134 [ file python2 ps rustPlatform.rust.rustc git cmake
135 which libffi
136 ]
137 # Only needed for the debuginfo tests
138 ++ optional (!stdenv.isDarwin) gdb;
139
140 buildInputs = [ ncurses ] ++ targetToolchains
141 ++ optional stdenv.isDarwin Security
142 ++ optional (!forceBundledLLVM) llvmShared;
143
144 outputs = [ "out" "man" "doc" ];
145 setOutputFlags = false;
146
147 # Disable codegen units and hardening for the tests.
148 preCheck = ''
149 export RUSTFLAGS=
150 export TZDIR=${tzdata}/share/zoneinfo
151 export hardeningDisable=all
152 '' +
153 # Ensure TMPDIR is set, and disable a test that removing the HOME
154 # variable from the environment falls back to another home
155 # directory.
156 optionalString stdenv.isDarwin ''
157 export TMPDIR=/tmp
158 sed -i '28s/home_dir().is_some()/true/' ./src/test/run-pass/env-home-dir.rs
159 '';
160
161 inherit doCheck;
162
163 configurePlatforms = [];
164
165 # https://github.com/NixOS/nixpkgs/pull/21742#issuecomment-272305764
166 # https://github.com/rust-lang/rust/issues/30181
167 # enableParallelBuilding = false;
168
169 meta = with stdenv.lib; {
170 homepage = https://www.rust-lang.org/;
171 description = "A safe, concurrent, practical language";
172 maintainers = with maintainers; [ madjar cstrahan wizeman globin havvy wkennington ];
173 license = [ licenses.mit licenses.asl20 ];
174 platforms = platforms.linux ++ platforms.darwin;
175 broken = broken;
176 };
177}