Skip to content

Commit fe15d55

Browse files
authored
Set Rust allocator mode on cgo rlib (#49)
## Summary - wrap the import_extractor rlib with `with_cfg` - set rules_rust's mangled allocator-symbol mode inside the transition - apply the same setting to the staticlib wrapper for consistency - remove the redundant global `.bazelrc` allocator setting ## Why Consumers and BCR do not necessarily read this repo's root `.bazelrc`. The cgo-linked Rust rlib should carry the allocator mode itself so Rust 1.95 mangled allocator references link with the matching allocator shim. ## Verification - `bazel test //crates/import_extractor:test //crates/import_extractor:ffi_symbols_test //py:py_test` - `bazel build @gazelle_py//py/... --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 --repo_env=BAZEL_NO_APPLE_CPP_TOOLCHAIN=1 --@llvm//config:experimental_stub_libgcc_s=True` from `bcr_test/`
1 parent 5e8b7a7 commit fe15d55

3 files changed

Lines changed: 38 additions & 23 deletions

File tree

.bazelrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ build --registry=https://bcr.bazel.build
99
common --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
1010
common --repo_env=BAZEL_NO_APPLE_CPP_TOOLCHAIN=1
1111
common --@llvm//config:experimental_stub_libgcc_s=True
12-
common --@rules_rust//rust/settings:experimental_use_allocator_libraries_with_mangled_symbols=true
1312

1413
# Go 1.27 (Aug 2026) makes Go binaries PIE-compatible — drop this then.
1514
# Until then, rules_go's external link via clang+lld needs -no-pie because

crates/import_extractor/BUILD.bazel

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("@rules_go//go:def.bzl", "go_test")
22
load("@rules_rs//rs:rust_library.bzl", "rust_library")
33
load("@rules_rs//rs:rust_test.bzl", "rust_test")
44
load("@rules_rust_prost//:defs.bzl", "rust_prost_library")
5-
load(":opt.bzl", "opt_rust_static_library")
5+
load(":opt.bzl", "cgo_rust_library", "opt_rust_static_library")
66

77
# proto_library lives at //proto:import_extractor_proto so that //py/proto can
88
# depend on it without loading any rules_rs symbols.
@@ -31,15 +31,23 @@ _LIB_DEPS = [
3131
"@gazelle_py_crates//:rayon",
3232
]
3333

34-
# rlib variant — used by the unit tests.
35-
rust_library(
34+
# rlib variant — linked through cgo by //py and used by the unit tests.
35+
cgo_rust_library(
3636
name = "import_extractor",
3737
srcs = _LIB_SRCS,
3838
edition = "2021",
3939
visibility = ["//visibility:public"],
4040
deps = _LIB_DEPS,
4141
)
4242

43+
rust_library(
44+
name = "import_extractor_test_lib",
45+
testonly = True,
46+
srcs = _LIB_SRCS,
47+
edition = "2021",
48+
deps = _LIB_DEPS,
49+
)
50+
4351
# staticlib variant — linked into the gazelle plugin's go_library via cgo.
4452
# Provides CcInfo so it can be passed directly as `cdeps` on go_library.
4553
# opt_rust_static_library is a with_cfg wrapper that pins compilation_mode
@@ -62,7 +70,7 @@ genrule(
6270

6371
rust_test(
6472
name = "test",
65-
crate = ":import_extractor",
73+
crate = ":import_extractor_test_lib",
6674
)
6775

6876
go_test(

crates/import_extractor/opt.bzl

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
1-
"""rust_static_library wrapper that pins compilation_mode and rustc flags.
1+
"""Rust target wrappers for cgo-compatible import_extractor builds.
22
3-
The gazelle plugin's go_library links the import_extractor staticlib via
4-
cgo. Without a transition, the staticlib's build flags would inherit the
5-
consumer's compilation_mode, so a consumer running plain `bazel build`
6-
would get a fastbuild rust binary inside their gazelle binary.
3+
The gazelle plugin's go_library links the import_extractor rlib via cgo.
4+
Rust 1.95's allocator symbols are mangled, so the rlib must be compiled
5+
with rules_rust's matching allocator-library setting. Pinning that here
6+
keeps consumers and BCR from needing a top-level command-line flag.
77
8-
with_cfg forces compilation_mode=opt and the release rustc flags
9-
(panic=abort, codegen-units=1, thin LTO, strip) on the staticlib and its
10-
transitive deps. The transition propagates so @gazelle_py_crates//
11-
crates are optimized too.
8+
The staticlib wrapper also forces compilation_mode=opt and the release
9+
rustc flags (panic=abort, codegen-units=1, thin LTO, strip).
1210
"""
1311

12+
load("@rules_rs//rs:rust_library.bzl", "rust_library")
1413
load("@rules_rs//rs:rust_static_library.bzl", "rust_static_library")
1514
load("@with_cfg.bzl", "with_cfg")
1615

16+
_ALLOCATOR_MANGLED_SYMBOLS = Label("@rules_rust//rust/settings:experimental_use_allocator_libraries_with_mangled_symbols")
17+
18+
cgo_rust_library, _cgo_rust_library_internal = (
19+
with_cfg(rust_library)
20+
.set(_ALLOCATOR_MANGLED_SYMBOLS, True)
21+
.build()
22+
)
23+
1724
opt_rust_static_library, _opt_rust_static_library_internal = (
1825
with_cfg(rust_static_library)
1926
.set("compilation_mode", "opt")
27+
.set(_ALLOCATOR_MANGLED_SYMBOLS, True)
2028
.set(
21-
Label("@rules_rust//:extra_rustc_flags"),
22-
[
23-
"-Ccodegen-units=1",
24-
"-Cpanic=abort",
25-
"-Cstrip=symbols",
26-
"-Clto=thin",
27-
"-Cembed-bitcode=yes",
28-
],
29-
)
29+
Label("@rules_rust//:extra_rustc_flags"),
30+
[
31+
"-Ccodegen-units=1",
32+
"-Cpanic=abort",
33+
"-Cstrip=symbols",
34+
"-Clto=thin",
35+
"-Cembed-bitcode=yes",
36+
],
37+
)
3038
.build()
3139
)

0 commit comments

Comments
 (0)