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