Skip to content

Commit 89e2929

Browse files
committed
Further isolate need for cc_toolchain
1 parent a2ad88a commit 89e2929

File tree

14 files changed

+100
-30
lines changed

14 files changed

+100
-30
lines changed

examples/cross_compile/.bazelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ coverage --experimental_fetch_all_coverage_outputs
2020
# https://bazel.build/reference/command-line-reference#flag--experimental_cc_shared_library
2121
common --experimental_cc_shared_library
2222

23+
# Disable the allocator library by pointing to a unique target which satisfies the `CcInfo`
24+
# contract but does not require a `cc_toolchain` to produce it.
25+
common --@rules_rust//rust/settings:default_allocator_library=@rules_rust//ffi/cc/empty
26+
2327
###############################################################################
2428
## Unique configuration groups
2529
###############################################################################

ffi/cc/allocator_library/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@rules_cc//cc:defs.bzl", "cc_library")
1+
load("@rules_cc//cc:cc_library.bzl", "cc_library")
22

33
cc_library(
44
name = "allocator_library",

ffi/cc/empty/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
alias(
2+
name = "empty",
3+
actual = "//rust/private/cc:empty",
4+
visibility = ["//visibility:public"],
5+
)

ffi/cc/global_allocator_library/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@rules_cc//cc:defs.bzl", "cc_library")
1+
load("@rules_cc//cc:cc_library.bzl", "cc_library")
22

33
cc_library(
44
name = "global_allocator_library",

rust/extensions.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def _rust_impl(module_ctx):
132132
return module_ctx.extension_metadata(**metadata_kwargs)
133133

134134
_COMMON_TAG_DEFAULTS = {
135-
"allocator_library": "@rules_rust//ffi/cc/allocator_library",
135+
"allocator_library": "",
136136
"rustfmt_version": DEFAULT_NIGHTLY_VERSION,
137137
"urls": DEFAULT_STATIC_RUST_URL_TEMPLATES,
138138
}

rust/private/cc/BUILD.bazel

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
load(":cc_utils.bzl", "cc_empty_library")
2+
3+
cc_empty_library(
4+
name = "empty",
5+
visibility = ["//visibility:public"],
6+
)
7+
8+
alias(
9+
name = "malloc",
10+
actual = select({
11+
"//rust/settings:experimental_use_cc_common_link_on": "@bazel_tools//tools/cpp:malloc",
12+
"//conditions:default": ":empty",
13+
}),
14+
visibility = ["//visibility:public"],
15+
)
16+
17+
alias(
18+
name = "global_allocator_library",
19+
actual = select({
20+
"//rust/settings:experimental_use_global_allocator_on": "//ffi/cc/global_allocator_library",
21+
"//conditions:default": ":empty",
22+
}),
23+
visibility = ["//visibility:public"],
24+
)

rust/private/cc/cc_utils.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Rust CC utilities"""
2+
3+
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
4+
5+
def _cc_empty_library_impl(_ctx):
6+
return [CcInfo()]
7+
8+
cc_empty_library = rule(
9+
doc = "A rule that provides an empty `CcInfo`.",
10+
implementation = _cc_empty_library_impl,
11+
provides = [CcInfo],
12+
)

rust/private/rust.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,11 +868,11 @@ _experimental_use_cc_common_link_attrs = {
868868
default = -1,
869869
),
870870
"malloc": attr.label(
871-
default = Label("@bazel_tools//tools/cpp:malloc"),
871+
default = Label("//rust/private/cc:malloc"),
872872
doc = """Override the default dependency on `malloc`.
873873
874874
By default, Rust binaries linked with cc_common.link are linked against
875-
`@bazel_tools//tools/cpp:malloc"`, which is an empty library and the resulting binary will use
875+
`//rust/private/cc:malloc"`, which is an empty library and the resulting binary will use
876876
libc's `malloc`. This label must refer to a `cc_library` rule.
877877
""",
878878
mandatory = False,

rust/private/rust_allocator_libraries.bzl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,22 @@ def make_libstd_and_allocator_ccinfo(
8989

9090
# Include C++ toolchain files as additional inputs for cross-compilation scenarios
9191
additional_inputs = []
92-
if cc_toolchain and cc_toolchain.all_files:
93-
additional_inputs = cc_toolchain.all_files.to_list()
92+
if cc_toolchain:
93+
if cc_toolchain.all_files:
94+
additional_inputs = cc_toolchain.all_files.to_list()
9495

95-
linking_context, _linking_outputs = cc_common.create_linking_context_from_compilation_outputs(
96-
name = label.name,
97-
actions = actions,
98-
feature_configuration = feature_configuration,
99-
cc_toolchain = cc_toolchain,
100-
compilation_outputs = compilation_outputs,
101-
additional_inputs = additional_inputs,
102-
)
96+
linking_context, _linking_outputs = cc_common.create_linking_context_from_compilation_outputs(
97+
name = label.name,
98+
actions = actions,
99+
feature_configuration = feature_configuration,
100+
cc_toolchain = cc_toolchain,
101+
compilation_outputs = compilation_outputs,
102+
additional_inputs = additional_inputs,
103+
)
103104

104-
cc_infos.append(CcInfo(
105-
linking_context = linking_context,
106-
))
105+
cc_infos.append(CcInfo(
106+
linking_context = linking_context,
107+
))
107108

108109
if rust_stdlib_info.std_rlibs:
109110
allocator_library_inputs = []

rust/private/rustc.bzl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,9 @@ def collect_inputs(
791791
if linker_script:
792792
nolinkstamp_compile_direct_inputs.append(linker_script)
793793

794-
if crate_info.type in ["dylib", "cdylib"]:
794+
if not cc_toolchain:
795+
runtime_libs = depset()
796+
elif crate_info.type in ["dylib", "cdylib"]:
795797
# For shared libraries we want to link C++ runtime library dynamically
796798
# (for example libstdc++.so or libc++.so).
797799
runtime_libs = cc_toolchain.dynamic_runtime_lib(feature_configuration = feature_configuration)
@@ -1342,10 +1344,7 @@ def rustc_compile_action(
13421344
# One or more of the transitive deps is a cc_library / cc_import
13431345
extra_disabled_features = []
13441346
cc_toolchain, feature_configuration = find_cc_toolchain(ctx, extra_disabled_features)
1345-
if not cc_toolchain or not _are_linkstamps_supported(
1346-
feature_configuration = feature_configuration,
1347-
has_grep_includes = hasattr(ctx.attr, "_use_grep_includes"),
1348-
):
1347+
if not cc_toolchain or not _are_linkstamps_supported(feature_configuration = feature_configuration):
13491348
linkstamps = depset([])
13501349

13511350
# Determine if the build is currently running with --stamp

0 commit comments

Comments
 (0)