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.

+16 -21
+2 -7
pkgs/build-support/rust/build-rust-package/default.nix
··· 82 targetIsJSON = lib.hasSuffix ".json" target; 83 useSysroot = targetIsJSON && !__internal_dontAddSysroot; 84 85 - # see https://github.com/rust-lang/cargo/blob/964a16a28e234a3d397b2a7031d4ab4a428b1391/src/cargo/core/compiler/compile_kind.rs#L151-L168 86 - # the "${}" is needed to transform the path into a /nix/store path before baseNameOf 87 - shortTarget = if targetIsJSON then 88 - (lib.removeSuffix ".json" (builtins.baseNameOf "${target}")) 89 - else target; 90 - 91 sysroot = callPackage ./sysroot { } { 92 - inherit target shortTarget; 93 RUSTFLAGS = args.RUSTFLAGS or ""; 94 originalCargoToml = src + /Cargo.toml; # profile info is later extracted 95 };
··· 82 targetIsJSON = lib.hasSuffix ".json" target; 83 useSysroot = targetIsJSON && !__internal_dontAddSysroot; 84 85 sysroot = callPackage ./sysroot { } { 86 + inherit target; 87 + shortTarget = rust.lib.toRustTargetSpecShort stdenv.hostPlatform; 88 RUSTFLAGS = args.RUSTFLAGS or ""; 89 originalCargoToml = src + /Cargo.toml; # profile info is later extracted 90 };
+4 -13
pkgs/build-support/rust/hooks/default.nix
··· 12 13 # This confusingly-named parameter indicates the *subdirectory of 14 # `target/` from which to copy the build artifacts. It is derived 15 - # from a stdenv platform (or a JSON file; see below). 16 - , target ? rust.toRustTargetSpec stdenv.hostPlatform 17 }: 18 19 - let 20 - targetIsJSON = lib.hasSuffix ".json" target; 21 - 22 - # see https://github.com/rust-lang/cargo/blob/964a16a28e234a3d397b2a7031d4ab4a428b1391/src/cargo/core/compiler/compile_kind.rs#L151-L168 23 - # the "${}" is needed to transform the path into a /nix/store path before baseNameOf 24 - targetSubdirectory = if targetIsJSON then 25 - (lib.removeSuffix ".json" (builtins.baseNameOf "${target}")) 26 - else target; 27 - 28 - in { 29 cargoBuildHook = callPackage ({ }: 30 makeSetupHook { 31 name = "cargo-build-hook.sh"; ··· 49 name = "cargo-install-hook.sh"; 50 propagatedBuildInputs = [ ]; 51 substitutions = { 52 - inherit targetSubdirectory; 53 }; 54 } ./cargo-install-hook.sh) {}; 55
··· 12 13 # This confusingly-named parameter indicates the *subdirectory of 14 # `target/` from which to copy the build artifacts. It is derived 15 + # from a stdenv platform (or a JSON file). 16 + , target ? rust.lib.toRustTargetSpecShort stdenv.hostPlatform 17 }: 18 19 + { 20 cargoBuildHook = callPackage ({ }: 21 makeSetupHook { 22 name = "cargo-build-hook.sh"; ··· 40 name = "cargo-install-hook.sh"; 41 propagatedBuildInputs = [ ]; 42 substitutions = { 43 + targetSubdirectory = target; 44 }; 45 } ./cargo-install-hook.sh) {}; 46
+10 -1
pkgs/build-support/rust/lib/default.nix
··· 63 then builtins.toFile (toRustTarget platform + ".json") (builtins.toJSON platform.rustc.platform) 64 else toRustTarget platform; 65 66 # When used as part of an environment variable name, triples are 67 # uppercased and have all hyphens replaced by underscores: 68 # ··· 72 toRustTargetForUseInEnvVars = platform: 73 lib.strings.replaceStrings ["-"] ["_"] 74 (lib.strings.toUpper 75 - (toRustTarget platform)); 76 77 # Returns true if the target is no_std 78 # https://github.com/rust-lang/rust/blob/2e44c17c12cec45b6a682b1e53a04ac5b5fcc9d2/src/bootstrap/config.rs#L415-L421
··· 63 then builtins.toFile (toRustTarget platform + ".json") (builtins.toJSON platform.rustc.platform) 64 else toRustTarget platform; 65 66 + # Returns the name of the rust target if it is standard, or the 67 + # basename of the file containing the custom target spec, without 68 + # the .json extension. 69 + # 70 + # This is the name used by Cargo for target subdirectories. 71 + toRustTargetSpecShort = platform: 72 + lib.removeSuffix ".json" 73 + (baseNameOf "${toRustTargetSpec platform}"); 74 + 75 # When used as part of an environment variable name, triples are 76 # uppercased and have all hyphens replaced by underscores: 77 # ··· 81 toRustTargetForUseInEnvVars = platform: 82 lib.strings.replaceStrings ["-"] ["_"] 83 (lib.strings.toUpper 84 + (toRustTargetSpecShort platform)); 85 86 # Returns true if the target is no_std 87 # https://github.com/rust-lang/rust/blob/2e44c17c12cec45b6a682b1e53a04ac5b5fcc9d2/src/bootstrap/config.rs#L415-L421