Skip to content

Commit b3cc7e4

Browse files
authored
#190, #200 :: Migrate to new cc provider. (#209)
* #190, #200 :: Migrate to new cc provider. * re-replace dep.cc.libs
1 parent e6aea30 commit b3cc7e4

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

bindgen/bindgen.bzl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")
16+
load("@io_bazel_rules_rust//rust:private/legacy_cc_starlark_api_shim.bzl", "get_libs_for_static_executable")
1617

1718
def rust_bindgen_library(
1819
name,
@@ -44,7 +45,7 @@ def _rust_bindgen_impl(ctx):
4445
# nb. We can't grab the cc_library`s direct headers, so a header must be provided.
4546
cc_lib = ctx.attr.cc_lib
4647
header = ctx.file.header
47-
if header not in cc_lib.cc.transitive_headers:
48+
if header not in cc_lib[CcInfo].compilation_context.headers:
4849
fail("Header {} is not in {}'s transitive headers.".format(ctx.attr.header, cc_lib), "header")
4950

5051
toolchain = ctx.toolchains["@io_bazel_rules_rust//bindgen:bindgen_toolchain"]
@@ -68,10 +69,10 @@ def _rust_bindgen_impl(ctx):
6869
output = ctx.outputs.out
6970

7071
# libclang should only have 1 output file
71-
libclang_dir = libclang.cc.libs.to_list()[0].dirname
72-
include_directories = cc_lib.cc.include_directories
73-
quote_include_directories = cc_lib.cc.quote_include_directories
74-
system_include_directories = cc_lib.cc.system_include_directories
72+
libclang_dir = get_libs_for_static_executable(libclang).to_list()[0].dirname
73+
include_directories = cc_lib[CcInfo].compilation_context.includes.to_list()
74+
quote_include_directories = cc_lib[CcInfo].compilation_context.quote_includes.to_list()
75+
system_include_directories = cc_lib[CcInfo].compilation_context.system_includes.to_list()
7576

7677
# Vanilla usage of bindgen produces formatted output, here we do the same if we have `rustfmt` in our toolchain.
7778
if rustfmt_bin:
@@ -92,7 +93,11 @@ def _rust_bindgen_impl(ctx):
9293
executable = bindgen_bin,
9394
inputs = depset(
9495
[header],
95-
transitive = [cc_lib.cc.transitive_headers, libclang.cc.libs, libstdcxx.cc.libs],
96+
transitive = [
97+
cc_lib[CcInfo].compilation_context.headers,
98+
get_libs_for_static_executable(libclang),
99+
get_libs_for_static_executable(libstdcxx),
100+
],
96101
),
97102
outputs = [unformatted_output],
98103
mnemonic = "RustBindgen",
@@ -102,7 +107,7 @@ def _rust_bindgen_impl(ctx):
102107
"CLANG_PATH": clang_bin.path,
103108
# Bindgen loads libclang at runtime, which also needs libstdc++, so we setup LD_LIBRARY_PATH
104109
"LIBCLANG_PATH": libclang_dir,
105-
"LD_LIBRARY_PATH": ":".join([f.dirname for f in libstdcxx.cc.libs]),
110+
"LD_LIBRARY_PATH": ":".join([f.dirname for f in get_libs_for_static_executable(libstdcxx)]),
106111
},
107112
arguments = [args],
108113
tools = [clang_bin],
@@ -126,7 +131,7 @@ rust_bindgen = rule(
126131
),
127132
"cc_lib": attr.label(
128133
doc = "The cc_library that contains the .h file. This is used to find the transitive includes.",
129-
providers = ["cc"],
134+
providers = [CcInfo],
130135
),
131136
"bindgen_flags": attr.string_list(
132137
doc = "Flags to pass directly to the bindgen executable. See https://rust-lang.github.io/rust-bindgen/ for details.",
@@ -173,12 +178,12 @@ rust_bindgen_toolchain = rule(
173178
"libclang": attr.label(
174179
doc = "A cc_library that provides bindgen's runtime dependency on libclang.",
175180
cfg = "host",
176-
providers = ["cc"],
181+
providers = [CcInfo],
177182
),
178183
"libstdcxx": attr.label(
179184
doc = "A cc_library that satisfies libclang's libstdc++ dependency.",
180185
cfg = "host",
181-
providers = ["cc"],
186+
providers = [CcInfo],
182187
),
183188
},
184189
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Part of --incompatible_disable_legacy_cc_provider migration (https://github.com/bazelbuild/bazel/issues/7036)
3+
"""
4+
5+
def get_libs_for_static_executable(dep):
6+
"""
7+
This replaces dep.cc.libs, which found the libraries
8+
used for linking a static executable.
9+
10+
Args:
11+
dep: A cc_library target.
12+
Returns:
13+
A depset[File]
14+
"""
15+
libraries_to_link = dep[CcInfo].linking_context.libraries_to_link
16+
return depset([_get_preferred_artifact(lib) for lib in libraries_to_link])
17+
18+
def _get_preferred_artifact(library_to_link):
19+
return (
20+
library_to_link.static_library or
21+
library_to_link.pic_static_library or
22+
library_to_link.interface_library or
23+
library_to_link.dynamic_library
24+
)

rust/private/rustc.bzl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
load("@io_bazel_rules_rust//rust:private/utils.bzl", "relative_path")
16+
load("@io_bazel_rules_rust//rust:private/legacy_cc_starlark_api_shim.bzl", "get_libs_for_static_executable")
1617
load(
1718
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
1819
"CPP_LINK_EXECUTABLE_ACTION_NAME",
@@ -106,10 +107,13 @@ def collect_deps(deps, toolchain):
106107
transitive_crates = depset(transitive = [transitive_crates, dep[DepInfo].transitive_crates])
107108
transitive_dylibs = depset(transitive = [transitive_dylibs, dep[DepInfo].transitive_dylibs])
108109
transitive_staticlibs = depset(transitive = [transitive_staticlibs, dep[DepInfo].transitive_staticlibs])
109-
elif hasattr(dep, "cc"):
110+
elif CcInfo in dep:
110111
# This dependency is a cc_library
111-
dylibs = [l for l in dep.cc.libs.to_list() if l.basename.endswith(toolchain.dylib_ext)]
112-
staticlibs = [l for l in dep.cc.libs.to_list() if l.basename.endswith(toolchain.staticlib_ext)]
112+
113+
# TODO: We could let the user choose how to link, instead of always preferring to link static libraries.
114+
libs = get_libs_for_static_executable(dep)
115+
dylibs = [l for l in libs.to_list() if l.basename.endswith(toolchain.dylib_ext)]
116+
staticlibs = [l for l in libs.to_list() if l.basename.endswith(toolchain.staticlib_ext)]
113117
transitive_dylibs = depset(transitive = [transitive_dylibs, depset(dylibs)])
114118
transitive_staticlibs = depset(transitive = [transitive_staticlibs, depset(staticlibs)])
115119
else:
@@ -182,8 +186,7 @@ def rustc_compile_action(
182186
toolchain,
183187
)
184188

185-
linker_script = getattr(ctx.file, "linker_script") if hasattr(ctx.file, "linker_script") \
186-
else None
189+
linker_script = getattr(ctx.file, "linker_script") if hasattr(ctx.file, "linker_script") else None
187190

188191
compile_inputs = depset(
189192
crate_info.srcs +

0 commit comments

Comments
 (0)