Skip to content

Commit 5eff703

Browse files
melvercopybara-github
authored andcommitted
Expose cc_binary.link_extra_lib label
Recently Bazel introduced support for C++ binaries linking against //tools/cpp:link_extra_lib which typically just depends on the label flag //tools/cpp:link_extra_libs. For normal C++ binaries this is the intended behaviour. However, certain special C++ binaries really do not want to be linked against any other targets for a variety of reasons: conflicting dependencies, pure C binaries that should not pull in C++ runtime libraries, or other very low level binaries that want to remain untouched. Our experience building many thousands of C++ targets shows that there are few cases where opting out linking of extra libraries always makes sense. To provide this flexibility, expose cc_binary.link_extra_lib, which can be set to an empty library such as @bazel_tools//tools/cpp:empty_lib. PiperOrigin-RevId: 548117556 Change-Id: Iaaa35ec9115031bfaa124ca2441aafe823145899
1 parent 3c7d571 commit 5eff703

File tree

11 files changed

+85
-8
lines changed

11 files changed

+85
-8
lines changed

src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCppRuleClasses.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,23 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
535535
.allowedFileTypes()
536536
.allowedRuleClasses("cc_library"))
537537
.add(attr(":default_malloc", LABEL).value(CppRuleClasses.DEFAULT_MALLOC))
538+
/*<!-- #BLAZE_RULE($cc_binary_base).ATTRIBUTE(link_extra_lib) -->
539+
Control linking of extra libraries.
540+
<p>
541+
By default, C++ binaries are linked against <code>//tools/cpp:link_extra_lib</code>,
542+
which by default depends on the label flag <code>//tools/cpp:link_extra_libs</code>.
543+
Without setting the flag, this library is empty by default. Setting the label flag
544+
allows linking optional dependencies, such as overrides for weak symbols, interceptors
545+
for shared library functions, or special runtime libraries (for malloc replacements,
546+
prefer <code>malloc</code> or <code>--custom_malloc</code>). Setting this attribute to
547+
<code>None</code> disables this behaviour.
548+
</p>
549+
<!-- #END_BLAZE_RULE.ATTRIBUTE -->*/
550+
.add(
551+
attr("link_extra_lib", LABEL)
552+
.value(env.getToolsLabel("//tools/cpp:link_extra_lib"))
553+
.allowedFileTypes()
554+
.allowedRuleClasses("cc_library"))
538555
/*<!-- #BLAZE_RULE($cc_binary_base).ATTRIBUTE(stamp) -->
539556
Whether to encode build information into the binary. Possible values:
540557
<ul>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ 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(
73+
"link_extra_lib": attr.label(
7474
default = Label("@" + semantics.get_repo() + "//tools/cpp:link_extra_lib"),
7575
providers = [CcInfo],
7676
aspects = [graph_structure_aspect],
@@ -118,6 +118,6 @@ cc_binary_attrs_without_aspects["malloc"] = attr.label(
118118
cc_binary_attrs_without_aspects["_default_malloc"] = attr.label(
119119
default = configuration_field(fragment = "cpp", name = "custom_malloc"),
120120
)
121-
cc_binary_attrs_without_aspects["_link_extra_lib"] = attr.label(
121+
cc_binary_attrs_without_aspects["link_extra_lib"] = attr.label(
122122
default = Label("@" + semantics.get_repo() + "//tools/cpp:link_extra_lib"),
123123
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def _get_cc_runtimes(ctx, is_library):
128128
if is_library:
129129
return []
130130

131-
runtimes = [ctx.attr._link_extra_lib]
131+
runtimes = [ctx.attr.link_extra_lib]
132132

133133
if ctx.fragments.cpp.custom_malloc != None:
134134
runtimes.append(ctx.attr._default_malloc)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +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')",
586+
// We add a :link_extra_lib target in case we need it.
587+
"cc_library(name = 'link_extra_lib', srcs = ['linkextra.cc'])",
588588
// We add an empty :malloc target in case we need it.
589589
"cc_library(name = 'malloc')",
590590
// Fake targets to get us through loading/analysis.

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,13 @@ public void testNoImplicitDeps() throws Exception {
10231023

10241024
// Implicit dependencies:
10251025
String hostDepsExpr = helper.getToolsRepository() + "//tools/cpp:malloc";
1026-
hostDepsExpr += " + " + helper.getToolsRepository() + "//tools/cpp:link_extra_lib";
1026+
hostDepsExpr +=
1027+
" + "
1028+
+ helper.getToolsRepository()
1029+
+ "//tools/cpp:link_extra_lib"
1030+
+ " + "
1031+
+ helper.getToolsRepository()
1032+
+ "//tools/cpp:linkextra.cc";
10271033
if (!analysisMock.isThisBazel()) {
10281034
hostDepsExpr += " + //tools/cpp:malloc.cc";
10291035
}

src/test/java/com/google/devtools/build/lib/rules/cpp/CcBinaryThinLtoObjDirTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ private CppLinkAction getIndexAction(LtoBackendAction backendAction) throws Exce
8282
@Before
8383
public void createBasePkg() throws IOException {
8484
scratch.overwriteFile(
85-
"base/BUILD", "cc_library(name = 'system_malloc', visibility = ['//visibility:public'])");
85+
"base/BUILD",
86+
"cc_library(name = 'system_malloc', visibility = ['//visibility:public'])",
87+
"cc_library(name = 'empty_lib', visibility = ['//visibility:public'])");
8688
}
8789

8890
public void createBuildFiles(String... extraCcBinaryParameters) throws Exception {
@@ -104,6 +106,7 @@ public void createBuildFiles(String... extraCcBinaryParameters) throws Exception
104106
" srcs = ['binfile.cc', ],",
105107
" deps = [ ':lib', ':tree', ':tree_2', 'tree_empty'], ",
106108
String.join("", extraCcBinaryParameters),
109+
" link_extra_lib = '//base:empty_lib', ",
107110
" malloc = '//base:system_malloc')",
108111
"cc_library(name = 'lib',",
109112
" srcs = ['libfile.cc'],",
@@ -200,13 +203,15 @@ public void createTestFiles(String extraTestParameters, String extraLibraryParam
200203
" srcs = ['bin_test.cc', ],",
201204
" deps = [ ':lib', ':tree', ], ",
202205
extraTestParameters,
206+
" link_extra_lib = '//base:empty_lib', ",
203207
" malloc = '//base:system_malloc'",
204208
")",
205209
"cc_test(",
206210
" name = 'bin_test2',",
207211
" srcs = ['bin_test2.cc', ],",
208212
" deps = [ ':lib', ':tree', ], ",
209213
extraTestParameters,
214+
" link_extra_lib = '//base:empty_lib', ",
210215
" malloc = '//base:system_malloc'",
211216
")",
212217
"cc_library(",

src/test/java/com/google/devtools/build/lib/rules/cpp/CcBinaryThinLtoTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ private CppLinkAction getIndexAction(LtoBackendAction backendAction) throws Exce
8181
@Before
8282
public void createBasePkg() throws IOException {
8383
scratch.overwriteFile(
84-
"base/BUILD", "cc_library(name = 'system_malloc', visibility = ['//visibility:public'])");
84+
"base/BUILD",
85+
"cc_library(name = 'system_malloc', visibility = ['//visibility:public'])",
86+
"cc_library(name = 'empty_lib', visibility = ['//visibility:public'])");
8587
}
8688

8789
public void createBuildFiles(String... extraCcBinaryParameters) throws Exception {
@@ -93,6 +95,7 @@ public void createBuildFiles(String... extraCcBinaryParameters) throws Exception
9395
" srcs = ['binfile.cc', ],",
9496
" deps = [ ':lib' ], ",
9597
String.join("", extraCcBinaryParameters),
98+
" link_extra_lib = '//base:empty_lib', ",
9699
" malloc = '//base:system_malloc')",
97100
"cc_library(name = 'lib',",
98101
" srcs = ['libfile.cc'],",
@@ -116,13 +119,15 @@ public void createTestFiles(String extraTestParameters, String extraLibraryParam
116119
" srcs = ['bin_test.cc', ],",
117120
" deps = [ ':lib' ], ",
118121
extraTestParameters,
122+
" link_extra_lib = '//base:empty_lib', ",
119123
" malloc = '//base:system_malloc'",
120124
")",
121125
"cc_test(",
122126
" name = 'bin_test2',",
123127
" srcs = ['bin_test2.cc', ],",
124128
" deps = [ ':lib' ], ",
125129
extraTestParameters,
130+
" link_extra_lib = '//base:empty_lib', ",
126131
" malloc = '//base:system_malloc'",
127132
")",
128133
"cc_library(",

src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,4 +1275,30 @@ public void testNoCoptsDisabled() throws Exception {
12751275
"This attribute was removed. See https://github.com/bazelbuild/bazel/issues/8706 for"
12761276
+ " details.");
12771277
}
1278+
1279+
@Test
1280+
public void testLinkExtra() throws Exception {
1281+
ConfiguredTarget target =
1282+
scratchConfiguredTarget(
1283+
"mypackage",
1284+
"mybinary",
1285+
"cc_binary(name = 'mybinary',",
1286+
" srcs = ['mybinary.cc'])");
1287+
List<String> artifactNames = baseArtifactNames(getLinkerInputs(target));
1288+
assertThat(artifactNames).contains("liblink_extra_lib.a");
1289+
}
1290+
1291+
@Test
1292+
public void testNoLinkExtra() throws Exception {
1293+
ConfiguredTarget target =
1294+
scratchConfiguredTarget(
1295+
"mypackage",
1296+
"mybinary",
1297+
"cc_library(name = 'empty_lib')",
1298+
"cc_binary(name = 'mybinary',",
1299+
" srcs = ['mybinary.cc'],",
1300+
" link_extra_lib = ':empty_lib')");
1301+
List<String> artifactNames = baseArtifactNames(getLinkerInputs(target));
1302+
assertThat(artifactNames).doesNotContain("liblink_extra_lib.a");
1303+
}
12781304
}

src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,10 +2142,12 @@ public void testRpathIsNotAddedWhenThereAreNoSoDeps() throws Exception {
21422142
" srcs = ['malloc.cc'],",
21432143
" linkstatic = 1,",
21442144
")",
2145+
"cc_library(name = 'empty_lib')",
21452146
"cc_binary(",
21462147
" name = 'main',",
21472148
" srcs = ['main.cc'],",
21482149
" malloc = ':malloc',",
2150+
" link_extra_lib = ':empty_lib',",
21492151
" linkstatic = 0,",
21502152
")");
21512153

@@ -2225,11 +2227,13 @@ public void testRpathRootIsAddedEvenWithTransitionedDepsOnly() throws Exception
22252227
" srcs = ['malloc.cc'],",
22262228
" linkstatic = 1,",
22272229
")",
2230+
"cc_library(name = 'empty_lib')",
22282231
"cc_binary(",
22292232
" name = 'main',",
22302233
" srcs = ['main.cc'],",
22312234
" linkstatic = 0,",
22322235
" malloc = ':malloc',",
2236+
" link_extra_lib = ':empty_lib',",
22332237
" deps = ['dep1'],",
22342238
")",
22352239
"",

src/test/java/com/google/devtools/build/lib/skyframe/rewinding/RewindingTestsHelper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,8 @@ final void runGeneratedHeaderRewound_lostInInputDiscovery(SpawnShim shim) throws
21062106
"Extracting include lines from genheader/consumes.cc",
21072107
"Extracting include lines from tools/cpp/malloc.cc",
21082108
"Compiling tools/cpp/malloc.cc",
2109+
"Extracting include lines from tools/cpp/linkextra.cc",
2110+
"Compiling tools/cpp/linkextra.cc",
21092111
String.format(
21102112
"Extracting include lines from %s-out/k8-fastbuild/bin/genheader/gen.h",
21112113
TestConstants.PRODUCT_NAME),
@@ -2170,6 +2172,8 @@ final void runGeneratedHeaderRewound_lostInActionExecution(SpawnShim shim) throw
21702172
"Extracting include lines from genheader/consumes.cc",
21712173
"Extracting include lines from tools/cpp/malloc.cc",
21722174
"Compiling tools/cpp/malloc.cc",
2175+
"Extracting include lines from tools/cpp/linkextra.cc",
2176+
"Compiling tools/cpp/linkextra.cc",
21732177
String.format(
21742178
"Extracting include lines from %s-out/k8-fastbuild/bin/genheader/gen.h",
21752179
TestConstants.PRODUCT_NAME),
@@ -2263,6 +2267,8 @@ final void runGeneratedTransitiveHeaderRewound_lostInInputDiscovery(SpawnShim sh
22632267
"Extracting include lines from genheader/consumes.cc",
22642268
"Extracting include lines from tools/cpp/malloc.cc",
22652269
"Compiling tools/cpp/malloc.cc",
2270+
"Extracting include lines from tools/cpp/linkextra.cc",
2271+
"Compiling tools/cpp/linkextra.cc",
22662272
"Compiling genheader/intermediate.cc",
22672273
String.format(
22682274
"Extracting include lines from %s-out/k8-fastbuild/bin/genheader/gen.h",
@@ -2332,6 +2338,8 @@ final void runGeneratedTransitiveHeaderRewound_lostInActionExecution(SpawnShim s
23322338
"Extracting include lines from genheader/intermediate.cc",
23332339
"Extracting include lines from tools/cpp/malloc.cc",
23342340
"Compiling tools/cpp/malloc.cc",
2341+
"Extracting include lines from tools/cpp/linkextra.cc",
2342+
"Compiling tools/cpp/linkextra.cc",
23352343
"Extracting include lines from genheader/consumes.cc",
23362344
"Compiling genheader/intermediate.cc",
23372345
String.format(

0 commit comments

Comments
 (0)