From 9ee10432a4b7464f0b068065a278e61ad61d0387 Mon Sep 17 00:00:00 2001 From: Asad Mehmood Date: Tue, 7 Jun 2022 11:28:03 +0100 Subject: rust: cargo: Use rustc and cargo built on Build 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 Co-authored-by: Sandro Co-authored-by: Rick van Schijndel --- pkgs/development/compilers/rust/rustc.nix | 44 ++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'pkgs/development/compilers/rust/rustc.nix') diff --git a/pkgs/development/compilers/rust/rustc.nix b/pkgs/development/compilers/rust/rustc.nix index 53f7257ecfc58..869dd15df0be9 100644 --- a/pkgs/development/compilers/rust/rustc.nix +++ b/pkgs/development/compilers/rust/rustc.nix @@ -15,6 +15,10 @@ , wezterm , firefox , thunderbird +# This only builds std for target and reuses the rustc from build. +, fastCross +, lndir +, makeWrapper }: let @@ -87,13 +91,13 @@ in stdenv.mkDerivation rec { # (build!=target): When cross-building a compiler we need to add # the build platform as well so rustc can compile build.rs # scripts. - ] ++ optionals (stdenv.buildPlatform != stdenv.targetPlatform) [ + ] ++ optionals (stdenv.buildPlatform != stdenv.targetPlatform && !fastCross) [ (rust.toRustTargetSpec stdenv.buildPlatform) # (host!=target): When building a cross-targeting compiler we # need to add the host platform as well so rustc can compile # build.rs scripts. - ] ++ optionals (stdenv.hostPlatform != stdenv.targetPlatform) [ + ] ++ optionals (stdenv.hostPlatform != stdenv.targetPlatform && !fastCross) [ (rust.toRustTargetSpec stdenv.hostPlatform) ])}" @@ -132,6 +136,37 @@ in stdenv.mkDerivation rec { "--set rust.jemalloc" ]; + # if we already have a rust compiler for build just compile the target std + # library and reuse compiler + buildPhase = if fastCross then " + runHook preBuild + + mkdir -p build/${rust.toRustTargetSpec stdenv.hostPlatform}/stage0-{std,rustc}/${rust.toRustTargetSpec stdenv.hostPlatform}/release/ + ln -s ${rustc}/lib/rustlib/${rust.toRustTargetSpec stdenv.hostPlatform}/libstd-*.so build/${rust.toRustTargetSpec stdenv.hostPlatform}/stage0-std/${rust.toRustTargetSpec stdenv.hostPlatform}/release/libstd.so + ln -s ${rustc}/lib/rustlib/${rust.toRustTargetSpec stdenv.hostPlatform}/librustc_driver-*.so build/${rust.toRustTargetSpec stdenv.hostPlatform}/stage0-rustc/${rust.toRustTargetSpec stdenv.hostPlatform}/release/librustc.so + ln -s ${rustc}/bin/rustc build/${rust.toRustTargetSpec stdenv.hostPlatform}/stage0-rustc/${rust.toRustTargetSpec stdenv.hostPlatform}/release/rustc-main + touch build/${rust.toRustTargetSpec stdenv.hostPlatform}/stage0-std/${rust.toRustTargetSpec stdenv.hostPlatform}/release/.libstd.stamp + touch build/${rust.toRustTargetSpec stdenv.hostPlatform}/stage0-rustc/${rust.toRustTargetSpec stdenv.hostPlatform}/release/.librustc.stamp + python ./x.py --keep-stage=0 --stage=1 build library/std + + runHook postBuild + " else null; + + installPhase = if fastCross then '' + runHook preInstall + + python ./x.py --keep-stage=0 --stage=1 install library/std + mkdir -v $out/bin $doc $man + makeWrapper ${rustc}/bin/rustc $out/bin/rustc --add-flags "--sysroot $out" + makeWrapper ${rustc}/bin/rustdoc $out/bin/rustdoc --add-flags "--sysroot $out" + ln -s ${rustc}/lib/rustlib/{manifest-rust-std-,}${rust.toRustTargetSpec stdenv.hostPlatform} $out/lib/rustlib/ + echo rust-std-${rust.toRustTargetSpec stdenv.hostPlatform} >> $out/lib/rustlib/components + lndir ${rustc.doc} $doc + lndir ${rustc.man} $man + + runHook postInstall + '' else null; + # The bootstrap.py will generated a Makefile that then executes the build. # The BOOTSTRAP_ARGS used by this Makefile must include all flags to pass # to the bootstrap builder. @@ -179,7 +214,8 @@ in stdenv.mkDerivation rec { nativeBuildInputs = [ file python3 rustc cmake which libffi removeReferencesTo pkg-config xz - ]; + ] + ++ optionals fastCross [ lndir makeWrapper ]; buildInputs = [ openssl ] ++ optionals stdenv.isDarwin [ libiconv Security ] @@ -188,7 +224,7 @@ in stdenv.mkDerivation rec { outputs = [ "out" "man" "doc" ]; setOutputFlags = false; - postInstall = lib.optionalString enableRustcDev '' + postInstall = lib.optionalString (enableRustcDev && !fastCross) '' # install rustc-dev components. Necessary to build rls, clippy... python x.py dist rustc-dev tar xf build/dist/rustc-dev*tar.gz -- cgit 1.4.1