Skip to content

Commit 3563a3d

Browse files
committed
Add rust-objcopy as optional rust_toolchain attribute
Adds `rust-objcopy` as a separate filegroup and optional attribute on `rust_toolchain`, following the same pattern as `rust-lld`. The filegroup uses `allow_empty = True` for compatibility with Rust versions before 1.84.0 where the binary is not available.
1 parent cdaf15f commit 3563a3d

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

rust/private/repository_utils.bzl

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,24 @@ filegroup(
9090
)
9191
"""
9292

93-
def BUILD_for_compiler(target_triple, include_linker = False):
93+
_build_file_for_objcopy_template = """\
94+
filegroup(
95+
name = "rust-objcopy",
96+
srcs = glob(
97+
["lib/rustlib/{target_triple}/bin/rust-objcopy{binary_ext}"],
98+
allow_empty = True,
99+
),
100+
visibility = ["//visibility:public"],
101+
)
102+
"""
103+
104+
def BUILD_for_compiler(target_triple, include_linker = False, include_objcopy = False):
94105
"""Emits a BUILD file the compiler archive.
95106
96107
Args:
97108
target_triple (str): The triple of the target platform
98109
include_linker (bool): Whether to generate targets for linkers.
110+
include_objcopy (bool): Whether to generate targets for rust-objcopy.
99111
100112
Returns:
101113
str: The contents of a BUILD file
@@ -117,6 +129,14 @@ def BUILD_for_compiler(target_triple, include_linker = False):
117129
),
118130
)
119131

132+
if include_objcopy:
133+
content.append(
134+
_build_file_for_objcopy_template.format(
135+
binary_ext = system_to_binary_ext(target_triple.system),
136+
target_triple = target_triple.str,
137+
),
138+
)
139+
120140
return "\n".join(content)
121141

122142
_build_file_for_cargo_template = """\
@@ -324,6 +344,7 @@ rust_toolchain(
324344
rustc = "//:rustc",
325345
linker = {linker_label},
326346
linker_type = {linker_type},
347+
rust_objcopy = {rust_objcopy_label},
327348
rustfmt = {rustfmt_label},
328349
cargo = "//:cargo",
329350
clippy_driver = "//:clippy_driver_bin",
@@ -361,6 +382,7 @@ def BUILD_for_rust_toolchain(
361382
include_rustfmt,
362383
include_llvm_tools,
363384
include_linker,
385+
include_objcopy = False,
364386
stdlib_linkflags = None,
365387
extra_rustc_flags = None,
366388
extra_exec_rustc_flags = None,
@@ -381,6 +403,7 @@ def BUILD_for_rust_toolchain(
381403
include_rustfmt (bool): Whether rustfmt is present in the toolchain.
382404
include_llvm_tools (bool): Whether llvm-tools are present in the toolchain.
383405
include_linker (bool): Whether a linker is available in the toolchain.
406+
include_objcopy (bool): Whether rust-objcopy is available in the toolchain.
384407
stdlib_linkflags (list, optional): Overridden flags needed for linking to rust
385408
stdlib, akin to BAZEL_LINKLIBS. Defaults to
386409
None.
@@ -418,6 +441,10 @@ def BUILD_for_rust_toolchain(
418441
linker_label = "//:rust-lld"
419442
linker_type = "direct"
420443

444+
rust_objcopy_label = None
445+
if include_objcopy:
446+
rust_objcopy_label = "//:rust-objcopy"
447+
421448
return _build_file_for_rust_toolchain_template.format(
422449
toolchain_name = name,
423450
binary_ext = system_to_binary_ext(target_triple.system),
@@ -435,6 +462,7 @@ def BUILD_for_rust_toolchain(
435462
llvm_lib_label = repr(llvm_lib_label),
436463
linker_label = repr(linker_label),
437464
linker_type = repr(linker_type),
465+
rust_objcopy_label = repr(rust_objcopy_label),
438466
extra_rustc_flags = extra_rustc_flags,
439467
extra_exec_rustc_flags = extra_exec_rustc_flags,
440468
opt_level = opt_level,
@@ -519,7 +547,7 @@ def load_rustfmt(ctx, target_triple, version, iso_date):
519547

520548
return BUILD_for_rustfmt(target_triple), sha256
521549

522-
def load_rust_compiler(ctx, iso_date, target_triple, version, include_linker = False):
550+
def load_rust_compiler(ctx, iso_date, target_triple, version, include_linker = False, include_objcopy = False):
523551
"""Loads a rust compiler and yields corresponding BUILD for it
524552
525553
Args:
@@ -528,6 +556,7 @@ def load_rust_compiler(ctx, iso_date, target_triple, version, include_linker = F
528556
target_triple (struct): The Rust-style target that this compiler runs on.
529557
version (str): The version of the tool among \"nightly\", \"beta\", or an exact version.
530558
include_linker (bool): Whether to include linker targets in the output BUILD contents.
559+
include_objcopy (bool): Whether to include rust-objcopy targets in the output BUILD contents.
531560
532561
Returns:
533562
Tuple[str, Dict[str, str]]: The BUILD file contents for this compiler and compiler library
@@ -543,7 +572,7 @@ def load_rust_compiler(ctx, iso_date, target_triple, version, include_linker = F
543572
version = version,
544573
)
545574

546-
return BUILD_for_compiler(target_triple, include_linker), sha256
575+
return BUILD_for_compiler(target_triple, include_linker, include_objcopy), sha256
547576

548577
def load_clippy(ctx, iso_date, target_triple, version):
549578
"""Loads Clippy and yields corresponding BUILD for it

rust/repositories.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,13 +449,15 @@ def _rust_toolchain_tools_repository_impl(ctx):
449449
exec_triple = triple(ctx.attr.exec_triple)
450450

451451
include_linker = True
452+
include_objcopy = True
452453

453454
rustc_content, rustc_sha256 = load_rust_compiler(
454455
ctx = ctx,
455456
iso_date = iso_date,
456457
target_triple = exec_triple,
457458
version = version,
458459
include_linker = include_linker,
460+
include_objcopy = include_objcopy,
459461
)
460462
clippy_content, clippy_sha256 = load_clippy(
461463
ctx = ctx,
@@ -534,6 +536,7 @@ def _rust_toolchain_tools_repository_impl(ctx):
534536
include_rustfmt = not (not ctx.attr.rustfmt_version),
535537
include_llvm_tools = include_llvm_tools,
536538
include_linker = include_linker,
539+
include_objcopy = include_objcopy,
537540
extra_rustc_flags = ctx.attr.extra_rustc_flags,
538541
extra_exec_rustc_flags = ctx.attr.extra_exec_rustc_flags,
539542
opt_level = ctx.attr.opt_level if ctx.attr.opt_level else None,
@@ -769,6 +772,7 @@ _RUST_ANALYZER_TOOLCHAIN_TOOLS_REPOSITORY_ATTRS = {
769772
def _rust_analyzer_toolchain_tools_repository_impl(repository_ctx):
770773
sha256s = dict(repository_ctx.attr.sha256s)
771774
include_linker = True
775+
include_objcopy = True
772776

773777
iso_date = None
774778
version = repository_ctx.attr.version
@@ -794,6 +798,7 @@ def _rust_analyzer_toolchain_tools_repository_impl(repository_ctx):
794798
target_triple = host_triple,
795799
version = version,
796800
include_linker = include_linker,
801+
include_objcopy = include_objcopy,
797802
)
798803
build_contents = [rustc_content]
799804
sha256s.update(rustc_sha256)
@@ -929,6 +934,7 @@ def _rustfmt_toolchain_tools_repository_impl(repository_ctx):
929934
))
930935

931936
include_linker = True
937+
include_objcopy = True
932938

933939
iso_date = None
934940
version = repository_ctx.attr.version
@@ -949,6 +955,7 @@ def _rustfmt_toolchain_tools_repository_impl(repository_ctx):
949955
target_triple = exec_triple,
950956
version = version,
951957
include_linker = include_linker,
958+
include_objcopy = include_objcopy,
952959
)
953960
rustfmt_content, rustfmt_sha256 = load_rustfmt(
954961
ctx = repository_ctx,

rust/toolchain.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ def _rust_toolchain_impl(ctx):
585585
llvm_cov = ctx.file.llvm_cov,
586586
llvm_profdata = ctx.file.llvm_profdata,
587587
llvm_lib = ctx.files.llvm_lib,
588+
rust_objcopy = ctx.file.rust_objcopy,
588589
lto = lto,
589590
make_variables = make_variable_info,
590591
rust_doc = sysroot.rustdoc,
@@ -751,6 +752,11 @@ rust_toolchain = rule(
751752
doc = "LLVM tools that are shipped with the Rust toolchain.",
752753
allow_files = True,
753754
),
755+
"rust_objcopy": attr.label(
756+
doc = "The location of the `rust-objcopy` binary. Can be a direct source or a filegroup containing one item.",
757+
allow_single_file = True,
758+
cfg = "exec",
759+
),
754760
"lto": attr.label(
755761
providers = [RustLtoInfo],
756762
default = Label("//rust/settings:lto"),

0 commit comments

Comments
 (0)