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

Conversation

osa1
Copy link
Contributor

@osa1 osa1 commented May 4, 2021

With this PR we build rustc and cargo from the source, with the LLVM version we
support in the linker and we use to build stubs.c (LLVM 10).

The main problem this fixes is: we can now update rustc without having to worry
about rustc using a different LLVM version than

  • what we support in the linker
  • what we use to build stubs.c

and breaking linking, or worse.

In addition, newer Rusts support building core, std, and alloc crates, so we
also ditch Xargo and use cargo (fixes #2251).

Not immediately needed, but having more control over how we build the compiler
might also be useful in the future. The previous derivation would download
pre-build nightly compiler from Rust's binary distribution site. We now build
it from source (including cargo, but not rustfmt).

Current status

I can enter nix-shell, build the RTS. All tests pass. nix-build doesn't work yet.

TODOs and questions

  • I can't build rustfmt in this derivation. This should be a bug in the Rust
    repo, but I'm not sure. I should try different commits to see if any of them
    is able to build rustfmt.

  • nix-build fails with

    cd motoko-rts && cargo build --release --target=wasm32-unknown-emscripten -Z build-std
    error: no matching package named `hashbrown` found
    location searched: registry `https://github.com/rust-lang/crates.io-index`
    required by package `std v0.0.0 (/nix/store/5yi0lk43ffjxg3s7z4gqx8sbwyf9yiww-rustc-custom/lib/rustlib/src/rust/library/std)`
    make: *** [Makefile:204: _build/wasm/libmotoko_rts.a] Error 101
    builder for '/nix/store/c0x9hwg9wr2yjq676mkw694amb0290ds-moc-rts.drv' failed with exit code 2
    error: build of '/nix/store/0fxy4wvdb5vw79pmasry2g364wlfsdr2-test-profiling-graphs.drv', '/nix/store/41qsl7rivm7l6mcpmdnjc0fclyq2mjxg-test-perf.drv', '/nix/store/579kr5cqfh6p93wzl1983dlqi9vzy64l-guid-examples-tc.drv', '/nix/store/59mwp4wpv3wscbfx9d61vlva6dxrhjhh-moc.drv', '/nix/store/5hdlb1racc0zxbxj17zylm541f9nfy1b-base-tests.drv', '/nix/store/63s4jnxdiw57x8dj1pv37kvrpfawq3l2-moc.js.drv', '/nix/store/6b6dv0dkh1mkq29rxl7975iaz8awi50v-test-drun.drv', '/nix/store/736dnsnk6yshmgljgwfpah7wwa928rkn-test-run-deser.drv', '/nix/store/767n9lzcf81mkx6riasdfp7dnkh4wwrb-test-lsp.drv', '/nix/store/9xl33g3nzx0v5gqh5193vvqj7bh8v8jq-didc.js.drv', '/nix/store/c0x9hwg9wr2yjq676mkw694amb0290ds-moc-rts.drv', '/nix/store/cavkqv9mfng9g5ma1bb82izavq9m8qc7-samples.drv', '/nix/store/cf5i8c4x364c89w5vvfrf9fmqd8ih6jp-test-repl.drv', '/nix/store/csvzraidbcyc8n3q8q5mp8h4vgm115al-test-ic-ref-run.drv', '/nix/store/g9jn81gr62bzfvalalqad38hnj37m2nn-docs.drv', '/nix/store/gvsrmvkxiba9yps18vjx530amjcma0sx-test-mo-idl.drv', '/nix/store/gziz1ic4rpgz33vmsm8bm8793a7mkdh4-moc_interpreter.js.drv', '/nix/store/j061hf4ds3gs8in77h126bck154j1mfc-test-trap.drv', '/nix/store/jalrjypjcncy0l5lh1rqpwnbz5f65wbx-test-fail.drv', '/nix/store/md0jn7k7hhk94dbynfd71m839x9lnr40-test-run-dbg.drv', '/nix/store/n231c9pvqq145qgwsrns5rjvva5ciw5b-test-candid.drv',
    '/nix/store/n9x9bhj7p29r8aj23585ayrkc5m8i3lq-test-qc.drv', '/nix/store/qwb5krsbp5w6zb66mxws2cfgdfhw13h4-all-systems-go.drv', '/nix/store/s7p802wicv4wkvfi2d3f4ii5v74907ni-test-ld.drv', '/nix/store/x7p7kp3p67hiyb27z41nanfijv2mc0la-test-drun-dbg.drv', '/nix/store/z5kp8nmmdbrg0d3dldjzcb4x3br03461-test-run.drv' failed
    

    I think I need to update one of the Cargo.lock files, but I'm not sure how.

  • To make sure we only use one version of Rust toolchain we should update
    nixpkgs.rustPlatform-nightly to use the toolchain we've built. Currently we
    use different toolchain for the vendoring the RTS dependencies and building
    the RTS.

@osa1 osa1 requested a review from nomeata May 4, 2021 09:32
@nomeata
Copy link
Collaborator

nomeata commented May 4, 2021

Is it ok if I have a closer look possibly only after launch?

@osa1
Copy link
Contributor Author

osa1 commented May 4, 2021

Is it ok if I have a closer look possibly only after launch?

Sure, no rush.

@osa1
Copy link
Contributor Author

osa1 commented May 5, 2021

I asked some questions about this in Rust user forum: https://users.rust-lang.org/t/cargo-vendor-and-zbuild-std-dont-work-together/59388

@nomeata
Copy link
Collaborator

nomeata commented May 17, 2021

Having a brief look, but here the build fails with

failed to run: curl -s -y 30 -Y 10 --connect-timeout 30 --retry 3 -Sf -o /build/tmpx97yropf.sha256 https://static.rust-lang.org/dist/2021-04-07/rust-std-beta-x86_64-unknown-linux-gnu.tar.xz.sha256

It looks like there is a build step that tries to download code (which isn’t kosher in a hermetic build environment)?

@nomeata
Copy link
Collaborator

nomeata commented May 17, 2021

Ah, that is rust trying to download a compiled rust compiler to bootstrap.

Building a compiler isn’t trivial, I have doubts if “let’s write our own derivation” will be worth it – just look at https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/compilers/rust/rustc.nix, we’d probably rediscover the reason and solution for most of the logic there

@nomeata
Copy link
Collaborator

nomeata commented May 17, 2021

I wonder if with NixOS/nixpkgs@5a923e5 we can actually use a the rustc from plain nixpkgs here. But that requires bumping to nixpkgs master. Which we might do on a branch anyways, to get latest stuff.

nomeata added a commit that referenced this pull request May 17, 2021
to see what breaks, and also to get
NixOS/nixpkgs@5a923e5
which may allow us to use `rustc` from `nixpkgs` again (see #2519)
@nomeata nomeata mentioned this pull request May 17, 2021
9 tasks
@osa1
Copy link
Contributor Author

osa1 commented May 25, 2021

Building a compiler isn’t trivial, I have doubts if “let’s write our own derivation” will be worth it

Just updating the compiler is easy, but I also want to make sure we will always use LLVM 10 (which is the version we can deal with in our linker). Anything else will cause compile or runtime errors.

I just checked the rustc.nix you linked. If I'm reading it right, https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/compilers/rust/rustc.nix#L2 these parameters allow linking rustc with a given LLVM, so this derivation should be good enough.

@nomeata
Copy link
Collaborator

nomeata commented May 25, 2021

I also want to make sure we will always use LLVM 10 (which is the version we can deal with in our linker). Anything else will cause compile or runtime errors.

What breaks with later versions with our linker?

@osa1
Copy link
Contributor Author

osa1 commented May 25, 2021

mo-ld can only link code generated by LLVM 10. Any other version will cause problems in compile time (maybe in runtime too, not sure). I tried with LLVM 11 (used by recent versions of rustc) and mo-ld was unable to link it. I don't remember the error message now.

@nomeata
Copy link
Collaborator

nomeata commented May 25, 2021

Well, maybe it’s easier fixing our tools to keep up than work hard to build against old versions? I noticed that there are more subsections to the names custom section with later LLVMs, and https://github.com/dfinity/motoko/pull/2532/files#diff-97b65fbcedab5db322d011881a04c834ce6821095543806ae3985118294d05dd works around that. If that’s all it takes, then isn’t that preferrable?

@osa1
Copy link
Contributor Author

osa1 commented May 25, 2021

maybe it’s easier fixing our tools to keep up than work hard to build against old versions

This stuff is not even documented. It was basically trial and error + reverse engineering to make it link LLVM 10 generated code. Until Wasm has a better (standard, documented) linking story I don't think we will be able to keep up with LLVM releases.

@osa1
Copy link
Contributor Author

osa1 commented May 25, 2021

If that’s all it takes

I don't remember if the issue was names section. I think I should have a comment in an issue/PR that shows at least some of the errors, but not sure where..

@nomeata
Copy link
Collaborator

nomeata commented May 25, 2021

This stuff is not even documented.

It’s not that bad: We are implementing https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md, so there is documentation.

In my experiments I was often facing problems with wasm-ld failing to (statically) link the .o files to a shared object. But that is a problem independent of our mo-ld (which then does dynamic linking)

mergify bot pushed a commit that referenced this pull request May 28, 2021
to see what breaks, and also to get
NixOS/nixpkgs@5a923e5
which may allow us to use `rustc` from `nixpkgs` again (see #2519)

Issues encountered:

 * [x] in nixpks, various `cargoSHA256` were wrong. (NixOS/nixpkgs#123522, NixOS/nixpkgs#123348, NixOS/nixpkgs#123349)

 * [x] Ocaml built with musl, used for static builds, is currently broken. 
        
    Reported at NixOS/nixpkgs#124476.

    Found a possible fix, submitted at NixOS/nixpkgs#124498 and currently included in this branch.
 * [x] New ocamlformat version
 * [x] Running new formatter on these files
 * [x] The RTS fails to build or link the C files (`stddef.h` missing)
    This stuff was very hairy before, and that doesn’t get better :-(

    If I use the wrapped `wasm32-unknown-wasi-clang`, the build makes some progress, but then fails with https://bugs.llvm.org/show_bug.cgi?id=43393 (as it used to before, and then somehow we fixed that)

    In the end, surgically adding an include to `CLANG_FLAGS` works, it seems.
 * [x] Some upgraded tool produces new `names` subsections, presumably from this Wasm proposal: https://github.com/WebAssembly/extended-name-section/blob/master/proposals/extended-name-section/Overview.md
   Worked around by jumping over them, but otherwise ignoring them. Should be refined.
 * [x] Haskell code ought to be adjusted to latest GHC
 * [x] Bump the `niv` that we install into the shell (also because of Haskell changes)
 * [x] The darwin rebuild of `rustc` and `git` keeps timing out on hydra.
mergify bot pushed a commit that referenced this pull request Sep 1, 2021
* Use nixpkgs master

to see what breaks, and also to get
NixOS/nixpkgs@5a923e5
which may allow us to use `rustc` from `nixpkgs` again (see #2519)

* Use buildDunePackage

* Less stdenv.lib

* Update generated nix files

* Bump to get fixes

* Bump nixpkgs

* Bump ocamlformat

* Add nixpkgs patch

* More patches

* Try building vlq differently

* Fix vlq

* Build vlq with buildDunePackage and dune2

* Revert "Build vlq with buildDunePackage and dune2"

This reverts commit d5e3399.

* Disable static ocaml build

* Allow requisites for now

* Quench warning

* More warnings

* Better error message

* Try GHC-8.8.4

* Actually bump to GHC-8.10.4

* Bump niv

* Fix parsing global names

* Call bindgen directly

* Bump nixpkgs, patches have been applied

* Run formatter

* Does this work?

* More comments

* Try static biuld again

* Revert "Try static biuld again"

This reverts commit ab44bbf.

* Actually follow the release branch

* Include patch from NixOS/nixpkgs#124498

* More patches

* Set allowedRequisites again

* Remove references to menhir

* Refresh patch

* Upgrade LLVM to version 12

* Apply suggestions from code review

Co-authored-by: Claudio Russo <[email protected]>

* Start decoding extended name sections

* Export apply_global_relocs in RTS module, call it before RTS init

* Make __wasm_apply_data_relocs optional in the linker (but not in codegen!), update linker test outputs

* Call wasm_apply_global_relocs from link_start

* Put the comment back

* Add patch from https://github.com/NixOS/nixpkgs/pull/125472/files

* Patch now upstream

* Linker: remove special case around __wasm_apply_global_relocs, add RTS start to merged module start

* Prepend m2 start in join_modules

Co-authored-by: Claudio Russo <[email protected]>
Co-authored-by: Ömer Sinan Ağacan <[email protected]>
@osa1
Copy link
Contributor Author

osa1 commented Sep 1, 2021

Closing this in favor of #2761

@osa1 osa1 closed this Sep 1, 2021
@osa1 osa1 deleted the osa1/custom-rustc-build branch September 1, 2021 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use cargo's build-std instead of xargo
2 participants