1{ stdenv, targetPackages, removeReferencesTo
2, fetchurl, fetchgit, fetchzip, file, python2, tzdata, ps
3, llvm, ncurses, darwin, rustPlatform, git, cmake, curl
4, which, libffi, gdb
5, version
6, withBundledLLVM ? 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 =
44 # when linking stage1 libstd: cc: undefined reference to `__cxa_begin_catch'
45 optional (stdenv.isLinux && !withBundledLLVM) "--push-state --as-needed -lstdc++ --pop-state"
46 ++ optional (stdenv.isDarwin && !withBundledLLVM) "-lc++"
47 ++ optional stdenv.isDarwin "-rpath ${llvmShared}/lib";
48
49 # Enable nightly features in stable compiles (used for
50 # bootstrapping, see https://github.com/rust-lang/rust/pull/37265).
51 # This loosens the hard restrictions on bootstrapping-compiler
52 # versions.
53 RUSTC_BOOTSTRAP = "1";
54
55 # Increase codegen units to introduce parallelism within the compiler.
56 RUSTFLAGS = "-Ccodegen-units=10";
57
58 # We need rust to build rust. If we don't provide it, configure will try to download it.
59 # Reference: https://github.com/rust-lang/rust/blob/master/src/bootstrap/configure.py
60 configureFlags = configureFlags
61 ++ [ "--enable-local-rust" "--local-rust-root=${rustPlatform.rust.rustc}" "--enable-rpath"
62 "--enable-vendor"
63 "--default-linker=${targetPackages.stdenv.cc}/bin/cc" ]
64 ++ optional (!withBundledLLVM) [ "--enable-llvm-link-shared" "--llvm-root=${llvmShared}" ]
65 ++ optional (targets != []) "--target=${target}";
66
67 # The bootstrap.py will generated a Makefile that then executes the build.
68 # The BOOTSTRAP_ARGS used by this Makefile must include all flags to pass
69 # to the bootstrap builder.
70 postConfigure = ''
71 substituteInPlace Makefile --replace 'BOOTSTRAP_ARGS :=' 'BOOTSTRAP_ARGS := --jobs $(NIX_BUILD_CORES)'
72 '';
73
74 patches = patches ++ targetPatches;
75
76 # the rust build system complains that nix alters the checksums
77 dontFixLibtool = true;
78
79 passthru.target = target;
80
81 postPatch = ''
82 patchShebangs src/etc
83
84 ${optionalString (!withBundledLLVM) ''rm -rf src/llvm''}
85
86 # Fix the configure script to not require curl as we won't use it
87 sed -i configure \
88 -e '/probe_need CFG_CURL curl/d'
89
90 # On Hydra: `TcpListener::bind(&addr)`: Address already in use (os error 98)'
91 sed '/^ *fn fast_rebind()/i#[ignore]' -i src/libstd/net/tcp.rs
92
93 # https://github.com/rust-lang/rust/issues/39522
94 echo removing gdb-version-sensitive tests...
95 find src/test/debuginfo -type f -execdir grep -q ignore-gdb-version '{}' \; -print -delete
96 rm src/test/debuginfo/{borrowed-c-style-enum.rs,c-style-enum-in-composite.rs,gdb-pretty-struct-and-enums.rs,generic-enum-with-different-disr-sizes.rs}
97
98 # Useful debugging parameter
99 # export VERBOSE=1
100 '' + optionalString stdenv.isDarwin ''
101 # Disable all lldb tests.
102 # error: Can't run LLDB test because LLDB's python path is not set
103 rm -vr src/test/debuginfo/*
104 rm -v src/test/run-pass/backtrace-debuginfo.rs || true
105
106 # error: No such file or directory
107 rm -v src/test/ui/run-pass/issues/issue-45731.rs || true
108
109 # Disable tests that fail when sandboxing is enabled.
110 substituteInPlace src/libstd/sys/unix/ext/net.rs \
111 --replace '#[test]' '#[test] #[ignore]'
112 substituteInPlace src/test/run-pass/env-home-dir.rs \
113 --replace 'home_dir().is_some()' true
114 rm -v src/test/run-pass/fds-are-cloexec.rs || true # FIXME: pipes?
115 rm -v src/test/ui/run-pass/threads-sendsync/sync-send-in-std.rs || true # FIXME: ???
116 '';
117
118 # rustc unfortunately need cmake for compiling llvm-rt but doesn't
119 # use it for the normal build. This disables cmake in Nix.
120 dontUseCmakeConfigure = true;
121
122 # ps is needed for one of the test cases
123 nativeBuildInputs =
124 [ file python2 ps rustPlatform.rust.rustc git cmake
125 which libffi removeReferencesTo
126 ]
127 # Only needed for the debuginfo tests
128 ++ optional (!stdenv.isDarwin) gdb;
129
130 buildInputs = targetToolchains
131 ++ optional stdenv.isDarwin Security
132 ++ optional (!withBundledLLVM) llvmShared;
133
134 outputs = [ "out" "man" "doc" ];
135 setOutputFlags = false;
136
137 # Disable codegen units and hardening for the tests.
138 preCheck = ''
139 export RUSTFLAGS=
140 export TZDIR=${tzdata}/share/zoneinfo
141 export hardeningDisable=all
142 '' +
143 # Ensure TMPDIR is set, and disable a test that removing the HOME
144 # variable from the environment falls back to another home
145 # directory.
146 optionalString stdenv.isDarwin ''
147 export TMPDIR=/tmp
148 sed -i '28s/home_dir().is_some()/true/' ./src/test/run-pass/env-home-dir.rs
149 '';
150
151 inherit doCheck;
152
153 # remove references to llvm-config in lib/rustlib/x86_64-unknown-linux-gnu/codegen-backends/librustc_codegen_llvm-llvm.so
154 # and thus a transitive dependency on ncurses
155 postInstall = ''
156 find $out/lib -name "*.so" -type f -exec remove-references-to -t ${llvmShared} '{}' '+'
157 '';
158
159 configurePlatforms = [];
160
161 # https://github.com/NixOS/nixpkgs/pull/21742#issuecomment-272305764
162 # https://github.com/rust-lang/rust/issues/30181
163 # enableParallelBuilding = false;
164
165 requiredSystemFeatures = [ "big-parallel" ];
166
167 meta = with stdenv.lib; {
168 homepage = https://www.rust-lang.org/;
169 description = "A safe, concurrent, practical language";
170 maintainers = with maintainers; [ madjar cstrahan wizeman globin havvy ];
171 license = [ licenses.mit licenses.asl20 ];
172 platforms = platforms.linux ++ platforms.darwin;
173 broken = broken;
174 };
175}