Skip to content

Commit e7e25a9

Browse files
authored
use an empty staticlib as an allocator library for cc_common.link (#3864)
No functional changes intended. As suggested on multiple channels, using an empty staticlib is a good effective way to ensure we get the rust allocator shims generated consistently. This is simpler than what we have and also looks like it uniformly supports stable and nightly across versions. closes #3459
1 parent 5f633d2 commit e7e25a9

5 files changed

Lines changed: 32 additions & 135 deletions

File tree

ffi/rs/allocator_library/BUILD.bazel

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
1-
load("@rules_rust//rust:defs.bzl", "rust_library")
1+
load("@rules_rust//rust:defs.bzl", "rust_static_library")
22

33
# buildifier: disable=bzl-visibility
44
load(
55
"@rules_rust//rust/private:rust.bzl",
6-
"rust_library_without_process_wrapper",
6+
"rust_static_library_without_process_wrapper",
77
)
88

99
package(
1010
default_visibility = ["@rules_rust//ffi/rs:__subpackages__"],
1111
)
1212

13-
srcs = select({
14-
# Windows doesn't support weak symbol linkage.
15-
# If someone can make this work on Windows, please do!
16-
# For now we will silently not supply any symbols, because it would be very messy to conditionally define the default allocator library on toolchains depending on the platform.
17-
"@platforms//os:windows": ["empty.rs"],
18-
"//conditions:default": ["allocator_library.rs"],
19-
})
13+
# When building a staticlib crate, rustc injects the default allocator
14+
# redirections (e.g., __rust_alloc -> __rdl_alloc).
15+
# So we use an empty static library to provide these.
2016

21-
rust_library(
17+
rust_static_library(
2218
name = "allocator_library",
23-
srcs = srcs,
19+
srcs = ["empty.rs"],
2420
allocator_libraries = "@rules_rust//ffi/rs:empty_allocator_libraries",
2521
edition = "2024",
2622
tags = ["manual"],
2723
)
2824

29-
rust_library_without_process_wrapper(
25+
rust_static_library_without_process_wrapper(
3026
name = "allocator_library_without_process_wrapper",
31-
srcs = srcs,
27+
srcs = ["empty.rs"],
3228
allocator_libraries = "@rules_rust//ffi/rs:empty_allocator_libraries",
3329
edition = "2024",
3430
tags = ["manual"],

ffi/rs/allocator_library/allocator_library.rs

Lines changed: 0 additions & 98 deletions
This file was deleted.

rust/private/rust.bzl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,26 @@ rust_library_without_process_wrapper = rule(
13821382
],
13831383
)
13841384

1385+
def _rust_static_library_without_process_wrapper_impl(ctx):
1386+
providers = _rust_static_library_impl(ctx)
1387+
return providers + [_RustBuiltWithoutProcessWrapperInfo()]
1388+
1389+
rust_static_library_without_process_wrapper = rule(
1390+
implementation = _rust_static_library_without_process_wrapper_impl,
1391+
doc = "A variant of `rust_static_library` that uses a minimal process wrapper for `Rustc` actions.",
1392+
attrs = dict(_common_attrs_for_binary_without_process_wrapper(_common_attrs).items()),
1393+
fragments = ["cpp"],
1394+
toolchains = [
1395+
str(Label("//rust:toolchain_type")),
1396+
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
1397+
],
1398+
provides = [
1399+
CcInfo,
1400+
rust_common.test_crate_info,
1401+
_RustBuiltWithoutProcessWrapperInfo,
1402+
],
1403+
)
1404+
13851405
def _test_attrs_for_binary_without_process_wrapper(attrs):
13861406
new_attrs = {}
13871407
new_attrs.update(attrs)

rust/private/rustc.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,9 @@ def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_co
19801980
cc_infos.append(libstd_and_allocator_cc_info)
19811981

19821982
providers = [cc_common.merge_cc_infos(cc_infos = cc_infos)]
1983+
if crate_info.type == "staticlib":
1984+
# The static archive is the output.
1985+
dot_a = crate_info.output
19831986
if dot_a:
19841987
providers.append(AllocatorLibrariesImplInfo(static_archive = dot_a))
19851988
return providers

test/integration/cc_common_link/MODULE.bazel

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,6 @@ bazel_dep(name = "platforms", version = "1.0.0")
1616
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
1717
rust.toolchain(
1818
edition = "2018",
19-
target_settings = [
20-
"@rules_rust//rust/settings:experimental_use_allocator_libraries_with_mangled_symbols_off",
21-
],
22-
)
23-
24-
# Generate a toolchain to be used for rust-based allocator symbols.
25-
26-
# A recent enough version of rustc that mangles the internal allocator symbols.
27-
VERSION = "nightly/2025-07-08"
28-
29-
rust.repository_set(
30-
name = "rust_with_alloc_mangling_linux_x86_64",
31-
allocator_library = "@rules_rust//ffi/rs:empty",
32-
edition = "2021",
33-
exec_triple = "x86_64-unknown-linux-gnu",
34-
target_compatible_with = [
35-
"@platforms//cpu:x86_64",
36-
"@platforms//os:linux",
37-
],
38-
target_settings = [
39-
"@rules_rust//rust/settings:experimental_use_allocator_libraries_with_mangled_symbols_on",
40-
],
41-
target_triple = "x86_64-unknown-linux-gnu",
42-
versions = [VERSION],
4319
)
4420
use_repo(rust, "rust_toolchains")
4521

0 commit comments

Comments
 (0)