rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

cargo_build_script should not forward data to build.rs rust_binary

Open ashi009 opened this issue 1 year ago • 0 comments

When embedding generated targets in the cargo_build_script's data attribute, it will be embedded as runfiles for the actual script's rust_binary target. As cargo_build_script performs config transition for the script attribute, all the data dependencies will be built again using the exec configuration. This is incorrect, as the data required by the build.rs should be built into the target configuration to be linked correctly when cross-compiling.

Taking glib-sys as an example:

rust_library(
    name = "glib_sys",
    deps = [
        "@crate_index__glib-sys-0.18.1//:build_script_build",
        "@crate_index__libc-0.2.155//:libc",
        "@glib//:glib",
    ],
	...
)

cargo_build_script(
    name = "glib-sys_bs",
    crate_name = "build_script_build",
    crate_root = "build.rs",
    data = glob(
        allow_empty = True,
        include = ["**"],
        exclude = [
            "**/* *",
            ".tmp_git_root/**/*",
            "BUILD",
            "BUILD.bazel",
            "WORKSPACE",
            "WORKSPACE.bazel",
        ],
    ) + [
        "@glib//:glib", # <-- target cfg
    ],
    deps = [
        "@crate_index__system-deps-6.2.2//:system_deps",
    ],
	...
)

which expands to:

cargo_build_script(
    name = "glib-sys_bs",
    data = [
		...
        "@glib//:glib",
    ],
    deps = ["@crate_index__system-deps-6.2.2//:system_deps"],
    script = "@crate_index__glib-sys-0.18.1//:glib-sys_bs_",
	...
)

rust_binary(
    name = "glib-sys_bs_",
    compile_data = [],
    crate_features = [],
    crate_name = "build_script_build",
    crate_root = "@crate_index__glib-sys-0.18.1//:build.rs",
    data = [
		...
        "@glib//:glib", # <-- exec cfg
    ],
    deps = ["@crate_index__system-deps-6.2.2//:system_deps"],
	...
)

ashi009 avatar Jul 29 '24 13:07 ashi009