1{
2 lib,
3 stdenv,
4 fetchurl,
5 mrustc,
6 mrustc-minicargo,
7 #llvm_12,
8 libffi,
9 cmake,
10 perl,
11 python3,
12 zlib,
13 libxml2,
14 pkg-config,
15 curl,
16 which,
17 time,
18}:
19
20let
21 mrustcTargetVersion = "1.54";
22 rustcVersion = "1.54.0";
23 rustcSrc = fetchurl {
24 url = "https://static.rust-lang.org/dist/rustc-${rustcVersion}-src.tar.gz";
25 sha256 = "0xk9dhfff16caambmwij67zgshd8v9djw6ha0fnnanlv7rii31dc";
26 };
27 rustcDir = "rustc-${rustcVersion}-src";
28 outputDir = "output-${rustcVersion}";
29in
30
31stdenv.mkDerivation rec {
32 pname = "mrustc-bootstrap";
33 version = "${mrustc.version}_${rustcVersion}";
34
35 inherit (mrustc) src;
36 postUnpack = "tar -xf ${rustcSrc} -C source/";
37
38 # the rust build system complains that nix alters the checksums
39 dontFixLibtool = true;
40
41 patches = [
42 ./patches/0001-dont-download-rustc.patch
43 ];
44
45 postPatch = ''
46 echo "applying patch ./rustc-${rustcVersion}-src.patch"
47 patch -p0 -d ${rustcDir}/ < rustc-${rustcVersion}-src.patch
48 '';
49
50 # rustc unfortunately needs cmake to compile llvm-rt but doesn't
51 # use it for the normal build. This disables cmake in Nix.
52 dontUseCmakeConfigure = true;
53
54 strictDeps = true;
55 nativeBuildInputs = [
56 cmake
57 mrustc
58 mrustc-minicargo
59 perl
60 pkg-config
61 python3
62 time
63 which
64 ];
65 buildInputs = [
66 # for rustc
67 #llvm_12
68 libffi
69 zlib
70 libxml2
71 # for cargo
72 curl
73 ];
74
75 makeFlags = [
76 # Use shared mrustc/minicargo/llvm instead of rebuilding them
77 "MRUSTC=${mrustc}/bin/mrustc"
78 #"MINICARGO=${mrustc-minicargo}/bin/minicargo" # FIXME: we need to rebuild minicargo locally so --manifest-overrides is applied
79 #"LLVM_CONFIG=${llvm_12.dev}/bin/llvm-config"
80 "RUSTC_TARGET=${stdenv.targetPlatform.rust.rustcTarget}"
81 ];
82
83 buildPhase = ''
84 runHook preBuild
85
86 local flagsArray=(
87 PARLEVEL=$NIX_BUILD_CORES
88 ${toString makeFlags}
89 )
90
91 touch ${rustcDir}/dl-version
92 export OUTDIR_SUF=-${rustcVersion}
93 export RUSTC_VERSION=${rustcVersion}
94 export MRUSTC_TARGET_VER=${mrustcTargetVersion}
95 export MRUSTC_PATH=${mrustc}/bin/mrustc
96
97 echo minicargo.mk: libs
98 make -f minicargo.mk "''${flagsArray[@]}" LIBS
99
100 echo test
101 make "''${flagsArray[@]}" test
102
103 # disabled because it expects ./bin/mrustc
104 #echo local_tests
105 #make "''${flagsArray[@]}" local_tests
106
107 echo minicargo.mk: rustc
108 make -f minicargo.mk "''${flagsArray[@]}" ${outputDir}/rustc
109
110 echo minicargo.mk: cargo
111 make -f minicargo.mk "''${flagsArray[@]}" ${outputDir}/cargo
112
113 echo run_rustc
114 make -C run_rustc "''${flagsArray[@]}"
115
116 unset flagsArray
117
118 runHook postBuild
119 '';
120
121 doCheck = true;
122 checkPhase = ''
123 runHook preCheck
124 run_rustc/${outputDir}/prefix/bin/hello_world | grep "hello, world"
125 runHook postCheck
126 '';
127
128 installPhase = ''
129 runHook preInstall
130 mkdir -p $out/bin/ $out/lib/
131 cp run_rustc/${outputDir}/prefix/bin/cargo $out/bin/cargo
132 cp run_rustc/${outputDir}/prefix/bin/rustc_binary $out/bin/rustc
133
134 cp -r run_rustc/${outputDir}/prefix/lib/* $out/lib/
135 cp $out/lib/rustlib/${stdenv.targetPlatform.rust.rustcTarget}/lib/*.so $out/lib/
136 runHook postInstall
137 '';
138
139 meta = with lib; {
140 inherit (src.meta) homepage;
141 description = "Minimal build of Rust";
142 longDescription = ''
143 A minimal build of Rust, built from source using mrustc.
144 This is useful for bootstrapping the main Rust compiler without
145 an initial binary toolchain download.
146 '';
147 maintainers = with maintainers; [
148 progval
149 r-burns
150 ];
151 license = with licenses; [
152 mit
153 asl20
154 ];
155 platforms = [ "x86_64-linux" ];
156 # rustc 1.54 only supports LLVM 12, which was removed from Nixpkgs.
157 # mrustc can bootstrap up to rustc 1.74, which supported LLVM 17,
158 # which has also been removed.
159 #
160 # 1.74 also shipped with the Cranelift backend, so perhaps that
161 # could be used instead? Alternatively, it may be possible to
162 # backport the upstream patches to support LLVM 18 to 1.74.
163 # Assuming LLVM 18 is still in Nixpkgs by the time you read this
164 # comment, anyway. But if not, then maybe mrustc has been updated
165 # to support newer rustc versions? Hope springs eternal.
166 #
167 # (Note that you still have to “draw the rest of the owl” to
168 # bootstrap the chain of rustc versions between this bootstrap
169 # and the version currently used in Nixpkgs, anyway, so this was
170 # already not useful for bootstrapping a Rust compiler for use with
171 # Nixpkgs without a lot of additional work. See Guix’s Rust
172 # bootstrap chain, or the non‐Rust minimal bootstrap in Guix and
173 # Nixpkgs, for inspiration.)
174 broken = true;
175 };
176}