Skip to content

Commit

Permalink
[bazel] Fix blacklisted_protos in cc_toolchain and add test (#7075)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannic authored and rafi-kamal committed Jan 15, 2020
1 parent 2d9b178 commit 948740b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
16 changes: 14 additions & 2 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Bazel (https://bazel.build/) BUILD file for Protobuf.

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test", "objc_library")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test", "objc_library", native_cc_proto_library = "cc_proto_library")
load("@rules_java//java:defs.bzl", "java_library")
load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library")
load("@rules_proto//proto/private:native.bzl", "native_proto_common")
load("@rules_python//python:defs.bzl", "py_library")
load(":cc_proto_blacklist_test.bzl", "cc_proto_blacklist_test")

licenses(["notice"])

Expand Down Expand Up @@ -315,6 +316,17 @@ cc_proto_library(
deps = [dep + "_proto" for dep in proto[1][1]],
) for proto in WELL_KNOWN_PROTO_MAP.items()]

[native_cc_proto_library(
name = proto + "_cc_proto",
deps = [proto + "_proto"],
visibility = ["//visibility:private"],
) for proto in WELL_KNOWN_PROTO_MAP.keys()]

cc_proto_blacklist_test(
name = "cc_proto_blacklist_test",
deps = [proto + "_cc_proto" for proto in WELL_KNOWN_PROTO_MAP.keys()]
)

################################################################################
# Protocol Buffers Compiler
################################################################################
Expand Down Expand Up @@ -989,7 +1001,7 @@ cc_library(

# Note: We use `native_proto_common` here because we depend on an implementation-detail of
# `proto_lang_toolchain`, which may not be available on `proto_common`.
reject_blacklisted_files = not hasattr(native_proto_common, "proto_lang_toolchain_rejects_files_do_not_use_or_we_will_break_you_without_mercy")
reject_blacklisted_files = hasattr(native_proto_common, "proto_lang_toolchain_rejects_files_do_not_use_or_we_will_break_you_without_mercy")
cc_toolchain_blacklisted_protos = [proto + "_proto" for proto in WELL_KNOWN_PROTO_MAP.keys()] if reject_blacklisted_files else [":well_known_protos"]
proto_lang_toolchain(
name = "cc_toolchain",
Expand Down
5 changes: 5 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,8 @@ bind(
name = "error_prone_annotations",
actual = "@error_prone_annotations_maven//jar",
)

# For `cc_proto_blacklist_test`.
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()
38 changes: 38 additions & 0 deletions cc_proto_blacklist_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Contains a unittest to verify that `cc_proto_library` does not generate code for blacklisted `.proto` sources (i.e. WKPs)."""

load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")

def _cc_proto_blacklist_test_impl(ctx):
"""Verifies that there are no C++ compile actions for Well-Known-Protos.
Args:
ctx: The rule context.
Returns: A (not further specified) sequence of providers.
"""

env = unittest.begin(ctx)

for dep in ctx.attr.deps:
files = len(dep.files.to_list())
asserts.equals(
env,
0,
files,
"Expected that target '{}' does not provide files, got {}".format(
dep.label,
files,
),
)

return unittest.end(env)

cc_proto_blacklist_test = unittest.make(
impl = _cc_proto_blacklist_test_impl,
attrs = {
"deps": attr.label_list(
mandatory = True,
providers = [CcInfo],
),
},
)
5 changes: 4 additions & 1 deletion kokoro/linux/bazel/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ cd $(dirname $0)/../../..
git submodule update --init --recursive

trap print_test_logs EXIT
bazel test :build_files_updated_unittest :protobuf_test --copt=-Werror --host_copt=-Werror
bazel test --copt=-Werror --host_copt=-Werror \
//:build_files_updated_unittest \
//:protobuf_test \
@com_google_protobuf//:cc_proto_blacklist_test
trap - EXIT

cd examples
Expand Down
8 changes: 5 additions & 3 deletions protobuf_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ def protobuf_deps():
if not native.existing_rule("bazel_skylib"):
http_archive(
name = "bazel_skylib",
sha256 = "bbccf674aa441c266df9894182d80de104cabd19be98be002f6d478aaa31574d",
strip_prefix = "bazel-skylib-2169ae1c374aab4a09aa90e65efe1a3aad4e279b",
urls = ["https://github.com/bazelbuild/bazel-skylib/archive/2169ae1c374aab4a09aa90e65efe1a3aad4e279b.tar.gz"],
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
],
)

if not native.existing_rule("zlib"):
Expand Down

0 comments on commit 948740b

Please sign in to comment.