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 bazelbuild#16124
  • Loading branch information
fmeum committed Sep 9, 2022
1 parent c4a1ab8 commit 4818687
Show file tree
Hide file tree
Showing 4 changed files with 35 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 @@ -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,
Expand Down Expand Up @@ -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,
)
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
29 changes: 29 additions & 0 deletions src/test/shell/bazel/cc_integration_test.sh
Expand Up @@ -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 <iostream>
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"

0 comments on commit 4818687

Please sign in to comment.