Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build-dependencies resolution when cross-compiling #372

Merged
merged 7 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions crate2nix/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ rec {
{
name = "libc";
packageId = "libc";
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "aarch64-linux-android");
target = { target, features }: (target.name == "aarch64-linux-android");
}
{
name = "libc";
Expand Down Expand Up @@ -2691,12 +2691,12 @@ rec {
{
name = "winapi-i686-pc-windows-gnu";
packageId = "winapi-i686-pc-windows-gnu";
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "i686-pc-windows-gnu");
target = { target, features }: (target.name == "i686-pc-windows-gnu");
}
{
name = "winapi-x86_64-pc-windows-gnu";
packageId = "winapi-x86_64-pc-windows-gnu";
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "x86_64-pc-windows-gnu");
target = { target, features }: (target.name == "x86_64-pc-windows-gnu");
}
];
features = {
Expand Down Expand Up @@ -3001,7 +3001,7 @@ rec {
{
name = "windows_aarch64_gnullvm";
packageId = "windows_aarch64_gnullvm";
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "aarch64-pc-windows-gnullvm");
target = { target, features }: (target.name == "aarch64-pc-windows-gnullvm");
}
{
name = "windows_aarch64_msvc";
Expand All @@ -3016,7 +3016,7 @@ rec {
{
name = "windows_i686_gnullvm";
packageId = "windows_i686_gnullvm";
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "i686-pc-windows-gnullvm");
target = { target, features }: (target.name == "i686-pc-windows-gnullvm");
}
{
name = "windows_i686_msvc";
Expand All @@ -3031,7 +3031,7 @@ rec {
{
name = "windows_x86_64_gnullvm";
packageId = "windows_x86_64_gnullvm";
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "x86_64-pc-windows-gnullvm");
target = { target, features }: (target.name == "x86_64-pc-windows-gnullvm");
}
{
name = "windows_x86_64_msvc";
Expand Down Expand Up @@ -3154,6 +3154,8 @@ rec {
This corresponds roughly to what buildRustCrate is setting.
*/
makeDefaultTarget = platform: {
name = platform.rust.rustcTarget;

unix = platform.isUnix;
windows = platform.isWindows;
fuchsia = true;
Expand Down Expand Up @@ -3468,7 +3470,7 @@ rec {
packageId: value: buildByPackageIdForPkgsImpl self pkgs packageId
)
crateConfigs;
target = makeTarget stdenv.hostPlatform;
target = makeTarget pkgs.stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
};
in
Expand Down
5 changes: 1 addition & 4 deletions crate2nix/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,7 @@ fn cfg_to_nix_expr_filter(
})?;
Ok(tera::Value::String(cfg_to_nix_expr(&expr)))
} else {
let condition = format!(
"(stdenv.hostPlatform.rust.rustcTarget == {})",
escape_nix_string(key)
);
let condition = format!("(target.name == {})", escape_nix_string(key));
Ok(tera::Value::String(condition))
}
}
Expand Down
6 changes: 3 additions & 3 deletions crate2nix/templates/Cargo.nix.tera
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ rec {
usesDefaultFeatures = false;
{%- endif -%}
{%- if dependency.target %}
target = { target, features }: {{dependency.target | cfg_to_nix_expr | safe }};
target = { target, features }: {{dependency.target | cfg_to_nix_expr | safe}};
{%- endif %}
{%- if dependency.features %}
features = [ {% for feature in dependency.features %}{{feature}} {% endfor %}];
Expand All @@ -246,7 +246,7 @@ rec {
usesDefaultFeatures = false;
{%- endif -%}
{%- if dependency.target %}
target = {target, features}: {{dependency.target | cfg_to_nix_expr | safe }};
target = { target, features }: {{dependency.target | cfg_to_nix_expr | safe}};
{%- endif %}
{%- if dependency.features %}
features = [ {% for feature in dependency.features %}{{feature}} {% endfor %}];
Expand All @@ -271,7 +271,7 @@ rec {
usesDefaultFeatures = false;
{%- endif -%}
{%- if dependency.target %}
target = {target, features}: {{dependency.target | cfg_to_nix_expr | safe }};
target = { target, features }: {{dependency.target | cfg_to_nix_expr | safe}};
{%- endif %}
{%- if dependency.features %}
features = [ {% for feature in dependency.features %}{{feature}} {% endfor %}];
Expand Down
4 changes: 3 additions & 1 deletion crate2nix/templates/nix/crate2nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ rec {
This corresponds roughly to what buildRustCrate is setting.
*/
makeDefaultTarget = platform: {
name = platform.rust.rustcTarget;

unix = platform.isUnix;
windows = platform.isWindows;
fuchsia = true;
Expand Down Expand Up @@ -339,7 +341,7 @@ rec {
packageId: value: buildByPackageIdForPkgsImpl self pkgs packageId
)
crateConfigs;
target = makeTarget stdenv.hostPlatform;
target = makeTarget pkgs.stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
};
in
Expand Down
8 changes: 5 additions & 3 deletions sample_projects/bin_with_git_submodule_dep/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,7 @@ rec {
{
name = "windows_aarch64_gnullvm";
packageId = "windows_aarch64_gnullvm";
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "aarch64-pc-windows-gnullvm");
target = { target, features }: (target.name == "aarch64-pc-windows-gnullvm");
}
{
name = "windows_aarch64_msvc";
Expand All @@ -1281,7 +1281,7 @@ rec {
{
name = "windows_x86_64_gnullvm";
packageId = "windows_x86_64_gnullvm";
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "x86_64-pc-windows-gnullvm");
target = { target, features }: (target.name == "x86_64-pc-windows-gnullvm");
}
{
name = "windows_x86_64_msvc";
Expand Down Expand Up @@ -1372,6 +1372,8 @@ rec {
This corresponds roughly to what buildRustCrate is setting.
*/
makeDefaultTarget = platform: {
name = platform.rust.rustcTarget;

unix = platform.isUnix;
windows = platform.isWindows;
fuchsia = true;
Expand Down Expand Up @@ -1686,7 +1688,7 @@ rec {
packageId: value: buildByPackageIdForPkgsImpl self pkgs packageId
)
crateConfigs;
target = makeTarget stdenv.hostPlatform;
target = makeTarget pkgs.stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
};
in
Expand Down
8 changes: 5 additions & 3 deletions sample_projects/codegen/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,12 @@ rec {
{
name = "winapi-i686-pc-windows-gnu";
packageId = "winapi-i686-pc-windows-gnu";
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "i686-pc-windows-gnu");
target = { target, features }: (target.name == "i686-pc-windows-gnu");
}
{
name = "winapi-x86_64-pc-windows-gnu";
packageId = "winapi-x86_64-pc-windows-gnu";
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "x86_64-pc-windows-gnu");
target = { target, features }: (target.name == "x86_64-pc-windows-gnu");
}
];
features = {
Expand Down Expand Up @@ -547,6 +547,8 @@ rec {
This corresponds roughly to what buildRustCrate is setting.
*/
makeDefaultTarget = platform: {
name = platform.rust.rustcTarget;

unix = platform.isUnix;
windows = platform.isWindows;
fuchsia = true;
Expand Down Expand Up @@ -861,7 +863,7 @@ rec {
packageId: value: buildByPackageIdForPkgsImpl self pkgs packageId
)
crateConfigs;
target = makeTarget stdenv.hostPlatform;
target = makeTarget pkgs.stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
};
in
Expand Down
21 changes: 21 additions & 0 deletions sample_projects/cross_compile_build_dependencies/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions sample_projects/cross_compile_build_dependencies/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "cross_compile_build_dependencies"
version = "1.0.0"
edition = "2021"

[build-dependencies]
alice.path = "./alice"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "alice"
version = "1.0.0"
edition = "2021"

[target.'cfg(unix)'.dependencies]
bob.path = "./bob"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "bob"
version = "1.0.0"
edition = "2021"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(unix)]
pub use bob;
4 changes: 4 additions & 0 deletions sample_projects/cross_compile_build_dependencies/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ generatedCargoNix
, pkgs ? import ../../nix/nixpkgs.nix { crossSystem.config = "wasm32-unknown-none"; }
}:
(pkgs.callPackage generatedCargoNix { }).rootCrate.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
4 changes: 3 additions & 1 deletion sample_projects/sub_dir_crates/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ rec {
This corresponds roughly to what buildRustCrate is setting.
*/
makeDefaultTarget = platform: {
name = platform.rust.rustcTarget;

unix = platform.isUnix;
windows = platform.isWindows;
fuchsia = true;
Expand Down Expand Up @@ -470,7 +472,7 @@ rec {
packageId: value: buildByPackageIdForPkgsImpl self pkgs packageId
)
crateConfigs;
target = makeTarget stdenv.hostPlatform;
target = makeTarget pkgs.stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
};
in
Expand Down
6 changes: 6 additions & 0 deletions tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ let
];
}

{
name = "cross_compile_build_dependencies";
src = ./sample_projects/cross_compile_build_dependencies;
customBuild = "sample_projects/cross_compile_build_dependencies/default.nix";
}

#
# Prefetch tests
#
Expand Down
Loading