diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl index e132b5e58f94c5..d85a1a0da82682 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl @@ -690,7 +690,7 @@ def cc_binary_impl(ctx, additional_linkopts): cc_toolchain = cc_toolchain, user_compile_flags = cc_helper.get_copts(ctx, common, feature_configuration, additional_make_variable_substitutions), defines = common.defines, - local_defines = common.local_defines, + local_defines = common.local_defines + cc_helper.get_local_defines_for_runfiles_lookup(ctx), loose_includes = common.loose_include_dirs, system_includes = common.system_include_dirs, private_hdrs = common.private_hdrs, diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl index f4226daf0030d6..f9030bf0a4e713 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl @@ -916,6 +916,9 @@ 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)] + cc_helper = struct( merge_cc_debug_contexts = _merge_cc_debug_contexts, is_code_coverage_enabled = _is_code_coverage_enabled, @@ -960,4 +963,5 @@ cc_helper = struct( build_linking_context_from_libraries = _build_linking_context_from_libraries, is_stamping_enabled = _is_stamping_enabled, is_stamping_enabled_for_aspect = _is_stamping_enabled_for_aspect, + get_local_defines_for_runfiles_lookup = _get_local_defines_for_runfiles_lookup, ) diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_library.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_library.bzl index 3df80e994e34c3..3e7d2a28651866 100755 --- a/src/main/starlark/builtins_bzl/common/cc/cc_library.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_library.bzl @@ -58,7 +58,7 @@ def _cc_library_impl(ctx): feature_configuration = feature_configuration, user_compile_flags = cc_helper.get_copts(ctx, common, feature_configuration, additional_make_variable_substitutions), defines = common.defines, - local_defines = common.local_defines, + local_defines = common.local_defines + cc_helper.get_local_defines_for_runfiles_lookup(ctx), loose_includes = common.loose_include_dirs, system_includes = common.system_include_dirs, copts_filter = common.copts_filter, diff --git a/src/test/shell/bazel/cc_integration_test.sh b/src/test/shell/bazel/cc_integration_test.sh index 24d94b5cd33c93..964e989de2d96b 100755 --- a/src/test/shell/bazel/cc_integration_test.sh +++ b/src/test/shell/bazel/cc_integration_test.sh @@ -1506,4 +1506,33 @@ EOF expect_log "\"PWD\": \"/proc/self/cwd\"" } +function test_bazel_current_repository_define() { + mkdir -p pkg + cat > pkg/BUILD.bazel <<'EOF' +cc_library( + name = "library", + srcs = ["define.cpp"], +) +cc_binary( + name = "binary", + srcs = ["define.cpp"], +) +cc_test( + name = "test", + srcs = ["define.cpp"], +) +EOF + + cat > pkg/define.cpp <<'EOF' +#include +int main() { + std::cout << BAZEL_CURRENT_REPOSITORY << std::endl; +} +EOF + + bazel build //pkg:library &>"$TEST_log" || fail "Build should succeed" + bazel build //pkg:binary &>"$TEST_log" || fail "Build should succeed" + bazel build //pkg:test &>"$TEST_log" || fail "Build should succeed" +} + run_suite "cc_integration_test"