Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only add BAZEL_CURRENT_REPOSITORY define to runfiles library users #20388

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def cc_binary_impl(ctx, additional_linkopts):
cc_toolchain = cc_toolchain,
user_compile_flags = cc_helper.get_copts(ctx, feature_configuration, additional_make_variable_substitutions),
defines = cc_helper.defines(ctx, additional_make_variable_substitutions),
local_defines = cc_helper.local_defines(ctx, additional_make_variable_substitutions) + cc_helper.get_local_defines_for_runfiles_lookup(ctx),
local_defines = cc_helper.local_defines(ctx, additional_make_variable_substitutions) + cc_helper.get_local_defines_for_runfiles_lookup(ctx, ctx.attr.deps),
system_includes = cc_helper.system_include_dirs(ctx, additional_make_variable_substitutions),
private_hdrs = cc_helper.get_private_hdrs(ctx),
public_hdrs = cc_helper.get_public_hdrs(ctx),
Expand Down
9 changes: 7 additions & 2 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,13 @@ def _is_stamping_enabled_for_aspect(ctx):
stamp = ctx.rule.attr.stamp
return stamp

def _get_local_defines_for_runfiles_lookup(ctx):
return ["BAZEL_CURRENT_REPOSITORY=\"{}\"".format(ctx.label.workspace_name)]
_RUNFILES_LIBRARY_TARGET = Label("@bazel_tools//tools/cpp/runfiles")

def _get_local_defines_for_runfiles_lookup(ctx, all_deps):
for dep in all_deps:
if dep.label == _RUNFILES_LIBRARY_TARGET:
return ["BAZEL_CURRENT_REPOSITORY=\"{}\"".format(ctx.label.workspace_name)]
return []

# This should be enough to assume if two labels are equal.
def _are_labels_equal(a, b):
Expand Down
2 changes: 1 addition & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _cc_library_impl(ctx):
feature_configuration = feature_configuration,
user_compile_flags = cc_helper.get_copts(ctx, feature_configuration, additional_make_variable_substitutions),
defines = cc_helper.defines(ctx, additional_make_variable_substitutions),
local_defines = cc_helper.local_defines(ctx, additional_make_variable_substitutions) + cc_helper.get_local_defines_for_runfiles_lookup(ctx),
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),
system_includes = cc_helper.system_include_dirs(ctx, additional_make_variable_substitutions),
copts_filter = cc_helper.copts_filter(ctx, additional_make_variable_substitutions),
purpose = "cc_library-compile",
Expand Down
21 changes: 17 additions & 4 deletions src/test/shell/bazel/cc_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1519,19 +1519,26 @@ cc_library(
name = "library",
srcs = ["library.cpp"],
hdrs = ["library.h"],
implementation_deps = ["@bazel_tools//tools/cpp/runfiles"],
visibility = ["//visibility:public"],
)

cc_binary(
name = "binary",
srcs = ["binary.cpp"],
deps = [":library"],
deps = [
":library",
"@bazel_tools//tools/cpp/runfiles",
],
)

cc_test(
name = "test",
srcs = ["test.cpp"],
deps = [":library"],
deps = [
":library",
"@bazel_tools//tools/cpp/runfiles",
],
)
EOF

Expand Down Expand Up @@ -1573,13 +1580,19 @@ EOF
cc_binary(
name = "binary",
srcs = ["binary.cpp"],
deps = ["@//pkg:library"],
deps = [
"@//pkg:library",
"@bazel_tools//tools/cpp/runfiles",
],
)

cc_test(
name = "test",
srcs = ["test.cpp"],
deps = ["@//pkg:library"],
deps = [
"@//pkg:library",
"@bazel_tools//tools/cpp/runfiles",
],
)
EOF

Expand Down
2 changes: 2 additions & 0 deletions tools/cpp/runfiles/runfiles_src.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
// ...
//
// The code above creates a Runfiles object and retrieves a runfile path.
// The BAZEL_CURRENT_REPOSITORY macro is available in every target that
// depends on the runfiles library.
//
// The Runfiles::Create function uses the runfiles manifest and the
// runfiles directory from the RUNFILES_MANIFEST_FILE and RUNFILES_DIR
Expand Down
Loading