1{ lib, stdenv
2, fetchurl
3, mrustc
4, mrustc-minicargo
5, rust
6, llvm_7
7, llvmPackages_7
8, libffi
9, cmake
10, python3
11, zlib
12, libxml2
13, openssl
14, pkg-config
15, curl
16, which
17, time
18}:
19
20let
21 rustcVersion = "1.29.0";
22 rustcSrc = fetchurl {
23 url = "https://static.rust-lang.org/dist/rustc-${rustcVersion}-src.tar.gz";
24 sha256 = "1sb15znckj8pc8q3g7cq03pijnida6cg64yqmgiayxkzskzk9sx4";
25 };
26 rustcDir = "rustc-${rustcVersion}-src";
27in
28
29stdenv.mkDerivation rec {
30 pname = "mrustc-bootstrap";
31 version = "${mrustc.version}_${rustcVersion}";
32
33 inherit (mrustc) src;
34 postUnpack = "tar -xf ${rustcSrc} -C source/";
35
36 # the rust build system complains that nix alters the checksums
37 dontFixLibtool = true;
38
39 patches = [
40 ./patches/0001-use-shared-llvm.patch
41 ./patches/0002-dont-build-llvm.patch
42 ./patches/0003-echo-newlines.patch
43 ./patches/0004-increase-parallelism.patch
44 ];
45
46 postPatch = ''
47 echo "applying patch ./rustc-${rustcVersion}-src.patch"
48 patch -p0 -d ${rustcDir}/ < rustc-${rustcVersion}-src.patch
49
50 for p in ${lib.concatStringsSep " " llvmPackages_7.compiler-rt.patches}; do
51 echo "applying patch $p"
52 patch -p1 -d ${rustcDir}/src/libcompiler_builtins/compiler-rt < $p
53 done
54 '';
55
56 # rustc unfortunately needs cmake to compile llvm-rt but doesn't
57 # use it for the normal build. This disables cmake in Nix.
58 dontUseCmakeConfigure = true;
59
60 strictDeps = true;
61 nativeBuildInputs = [
62 cmake
63 mrustc
64 mrustc-minicargo
65 pkg-config
66 python3
67 time
68 which
69 ];
70 buildInputs = [
71 # for rustc
72 llvm_7 libffi zlib libxml2
73 # for cargo
74 openssl curl
75 ];
76
77 makeFlags = [
78 # Use shared mrustc/minicargo/llvm instead of rebuilding them
79 "MRUSTC=${mrustc}/bin/mrustc"
80 "MINICARGO=${mrustc-minicargo}/bin/minicargo"
81 "LLVM_CONFIG=${llvm_7.dev}/bin/llvm-config"
82 "RUSTC_TARGET=${rust.toRustTarget stdenv.targetPlatform}"
83 ];
84
85 buildPhase = ''
86 runHook preBuild
87
88 local flagsArray=(
89 PARLEVEL=$NIX_BUILD_CORES
90 ${toString makeFlags}
91 )
92
93 echo minicargo.mk: libs
94 make -f minicargo.mk "''${flagsArray[@]}" LIBS
95
96 echo minicargo.mk: deps
97 mkdir -p output/cargo-build
98 # minicargo has concurrency issues when running these; let's build them
99 # without parallelism
100 for crate in regex regex-0.2.11 curl-sys
101 do
102 echo "building $crate"
103 minicargo ${rustcDir}/src/vendor/$crate \
104 --vendor-dir ${rustcDir}/src/vendor \
105 --output-dir output/cargo-build -L output/
106 done
107
108 echo minicargo.mk: rustc
109 make -f minicargo.mk "''${flagsArray[@]}" output/rustc
110
111 echo minicargo.mk: cargo
112 make -f minicargo.mk "''${flagsArray[@]}" output/cargo
113
114 echo run_rustc
115 make -C run_rustc "''${flagsArray[@]}"
116
117 unset flagsArray
118
119 runHook postBuild
120 '';
121
122 doCheck = true;
123 checkPhase = ''
124 runHook preCheck
125 run_rustc/output/prefix/bin/hello_world | grep "hello, world"
126 runHook postCheck
127 '';
128
129 installPhase = ''
130 runHook preInstall
131 mkdir -p $out/bin/ $out/lib/
132 cp run_rustc/output/prefix/bin/cargo $out/bin/cargo
133 cp run_rustc/output/prefix/bin/rustc_binary $out/bin/rustc
134
135 cp -r run_rustc/output/prefix/lib/* $out/lib/
136 cp $out/lib/rustlib/${rust.toRustTarget stdenv.targetPlatform}/lib/*.so $out/lib/
137 runHook postInstall
138 '';
139
140 meta = with lib; {
141 inherit (src.meta) homepage;
142 description = "A minimal build of Rust";
143 longDescription = ''
144 A minimal build of Rust, built from source using mrustc.
145 This is useful for bootstrapping the main Rust compiler without
146 an initial binary toolchain download.
147 '';
148 maintainers = with maintainers; [ progval r-burns ];
149 license = with licenses; [ mit asl20 ];
150 platforms = [ "x86_64-linux" ];
151 };
152}
153