rust: hooks: fix cross compilation

Currently there is a state of severe confusion in
pkgs/build-support/rust/hooks/ regarding host vs target; right now
there is only "host" defined, but whether it means "host" or
"target" seems to fluctuate.

This commit corrects that, ensuring that all variables come in all
three flavors (build, host, target) and are used consistently with
the nixpkgs convention.

This also fixes the cross-compilation of packages which use
`maturinBuildHook` -- hooks go in `nativeBuildInputs` and are
phase-shifted backwards by one platform, so they need to be careful
about distinguishing between build and host.

Closes #247441
This commit is contained in:
Adam Joseph 2023-08-05 20:21:02 -07:00 committed by Alyssa Ross
parent c1df604e9f
commit 67a4f828b4
6 changed files with 28 additions and 38 deletions

View File

@ -30,13 +30,8 @@ cargoBuildHook() {
(
set -x
env \
"CC_@rustBuildPlatform@=@ccForBuild@" \
"CXX_@rustBuildPlatform@=@cxxForBuild@" \
"CC_@rustTargetPlatform@=@ccForHost@" \
"CXX_@rustTargetPlatform@=@cxxForHost@" \
cargo build -j $NIX_BUILD_CORES \
--target @rustTargetPlatformSpec@ \
@setEnv@ cargo build -j $NIX_BUILD_CORES \
--target @rustHostPlatformSpec@ \
--frozen \
${cargoBuildProfileFlag} \
${cargoBuildNoDefaultFeaturesFlag} \

View File

@ -29,7 +29,7 @@ cargoCheckHook() {
fi
argstr="${cargoCheckProfileFlag} ${cargoCheckNoDefaultFeaturesFlag} ${cargoCheckFeaturesFlag}
--target @rustTargetPlatformSpec@ --frozen ${cargoTestFlags}"
--target @rustHostPlatformSpec@ --frozen ${cargoTestFlags}"
(
set -x

View File

@ -1,7 +1,7 @@
cargoInstallPostBuildHook() {
echo "Executing cargoInstallPostBuildHook"
releaseDir=target/@shortTarget@/$cargoBuildType
releaseDir=target/@targetSubdirectory@/$cargoBuildType
tmpDir="${releaseDir}-tmp";
mkdir -p $tmpDir
@ -21,7 +21,7 @@ cargoInstallHook() {
# rename the output dir to a architecture independent one
releaseDir=target/@shortTarget@/$cargoBuildType
releaseDir=target/@targetSubdirectory@/$cargoBuildType
tmpDir="${releaseDir}-tmp";
mapfile -t targets < <(find "$NIX_BUILD_TOP" -type d | grep "${tmpDir}$")

View File

@ -29,7 +29,7 @@ cargoNextestHook() {
fi
argstr="${cargoCheckProfileFlag} ${cargoCheckNoDefaultFeaturesFlag} ${cargoCheckFeaturesFlag}
--target @rustTargetPlatformSpec@ --frozen ${cargoTestFlags}"
--target @rustHostPlatformSpec@ --frozen ${cargoTestFlags}"
(
set -x

View File

@ -9,6 +9,10 @@
, rust
, rustc
, stdenv
# 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
}:
@ -17,24 +21,17 @@ let
# 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
targetSubdirectory = if targetIsJSON then
(lib.removeSuffix ".json" (builtins.baseNameOf "${target}"))
else target;
ccForBuild = "${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}cc";
cxxForBuild = "${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}c++";
ccForHost = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
cxxForHost = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++";
rustBuildPlatform = rust.toRustTarget stdenv.buildPlatform;
rustTargetPlatform = rust.toRustTarget stdenv.hostPlatform;
rustTargetPlatformSpec = rust.toRustTargetSpec stdenv.hostPlatform;
in {
cargoBuildHook = callPackage ({ }:
makeSetupHook {
name = "cargo-build-hook.sh";
propagatedBuildInputs = [ cargo ];
substitutions = {
inherit ccForBuild ccForHost cxxForBuild cxxForHost
rustBuildPlatform rustTargetPlatform rustTargetPlatformSpec;
inherit (rust.envVars) rustHostPlatformSpec setEnv;
};
} ./cargo-build-hook.sh) {};
@ -43,7 +40,7 @@ in {
name = "cargo-check-hook.sh";
propagatedBuildInputs = [ cargo ];
substitutions = {
inherit rustTargetPlatformSpec;
inherit (rust.envVars) rustHostPlatformSpec;
};
} ./cargo-check-hook.sh) {};
@ -52,7 +49,7 @@ in {
name = "cargo-install-hook.sh";
propagatedBuildInputs = [ ];
substitutions = {
inherit shortTarget;
inherit targetSubdirectory;
};
} ./cargo-install-hook.sh) {};
@ -61,7 +58,7 @@ in {
name = "cargo-nextest-hook.sh";
propagatedBuildInputs = [ cargo cargo-nextest ];
substitutions = {
inherit rustTargetPlatformSpec;
inherit (rust.envVars) rustHostPlatformSpec;
};
} ./cargo-nextest-hook.sh) {};
@ -78,23 +75,26 @@ in {
cargoConfig = ''
[target."${rust.toRustTarget stdenv.buildPlatform}"]
"linker" = "${ccForBuild}"
${lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) ''
[target."${shortTarget}"]
"linker" = "${ccForHost}"
"linker" = "${rust.envVars.ccForBuild}"
${lib.optionalString (stdenv.hostPlatform.config != stdenv.targetPlatform.config) ''
[target."${rust.toRustTarget stdenv.targetPlatform}"]
"linker" = "${rust.envVars.ccForTarget}"
''}
"rustflags" = [ "-C", "target-feature=${if stdenv.hostPlatform.isStatic then "+" else "-"}crt-static" ]
'';
};
} ./cargo-setup-hook.sh) {};
maturinBuildHook = callPackage ({ }:
maturinBuildHook = callPackage ({ pkgsHostTarget }:
makeSetupHook {
name = "maturin-build-hook.sh";
propagatedBuildInputs = [ cargo maturin rustc ];
propagatedBuildInputs = [
pkgsHostTarget.maturin
pkgsHostTarget.cargo
pkgsHostTarget.rustc
];
substitutions = {
inherit ccForBuild ccForHost cxxForBuild cxxForHost
rustBuildPlatform rustTargetPlatform rustTargetPlatformSpec;
inherit (rust.envVars) rustTargetPlatformSpec setEnv;
};
} ./maturin-build-hook.sh) {};

View File

@ -9,12 +9,7 @@ maturinBuildHook() {
(
set -x
env \
"CC_@rustBuildPlatform@=@ccForBuild@" \
"CXX_@rustBuildPlatform@=@cxxForBuild@" \
"CC_@rustTargetPlatform@=@ccForHost@" \
"CXX_@rustTargetPlatform@=@cxxForHost@" \
maturin build \
@setEnv@ maturin build \
--jobs=$NIX_BUILD_CORES \
--frozen \
--target @rustTargetPlatformSpec@ \