Merge pull request #71899 from decentriq/aslemmer/build-rust-package-add-target
build-support/rust: Add target option
This commit is contained in:
commit
56240d7f20
|
@ -68,6 +68,17 @@ build-time.
|
||||||
When `verifyCargoDeps` is set to `true`, the build will also verify that the
|
When `verifyCargoDeps` is set to `true`, the build will also verify that the
|
||||||
`cargoSha256` is not out of date by comparing the `Cargo.lock` file in both the `cargoDeps` and `src`. Note that this option changes the value of `cargoSha256` since it also copies the `Cargo.lock` in it. To avoid breaking backward-compatibility this option is not enabled by default but hopefully will be in the future.
|
`cargoSha256` is not out of date by comparing the `Cargo.lock` file in both the `cargoDeps` and `src`. Note that this option changes the value of `cargoSha256` since it also copies the `Cargo.lock` in it. To avoid breaking backward-compatibility this option is not enabled by default but hopefully will be in the future.
|
||||||
|
|
||||||
|
### Building a crate for a different target
|
||||||
|
|
||||||
|
To build your crate with a different cargo `--target` simply specify the `target` attribute:
|
||||||
|
|
||||||
|
```nix
|
||||||
|
pkgs.rustPlatform.buildRustPackage {
|
||||||
|
(...)
|
||||||
|
target = "x86_64-fortanix-unknown-sgx";
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Compiling Rust crates using Nix instead of Cargo
|
## Compiling Rust crates using Nix instead of Cargo
|
||||||
|
|
||||||
### Simple operation
|
### Simple operation
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
verifyCargoDeps ? false
|
verifyCargoDeps ? false
|
||||||
, buildType ? "release"
|
, buildType ? "release"
|
||||||
, meta ? {}
|
, meta ? {}
|
||||||
|
, target ? null
|
||||||
|
|
||||||
, cargoVendorDir ? null
|
, cargoVendorDir ? null
|
||||||
, ... } @ args:
|
, ... } @ args:
|
||||||
|
@ -50,12 +51,13 @@ let
|
||||||
rustHostConfig = {
|
rustHostConfig = {
|
||||||
x86_64-pc-mingw32 = "x86_64-pc-windows-gnu";
|
x86_64-pc-mingw32 = "x86_64-pc-windows-gnu";
|
||||||
}.${hostConfig} or hostConfig;
|
}.${hostConfig} or hostConfig;
|
||||||
|
rustTarget = if target == null then rustHostConfig else target;
|
||||||
|
|
||||||
ccForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}cc";
|
ccForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}cc";
|
||||||
cxxForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}c++";
|
cxxForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}c++";
|
||||||
ccForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
|
ccForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
|
||||||
cxxForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++";
|
cxxForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++";
|
||||||
releaseDir = "target/${rustHostConfig}/${buildType}";
|
releaseDir = "target/${rustTarget}/${buildType}";
|
||||||
in
|
in
|
||||||
|
|
||||||
stdenv.mkDerivation (args // {
|
stdenv.mkDerivation (args // {
|
||||||
|
@ -88,7 +90,7 @@ stdenv.mkDerivation (args // {
|
||||||
[target."${stdenv.buildPlatform.config}"]
|
[target."${stdenv.buildPlatform.config}"]
|
||||||
"linker" = "${ccForBuild}"
|
"linker" = "${ccForBuild}"
|
||||||
${stdenv.lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) ''
|
${stdenv.lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) ''
|
||||||
[target."${rustHostConfig}"]
|
[target."${rustTarget}"]
|
||||||
"linker" = "${ccForHost}"
|
"linker" = "${ccForHost}"
|
||||||
${# https://github.com/rust-lang/rust/issues/46651#issuecomment-433611633
|
${# https://github.com/rust-lang/rust/issues/46651#issuecomment-433611633
|
||||||
stdenv.lib.optionalString (stdenv.hostPlatform.isMusl && stdenv.hostPlatform.isAarch64) ''
|
stdenv.lib.optionalString (stdenv.hostPlatform.isMusl && stdenv.hostPlatform.isAarch64) ''
|
||||||
|
@ -133,7 +135,7 @@ stdenv.mkDerivation (args // {
|
||||||
"CXX_${stdenv.hostPlatform.config}"="${cxxForHost}" \
|
"CXX_${stdenv.hostPlatform.config}"="${cxxForHost}" \
|
||||||
cargo build \
|
cargo build \
|
||||||
${stdenv.lib.optionalString (buildType == "release") "--release"} \
|
${stdenv.lib.optionalString (buildType == "release") "--release"} \
|
||||||
--target ${rustHostConfig} \
|
--target ${rustTarget} \
|
||||||
--frozen ${concatStringsSep " " cargoBuildFlags}
|
--frozen ${concatStringsSep " " cargoBuildFlags}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue