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