Skip to content

Commit ab4e325

Browse files
committed
PR feedback
1 parent 57c23ee commit ab4e325

File tree

3 files changed

+57
-40
lines changed

3 files changed

+57
-40
lines changed

rust/private/repository_utils.bzl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -383,46 +383,46 @@ def BUILD_for_rust_toolchain(
383383
if stdlib_linkflags == None:
384384
stdlib_linkflags = ", ".join(['"%s"' % x for x in system_to_stdlib_linkflags(target_triple.system)])
385385

386-
rustfmt_label = "None"
386+
rustfmt_label = None
387387
if include_rustfmt:
388-
rustfmt_label = "\"//:rustfmt_bin\""
389-
llvm_cov_label = "None"
390-
llvm_profdata_label = "None"
391-
llvm_lib_label = "None"
388+
rustfmt_label = "//:rustfmt_bin"
389+
llvm_cov_label = None
390+
llvm_profdata_label = None
391+
llvm_lib_label = None
392392
if include_llvm_tools:
393-
llvm_cov_label = "\"//:llvm_cov_bin\""
394-
llvm_profdata_label = "\"//:llvm_profdata_bin\""
395-
llvm_lib_label = "\"//:llvm_lib\""
396-
allocator_library_label = "None"
393+
llvm_cov_label = "//:llvm_cov_bin"
394+
llvm_profdata_label = "//:llvm_profdata_bin"
395+
llvm_lib_label = "//:llvm_lib"
396+
allocator_library_label = None
397397
if allocator_library:
398-
allocator_library_label = "\"{allocator_library}\"".format(allocator_library = allocator_library)
399-
global_allocator_library_label = "None"
398+
allocator_library_label = allocator_library
399+
global_allocator_library_label = None
400400
if global_allocator_library:
401-
global_allocator_library_label = "\"{global_allocator_library}\"".format(global_allocator_library = global_allocator_library)
401+
global_allocator_library_label = global_allocator_library
402402

403-
linker_label = "None"
404-
linker_type = "None"
403+
linker_label = None
404+
linker_type = None
405405
if include_linker:
406-
linker_label = "\"//:rust-lld\""
407-
linker_type = "\"direct\""
406+
linker_label = "//:rust-lld"
407+
linker_type = "direct"
408408

409409
return _build_file_for_rust_toolchain_template.format(
410410
toolchain_name = name,
411411
binary_ext = system_to_binary_ext(target_triple.system),
412412
staticlib_ext = system_to_staticlib_ext(target_triple.system),
413413
dylib_ext = system_to_dylib_ext(target_triple.system),
414-
allocator_library = allocator_library_label,
415-
global_allocator_library = global_allocator_library_label,
414+
allocator_library = repr(allocator_library_label),
415+
global_allocator_library = repr(global_allocator_library_label),
416416
stdlib_linkflags = stdlib_linkflags,
417417
default_edition = default_edition,
418418
exec_triple = exec_triple.str,
419419
target_triple = target_triple.str,
420-
rustfmt_label = rustfmt_label,
421-
llvm_cov_label = llvm_cov_label,
422-
llvm_profdata_label = llvm_profdata_label,
423-
llvm_lib_label = llvm_lib_label,
424-
linker_label = linker_label,
425-
linker_type = linker_type,
420+
rustfmt_label = repr(rustfmt_label),
421+
llvm_cov_label = repr(llvm_cov_label),
422+
llvm_profdata_label = repr(llvm_profdata_label),
423+
llvm_lib_label = repr(llvm_lib_label),
424+
linker_label = repr(linker_label),
425+
linker_type = repr(linker_type),
426426
extra_rustc_flags = extra_rustc_flags,
427427
extra_exec_rustc_flags = extra_exec_rustc_flags,
428428
opt_level = opt_level,

rust/settings/settings.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,12 @@ def toolchain_linker_preference():
251251
"""A flag to control which linker is preferred for linking Rust binaries.
252252
253253
Accepts three values:
254-
- "rust": Use `rust_toolchain.linker` always.
255-
- "cc": Use the linker provided by the configured `cc_toolchain`
256-
- "none": Default to `cc` being the preference and falling back to `rust`.
254+
- "rust": Use `rust_toolchain.linker` always (e.g., `rust-lld`). This uses rustc to invoke
255+
the linker directly.
256+
- "cc": Use the linker provided by the configured `cc_toolchain`. This uses rustc to invoke
257+
the C++ toolchain's linker (e.g., `clang`, `gcc`, `link.exe`).
258+
- "none": Default to `cc` being the preference and falling back to `rust` if no `cc_toolchain`
259+
is available.
257260
"""
258261
string_flag(
259262
name = "toolchain_linker_preference",

rust/toolchain.bzl

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def _generate_sysroot(
238238

239239
# Rustc
240240
sysroot_rustc = _symlink_sysroot_bin(ctx, name, "bin", rustc)
241-
direct_files.extend([sysroot_rustc])
241+
direct_files.append(sysroot_rustc)
242242

243243
# Rustc dependencies
244244
sysroot_rustc_lib = None
@@ -248,31 +248,31 @@ def _generate_sysroot(
248248

249249
# Rustdoc
250250
sysroot_rustdoc = _symlink_sysroot_bin(ctx, name, "bin", rustdoc)
251-
direct_files.extend([sysroot_rustdoc])
251+
direct_files.append(sysroot_rustdoc)
252252

253253
# Clippy
254254
sysroot_clippy = None
255255
if clippy:
256256
sysroot_clippy = _symlink_sysroot_bin(ctx, name, "bin", clippy)
257-
direct_files.extend([sysroot_clippy])
257+
direct_files.append(sysroot_clippy)
258258

259259
# Cargo
260260
sysroot_cargo = None
261261
if cargo:
262262
sysroot_cargo = _symlink_sysroot_bin(ctx, name, "bin", cargo)
263-
direct_files.extend([sysroot_cargo])
263+
direct_files.append(sysroot_cargo)
264264

265265
# Cargo-clippy
266266
sysroot_cargo_clippy = None
267267
if cargo_clippy:
268268
sysroot_cargo_clippy = _symlink_sysroot_bin(ctx, name, "bin", cargo_clippy)
269-
direct_files.extend([sysroot_cargo_clippy])
269+
direct_files.append(sysroot_cargo_clippy)
270270

271271
# Rustfmt
272272
sysroot_rustfmt = None
273273
if rustfmt:
274274
sysroot_rustfmt = _symlink_sysroot_bin(ctx, name, "bin", rustfmt)
275-
direct_files.extend([sysroot_rustfmt])
275+
direct_files.append(sysroot_rustfmt)
276276

277277
# Linker
278278
sysroot_linker = None
@@ -292,8 +292,8 @@ def _generate_sysroot(
292292

293293
sysroot_linker = _symlink_sysroot_bin(ctx, name, dest, linker_bin)
294294
sysroot_linker_files = _symlink_sysroot_tree(ctx, name, linker, linker[DefaultInfo].default_runfiles.files)
295-
direct_files.extend([sysroot_linker])
296-
transitive_file_sets.extend([sysroot_linker_files])
295+
direct_files.append(sysroot_linker)
296+
transitive_file_sets.append(sysroot_linker_files)
297297

298298
# Llvm tools
299299
sysroot_llvm_tools = None
@@ -720,11 +720,8 @@ rust_toolchain = rule(
720720
default = "@rules_rust//ffi/cc/global_allocator_library",
721721
),
722722
"linker": attr.label(
723-
doc = "The label to an explicit linker to use (e.g. `rust-lld`, `ld`, `link-ld.exe`, etc...)",
724-
# `target` cfg is used so a linker can be chose based on the target
725-
# platform. Linker binaries are still required to be runnable in the
726-
# `exec` configuration.
727-
cfg = "target",
723+
doc = "The label to an explicit linker to use (e.g. rust-lld, ld, link-ld.exe, etc.). Linker binaries must be runnable in the exec configuration, so cfg = \"exec\" is used. To choose a linker based on the target platform, use a select() when providing this attribute. The select() will be evaluated against the target platform before the exec transition is applied, allowing platform-specific linker selection while ensuring the selected linker is built for the exec platform.",
724+
cfg = "exec",
728725
allow_single_file = True,
729726
),
730727
"linker_preference": attr.string(
@@ -935,5 +932,22 @@ it to the `"--extra_toolchains"` flag for Bazel, and it will be used.
935932
936933
See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpuX` repository \
937934
with the actual binaries and libraries.
935+
936+
To use a platform-specific linker, you can use a `select()` in the `linker` attribute:
937+
938+
```python
939+
rust_toolchain(
940+
name = "rust_toolchain_impl",
941+
# ... other attributes ...
942+
linker = select({
943+
"@platforms//os:linux": "//tools:rust-lld-linux",
944+
"@platforms//os:windows": "//tools:rust-lld-windows",
945+
"//conditions:default": "//tools:rust-lld",
946+
}),
947+
)
948+
```
949+
950+
The `select()` is evaluated against the target platform before the exec transition is applied, \
951+
allowing platform-specific linker selection while ensuring the selected linker is built for the exec platform.
938952
""",
939953
)

0 commit comments

Comments
 (0)