Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build rustc from source, fix a few issues with the RTS build system #2519

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 76 additions & 12 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,83 @@ let ic-ref = ic-ref-pkgs.ic-ref; in
let haskellPackages = nixpkgs.haskellPackages.override {
overrides = import nix/haskell-packages.nix nixpkgs subpath;
}; in

let
rustc-custom = stdenv.mkDerivation rec {
name = "rustc-custom";
rev = "e327a823d8b64e294e490efeca7829f383aa119d";
src = nixpkgs.fetchgit {
url = "https://github.com/rust-lang/rust";
rev = "e327a823d8b64e294e490efeca7829f383aa119d";
sha256 = "1jw64zdf0ikvgnxn72rpbl548s5wzgbc47sx920944872651b7bl";
};

buildInputs = [
nixpkgs.llvm_10
nixpkgs.python3
nixpkgs.curl
nixpkgs.cacert
nixpkgs.cmake
nixpkgs.ninja
nixpkgs.libxml2
nixpkgs.openssl
nixpkgs.pkgconfig
# https://github.com/sfackler/rust-openssl/issues/948
nixpkgs.libiconv
];

configurePhase = ''
echo -n "Using llvm-config version "
${nixpkgs.llvm_10}/bin/llvm-config --version

{
echo 'changelog-seen = 2'

echo '[build]'
echo 'extended = true'
echo 'tools = ["cargo", "src"]'

echo '[target.x86_64-unknown-linux-gnu]'
echo 'llvm-config = "${nixpkgs.llvm_10}/bin/llvm-config"'

echo '[install]'
echo "prefix = \"$out\""
echo "sysconfdir = \"$out/etc\""
} > config.toml

echo "config.toml contents:"
echo "-------------------------------------------------------------------"
cat config.toml
echo "-------------------------------------------------------------------"
'';

buildPhase = ''
mkdir home
export HOME=$PWD/home
patchShebangs .
./x.py build
'';

installPhase = ''
./x.py install
'';
};
in

let
rtsBuildInputs = with nixpkgs; [
clang_10 # for native/wasm building
lld_10 # for wasm building
llvmPackages_10.bintools
rustc-nightly
cargo-nightly
xargo
wasmtime
rust-bindgen
rustfmt
rtsBuildInputs = [
nixpkgs.clang_10 # for native/wasm building
nixpkgs.lld_10 # for wasm building
nixpkgs.llvmPackages_10.bintools
nixpkgs.wasmtime
nixpkgs.rust-bindgen

rustc-custom

# For some reason we can't build rustfmt component in the rustc-custom
# derivation above (probably a bug in the Rust repo) so we use the one from
# nixpkgs
nixpkgs.rustfmt
];

llvmEnv = ''
Expand Down Expand Up @@ -152,7 +218,6 @@ rec {
buildInputs = rtsBuildInputs;

preBuild = ''
export XARGO_HOME=$PWD/xargo-home
export CARGO_HOME=$PWD/cargo-home

# this replicates logic from nixpkgs’ pkgs/build-support/rust/default.nix
Expand Down Expand Up @@ -414,7 +479,6 @@ rec {
inherit (nixpkgs) wabt wasmtime wasm;
filecheck = nixpkgs.linkFarm "FileCheck"
[ { name = "bin/FileCheck"; path = "${nixpkgs.llvm}/bin/FileCheck";} ];
inherit (nixpkgs) xargo;

# gitMinimal is used by nix/gitSource.nix; building it here warms the nix cache
inherit (nixpkgs) gitMinimal;
Expand Down
1 change: 0 additions & 1 deletion nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ let
rustc = rustc-nightly;
cargo = cargo-nightly;
};
xargo = self.callPackage ./xargo.nix {};
})

# wasm-profiler
Expand Down
36 changes: 0 additions & 36 deletions nix/xargo.nix

This file was deleted.

7 changes: 3 additions & 4 deletions rts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ $(TOMMATH_BINDINGS_RS): | _build


$(RTS_RUST_WASM_A): $(TOMMATH_BINDINGS_RS) $(wildcard motoko-rts/src/*.rs) | _build/wasm
cd motoko-rts && xargo build --release --target=wasm32-unknown-emscripten
cd motoko-rts && cargo build --release --target=wasm32-unknown-emscripten -Z build-std=core
cp motoko-rts/target/wasm32-unknown-emscripten/release/libmotoko_rts.a $@

$(RTS_RUST_DEBUG_WASM_A): $(TOMMATH_BINDINGS_RS) $(wildcard motoko-rts/src/*.rs) | _build/wasm
cd motoko-rts && xargo build --target=wasm32-unknown-emscripten
cd motoko-rts && cargo build --target=wasm32-unknown-emscripten -Z build-std=core
cp motoko-rts/target/wasm32-unknown-emscripten/debug/libmotoko_rts.a $@

#
Expand All @@ -214,7 +214,7 @@ $(RTS_RUST_DEBUG_WASM_A): $(TOMMATH_BINDINGS_RS) $(wildcard motoko-rts/src/*.rs)

.PHONY: test
test: $(TOMMATH_WASM_A) $(TOMMATH_BINDINGS_RS)
cd motoko-rts-tests && cargo build --target=wasm32-wasi
cd motoko-rts-tests && cargo build --target=wasm32-wasi -Z build-std=core
wasmtime --disable-cache --cranelift motoko-rts-tests/target/wasm32-wasi/debug/motoko-rts-tests.wasm

#
Expand Down Expand Up @@ -259,5 +259,4 @@ clean:
mo-rts-debug.wasm \
motoko-rts/target \
motoko-rts-tests/target \
motoko-rts/xargo-home \
motoko-rts/cargo-home
2 changes: 1 addition & 1 deletion rts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ See `motoko-rts/src/bigint.rs` for the technical details.
Rust build
----------

The Rust parts are built from `motoko-rts`, using `xargo` and `cargo`.
The Rust parts are built from `motoko-rts`, using `cargo`.

To build this in nix, we need pre-fetch some dependencies (currently
`compiler_builtins` and `libc`). This works in `nix-build` by:
Expand Down
7 changes: 1 addition & 6 deletions rts/motoko-rts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions rts/motoko-rts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,6 @@ panic_handler = []
[dependencies]
libc = { version = "0.2.81", default_features = false }

# Added here so that it ends up in Cargo.lock, so that nix will pre-fetch it
[dependencies.compiler_builtins]
version = "0.1.39"
# Without this feature we get dozens of duplicate symbol errors when generating
# the final shared .wasm file:
#
# wasm-ld: error: duplicate symbol: __multi3
# >>> defined in _build/wasm/libmotoko_rts.a(compiler_builtins-d709bd899857aa61.compiler_builtins.3abndchk-cgu.0.rcgu.o)
# >>> defined in _build/wasm/libmotoko_rts.a(compiler_builtins-06d1ead628e1f468.compiler_builtins.6moz1ltd-cgu.0.rcgu.o)
#
# It seems like we're linking multiple versions of compiler_builtins in the same
# shared library, which we should fix at some point. TODO
features = ["mangled-names"]

[profile.dev]
panic = "abort"

Expand Down
6 changes: 0 additions & 6 deletions rts/motoko-rts/Xargo.toml

This file was deleted.

4 changes: 0 additions & 4 deletions rts/motoko-rts/native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ path = "../src/lib.rs"
[dependencies]
libc = { version = "0.2.73", default_features = false }

[dependencies.compiler_builtins]
version = "0.1.39"
features = ["mangled-names"]

[profile.dev]
panic = "abort"

Expand Down