Skip to content

Commit

Permalink
Provide BAZEL_CURRENT_REPOSITORY local define in cc_* rules
Browse files Browse the repository at this point in the history
`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

Closes #16216.

PiperOrigin-RevId: 474770711
Change-Id: Icfe179607abfb405b3a0bfb81fac18c21f744333
  • Loading branch information
fmeum authored and Copybara-Service committed Sep 16, 2022
1 parent d8d290f commit eb18166
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
2 changes: 1 addition & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_library.bzl
Expand Up @@ -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,
Expand Down
114 changes: 114 additions & 0 deletions src/test/shell/bazel/cc_integration_test.sh
Expand Up @@ -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 <iostream>
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 <iostream>
#include "library.h"
int main() {
std::cout << "in " << __FILE__ << ": '" << BAZEL_CURRENT_REPOSITORY << "'" << std::endl;
print_repo_name();
}
EOF

cat > pkg/test.cpp <<'EOF'
#include <iostream>
#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 <iostream>
#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 <iostream>
#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"

0 comments on commit eb18166

Please sign in to comment.