Skip to content

rust: cargo: Use rustc and cargo built on Build for cross-compiling#176694

Merged
yorickvP merged 1 commit intoNixOS:masterfrom
mehmooda:rust_fast_cross
Sep 10, 2023
Merged

rust: cargo: Use rustc and cargo built on Build for cross-compiling#176694
yorickvP merged 1 commit intoNixOS:masterfrom
mehmooda:rust_fast_cross

Conversation

@mehmooda
Copy link
Contributor

@mehmooda mehmooda commented Jun 7, 2022

When cross-compiling a rust package, all we need is the std library compiled
for the target. This uses the final stage compiler which was built for Build
and then uses that as a stage0 compiler for target std library.

It also copies the rust binary from pkgsBuildBuild so that it find the new
lib/rustlib directory.

We also need to create a cargo wrapper which will use the "new" rust compiler

Closes #207435.

Description of changes
Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 22.11 Release Notes (or backporting 22.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@mehmooda mehmooda requested review from LnL7, Mic92 and zowoq as code owners June 7, 2022 10:46
@github-actions github-actions bot added the 6.topic: rust General-purpose programming language emphasizing performance, type safety, and concurrency. label Jun 7, 2022
@alyssais alyssais requested a review from sternenseemann June 7, 2022 10:50
@alyssais alyssais requested a review from Ericson2314 June 7, 2022 10:53
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux. labels Jun 7, 2022
@mehmooda mehmooda force-pushed the rust_fast_cross branch 2 times, most recently from 9dc4996 to c5e88dd Compare June 7, 2022 13:12
@mehmooda mehmooda requested a review from alyssais June 7, 2022 18:49
@Ericson2314
Copy link
Member

I think this sort of thing is good to have.

@mehmooda
Copy link
Contributor Author

I think this is ready now.
I'll queue up a clean up to staging for the if else null into a lib.optionalString after this and master gets merged into staging.

@mehmooda
Copy link
Contributor Author

TIL: if you ln -s a directory straight onto the output, it works when nix is installed single user but fails when nix is running as a daemon due to it trying to chown the existing directory. Using lndir instead works.

@06kellyjac
Copy link
Member

anything else holding this up other than conflicts?

@SuperSandro2000 SuperSandro2000 added the 2.status: merge conflict This PR has merge conflicts with the target branch label Sep 4, 2022
@ofborg ofborg bot added the 6.topic: cross-compilation Building packages on a different platform than they will be used on label Aug 3, 2023
@yu-re-ka
Copy link
Contributor

yu-re-ka commented Aug 4, 2023

This does not work with the current version of Rust. The compiler even after being copied to the new location is still looking for core crate exclusively in its original path, and this seems to be the compiled in sysroot (rustc --print sysroot).
Once more a wakeup call for someone to come up with a rustc wrapper ;)

We can still work around this however, by rewriting this reference in the binary to the new $out. But for that, the derivation name length has to match.

@winterqt
Copy link
Member

winterqt commented Aug 4, 2023

Once more a wakeup call for someone to come up with a rustc wrapper ;)

If it's compiled in, how would a wrapper help?

@yu-re-ka
Copy link
Contributor

yu-re-ka commented Aug 4, 2023

It can be altered with the --sysroot parameter

@yu-re-ka yu-re-ka force-pushed the rust_fast_cross branch 2 times, most recently from acebd66 to 101d373 Compare August 4, 2023 00:29
@yu-re-ka
Copy link
Contributor

yu-re-ka commented Aug 4, 2023

cross-compiling fd fails with error: failed to get bitcode from object file for LTO (Bitcode section not found in object file), presumably because of other assumptions that are compiled into the rustc binary

@yu-re-ka
Copy link
Contributor

yu-re-ka commented Aug 4, 2023

The std lib produced with fastCross is a stage 0 std lib, and thus does not include LLVM bitcode.

https://github.com/rust-lang/rust/blob/master/src/bootstrap/compile.rs#L409

@yu-re-ka yu-re-ka force-pushed the rust_fast_cross branch 3 times, most recently from 0cc5610 to dd44e56 Compare August 4, 2023 05:40
@yu-re-ka
Copy link
Contributor

yu-re-ka commented Aug 7, 2023

This should now work (again). Please review.

Copy link
Member

@kloenk kloenk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wegank wegank added the 12.approvals: 1 This PR was reviewed and approved by one person. label Sep 7, 2023
@mweinelt mweinelt closed this Sep 10, 2023
@mweinelt mweinelt reopened this Sep 10, 2023
@yu-re-ka
Copy link
Contributor

@ofborg build pkgsCross.aarch64-multiplatform.fd

When cross-compiling a rust package, all we need is the std library compiled
for the target. This uses the final stage compiler which was built for Build
and then uses that as a stage0 compiler for target std library.

It also copies the rust binary from pkgsBuildBuild so that it find the new
lib/rustlib directory.

We also need to create a cargo wrapper which will use the "new" rust compiler

Also makes sure man pages and doc pages are propagated

Co-authored-by: Alyssa Ross <hi@alyssa.is>
Co-authored-by: Sandro <sandro.jaeckel@gmail.com>
Co-authored-by: Rick van Schijndel <Mindavi@users.noreply.github.com>
@yu-re-ka
Copy link
Contributor

@ofborg build pkgsCross.aarch64-multiplatform.fd

@yu-re-ka
Copy link
Contributor

@ofborg build pkgsCross.musl64.fd

@yu-re-ka
Copy link
Contributor

@ofborg build pkgsStatic.fd

@yorickvP yorickvP merged commit 74bb255 into NixOS:master Sep 10, 2023
@yu-re-ka
Copy link
Contributor

Note: pkgsStatic.fd on aarch64-linux breakage is unrelated; was already broken on master

@infinisil
Copy link
Member

This doesn't appear to have fixed the linked issue, see #207435 (comment). Ping @winterqt

@yu-re-ka
Copy link
Contributor

yu-re-ka commented Sep 15, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: cross-compilation Building packages on a different platform than they will be used on 6.topic: rust General-purpose programming language emphasizing performance, type safety, and concurrency. 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pkgsStatic.xxx with Rust requires to recompile rustc