about summary refs log tree commit diff
path: root/pkgs/build-support/rust
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2023-09-26 07:37:57 +0000
committerAlyssa Ross <hi@alyssa.is>2023-10-03 12:30:04 +0000
commit1cbe5c3e8ba7ad5701291acce3ac78dadf388231 (patch)
tree2c84532e85330f684bccf965f39e1f5599d45555 /pkgs/build-support/rust
parentb135595213dd61fa70ebd2b2d19b8eab16a8f174 (diff)
downloadnixlib-1cbe5c3e8ba7ad5701291acce3ac78dadf388231.tar
nixlib-1cbe5c3e8ba7ad5701291acce3ac78dadf388231.tar.gz
nixlib-1cbe5c3e8ba7ad5701291acce3ac78dadf388231.tar.bz2
nixlib-1cbe5c3e8ba7ad5701291acce3ac78dadf388231.tar.lz
nixlib-1cbe5c3e8ba7ad5701291acce3ac78dadf388231.tar.xz
nixlib-1cbe5c3e8ba7ad5701291acce3ac78dadf388231.tar.zst
nixlib-1cbe5c3e8ba7ad5701291acce3ac78dadf388231.zip
rust.toRustTargetForUseInEnvVars: support custom targets
> If using a target spec JSON file, the <triple> value is the filename
> stem. For example --target foo/bar.json would match [target.bar].

- https://doc.rust-lang.org/cargo/reference/config.html#target

I've also exposed toRustTargetSpecShort as a public function, because
it's useful to be able to know what the target subdirectory will be.
Diffstat (limited to 'pkgs/build-support/rust')
-rw-r--r--pkgs/build-support/rust/build-rust-package/default.nix9
-rw-r--r--pkgs/build-support/rust/hooks/default.nix17
-rw-r--r--pkgs/build-support/rust/lib/default.nix11
3 files changed, 16 insertions, 21 deletions
diff --git a/pkgs/build-support/rust/build-rust-package/default.nix b/pkgs/build-support/rust/build-rust-package/default.nix
index cb83a7bc95cf..da868861e2ca 100644
--- a/pkgs/build-support/rust/build-rust-package/default.nix
+++ b/pkgs/build-support/rust/build-rust-package/default.nix
@@ -82,14 +82,9 @@ let
   targetIsJSON = lib.hasSuffix ".json" target;
   useSysroot = targetIsJSON && !__internal_dontAddSysroot;
 
-  # see https://github.com/rust-lang/cargo/blob/964a16a28e234a3d397b2a7031d4ab4a428b1391/src/cargo/core/compiler/compile_kind.rs#L151-L168
-  # the "${}" is needed to transform the path into a /nix/store path before baseNameOf
-  shortTarget = if targetIsJSON then
-      (lib.removeSuffix ".json" (builtins.baseNameOf "${target}"))
-    else target;
-
   sysroot = callPackage ./sysroot { } {
-    inherit target shortTarget;
+    inherit target;
+    shortTarget = rust.lib.toRustTargetSpecShort stdenv.hostPlatform;
     RUSTFLAGS = args.RUSTFLAGS or "";
     originalCargoToml = src + /Cargo.toml; # profile info is later extracted
   };
diff --git a/pkgs/build-support/rust/hooks/default.nix b/pkgs/build-support/rust/hooks/default.nix
index cf06300096f3..c73ec30082dc 100644
--- a/pkgs/build-support/rust/hooks/default.nix
+++ b/pkgs/build-support/rust/hooks/default.nix
@@ -12,20 +12,11 @@
 
 # This confusingly-named parameter indicates the *subdirectory of
 # `target/` from which to copy the build artifacts.  It is derived
-# from a stdenv platform (or a JSON file; see below).
-, target ? rust.toRustTargetSpec stdenv.hostPlatform
+# from a stdenv platform (or a JSON file).
+, target ? rust.lib.toRustTargetSpecShort stdenv.hostPlatform
 }:
 
-let
-  targetIsJSON = lib.hasSuffix ".json" target;
-
-  # see https://github.com/rust-lang/cargo/blob/964a16a28e234a3d397b2a7031d4ab4a428b1391/src/cargo/core/compiler/compile_kind.rs#L151-L168
-  # the "${}" is needed to transform the path into a /nix/store path before baseNameOf
-  targetSubdirectory = if targetIsJSON then
-      (lib.removeSuffix ".json" (builtins.baseNameOf "${target}"))
-    else target;
-
-in {
+{
   cargoBuildHook = callPackage ({ }:
     makeSetupHook {
       name = "cargo-build-hook.sh";
@@ -49,7 +40,7 @@ in {
       name = "cargo-install-hook.sh";
       propagatedBuildInputs = [ ];
       substitutions = {
-        inherit targetSubdirectory;
+        targetSubdirectory = target;
       };
     } ./cargo-install-hook.sh) {};
 
diff --git a/pkgs/build-support/rust/lib/default.nix b/pkgs/build-support/rust/lib/default.nix
index 8ca3758e5147..ceca7323176c 100644
--- a/pkgs/build-support/rust/lib/default.nix
+++ b/pkgs/build-support/rust/lib/default.nix
@@ -63,6 +63,15 @@ rec {
     then builtins.toFile (toRustTarget platform + ".json") (builtins.toJSON platform.rustc.platform)
     else toRustTarget platform;
 
+  # Returns the name of the rust target if it is standard, or the
+  # basename of the file containing the custom target spec, without
+  # the .json extension.
+  #
+  # This is the name used by Cargo for target subdirectories.
+  toRustTargetSpecShort = platform:
+    lib.removeSuffix ".json"
+      (baseNameOf "${toRustTargetSpec platform}");
+
   # When used as part of an environment variable name, triples are
   # uppercased and have all hyphens replaced by underscores:
   #
@@ -72,7 +81,7 @@ rec {
   toRustTargetForUseInEnvVars = platform:
     lib.strings.replaceStrings ["-"] ["_"]
       (lib.strings.toUpper
-        (toRustTarget platform));
+        (toRustTargetSpecShort platform));
 
   # Returns true if the target is no_std
   # https://github.com/rust-lang/rust/blob/2e44c17c12cec45b6a682b1e53a04ac5b5fcc9d2/src/bootstrap/config.rs#L415-L421