Skip to content

Commit 980b662

Browse files
authored
add static_library to get_preferred_artifact with pic (#1183)
1 parent 97fd329 commit 980b662

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

examples/shared_libs/BUILD.bazel

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library")
2+
load("@rules_rust//rust:defs.bzl", "rust_shared_library")
3+
4+
# A rust_shared_library (forcing the use of pic) that depends on a native
5+
# linker library with only a static_library member.
6+
rust_shared_library(
7+
name = "rust_shared_lib_with_static_dep",
8+
srcs = ["rust_shared_lib_with_static_dep.rs"],
9+
deps = [":static_cclib"],
10+
)
11+
12+
cc_library(
13+
name = "nonstandard_name_cc_lib",
14+
srcs = ["cc_library_with_func.cc"],
15+
)
16+
17+
genrule(
18+
name = "nonstandard_name_gen",
19+
srcs = [":nonstandard_name_cc_lib"],
20+
outs = ["nonstandard_name_gen.a"],
21+
# Copy the first member (libnonstandard_name_cc_lib.a) from the srcs to the
22+
# output nonstandard_name_gen.a.
23+
cmd = "cp $$(awk '{print $$1}' <<< '$(SRCS)') $@",
24+
)
25+
26+
cc_import(
27+
name = "static_cclib",
28+
static_library = "nonstandard_name_gen.a",
29+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extern "C" int func() {
2+
return 123;
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use std::os::raw::c_int;
2+
3+
extern "C" {
4+
pub fn func() -> c_int;
5+
}
6+
7+
pub fn f() {
8+
println!("hi {}", unsafe { func() });
9+
}

rust/private/utils.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ def get_preferred_artifact(library_to_link, use_pic):
162162
File: Returns the first valid library type (only one is expected)
163163
"""
164164
if use_pic:
165+
# Order consistent with https://github.com/bazelbuild/bazel/blob/815dfdabb7df31d4e99b6fc7616ff8e2f9385d98/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingContext.java#L437.
165166
return (
166167
library_to_link.pic_static_library or
168+
library_to_link.static_library or
167169
library_to_link.interface_library or
168170
library_to_link.dynamic_library
169171
)

0 commit comments

Comments
 (0)