2021-02-09 11:38:25 +01:00
|
|
|
{ buildPackages
|
|
|
|
, callPackage
|
2021-02-11 17:32:47 +01:00
|
|
|
, cargo
|
2022-11-28 22:59:52 +01:00
|
|
|
, cargo-nextest
|
2021-02-09 11:38:25 +01:00
|
|
|
, lib
|
|
|
|
, makeSetupHook
|
2021-02-11 19:17:06 +01:00
|
|
|
, maturin
|
2021-02-09 11:38:25 +01:00
|
|
|
, rust
|
2021-05-21 07:03:49 +02:00
|
|
|
, rustc
|
2021-02-09 11:38:25 +01:00
|
|
|
, stdenv
|
|
|
|
, target ? rust.toRustTargetSpec 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
|
|
|
|
shortTarget = if targetIsJSON then
|
|
|
|
(lib.removeSuffix ".json" (builtins.baseNameOf "${target}"))
|
|
|
|
else target;
|
2021-02-11 17:32:47 +01:00
|
|
|
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;
|
2021-02-09 11:38:25 +01:00
|
|
|
in {
|
2021-02-11 17:32:47 +01:00
|
|
|
cargoBuildHook = callPackage ({ }:
|
|
|
|
makeSetupHook {
|
|
|
|
name = "cargo-build-hook.sh";
|
|
|
|
deps = [ cargo ];
|
|
|
|
substitutions = {
|
|
|
|
inherit ccForBuild ccForHost cxxForBuild cxxForHost
|
|
|
|
rustBuildPlatform rustTargetPlatform rustTargetPlatformSpec;
|
|
|
|
};
|
|
|
|
} ./cargo-build-hook.sh) {};
|
|
|
|
|
2021-02-15 10:26:40 +01:00
|
|
|
cargoCheckHook = callPackage ({ }:
|
|
|
|
makeSetupHook {
|
|
|
|
name = "cargo-check-hook.sh";
|
|
|
|
deps = [ cargo ];
|
|
|
|
substitutions = {
|
|
|
|
inherit rustTargetPlatformSpec;
|
|
|
|
};
|
|
|
|
} ./cargo-check-hook.sh) {};
|
|
|
|
|
2021-02-15 06:54:18 +01:00
|
|
|
cargoInstallHook = callPackage ({ }:
|
|
|
|
makeSetupHook {
|
|
|
|
name = "cargo-install-hook.sh";
|
|
|
|
deps = [ ];
|
|
|
|
substitutions = {
|
|
|
|
inherit shortTarget;
|
|
|
|
};
|
|
|
|
} ./cargo-install-hook.sh) {};
|
|
|
|
|
2022-11-28 22:59:52 +01:00
|
|
|
cargoNextestHook = callPackage ({ }:
|
|
|
|
makeSetupHook {
|
|
|
|
name = "cargo-nextest-hook.sh";
|
|
|
|
deps = [ cargo cargo-nextest ];
|
|
|
|
substitutions = {
|
|
|
|
inherit rustTargetPlatformSpec;
|
|
|
|
};
|
|
|
|
} ./cargo-nextest-hook.sh) {};
|
|
|
|
|
2021-02-09 11:38:25 +01:00
|
|
|
cargoSetupHook = callPackage ({ }:
|
|
|
|
makeSetupHook {
|
|
|
|
name = "cargo-setup-hook.sh";
|
|
|
|
deps = [ ];
|
|
|
|
substitutions = {
|
|
|
|
defaultConfig = ../fetchcargo-default-config.toml;
|
|
|
|
|
|
|
|
# Specify the stdenv's `diff` by abspath to ensure that the user's build
|
|
|
|
# inputs do not cause us to find the wrong `diff`.
|
2022-11-18 05:55:23 +01:00
|
|
|
diff = "${lib.getBin buildPackages.diffutils}/bin/diff";
|
2021-02-09 11:38:25 +01:00
|
|
|
|
2022-10-28 20:34:04 +02:00
|
|
|
# We want to specify the correct crt-static flag for both
|
|
|
|
# the build and host platforms. This is important when the wanted
|
|
|
|
# value for crt-static does not match the defaults in the rustc target,
|
|
|
|
# like for pkgsMusl or pkgsCross.musl64; Upstream rustc still assumes
|
|
|
|
# that musl = static[1].
|
|
|
|
#
|
|
|
|
# By default, Cargo doesn't apply RUSTFLAGS when building build.rs
|
|
|
|
# if --target is passed, so the only good way to set crt-static for
|
|
|
|
# build.rs files is to use the unstable -Zhost-config Cargo feature.
|
|
|
|
# This allows us to specify flags that should be passed to rustc
|
|
|
|
# when building for the build platform. We also need to use
|
|
|
|
# -Ztarget-applies-to-host, because using -Zhost-config requires it.
|
|
|
|
#
|
|
|
|
# When doing this, we also have to specify the linker, or cargo
|
|
|
|
# won't pass a -C linker= argument to rustc. This will make rustc
|
|
|
|
# try to use its default value of "cc", which won't be available
|
|
|
|
# when cross-compiling.
|
|
|
|
#
|
|
|
|
# [1]: https://github.com/rust-lang/compiler-team/issues/422
|
|
|
|
cargoConfig = ''
|
|
|
|
[host]
|
2021-02-09 11:38:25 +01:00
|
|
|
"linker" = "${ccForBuild}"
|
2022-10-28 20:34:04 +02:00
|
|
|
"rustflags" = [ "-C", "target-feature=${if stdenv.buildPlatform.isStatic then "+" else "-"}crt-static" ]
|
|
|
|
|
|
|
|
[target."${shortTarget}"]
|
|
|
|
"linker" = "${ccForHost}"
|
2022-08-13 14:43:50 +02:00
|
|
|
"rustflags" = [ "-C", "target-feature=${if stdenv.hostPlatform.isStatic then "+" else "-"}crt-static" ]
|
2022-10-28 20:34:04 +02:00
|
|
|
|
|
|
|
[unstable]
|
|
|
|
host-config = true
|
|
|
|
target-applies-to-host = true
|
2021-02-09 11:38:25 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
} ./cargo-setup-hook.sh) {};
|
2021-02-11 19:17:06 +01:00
|
|
|
|
|
|
|
maturinBuildHook = callPackage ({ }:
|
|
|
|
makeSetupHook {
|
|
|
|
name = "maturin-build-hook.sh";
|
2021-05-21 07:03:49 +02:00
|
|
|
deps = [ cargo maturin rustc ];
|
2021-02-11 19:17:06 +01:00
|
|
|
substitutions = {
|
|
|
|
inherit ccForBuild ccForHost cxxForBuild cxxForHost
|
|
|
|
rustBuildPlatform rustTargetPlatform rustTargetPlatformSpec;
|
|
|
|
};
|
|
|
|
} ./maturin-build-hook.sh) {};
|
2022-02-13 13:00:00 +01:00
|
|
|
|
|
|
|
bindgenHook = callPackage ({}: makeSetupHook {
|
|
|
|
name = "rust-bindgen-hook";
|
|
|
|
substitutions = {
|
2022-12-23 02:08:09 +01:00
|
|
|
libclang = rustc.llvmPackages.clang.cc.lib;
|
|
|
|
clang = rustc.llvmPackages.clang;
|
2022-02-13 13:00:00 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
./rust-bindgen-hook.sh) {};
|
2021-02-09 11:38:25 +01:00
|
|
|
}
|