Skip to content

Commit 454b550

Browse files
fmeumcopybara-github
authored andcommitted
Only add BAZEL_CURRENT_REPOSITORY define to runfiles library users
RELNOTES[INC]: The `BAZEL_CURRENT_REPOSITORY` preprocessor variable, which holds the canonical name of the Bazel repository containing a `cc_*` target, is now only set during compilation if the target depends on the C/C++ runfiles library `@bazel_tools//tools/cpp/runfiles` via `deps` or `implementation_deps`. Fixes #20371 Closes #20388. PiperOrigin-RevId: 587070254 Change-Id: I774dc38b031199179df1c63e04000a2ecc15e010
1 parent a3a2bf2 commit 454b550

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ def cc_binary_impl(ctx, additional_linkopts):
633633
cc_toolchain = cc_toolchain,
634634
user_compile_flags = cc_helper.get_copts(ctx, feature_configuration, additional_make_variable_substitutions),
635635
defines = cc_helper.defines(ctx, additional_make_variable_substitutions),
636-
local_defines = cc_helper.local_defines(ctx, additional_make_variable_substitutions) + cc_helper.get_local_defines_for_runfiles_lookup(ctx),
636+
local_defines = cc_helper.local_defines(ctx, additional_make_variable_substitutions) + cc_helper.get_local_defines_for_runfiles_lookup(ctx, ctx.attr.deps),
637637
system_includes = cc_helper.system_include_dirs(ctx, additional_make_variable_substitutions),
638638
private_hdrs = cc_helper.get_private_hdrs(ctx),
639639
public_hdrs = cc_helper.get_public_hdrs(ctx),

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,8 +967,13 @@ def _is_stamping_enabled_for_aspect(ctx):
967967
stamp = ctx.rule.attr.stamp
968968
return stamp
969969

970-
def _get_local_defines_for_runfiles_lookup(ctx):
971-
return ["BAZEL_CURRENT_REPOSITORY=\"{}\"".format(ctx.label.workspace_name)]
970+
_RUNFILES_LIBRARY_TARGET = Label("@bazel_tools//tools/cpp/runfiles")
971+
972+
def _get_local_defines_for_runfiles_lookup(ctx, all_deps):
973+
for dep in all_deps:
974+
if dep.label == _RUNFILES_LIBRARY_TARGET:
975+
return ["BAZEL_CURRENT_REPOSITORY=\"{}\"".format(ctx.label.workspace_name)]
976+
return []
972977

973978
# This should be enough to assume if two labels are equal.
974979
def _are_labels_equal(a, b):

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def _cc_library_impl(ctx):
5757
feature_configuration = feature_configuration,
5858
user_compile_flags = cc_helper.get_copts(ctx, feature_configuration, additional_make_variable_substitutions),
5959
defines = cc_helper.defines(ctx, additional_make_variable_substitutions),
60-
local_defines = cc_helper.local_defines(ctx, additional_make_variable_substitutions) + cc_helper.get_local_defines_for_runfiles_lookup(ctx),
60+
local_defines = cc_helper.local_defines(ctx, additional_make_variable_substitutions) + cc_helper.get_local_defines_for_runfiles_lookup(ctx, ctx.attr.deps + ctx.attr.implementation_deps),
6161
system_includes = cc_helper.system_include_dirs(ctx, additional_make_variable_substitutions),
6262
copts_filter = cc_helper.copts_filter(ctx, additional_make_variable_substitutions),
6363
purpose = "cc_library-compile",

src/test/shell/bazel/cc_integration_test.sh

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,19 +1519,26 @@ cc_library(
15191519
name = "library",
15201520
srcs = ["library.cpp"],
15211521
hdrs = ["library.h"],
1522+
implementation_deps = ["@bazel_tools//tools/cpp/runfiles"],
15221523
visibility = ["//visibility:public"],
15231524
)
15241525
15251526
cc_binary(
15261527
name = "binary",
15271528
srcs = ["binary.cpp"],
1528-
deps = [":library"],
1529+
deps = [
1530+
":library",
1531+
"@bazel_tools//tools/cpp/runfiles",
1532+
],
15291533
)
15301534
15311535
cc_test(
15321536
name = "test",
15331537
srcs = ["test.cpp"],
1534-
deps = [":library"],
1538+
deps = [
1539+
":library",
1540+
"@bazel_tools//tools/cpp/runfiles",
1541+
],
15351542
)
15361543
EOF
15371544

@@ -1573,13 +1580,19 @@ EOF
15731580
cc_binary(
15741581
name = "binary",
15751582
srcs = ["binary.cpp"],
1576-
deps = ["@//pkg:library"],
1583+
deps = [
1584+
"@//pkg:library",
1585+
"@bazel_tools//tools/cpp/runfiles",
1586+
],
15771587
)
15781588
15791589
cc_test(
15801590
name = "test",
15811591
srcs = ["test.cpp"],
1582-
deps = ["@//pkg:library"],
1592+
deps = [
1593+
"@//pkg:library",
1594+
"@bazel_tools//tools/cpp/runfiles",
1595+
],
15831596
)
15841597
EOF
15851598

tools/cpp/runfiles/runfiles_src.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
// ...
4949
//
5050
// The code above creates a Runfiles object and retrieves a runfile path.
51+
// The BAZEL_CURRENT_REPOSITORY macro is available in every target that
52+
// depends on the runfiles library.
5153
//
5254
// The Runfiles::Create function uses the runfiles manifest and the
5355
// runfiles directory from the RUNFILES_MANIFEST_FILE and RUNFILES_DIR

0 commit comments

Comments
 (0)