Skip to content

Commit 9426a38

Browse files
authored
add buildscript link flags only to parent crate (#448)
@illicitonion Would you mind looking over this and seeing if it's sane? It's modifying code that was added in a previous pull request of yours: #346 I use a crate for creating Python extension modules called pyo3. It has code in it like this: ``` #[cfg_attr(windows, link(name = "pythonXY"))] extern "C" { pub fn PyNode_Compile(arg1: *mut _node, arg2: *const c_char) -> *mut PyCodeObject; ``` Its build.rs script locates the relevant library name, and spits out a link flag that maps it to the alias: ``` -lpythonXY:python38 ``` The pyo3 crate compiles successfully, but when I attempt to compile a crate that depends on pyo3 on Windows, the build fails, complaining that the above alias has been provided but there are no mentions of pythonXY in my crate - because the aliases are only in the pyo3 crate. When I compile my crate using cargo instead of bazel, I can see that pyo3's build script link flags are not being passed in when compiling my crate - so the current behaviour of rules_rust does not seem to match what cargo is doing. The change in this PR fixes the issue for me, and hasn't caused any problems here. But your PR specifically mentions transitive deps, so I wonder if I am missing something. Does this change cause a regression in the problem you were originally trying to fix?
1 parent 21e090e commit 9426a38

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

rust/private/rustc.bzl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -732,15 +732,10 @@ def _create_extra_input_args(ctx, file, build_info, dep_info):
732732
if build_info:
733733
out_dir = build_info.out_dir.path
734734
build_env_file = build_info.rustc_env.path
735-
736-
# out_dir will be added as input by the transitive_build_infos loop below.
737735
build_flags_files.append(build_info.flags.path)
738-
739-
# This should probably only actually be exposed to actions which link.
740-
for dep_build_info in dep_info.transitive_build_infos.to_list():
741-
input_files.append(dep_build_info.out_dir)
742-
build_flags_files.append(dep_build_info.link_flags.path)
743-
input_files.append(dep_build_info.link_flags)
736+
build_flags_files.append(build_info.link_flags.path)
737+
input_files.append(build_info.out_dir)
738+
input_files.append(build_info.link_flags)
744739

745740
return input_files, out_dir, build_env_file, build_flags_files
746741

test/transitive_lib/BUILD

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
load(
2+
"@io_bazel_rules_rust//cargo:cargo_build_script.bzl",
3+
"cargo_build_script",
4+
)
5+
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary", "rust_library")
6+
7+
# sets link alias
8+
cargo_build_script(
9+
name = "buildscript",
10+
srcs = ["build.rs"],
11+
crate_root = "build.rs",
12+
edition = "2018",
13+
)
14+
15+
# links to a symbol in shell32
16+
rust_library(
17+
name = "dll_user",
18+
srcs = ["dll_user.rs"],
19+
crate_type = "lib",
20+
edition = "2018",
21+
deps = [
22+
":buildscript",
23+
],
24+
)
25+
26+
# does not link to any symbol in shell32
27+
rust_binary(
28+
name = "dll_user_user",
29+
srcs = ["dll_user_user.rs"],
30+
edition = "2018",
31+
deps = [
32+
":dll_user",
33+
],
34+
)

test/transitive_lib/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("cargo:rustc-link-lib=alias:shell32");
3+
}

test/transitive_lib/dll_user.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[link(name = "alias")]
2+
extern "C" {
3+
// random symbol from shell32
4+
pub fn LocalFree(ptr: *mut std::os::raw::c_void);
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
// this file does not link to any shell32 symbols directly, and
3+
// will thus cause a compile error if -lalias:shell32
4+
// is present in the link flags
5+
}

0 commit comments

Comments
 (0)