1{ lib, stdenv, removeReferencesTo, pkgsBuildBuild, pkgsBuildHost, pkgsBuildTarget
2, llvmShared, llvmSharedForBuild, llvmSharedForHost, llvmSharedForTarget
3, fetchurl, file, python3
4, darwin, cmake, rust, rustPlatform
5, pkg-config, openssl
6, libiconv
7, which, libffi
8, withBundledLLVM ? false
9, enableRustcDev ? true
10, version
11, sha256
12, patches ? []
13}:
14
15let
16 inherit (lib) optionals optional optionalString concatStringsSep;
17 inherit (darwin.apple_sdk.frameworks) Security;
18in stdenv.mkDerivation rec {
19 pname = "rustc";
20 inherit version;
21
22 src = fetchurl {
23 url = "https://static.rust-lang.org/dist/rustc-${version}-src.tar.gz";
24 inherit sha256;
25 };
26
27 __darwinAllowLocalNetworking = true;
28
29 # rustc complains about modified source files otherwise
30 dontUpdateAutotoolsGnuConfigScripts = true;
31
32 # Running the default `strip -S` command on Darwin corrupts the
33 # .rlib files in "lib/".
34 #
35 # See https://github.com/NixOS/nixpkgs/pull/34227
36 #
37 # Running `strip -S` when cross compiling can harm the cross rlibs.
38 # See: https://github.com/NixOS/nixpkgs/pull/56540#issuecomment-471624656
39 stripDebugList = [ "bin" ];
40
41 NIX_LDFLAGS = toString (
42 # when linking stage1 libstd: cc: undefined reference to `__cxa_begin_catch'
43 optional (stdenv.isLinux && !withBundledLLVM) "--push-state --as-needed -lstdc++ --pop-state"
44 ++ optional (stdenv.isDarwin && !withBundledLLVM) "-lc++"
45 ++ optional stdenv.isDarwin "-rpath ${llvmSharedForHost}/lib");
46
47 # Increase codegen units to introduce parallelism within the compiler.
48 RUSTFLAGS = "-Ccodegen-units=10";
49
50 # We need rust to build rust. If we don't provide it, configure will try to download it.
51 # Reference: https://github.com/rust-lang/rust/blob/master/src/bootstrap/configure.py
52 configureFlags = let
53 setBuild = "--set=target.${rust.toRustTarget stdenv.buildPlatform}";
54 setHost = "--set=target.${rust.toRustTarget stdenv.hostPlatform}";
55 setTarget = "--set=target.${rust.toRustTarget stdenv.targetPlatform}";
56 ccForBuild = "${pkgsBuildBuild.targetPackages.stdenv.cc}/bin/${pkgsBuildBuild.targetPackages.stdenv.cc.targetPrefix}cc";
57 cxxForBuild = "${pkgsBuildBuild.targetPackages.stdenv.cc}/bin/${pkgsBuildBuild.targetPackages.stdenv.cc.targetPrefix}c++";
58 ccForHost = "${pkgsBuildHost.targetPackages.stdenv.cc}/bin/${pkgsBuildHost.targetPackages.stdenv.cc.targetPrefix}cc";
59 cxxForHost = "${pkgsBuildHost.targetPackages.stdenv.cc}/bin/${pkgsBuildHost.targetPackages.stdenv.cc.targetPrefix}c++";
60 ccForTarget = "${pkgsBuildTarget.targetPackages.stdenv.cc}/bin/${pkgsBuildTarget.targetPackages.stdenv.cc.targetPrefix}cc";
61 cxxForTarget = "${pkgsBuildTarget.targetPackages.stdenv.cc}/bin/${pkgsBuildTarget.targetPackages.stdenv.cc.targetPrefix}c++";
62 in [
63 "--release-channel=stable"
64 "--set=build.rustc=${rustPlatform.rust.rustc}/bin/rustc"
65 "--set=build.cargo=${rustPlatform.rust.cargo}/bin/cargo"
66 "--enable-rpath"
67 "--enable-vendor"
68 "--build=${rust.toRustTargetSpec stdenv.buildPlatform}"
69 "--host=${rust.toRustTargetSpec stdenv.hostPlatform}"
70 # std is built for all platforms in --target. When building a cross-compiler
71 # we need to add the host platform as well so rustc can compile build.rs
72 # scripts.
73 "--target=${concatStringsSep "," ([
74 (rust.toRustTargetSpec stdenv.targetPlatform)
75 ] ++ optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
76 (rust.toRustTargetSpec stdenv.hostPlatform)
77 ])}"
78
79 "${setBuild}.cc=${ccForBuild}"
80 "${setHost}.cc=${ccForHost}"
81 "${setTarget}.cc=${ccForTarget}"
82
83 "${setBuild}.linker=${ccForBuild}"
84 "${setHost}.linker=${ccForHost}"
85 "${setTarget}.linker=${ccForTarget}"
86
87 "${setBuild}.cxx=${cxxForBuild}"
88 "${setHost}.cxx=${cxxForHost}"
89 "${setTarget}.cxx=${cxxForTarget}"
90 ] ++ optionals (!withBundledLLVM) [
91 "--enable-llvm-link-shared"
92 "${setBuild}.llvm-config=${llvmSharedForBuild.dev}/bin/llvm-config"
93 "${setHost}.llvm-config=${llvmSharedForHost.dev}/bin/llvm-config"
94 "${setTarget}.llvm-config=${llvmSharedForTarget.dev}/bin/llvm-config"
95 ] ++ optionals (stdenv.isLinux && !stdenv.targetPlatform.isRedox) [
96 "--enable-profiler" # build libprofiler_builtins
97 ] ++ optionals stdenv.buildPlatform.isMusl [
98 "${setBuild}.musl-root=${pkgsBuildBuild.targetPackages.stdenv.cc.libc}"
99 ] ++ optionals stdenv.hostPlatform.isMusl [
100 "${setHost}.musl-root=${pkgsBuildHost.targetPackages.stdenv.cc.libc}"
101 ] ++ optionals stdenv.targetPlatform.isMusl [
102 "${setTarget}.musl-root=${pkgsBuildTarget.targetPackages.stdenv.cc.libc}"
103 ];
104
105 # The bootstrap.py will generated a Makefile that then executes the build.
106 # The BOOTSTRAP_ARGS used by this Makefile must include all flags to pass
107 # to the bootstrap builder.
108 postConfigure = ''
109 substituteInPlace Makefile \
110 --replace 'BOOTSTRAP_ARGS :=' 'BOOTSTRAP_ARGS := --jobs $(NIX_BUILD_CORES)'
111 '';
112
113 # the rust build system complains that nix alters the checksums
114 dontFixLibtool = true;
115
116 inherit patches;
117
118 postPatch = ''
119 patchShebangs src/etc
120
121 ${optionalString (!withBundledLLVM) "rm -rf src/llvm"}
122
123 # Fix the configure script to not require curl as we won't use it
124 sed -i configure \
125 -e '/probe_need CFG_CURL curl/d'
126
127 # Useful debugging parameter
128 # export VERBOSE=1
129 '';
130
131 # rustc unfortunately needs cmake to compile llvm-rt but doesn't
132 # use it for the normal build. This disables cmake in Nix.
133 dontUseCmakeConfigure = true;
134
135 nativeBuildInputs = [
136 file python3 rustPlatform.rust.rustc cmake
137 which libffi removeReferencesTo pkg-config
138 ];
139
140 buildInputs = [ openssl ]
141 ++ optionals stdenv.isDarwin [ libiconv Security ]
142 ++ optional (!withBundledLLVM) llvmShared;
143
144 outputs = [ "out" "man" "doc" ];
145 setOutputFlags = false;
146
147 postInstall = lib.optionalString enableRustcDev ''
148 # install rustc-dev components. Necessary to build rls, clippy...
149 python x.py dist rustc-dev
150 tar xf build/dist/rustc-dev*tar.gz
151 cp -r rustc-dev*/rustc-dev*/lib/* $out/lib/
152 rm $out/lib/rustlib/install.log
153 for m in $out/lib/rustlib/manifest-rust*
154 do
155 sort --output=$m < $m
156 done
157
158 '' + ''
159 # remove references to llvm-config in lib/rustlib/x86_64-unknown-linux-gnu/codegen-backends/librustc_codegen_llvm-llvm.so
160 # and thus a transitive dependency on ncurses
161 find $out/lib -name "*.so" -type f -exec remove-references-to -t ${llvmShared} '{}' '+'
162 '';
163
164 configurePlatforms = [];
165
166 # https://github.com/NixOS/nixpkgs/pull/21742#issuecomment-272305764
167 # https://github.com/rust-lang/rust/issues/30181
168 # enableParallelBuilding = false;
169
170 setupHooks = ./setup-hook.sh;
171
172 requiredSystemFeatures = [ "big-parallel" ];
173
174 passthru.llvm = llvmShared;
175
176 meta = with lib; {
177 homepage = "https://www.rust-lang.org/";
178 description = "A safe, concurrent, practical language";
179 maintainers = with maintainers; [ madjar cstrahan globin havvy ];
180 license = [ licenses.mit licenses.asl20 ];
181 platforms = platforms.linux ++ platforms.darwin;
182 };
183}