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

rustc: add a compiler wrapper #267876

Merged
3 commits merged into from
Nov 30, 2023
Merged
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
3 changes: 1 addition & 2 deletions doc/languages-frameworks/rust.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ repository:
lib.updateManyAttrsByPath [{
path = [ "packages" "stable" ];
update = old: old.overrideScope(final: prev: {
rustc = prev.rustc.overrideAttrs (_: {
rustc-unwrapped = prev.rustc-unwrapped.overrideAttrs (_: {
src = lib.cleanSource /git/scratch/rust;
# do *not* put passthru.isReleaseTarball=true here
});
Expand Down Expand Up @@ -1003,4 +1003,3 @@ nix-build $NIXPKGS -A package-broken-by-rust-changes
The `git submodule update --init` and `cargo vendor` commands above
require network access, so they can't be performed from within the
`rustc` derivation, unfortunately.

30 changes: 30 additions & 0 deletions pkgs/build-support/rust/rustc-wrapper/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{ lib, runCommand, rustc-unwrapped, sysroot ? null }:

runCommand "${rustc-unwrapped.pname}-wrapper-${rustc-unwrapped.version}" {
preferLocalBuild = true;
strictDeps = true;
inherit (rustc-unwrapped) outputs;

env = {
prog = "${rustc-unwrapped}/bin/rustc";
sysroot = lib.optionalString (sysroot != null) "--sysroot ${sysroot}";
};

passthru = {
inherit (rustc-unwrapped) pname version src llvm llvmPackages;
unwrapped = rustc-unwrapped;
};

meta = rustc-unwrapped.meta // {
description = "${rustc-unwrapped.meta.description} (wrapper script)";
priority = 10;
};
} ''
mkdir -p $out/bin
ln -s ${rustc-unwrapped}/bin/* $out/bin
rm $out/bin/rustc
substituteAll ${./rustc-wrapper.sh} $out/bin/rustc
chmod +x $out/bin/rustc
${lib.concatMapStrings (output: "ln -s ${rustc-unwrapped.${output}} \$${output}\n")
(lib.remove "out" rustc-unwrapped.outputs)}
''
16 changes: 16 additions & 0 deletions pkgs/build-support/rust/rustc-wrapper/rustc-wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!@shell@

extraBefore=(@sysroot@)
extraAfter=($NIX_RUSTFLAGS)

# Optionally print debug info.
if (( "${NIX_DEBUG:-0}" >= 1 )); then
echo "extra flags before to @prog@:" >&2
printf " %q\n" "${extraBefore[@]}" >&2
echo "original flags to @prog@:" >&2
printf " %q\n" "$@" >&2
echo "extra flags after to @prog@:" >&2
printf " %q\n" "${extraAfter[@]}" >&2
fi

exec @prog@ "${extraBefore[@]}" "$@" "${extraAfter[@]}"
2 changes: 1 addition & 1 deletion pkgs/build-support/setup-hooks/separate-debug-info.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export NIX_SET_BUILD_ID=1
export NIX_LDFLAGS+=" --compress-debug-sections=zlib"
export NIX_CFLAGS_COMPILE+=" -ggdb -Wa,--compress-debug-sections"
export RUSTFLAGS+=" -g"
export NIX_RUSTFLAGS+=" -g"

fixupOutputHooks+=(_separateDebugInfo)

Expand Down
1 change: 1 addition & 0 deletions pkgs/development/compilers/rust/1_74.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
, CoreFoundation, Security, SystemConfiguration
, pkgsBuildTarget, pkgsBuildBuild, pkgsBuildHost
, makeRustPlatform
, wrapRustcWith
, llvmPackages_16, llvm_16
} @ args:

Expand Down
6 changes: 4 additions & 2 deletions pkgs/development/compilers/rust/binary.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ lib, stdenv, makeWrapper, bash, curl, darwin, zlib
{ lib, stdenv, makeWrapper, wrapRustc, bash, curl, darwin, zlib
, autoPatchelfHook, gcc
, version
, src
Expand All @@ -19,7 +19,7 @@ let
in

rec {
rustc = stdenv.mkDerivation {
rustc-unwrapped = stdenv.mkDerivation {
pname = "rustc-${versionType}";

inherit version;
Expand Down Expand Up @@ -61,6 +61,8 @@ rec {
setupHooks = ./setup-hook.sh;
};

rustc = wrapRustc rustc-unwrapped;

cargo = stdenv.mkDerivation {
pname = "cargo-${versionType}";

Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/compilers/rust/cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rustPlatform.buildRustPackage.override {
cargo-auditable = cargo-auditable.bootstrap;
} ({
pname = "cargo";
inherit (rustc) version src;
inherit (rustc.unwrapped) version src;

# the rust source tarball already has all the dependencies vendored, no need to fetch them again
cargoVendorDir = "vendor";
Expand Down
7 changes: 6 additions & 1 deletion pkgs/development/compilers/rust/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
, CoreFoundation, Security, SystemConfiguration
, pkgsBuildBuild
, makeRustPlatform
, wrapRustcWith
}:

let
Expand Down Expand Up @@ -64,7 +65,7 @@ in
buildRustPackages = (selectRustPackage buildPackages).packages.stable;
# Analogous to stdenv
rustPlatform = makeRustPlatform self.buildRustPackages;
rustc = self.callPackage ./rustc.nix ({
rustc-unwrapped = self.callPackage ./rustc.nix ({
version = rustcVersion;
sha256 = rustcSha256;
inherit enableRustcDev;
Expand All @@ -75,6 +76,10 @@ in
# Use boot package set to break cycle
inherit (bootstrapRustPackages) cargo rustc rustfmt;
});
rustc = wrapRustcWith {
inherit (self) rustc-unwrapped;
sysroot = if fastCross then self.rustc-unwrapped else null;
};
rustfmt = self.callPackage ./rustfmt.nix {
inherit Security;
inherit (self.buildRustPackages) rustc;
Expand Down
12 changes: 6 additions & 6 deletions pkgs/development/compilers/rust/rustc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ in stdenv.mkDerivation (finalAttrs: {
runHook preBuild

mkdir -p build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-{std,rustc}/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/
ln -s ${rustc}/lib/rustlib/${stdenv.hostPlatform.rust.rustcTargetSpec}/libstd-*.so build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-std/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/libstd.so
ln -s ${rustc}/lib/rustlib/${stdenv.hostPlatform.rust.rustcTargetSpec}/librustc_driver-*.so build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/librustc.so
ln -s ${rustc}/bin/rustc build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/rustc-main
ln -s ${rustc.unwrapped}/lib/rustlib/${stdenv.hostPlatform.rust.rustcTargetSpec}/libstd-*.so build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-std/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/libstd.so
ln -s ${rustc.unwrapped}/lib/rustlib/${stdenv.hostPlatform.rust.rustcTargetSpec}/librustc_driver-*.so build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/librustc.so
ln -s ${rustc.unwrapped}/bin/rustc build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/rustc-main
touch build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-std/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/.libstd.stamp
touch build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/.librustc.stamp
python ./x.py --keep-stage=0 --stage=1 build library/std
Expand All @@ -169,9 +169,9 @@ in stdenv.mkDerivation (finalAttrs: {

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-,}${stdenv.hostPlatform.rust.rustcTargetSpec} $out/lib/rustlib/
ln -s ${rustc.unwrapped}/bin/rustc $out/bin
makeWrapper ${rustc.unwrapped}/bin/rustdoc $out/bin/rustdoc --add-flags "--sysroot $out"
ln -s ${rustc.unwrapped}/lib/rustlib/{manifest-rust-std-,}${stdenv.hostPlatform.rust.rustcTargetSpec} $out/lib/rustlib/
echo rust-std-${stdenv.hostPlatform.rust.rustcTargetSpec} >> $out/lib/rustlib/components
lndir ${rustc.doc} $doc
lndir ${rustc.man} $man
Expand Down
7 changes: 5 additions & 2 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16933,6 +16933,9 @@ with pkgs;
inherit (darwin) apple_sdk;
};

wrapRustcWith = { rustc-unwrapped, ... } @ args: callPackage ../build-support/rust/rustc-wrapper args;
wrapRustc = rustc-unwrapped: wrapRustcWith { inherit rustc-unwrapped; };

rust_1_74 = callPackage ../development/compilers/rust/1_74.nix {
inherit (darwin.apple_sdk.frameworks) CoreFoundation Security SystemConfiguration;
llvm_16 = llvmPackages_16.libllvm;
Expand All @@ -16951,7 +16954,7 @@ with pkgs;
inherit (rustPackages) cargo cargo-auditable cargo-auditable-cargo-wrapper clippy rustc rustPlatform;

# https://github.com/NixOS/nixpkgs/issues/89426
rustc-wasm32 = (rustc.override {
rustc-wasm32 = wrapRustc ((rustc.unwrapped.override {
stdenv = stdenv.override {
targetPlatform = stdenv.targetPlatform // {
parsed = {
Expand All @@ -16964,7 +16967,7 @@ with pkgs;
};
}).overrideAttrs (old: {
configureFlags = old.configureFlags ++ ["--set=build.docs=false"];
});
}));

makeRustPlatform = callPackage ../development/compilers/rust/make-rust-platform.nix { };

Expand Down