From 52eec61e68d664dfdc3395a6fef394d67ddc373f Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Fri, 9 Sep 2022 12:57:58 +0200 Subject: [PATCH] Provide `BAZEL_CURRENT_REPOSITORY` local define in cc_* rules `BAZEL_CURRENT_REPOSITORY` contains the canonical name of the repository containing the current target and is required to look up runfiles using apparent repository names when repository mappings are used, e.g. with Bzlmod. Work towards #16124 --- .../builtins_bzl/common/cc/cc_binary.bzl | 2 +- .../builtins_bzl/common/cc/cc_helper.bzl | 4 + .../builtins_bzl/common/cc/cc_library.bzl | 2 +- src/test/shell/bazel/cc_integration_test.sh | 114 ++++++++++++++++++ 4 files changed, 120 insertions(+), 2 deletions(-) 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 9c30a28fa1fde6..d27c9baa843685 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 2e5b3351004c07..c3c80d47d2a09f 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl @@ -927,6 +927,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, @@ -972,4 +975,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 0b698aef8384a1..317313e77bbd53 100755 --- a/src/test/shell/bazel/cc_integration_test.sh +++ b/src/test/shell/bazel/cc_integration_test.sh @@ -1594,4 +1594,118 @@ function test_external_cc_test_local_sibling_repository_layout() { @other_repo//test >& $TEST_log || fail "Test should pass" } +function test_bazel_current_repository_define() { + cat >> WORKSPACE <<'EOF' +local_repository( + name = "other_repo", + path = "other_repo", +) +EOF + + mkdir -p pkg + cat > pkg/BUILD.bazel <<'EOF' +cc_library( + name = "library", + srcs = ["library.cpp"], + hdrs = ["library.h"], + visibility = ["//visibility:public"], +) + +cc_binary( + name = "binary", + srcs = ["binary.cpp"], + deps = [":library"], +) + +cc_test( + name = "test", + srcs = ["test.cpp"], + deps = [":library"], +) +EOF + + cat > pkg/library.cpp <<'EOF' +#include "library.h" +#include +void print_repo_name() { + std::cout << "in " << __FILE__ << ": '" << BAZEL_CURRENT_REPOSITORY << "'" << std::endl; +} +EOF + + cat > pkg/library.h <<'EOF' +void print_repo_name(); +EOF + + cat > pkg/binary.cpp <<'EOF' +#include +#include "library.h" +int main() { + std::cout << "in " << __FILE__ << ": '" << BAZEL_CURRENT_REPOSITORY << "'" << std::endl; + print_repo_name(); +} +EOF + + cat > pkg/test.cpp <<'EOF' +#include +#include "library.h" +int main() { + std::cout << "in " << __FILE__ << ": '" << BAZEL_CURRENT_REPOSITORY << "'" << std::endl; + print_repo_name(); +} +EOF + + mkdir -p other_repo + touch other_repo/WORKSPACE + + mkdir -p other_repo/pkg + cat > other_repo/pkg/BUILD.bazel <<'EOF' +cc_binary( + name = "binary", + srcs = ["binary.cpp"], + deps = ["@//pkg:library"], +) + +cc_test( + name = "test", + srcs = ["test.cpp"], + deps = ["@//pkg:library"], +) +EOF + + cat > other_repo/pkg/binary.cpp <<'EOF' +#include +#include "pkg/library.h" +int main() { + std::cout << "in " << __FILE__ << ": '" << BAZEL_CURRENT_REPOSITORY << "'" << std::endl; + print_repo_name(); +} +EOF + + cat > other_repo/pkg/test.cpp <<'EOF' +#include +#include "pkg/library.h" +int main() { + std::cout << "in " << __FILE__ << ": '" << BAZEL_CURRENT_REPOSITORY << "'" << std::endl; + print_repo_name(); +} +EOF + + bazel run //pkg:binary &>"$TEST_log" || fail "Run should succeed" + expect_log "in pkg/binary.cpp: ''" + expect_log "in pkg/library.cpp: ''" + + bazel test --test_output=streamed //pkg:test &>"$TEST_log" || fail "Test should succeed" + expect_log "in pkg/test.cpp: ''" + expect_log "in pkg/library.cpp: ''" + + bazel run @other_repo//pkg:binary &>"$TEST_log" || fail "Run should succeed" + expect_log "in external/other_repo/pkg/binary.cpp: 'other_repo'" + expect_log "in pkg/library.cpp: ''" + + bazel test --test_output=streamed \ + @other_repo//pkg:test &>"$TEST_log" || fail "Test should succeed" + expect_log "in external/other_repo/pkg/test.cpp: 'other_repo'" + expect_log "in pkg/library.cpp: ''" +} + run_suite "cc_integration_test"