Skip to content

Commit

Permalink
Define BAZEL_CURRENT_REPOSITORY for runfiles library dependents
Browse files Browse the repository at this point in the history
When a cc_* target directly depends on a target that advertises
`RunfilesLibraryInfo`, `BAZEL_CURRENT_REPOSITORY` is added as a local
define with value the canonical name of the repository containing the
target.
  • Loading branch information
fmeum committed Sep 4, 2022
1 parent c4a1ab8 commit 98dc6a4
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 7 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_libraries(ctx),
loose_includes = common.loose_include_dirs,
system_includes = common.system_include_dirs,
private_hdrs = common.private_hdrs,
Expand Down
10 changes: 10 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Expand Up @@ -916,6 +916,15 @@ def _is_stamping_enabled_for_aspect(ctx):
stamp = ctx.rule.attr.stamp
return stamp

_DEPS_ATTRS = ["deps", "implementation_deps"]

def _get_local_defines_for_runfiles_libraries(ctx):
for attr in _DEPS_ATTRS:
for dep in getattr(ctx.attr, attr, []):
if RunfilesLibraryInfo in dep:
return ["BAZEL_CURRENT_REPOSITORY=\"{}\"".format(ctx.label.workspace_name)]
return []

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 +969,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_libraries = _get_local_defines_for_runfiles_libraries,
)
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_libraries(ctx),
loose_includes = common.loose_include_dirs,
system_includes = common.system_include_dirs,
copts_filter = common.copts_filter,
Expand Down
63 changes: 63 additions & 0 deletions src/test/shell/bazel/cc_integration_test.sh
Expand Up @@ -1506,4 +1506,67 @@ EOF
expect_log "\"PWD\": \"/proc/self/cwd\""
}

function test_bazel_current_repository_define() {
add_to_bazelrc "build --experimental_cc_implementation_deps"

mkdir -p pkg
cat > pkg/BUILD.bazel <<'EOF'
cc_library(
name = "library",
srcs = ["define.cpp"],
deps = ["@bazel_tools//tools/cpp/runfiles"],
)
cc_library(
name = "library_implementation_deps",
srcs = ["define.cpp"],
implementation_deps = ["@bazel_tools//tools/cpp/runfiles"],
)
cc_library(
name = "library_fail",
srcs = ["define.cpp"],
)
cc_binary(
name = "binary",
srcs = ["define.cpp"],
deps = ["@bazel_tools//tools/cpp/runfiles"],
)
cc_binary(
name = "binary_fail",
srcs = ["define.cpp"],
deps = [":library"],
)
cc_test(
name = "test",
srcs = ["define.cpp"],
deps = ["@bazel_tools//tools/cpp/runfiles"],
)
cc_test(
name = "test_fail",
srcs = ["define.cpp"],
deps = [":library"],
)
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:library_implementation_deps &>"$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"

bazel build //pkg:library_fail &>"$TEST_log" && fail "Build should fail"
expect_log "'BAZEL_CURRENT_REPOSITORY'"
bazel build //pkg:binary_fail &>"$TEST_log" && fail "Build should fail"
expect_log "'BAZEL_CURRENT_REPOSITORY'"
bazel build //pkg:test_fail &>"$TEST_log" && fail "Build should fail"
expect_log "'BAZEL_CURRENT_REPOSITORY'"
}

run_suite "cc_integration_test"
4 changes: 3 additions & 1 deletion tools/cpp/BUILD
Expand Up @@ -34,7 +34,9 @@ filegroup(

filegroup(
name = "bzl_srcs",
srcs = glob(["*.bzl"]),
srcs = glob(["*.bzl"]) + [
"//tools/cpp/runfiles:bzl_srcs",
],
visibility = ["//tools:__pkg__"],
)

Expand Down
4 changes: 3 additions & 1 deletion tools/cpp/BUILD.tools
Expand Up @@ -137,7 +137,9 @@ filegroup(

filegroup(
name = "bzl_srcs",
srcs = glob(["*.bzl"]),
srcs = glob(["*.bzl"]) + [
"//tools/cpp/runfiles:bzl_srcs",
],
visibility = ["//tools:__pkg__"],
)

Expand Down
7 changes: 7 additions & 0 deletions tools/cpp/runfiles/BUILD
Expand Up @@ -16,11 +16,18 @@ filegroup(
name = "embedded_tools",
srcs = [
"BUILD.tools",
"cc_runfiles_library.bzl",
":srcs_for_embedded_tools",
],
visibility = ["//tools/cpp:__pkg__"],
)

filegroup(
name = "bzl_srcs",
srcs = glob(["*.bzl"]),
visibility = ["//tools/cpp:__pkg__"],
)

# Rewrite the include path for runfiles.h in runfiles_src.cc, and create
# "generated_runfiles.{h,cc}". These files are renamed to "runfiles.{h,cc}" as
# part of creating the embedded tools of Bazel.
Expand Down
17 changes: 14 additions & 3 deletions tools/cpp/runfiles/BUILD.tools
@@ -1,8 +1,19 @@
# This package will host the C++ runfiles library when it's finally released.
load(":cc_runfiles_library.bzl", "cc_runfiles_library")

cc_library(
cc_runfiles_library(
name = "runfiles",
exports = ":runfiles_impl",
visibility = ["//visibility:public"],
)

cc_library(
name = "runfiles_impl",
srcs = ["runfiles.cc"],
hdrs = ["runfiles.h"],
visibility = ["//visibility:public"],
)

filegroup(
name = "bzl_srcs",
srcs = glob(["*.bzl"]),
visibility = ["//tools/cpp:__pkg__"],
)
13 changes: 13 additions & 0 deletions tools/cpp/runfiles/cc_runfiles_library.bzl
@@ -0,0 +1,13 @@
def _cc_runfiles_library_impl(ctx):
return [
ctx.attr.exports[DefaultInfo],
ctx.attr.exports[CcInfo],
RunfilesLibraryInfo(),
]

cc_runfiles_library = rule(
implementation = _cc_runfiles_library_impl,
attrs = {
"exports": attr.label(),
},
)

0 comments on commit 98dc6a4

Please sign in to comment.