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.
This commit is contained in:
Alyssa Ross 2023-09-26 07:37:57 +00:00
parent b135595213
commit 1cbe5c3e8b
3 changed files with 16 additions and 21 deletions

View File

@ -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
};

View File

@ -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) {};

View File

@ -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