about summary refs log tree commit diff
path: root/pkgs/build-support/rust
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2023-10-18 11:02:45 +0000
committerAdam Joseph <54836058+amjoseph-nixpkgs@users.noreply.github.com>2023-11-30 09:23:06 +0000
commit8b51cdd3bea18e806e3ed63add8e19292dfc84ec (patch)
treee7a535d97de8a4d2fd38fcbe7239ba967ca17c32 /pkgs/build-support/rust
parentc6f3c0403fd012327b69b6cd475aeee7c6ae9592 (diff)
downloadnixlib-8b51cdd3bea18e806e3ed63add8e19292dfc84ec.tar
nixlib-8b51cdd3bea18e806e3ed63add8e19292dfc84ec.tar.gz
nixlib-8b51cdd3bea18e806e3ed63add8e19292dfc84ec.tar.bz2
nixlib-8b51cdd3bea18e806e3ed63add8e19292dfc84ec.tar.lz
nixlib-8b51cdd3bea18e806e3ed63add8e19292dfc84ec.tar.xz
nixlib-8b51cdd3bea18e806e3ed63add8e19292dfc84ec.tar.zst
nixlib-8b51cdd3bea18e806e3ed63add8e19292dfc84ec.zip
rustc: add a compiler wrapper
We keep running into situations where we can't get the right
combination of rustc flags through build systems into rustc.
RUSTFLAGS is the only variable supported across build systems, but if
RUSTFLAGS is set, Cargo will ignore all other ways of specifying rustc
flags, including the target-specific ones, which we need to make
dynamic musl builds work.  (This is why pkgsCross.musl64.crosvm is
currently broken — it works if you unset separateDebugInfo, which
causes RUSTFLAGS not to be set.)

So, we need to do the same thing we do for C and C++ compilers, and
add a compiler wrapper so we can inject the flags we need, regardless
of the build system.

Currently the wrapper only supports a single mechanism for injecting
flags — the NIX_RUSTFLAGS environment variable.  As time goes on,
we'll probably want to add additional features, like target-specific
environment variables.
Diffstat (limited to 'pkgs/build-support/rust')
-rw-r--r--pkgs/build-support/rust/rustc-wrapper/default.nix29
-rw-r--r--pkgs/build-support/rust/rustc-wrapper/rustc-wrapper.sh11
2 files changed, 40 insertions, 0 deletions
diff --git a/pkgs/build-support/rust/rustc-wrapper/default.nix b/pkgs/build-support/rust/rustc-wrapper/default.nix
new file mode 100644
index 000000000000..06cfcbb5457f
--- /dev/null
+++ b/pkgs/build-support/rust/rustc-wrapper/default.nix
@@ -0,0 +1,29 @@
+{ lib, runCommand, rustc-unwrapped }:
+
+runCommand "${rustc-unwrapped.pname}-wrapper-${rustc-unwrapped.version}" {
+  preferLocalBuild = true;
+  strictDeps = true;
+  inherit (rustc-unwrapped) outputs;
+
+  env = {
+    prog = "${rustc-unwrapped}/bin/rustc";
+  };
+
+  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)}
+''
diff --git a/pkgs/build-support/rust/rustc-wrapper/rustc-wrapper.sh b/pkgs/build-support/rust/rustc-wrapper/rustc-wrapper.sh
new file mode 100644
index 000000000000..866a6912e090
--- /dev/null
+++ b/pkgs/build-support/rust/rustc-wrapper/rustc-wrapper.sh
@@ -0,0 +1,11 @@
+#!@shell@
+
+# Optionally print debug info.
+if (( "${NIX_DEBUG:-0}" >= 1 )); then
+    echo "original flags to @prog@:" >&2
+    printf "  %q\n" "$@" >&2
+    echo "extra flags after to @prog@:" >&2
+    printf "  %q\n" $NIX_RUSTFLAGS >&2
+fi
+
+exec @prog@ "$@" $NIX_RUSTFLAGS