Skip to content

Commit 9f2c62a

Browse files
Googlercopybara-github
authored andcommitted
Introduce flag --@bazel_tools//tools/cpp:link_extra_libs
Numerous tools override --custom_malloc to add debugging or monitoring runtimes (see e.g. sanitizers). While this is fine for cases where the tool must also override malloc to function, in other cases it's simply misuse of --custom_malloc where no other mechanism exists to link an extra library. This becomes especially problematic where a runtime library is supposed to be added in certain configurations that should run in production or other performance sensitive builds. In these cases, we should _not_ override malloc, which may also be specified by a cc_binary target. Doing so would introduce unwanted changes, potentially affecting performance negatively. To fix abuse of --custom_malloc, add --@bazel_tools//tools/cpp:link_extra_libs, which is similar, but otherwise does not override other runtime libraries. Finally, extra libraries are collected in //tools/cpp:link_extra_lib, which can be used to also introduce libraries based on global configuration rather than via the command line option. PiperOrigin-RevId: 509191255 Change-Id: I932afafed574b0cf5f37a8358f6e12c43f59ba1c
1 parent f2ae8f8 commit 9f2c62a

File tree

14 files changed

+143
-17
lines changed

14 files changed

+143
-17
lines changed

src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_ndk_build_file_template.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ filegroup(
2424
srcs = ["ndk"],
2525
)
2626

27+
cc_library(
28+
name = "link_extra_lib",
29+
srcs = [],
30+
)
31+
2732
cc_library(
2833
name = "malloc",
2934
srcs = [],

src/main/starlark/builtins_bzl/common/cc/cc_binary_attrs.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ cc_binary_attrs_with_aspects = {
7070
default = configuration_field(fragment = "cpp", name = "custom_malloc"),
7171
aspects = [graph_structure_aspect],
7272
),
73+
"_link_extra_lib": attr.label(
74+
default = Label("@" + semantics.get_repo() + "//tools/cpp:link_extra_lib"),
75+
providers = [CcInfo],
76+
aspects = [graph_structure_aspect],
77+
),
7378
"stamp": attr.int(
7479
values = [-1, 0, 1],
7580
default = -1,
@@ -112,3 +117,6 @@ cc_binary_attrs_without_aspects["malloc"] = attr.label(
112117
cc_binary_attrs_without_aspects["_default_malloc"] = attr.label(
113118
default = configuration_field(fragment = "cpp", name = "custom_malloc"),
114119
)
120+
cc_binary_attrs_without_aspects["_link_extra_lib"] = attr.label(
121+
default = Label("@" + semantics.get_repo() + "//tools/cpp:link_extra_lib"),
122+
)

src/main/starlark/builtins_bzl/common/cc/semantics.bzl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,15 @@ def _get_coverage_env(ctx):
132132
def _get_cc_runtimes(ctx, is_library):
133133
if is_library:
134134
return []
135+
136+
runtimes = [ctx.attr._link_extra_lib]
137+
135138
if ctx.fragments.cpp.custom_malloc != None:
136-
return [ctx.attr._default_malloc]
137-
return [ctx.attr.malloc]
139+
runtimes.append(ctx.attr._default_malloc)
140+
else:
141+
runtimes.append(ctx.attr.malloc)
142+
143+
return runtimes
138144

139145
def _should_use_legacy_cc_test(_):
140146
return True

src/test/java/com/google/devtools/build/lib/packages/util/Crosstool.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ public void write() throws IOException {
583583
" name = 'interface_library_builder',",
584584
" srcs = ['build_interface_so'],",
585585
")",
586+
// We add an empty :link_extra_lib target in case we need it.
587+
"cc_library(name = 'link_extra_lib')",
586588
// We add an empty :malloc target in case we need it.
587589
"cc_library(name = 'malloc')",
588590
// Fake targets to get us through loading/analysis.

src/test/java/com/google/devtools/build/lib/query2/testutil/AbstractQueryTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,8 @@ public void testDeps() throws Exception {
684684
}
685685
String expectedDependencies =
686686
helper.getToolsRepository()
687+
+ "//tools/cpp:link_extra_lib + "
688+
+ helper.getToolsRepository()
687689
+ "//tools/cpp:malloc + //configurable:main + "
688690
+ "//configurable:main.cc + //configurable:adep + //configurable:bdep + "
689691
+ "//configurable:defaultdep + //conditions:a + //conditions:b "
@@ -1016,6 +1018,7 @@ public void testNoImplicitDeps() throws Exception {
10161018

10171019
// Implicit dependencies:
10181020
String hostDepsExpr = helper.getToolsRepository() + "//tools/cpp:malloc";
1021+
hostDepsExpr += " + " + helper.getToolsRepository() + "//tools/cpp:link_extra_lib";
10191022
if (!analysisMock.isThisBazel()) {
10201023
hostDepsExpr += " + //tools/cpp:malloc.cc";
10211024
}

src/test/shell/bazel/cc_flags_supplier_test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ function write_crosstool() {
5656
package(default_visibility = ["//visibility:public"])
5757
5858
load(":cc_toolchain_config.bzl", "cc_toolchain_config")
59+
60+
cc_library(
61+
name = "link_extra_lib",
62+
)
63+
5964
cc_library(
6065
name = "malloc",
6166
)

src/test/shell/integration/cpp_test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ package(default_visibility = ["//visibility:public"])
168168
169169
load(":toolchain.bzl", "toolchains")
170170
171+
cc_library(
172+
name = "link_extra_lib",
173+
)
174+
171175
cc_library(
172176
name = "malloc",
173177
)

tools/cpp/BUILD.empty.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ load(":cc_toolchain_config.bzl", "cc_toolchain_config")
1616

1717
package(default_visibility = ["//visibility:public"])
1818

19+
cc_library(
20+
name = "link_extra_lib",
21+
)
22+
1923
cc_library(
2024
name = "malloc",
2125
)

tools/cpp/BUILD.static.bsd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ load("@rules_cc//cc:defs.bzl", "cc_library", "cc_toolchain", "cc_toolchain_suite
1919

2020
package(default_visibility = ["//visibility:public"])
2121

22+
cc_library(name = "empty_lib")
23+
24+
# Label flag for extra libraries to be linked into every binary.
25+
# TODO(bazel-team): Support passing flag multiple times to build a list.
26+
label_flag(
27+
name = "link_extra_libs",
28+
build_setting_default = ":empty_lib",
29+
)
30+
31+
# The final extra library to be linked into every binary target. This collects
32+
# the above flag, but may also include more libraries depending on config.
33+
cc_library(
34+
name = "link_extra_lib",
35+
deps = [
36+
":link_extra_libs",
37+
],
38+
)
39+
2240
cc_library(
2341
name = "malloc",
2442
)

tools/cpp/BUILD.tools

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ cc_host_toolchain_alias(name = "current_cc_host_toolchain")
6666

6767
cc_libc_top_alias(name = "current_libc_top")
6868

69+
cc_library(
70+
name = "empty_lib",
71+
tags = ["__DONT_DEPEND_ON_DEF_PARSER__"],
72+
)
73+
74+
# Label flag for extra libraries to be linked into every binary.
75+
# TODO(bazel-team): Support passing flag multiple times to build a list.
76+
label_flag(
77+
name = "link_extra_libs",
78+
build_setting_default = ":empty_lib",
79+
)
80+
81+
# The final extra library to be linked into every binary target. This collects
82+
# the above flag, but may also include more libraries depending on config.
83+
cc_library(
84+
name = "link_extra_lib",
85+
tags = ["__DONT_DEPEND_ON_DEF_PARSER__"],
86+
deps = [
87+
":link_extra_libs",
88+
],
89+
)
90+
6991
cc_library(
7092
name = "malloc",
7193
tags = ["__DONT_DEPEND_ON_DEF_PARSER__"],

0 commit comments

Comments
 (0)