Skip to content

Commit f197ab8

Browse files
Make libclang attr as optional in the rust_bindgen_toolchain (#3788)
The `libclang` attr is currently used to set env variables for the underlying `clang-sys` crate to dynamically link libclang. In build systems that use static linking, these are redundant. Making libclang attr optional simplifies the toolchain config for static linking. --------- Co-authored-by: Krasimir Georgiev <krasimir@google.com>
1 parent b0daae0 commit f197ab8

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

extensions/bindgen/private/bindgen.bzl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,24 @@ def _rust_bindgen_impl(ctx):
216216
toolchain = ctx.toolchains[Label("//:toolchain_type")]
217217
bindgen_bin = toolchain.bindgen
218218
clang_bin = toolchain.clang
219-
libclang = toolchain.libclang
220-
libstdcxx = toolchain.libstdcxx
219+
libclang = getattr(toolchain, "libclang", None)
220+
libstdcxx = getattr(toolchain, "libstdcxx", None)
221221

222222
output = ctx.outputs.out
223223

224224
cc_toolchain, feature_configuration = find_cc_toolchain(ctx = ctx)
225225

226226
tools = depset(([clang_bin] if clang_bin else []), transitive = [cc_toolchain.all_files])
227227

228-
# libclang should only have 1 output file
229-
libclang_dir = _get_libs_for_static_executable(libclang).to_list()[0].dirname
230-
231228
env = {
232-
"LIBCLANG_PATH": libclang_dir,
233229
"RUST_BACKTRACE": "1",
234230
}
231+
232+
if libclang:
233+
# libclang should only have 1 output file
234+
libclang_dir = _get_libs_for_static_executable(libclang).to_list()[0].dirname
235+
env["LIBCLANG_PATH"] = libclang_dir
236+
235237
if clang_bin:
236238
env["CLANG_PATH"] = clang_bin.path
237239

@@ -381,8 +383,9 @@ def _rust_bindgen_impl(ctx):
381383
[header],
382384
transitive = [
383385
cc_lib[CcInfo].compilation_context.headers,
384-
_get_libs_for_static_executable(libclang),
385386
] + ([
387+
_get_libs_for_static_executable(libclang),
388+
] if libclang else []) + ([
386389
_get_libs_for_static_executable(libstdcxx),
387390
] if libstdcxx else []),
388391
),
@@ -528,10 +531,11 @@ For additional information, see the [Bazel toolchains documentation](https://doc
528531
default = True,
529532
),
530533
"libclang": attr.label(
531-
doc = "A cc_library that provides bindgen's runtime dependency on libclang.",
534+
doc = "A cc_library providing bindgen's runtime dependency on libclang. This attribute is required for hermeticity when bindgen is dynamically linked. If None, bindgen must be statically linked; else, system libraries will be used instead.",
532535
cfg = "exec",
533536
providers = [CcInfo],
534537
allow_files = True,
538+
mandatory = False,
535539
),
536540
"libstdcxx": attr.label(
537541
doc = "A cc_library that satisfies libclang's libstdc++ dependency. This is used to make the execution of clang hermetic. If None, system libraries will be used instead.",

0 commit comments

Comments
 (0)