Skip to content

Commit 692bdb0

Browse files
authored
Update Rust (#2761)
This PR bumps rustc version and removes xargo. std dependencies are now built with cargo's new `-Zbuild-std` parameter. To work around rust-lang/wg-cargo-std-aware#23, we implement a tool "vendor-rust-std-deps" that reads `Cargo.lock` of a Rust toolchain std and manually vendors all the dependencies. These dependencies are then merged with the Rust RTS dependencies before the building the RTS in nix. RTS README updated with instructions to bump rustc. New rustc will enable more const functions, new API functions, stabilizations, and features for other RTS PRs.
1 parent 6a02e24 commit 692bdb0

File tree

16 files changed

+580
-110
lines changed

16 files changed

+580
-110
lines changed

default.nix

+73-16
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ let
3232
llvmPackages_12.bintools
3333
rustc-nightly
3434
cargo-nightly
35-
xargo
3635
wasmtime
3736
rust-bindgen
37+
python3
38+
] ++ pkgs.lib.optional pkgs.stdenv.isDarwin [
39+
libiconv
3840
];
3941

4042
llvmEnv = ''
@@ -152,46 +154,97 @@ in
152154
rec {
153155
rts =
154156
let
155-
# We run this on motoko-rts-tests, to get the union of all
156-
# dependencies
157-
rustDeps = nixpkgs.rustPlatform-nightly.fetchCargoTarball {
157+
# Build Rust package cargo-vendor-tools
158+
cargoVendorTools = nixpkgs.rustPlatform.buildRustPackage rec {
159+
name = "cargo-vendor-tools";
160+
src = ./rts/cargo-vendor-tools;
161+
cargoSha256 = "0zi3fiq9sy6c9dv7fd2xc9lan85d16gfax47n6g6f5q5c1zb5r47";
162+
};
163+
164+
# Path to vendor-rust-std-deps, provided by cargo-vendor-tools
165+
vendorRustStdDeps = "${cargoVendorTools}/bin/vendor-rust-std-deps";
166+
167+
# SHA256 of Rust std deps
168+
rustStdDepsHash = "0wxx8prh66i19vd5078iky6x5bzs6ppz7c1vbcyx9h4fg0f7pfj6";
169+
170+
# Vendor directory for Rust std deps
171+
rustStdDeps = nixpkgs.stdenvNoCC.mkDerivation {
172+
name = "rustc-std-deps";
173+
174+
nativeBuildInputs = with nixpkgs; [
175+
curl
176+
];
177+
178+
buildCommand = ''
179+
mkdir $out
180+
cd $out
181+
${vendorRustStdDeps} ${nixpkgs.rustc-nightly} .
182+
'';
183+
184+
outputHash = rustStdDepsHash;
185+
outputHashAlgo = "sha256";
186+
outputHashMode = "recursive";
187+
};
188+
189+
# Vendor tarball of the RTS
190+
rtsDeps = nixpkgs.rustPlatform.fetchCargoTarball {
158191
name = "motoko-rts-deps";
159192
src = subpath ./rts;
160193
sourceRoot = "rts/motoko-rts-tests";
161-
sha256 = "0jyp3j8n5bj5cy1fd26d7h55zmc4v14qc2w8adxqwmsv5riqz41g";
194+
sha256 = "129gfmn96vm7di903pxirg7zybl83q6nkwiqr3rsy7l1q8667kxx";
162195
copyLockfile = true;
163196
};
197+
198+
# Unpacked RTS deps
199+
rtsDepsUnpacked = nixpkgs.stdenvNoCC.mkDerivation {
200+
name = rtsDeps.name + "-unpacked";
201+
buildCommand = ''
202+
tar xf ${rtsDeps}
203+
mv *.tar.gz $out
204+
'';
205+
};
206+
207+
# All dependencies needed to build the RTS, including Rust std deps, to
208+
# allow `cargo -Zbuild-std`. (rust-lang/wg-cargo-std-aware#23)
209+
allDeps = nixpkgs.stdenvNoCC.mkDerivation {
210+
name = "merged-rust-deps";
211+
212+
buildCommand = ''
213+
mkdir -p $out
214+
cp -r ${rtsDepsUnpacked}/* $out/
215+
cp -r ${rustStdDeps}/* $out/
216+
'';
217+
};
164218
in
165219

166220
stdenv.mkDerivation {
167221
name = "moc-rts";
168222

169223
src = subpath ./rts;
170-
nativeBuildInputs = [ nixpkgs.makeWrapper nixpkgs.removeReferencesTo ];
224+
225+
nativeBuildInputs = [ nixpkgs.makeWrapper nixpkgs.removeReferencesTo nixpkgs.cacert ];
171226

172227
buildInputs = rtsBuildInputs;
173228

174229
preBuild = ''
175-
export XARGO_HOME=$PWD/xargo-home
176230
export CARGO_HOME=$PWD/cargo-home
177231
178-
# this replicates logic from nixpkgs’ pkgs/build-support/rust/default.nix
232+
# This replicates logic from nixpkgs’ pkgs/build-support/rust/default.nix
179233
mkdir -p $CARGO_HOME
180-
echo "Using vendored sources from ${rustDeps}"
181-
unpackFile ${rustDeps}
234+
echo "Using vendored sources from ${rtsDeps}"
235+
unpackFile ${allDeps}
182236
cat > $CARGO_HOME/config <<__END__
183237
[source."crates-io"]
184238
"replace-with" = "vendored-sources"
185239
186240
[source."vendored-sources"]
187-
"directory" = "$(stripHash ${rustDeps})"
241+
"directory" = "$(stripHash ${allDeps})"
188242
__END__
189243
190244
${llvmEnv}
191245
export TOMMATHSRC=${nixpkgs.sources.libtommath}
192246
export MUSLSRC=${nixpkgs.sources.musl-wasi}/libc-top-half/musl
193247
export MUSL_WASI_SYSROOT=${musl-wasi-sysroot}
194-
195248
'';
196249

197250
doCheck = true;
@@ -206,11 +259,16 @@ rec {
206259
cp mo-rts-debug.wasm $out/rts
207260
'';
208261

209-
# This needs to be self-contained. Remove mention of
210-
# nix path in debug message.
262+
# This needs to be self-contained. Remove mention of nix path in debug
263+
# message.
211264
preFixup = ''
212-
remove-references-to -t ${nixpkgs.rustc-nightly} -t ${rustDeps} $out/rts/mo-rts.wasm $out/rts/mo-rts-debug.wasm
265+
remove-references-to \
266+
-t ${nixpkgs.rustc-nightly} \
267+
-t ${rtsDeps} \
268+
-t ${rustStdDeps} \
269+
$out/rts/mo-rts.wasm $out/rts/mo-rts-debug.wasm
213270
'';
271+
214272
allowedRequisites = [];
215273
};
216274

@@ -440,7 +498,6 @@ rec {
440498

441499
filecheck = nixpkgs.linkFarm "FileCheck"
442500
[ { name = "bin/FileCheck"; path = "${nixpkgs.llvm}/bin/FileCheck";} ];
443-
inherit (nixpkgs) xargo;
444501

445502
# gitMinimal is used by nix/gitSource.nix; building it here warms the nix cache
446503
inherit (nixpkgs) gitMinimal;

nix/default.nix

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let
4646
# Rust nightly
4747
(self: super: let
4848
moz_overlay = import self.sources.nixpkgs-mozilla self super;
49-
rust-channel = moz_overlay.rustChannelOf { date = "2020-07-22"; channel = "nightly"; };
49+
rust-channel = moz_overlay.rustChannelOf { date = "2021-10-25"; channel = "nightly"; };
5050
in rec {
5151
rustc-nightly = rust-channel.rust.override {
5252
targets = [
@@ -62,7 +62,6 @@ let
6262
rustc = rustc-nightly;
6363
cargo = cargo-nightly;
6464
};
65-
xargo = self.callPackage ./xargo.nix {};
6665
})
6766

6867
# wasm-profiler

nix/xargo.nix

-31
This file was deleted.

rts/Makefile

+2-3
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ $(TOMMATH_BINDINGS_RS): | _build
223223

224224

225225
$(RTS_RUST_WASM_A): $(TOMMATH_BINDINGS_RS) $(RTS_RUST_FILES) $(RTS_CARGO_FILES) | _build/wasm
226-
cd motoko-rts && xargo build --release --target=wasm32-unknown-emscripten
226+
cd motoko-rts && cargo build --release --target=wasm32-unknown-emscripten -Zbuild-std=core,alloc
227227
cp motoko-rts/target/wasm32-unknown-emscripten/release/libmotoko_rts.a $@
228228

229229
$(RTS_RUST_DEBUG_WASM_A): $(TOMMATH_BINDINGS_RS) $(RTS_RUST_FILES) $(RTS_CARGO_FILES) | _build/wasm
230-
cd motoko-rts && xargo build --target=wasm32-unknown-emscripten
230+
cd motoko-rts && cargo build --target=wasm32-unknown-emscripten -Zbuild-std=core,alloc
231231
cp motoko-rts/target/wasm32-unknown-emscripten/debug/libmotoko_rts.a $@
232232

233233
#
@@ -286,5 +286,4 @@ clean:
286286
mo-rts-debug.wasm \
287287
motoko-rts/target \
288288
motoko-rts-tests/target \
289-
motoko-rts/xargo-home \
290289
motoko-rts/cargo-home

rts/README.md

+18-13
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,36 @@ See `motoko-rts/src/bigint.rs` for the technical details.
5858
Rust build
5959
----------
6060

61-
The Rust parts are built from `motoko-rts`, using `xargo` and `cargo`.
61+
To build Motoko RTS in nix we need pre-fetch Rust dependencies. This works in
62+
`nix-build` by:
6263

63-
To build this in nix, we need pre-fetch some dependencies (currently
64-
`compiler_builtins` and `libc`). This works in `nix-build` by:
65-
66-
* Adding`compiler_builtins` as a dependency in `Cargo.toml` (even though not
67-
needed), so that it shows up with a hash in `Cargo.lock`
68-
69-
* Building a directory with vendored sources in `default.nix` (see `rustDeps`)
64+
* Building a directory with vendored sources in `default.nix`
7065

7166
* Configuring `cargo` to use that vendored directory (see `preBuild`)
7267

7368
If you change dependencies (e.g. bump versions, add more crates),
7469

75-
1. Add them to `Cargo.toml`
76-
2. Make sure that `Cargo.lock` is up to date
77-
3. In `default.nix`, invalidate the `sha256` of `rustDeps` (e.g. change one
70+
1. Make sure that `motoko-rts-tests/Cargo.lock` is up to date. This can be
71+
done by running `cargo build --target=wasm32-wasi` in `motoko-rts-tests/`
72+
directory.
73+
2. In `default.nix`, invalidate the `sha256` of `rtsDeps` (e.g. change one
7874
character)
79-
4. Run `nix-build -A rts`. You should get an error message about the actual
75+
3. Run `nix-build -A rts`. You should get an error message about the actual
8076
checksum.
81-
5. Set that as `sha256` of `rustDeps` in `default.nix`
77+
4. Set that as `sha256` of `rtsDeps` in `default.nix`
8278

8379
Warning: nix will happily use a stale version of the dependencies if you do not
8480
do step 3.
8581

82+
**Updating rustc**:
83+
84+
1. Update Rust version in `nix/default.nix`, in the line with
85+
`moz_overlay.rustChannelOf { ... }`.
86+
2. Invalidate `rustStdDepsHash` in `default.nix`.
87+
3. Run `nix-build -A rts`. You should get an error message about the expected
88+
value of `rustStdDepsHash`.
89+
4. Update `rustStdDepsHash` with the expected value in the error message.
90+
8691
Running RTS tests
8792
-----------------
8893

0 commit comments

Comments
 (0)