about summary refs log tree commit diff
path: root/pkgs/build-support/rust/lib/default.nix
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2023-05-09 13:38:32 +0000
committerAlyssa Ross <hi@alyssa.is>2023-11-09 10:02:24 +0100
commite3e57b8f1885bf1dc4787728479711f46a4171cc (patch)
treee83bec78d2ee16e3453cfb39b45c03f759cb06c8 /pkgs/build-support/rust/lib/default.nix
parentfecd99b105c644b1b61abfec6c203b304269e1e2 (diff)
downloadnixlib-e3e57b8f1885bf1dc4787728479711f46a4171cc.tar
nixlib-e3e57b8f1885bf1dc4787728479711f46a4171cc.tar.gz
nixlib-e3e57b8f1885bf1dc4787728479711f46a4171cc.tar.bz2
nixlib-e3e57b8f1885bf1dc4787728479711f46a4171cc.tar.lz
nixlib-e3e57b8f1885bf1dc4787728479711f46a4171cc.tar.xz
nixlib-e3e57b8f1885bf1dc4787728479711f46a4171cc.tar.zst
nixlib-e3e57b8f1885bf1dc4787728479711f46a4171cc.zip
lib.systems: elaborate Rust metadata
We need this stuff to be available in lib so make-derivation.nix can
access it to construct the Meson cross file.

This has a couple of other advantages:

 - It makes Rust less special.  Now figuring out what Rust calls a
   platform is the same as figuring out what Linux or QEMU call it.

 - We can unify the schema used to define Rust targets, and the schema
   used to access those values later.  Just like you can set "config"
   or "system" in a platform definition, and then access those same
   keys on the elaborated platform, you can now set "rustcTarget" in
   your crossSystem, and then access "stdenv.hostPlatform.rustcTarget"
   in your code.

"rustcTarget", "rustcTargetSpec", "cargoShortTarget", and
"cargoEnvVarTarget" have the "rustc" and "cargo" prefixes because
these are not exposed to code by the compiler, and are not
standardized.  The arch/os/etc. variables are all named to match the
forms in the Rust target spec JSON.

The new rust.target-family only takes a list, since we don't need to
worry about backwards compatibility when that name is used.

The old APIs are all still functional with no warning for now, so that
it's possible for external code to use a single API on both 23.05 and
23.11.  We can introduce the warnings once 23.05 is EOL, and make them
hard errors when 23.11 is EOL.
Diffstat (limited to 'pkgs/build-support/rust/lib/default.nix')
-rw-r--r--pkgs/build-support/rust/lib/default.nix127
1 files changed, 29 insertions, 98 deletions
diff --git a/pkgs/build-support/rust/lib/default.nix b/pkgs/build-support/rust/lib/default.nix
index ceca7323176c..dad8ab528235 100644
--- a/pkgs/build-support/rust/lib/default.nix
+++ b/pkgs/build-support/rust/lib/default.nix
@@ -5,89 +5,6 @@
 }:
 
 rec {
-  # https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch
-  toTargetArch = platform:
-    /**/ if platform ? rustc.platform then platform.rustc.platform.arch
-    else if platform.isAarch32 then "arm"
-    else if platform.isMips64  then "mips64"     # never add "el" suffix
-    else if platform.isPower64 then "powerpc64"  # never add "le" suffix
-    else platform.parsed.cpu.name;
-
-  # https://doc.rust-lang.org/reference/conditional-compilation.html#target_os
-  toTargetOs = platform:
-    /**/ if platform ? rustc.platform then platform.rustc.platform.os or "none"
-    else if platform.isDarwin then "macos"
-    else platform.parsed.kernel.name;
-
-  # https://doc.rust-lang.org/reference/conditional-compilation.html#target_family
-  toTargetFamily = platform:
-    if platform ? rustc.platform.target-family
-    then
-      (
-        # Since https://github.com/rust-lang/rust/pull/84072
-        # `target-family` is a list instead of single value.
-        let
-          f = platform.rustc.platform.target-family;
-        in
-        if builtins.isList f then f else [ f ]
-      )
-    else lib.optional platform.isUnix "unix"
-      ++ lib.optional platform.isWindows "windows";
-
-  # https://doc.rust-lang.org/reference/conditional-compilation.html#target_vendor
-  toTargetVendor = platform: let
-    inherit (platform.parsed) vendor;
-  in platform.rustc.platform.vendor or {
-    "w64" = "pc";
-  }.${vendor.name} or vendor.name;
-
-  # Returns the name of the rust target, even if it is custom. Adjustments are
-  # because rust has slightly different naming conventions than we do.
-  toRustTarget = platform: let
-    inherit (platform.parsed) cpu kernel abi;
-    cpu_ = platform.rustc.platform.arch or {
-      "armv7a" = "armv7";
-      "armv7l" = "armv7";
-      "armv6l" = "arm";
-      "armv5tel" = "armv5te";
-      "riscv64" = "riscv64gc";
-    }.${cpu.name} or cpu.name;
-    vendor_ = toTargetVendor platform;
-  in platform.rustc.config
-    or "${cpu_}-${vendor_}-${kernel.name}${lib.optionalString (abi.name != "unknown") "-${abi.name}"}";
-
-  # Returns the name of the rust target if it is standard, or the json file
-  # containing the custom target spec.
-  toRustTargetSpec = platform:
-    if platform ? rustc.platform
-    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:
-  #
-  # https://github.com/rust-lang/cargo/pull/9169
-  # https://github.com/rust-lang/cargo/issues/8285#issuecomment-634202431
-  #
-  toRustTargetForUseInEnvVars = platform:
-    lib.strings.replaceStrings ["-"] ["_"]
-      (lib.strings.toUpper
-        (toRustTargetSpecShort platform));
-
-  # Returns true if the target is no_std
-  # https://github.com/rust-lang/rust/blob/2e44c17c12cec45b6a682b1e53a04ac5b5fcc9d2/src/bootstrap/config.rs#L415-L421
-  IsNoStdTarget = platform: let rustTarget = toRustTarget platform; in
-    builtins.any (t: lib.hasInfix t rustTarget) ["-none" "nvptx" "switch" "-uefi"];
-
   # These environment variables must be set when using `cargo-c` and
   # several other tools which do not deal well with cross
   # compilation.  The symptom of the problem they fix is errors due
@@ -107,12 +24,12 @@ rec {
     ccForTarget = "${targetPackages.stdenv.cc}/bin/${targetPackages.stdenv.cc.targetPrefix}cc";
     cxxForTarget = "${targetPackages.stdenv.cc}/bin/${targetPackages.stdenv.cc.targetPrefix}c++";
 
-    rustBuildPlatform = toRustTarget stdenv.buildPlatform;
-    rustBuildPlatformSpec = toRustTargetSpec stdenv.buildPlatform;
-    rustHostPlatform = toRustTarget stdenv.hostPlatform;
-    rustHostPlatformSpec = toRustTargetSpec stdenv.hostPlatform;
-    rustTargetPlatform = toRustTarget stdenv.targetPlatform;
-    rustTargetPlatformSpec = toRustTargetSpec stdenv.targetPlatform;
+    rustBuildPlatform = stdenv.buildPlatform.rust.rustcTarget;
+    rustBuildPlatformSpec = stdenv.buildPlatform.rust.rustcTargetSpec;
+    rustHostPlatform = stdenv.hostPlatform.rust.rustcTarget;
+    rustHostPlatformSpec = stdenv.hostPlatform.rust.rustcTargetSpec;
+    rustTargetPlatform = stdenv.targetPlatform.rust.rustcTarget;
+    rustTargetPlatformSpec = stdenv.targetPlatform.rust.rustcTargetSpec;
   in {
     inherit
       ccForBuild  cxxForBuild  rustBuildPlatform   rustBuildPlatformSpec
@@ -131,20 +48,34 @@ rec {
     # the following lines when rustTargetPlatform collides with
     # rustHostPlatform.
     + lib.optionalString (rustTargetPlatform != rustHostPlatform) ''
-      "CC_${toRustTargetForUseInEnvVars stdenv.targetPlatform}=${ccForTarget}" \
-      "CXX_${toRustTargetForUseInEnvVars stdenv.targetPlatform}=${cxxForTarget}" \
-      "CARGO_TARGET_${toRustTargetForUseInEnvVars stdenv.targetPlatform}_LINKER=${ccForTarget}" \
+      "CC_${stdenv.targetPlatform.rust.cargoEnvVarTarget}=${ccForTarget}" \
+      "CXX_${stdenv.targetPlatform.rust.cargoEnvVarTarget}=${cxxForTarget}" \
+      "CARGO_TARGET_${stdenv.targetPlatform.rust.cargoEnvVarTarget}_LINKER=${ccForTarget}" \
     '' + ''
-      "CC_${toRustTargetForUseInEnvVars stdenv.hostPlatform}=${ccForHost}" \
-      "CXX_${toRustTargetForUseInEnvVars stdenv.hostPlatform}=${cxxForHost}" \
-      "CARGO_TARGET_${toRustTargetForUseInEnvVars stdenv.hostPlatform}_LINKER=${ccForHost}" \
+      "CC_${stdenv.hostPlatform.rust.cargoEnvVarTarget}=${ccForHost}" \
+      "CXX_${stdenv.hostPlatform.rust.cargoEnvVarTarget}=${cxxForHost}" \
+      "CARGO_TARGET_${stdenv.hostPlatform.rust.cargoEnvVarTarget}_LINKER=${ccForHost}" \
     '' + ''
-      "CC_${toRustTargetForUseInEnvVars stdenv.buildPlatform}=${ccForBuild}" \
-      "CXX_${toRustTargetForUseInEnvVars stdenv.buildPlatform}=${cxxForBuild}" \
-      "CARGO_TARGET_${toRustTargetForUseInEnvVars stdenv.buildPlatform}_LINKER=${ccForBuild}" \
+      "CC_${stdenv.buildPlatform.rust.cargoEnvVarTarget}=${ccForBuild}" \
+      "CXX_${stdenv.buildPlatform.rust.cargoEnvVarTarget}=${cxxForBuild}" \
+      "CARGO_TARGET_${stdenv.buildPlatform.rust.cargoEnvVarTarget}_LINKER=${ccForBuild}" \
       "CARGO_BUILD_TARGET=${rustBuildPlatform}" \
       "HOST_CC=${buildPackages.stdenv.cc}/bin/cc" \
       "HOST_CXX=${buildPackages.stdenv.cc}/bin/c++" \
     '';
   };
+} // lib.mapAttrs (old: new: platform:
+  # TODO: enable warning after 23.05 is EOL.
+  # lib.warn "`rust.${old} platform` is deprecated. Use `platform.rust.${new}` instead."
+    lib.getAttrFromPath new platform.rust)
+{
+  toTargetArch = [ "platform" "arch" ];
+  toTargetOs = [ "platform" "os" ];
+  toTargetFamily = [ "platform" "target-family" ];
+  toTargetVendor = [ "platform" "vendor" ];
+  toRustTarget = [ "rustcTarget" ];
+  toRustTargetSpec = [ "rustcTargetSpec" ];
+  toRustTargetSpecShort = [ "cargoShortTarget" ];
+  toRustTargetForUseInEnvVars = [ "cargoEnvVarTarget" ];
+  IsNoStdTarget = [ "isNoStdTarget" ];
 }