From c8fa0afdfc907452edbf2f5f3110f2f673fc4a70 Mon Sep 17 00:00:00 2001 From: Shengzhe Date: Tue, 21 Jul 2020 11:08:01 -0700 Subject: [PATCH] Allow to run cpp tests under grpc/tests/ using bazel. (#6040) This is the first step to upgrade grpc dependency to the latest version. - Patch protobuf 3.6.1. and grpc 1.15.1 to fix build errors when using the latest Bazel version (3.4.1). - Add grpc/tests:grpc_test. One can kick off tests in Bazel by calling `bazel test grpc/tests/...`. - Add missing build targets in tests/BUILD in order to support grpc/tests/BUILD --- .gitignore | 1 + BUILD | 1 + WORKSPACE | 23 +++++++++++++--- bazel/grpc_deps_1_15_1_02.patch | 48 +++++++++++++++++++++++++++++++++ bazel/protobuf_deps_3_6_1.patch | 24 +++++++++++++++++ grpc/BUILD | 0 grpc/README.md | 12 +++++++++ grpc/src/compiler/BUILD | 2 +- grpc/tests/BUILD | 17 ++++++++++++ src/BUILD | 2 +- tests/BUILD | 42 ++++++++++++++++++++++++++++- 11 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 bazel/grpc_deps_1_15_1_02.patch create mode 100644 bazel/protobuf_deps_3_6_1.patch create mode 100644 grpc/BUILD create mode 100644 grpc/tests/BUILD diff --git a/.gitignore b/.gitignore index ae7aa58ad..fad67debb 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,4 @@ grpc/google/ **/Package.resolved .clangd/** package-lock.json +.clwb \ No newline at end of file diff --git a/BUILD b/BUILD index 4f8107b26..936ef9df4 100644 --- a/BUILD +++ b/BUILD @@ -27,6 +27,7 @@ filegroup( "include/flatbuffers/code_generators.h", "include/flatbuffers/flatbuffers.h", "include/flatbuffers/flexbuffers.h", + "include/flatbuffers/grpc.h", "include/flatbuffers/hash.h", "include/flatbuffers/idl.h", "include/flatbuffers/minireflect.h", diff --git a/WORKSPACE b/WORKSPACE index 7aea457c2..3973a89fe 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -4,27 +4,44 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "io_bazel_rules_go", + sha256 = "e88471aea3a3a4f19ec1310a55ba94772d087e9ce46e41ae38ecebe17935de7b", urls = [ "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/v0.20.3/rules_go-v0.20.3.tar.gz", "https://github.com/bazelbuild/rules_go/releases/download/v0.20.3/rules_go-v0.20.3.tar.gz", ], - sha256 = "e88471aea3a3a4f19ec1310a55ba94772d087e9ce46e41ae38ecebe17935de7b", ) -load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains") +load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") go_rules_dependencies() go_register_toolchains() +##### Protobuf +_PROTOBUF_VERSION = "3.6.1" + +http_archive( + name = "com_google_protobuf", + patch_args = ["-p1"], + patches = [ + "//bazel:protobuf_deps_3_6_1.patch", + ], + strip_prefix = "protobuf-" + _PROTOBUF_VERSION, + urls = [ + "https://github.com/protocolbuffers/protobuf/archive/v" + _PROTOBUF_VERSION + ".tar.gz", + ], +) ##### GRPC _GRPC_VERSION = "1.15.1" http_archive( name = "com_github_grpc_grpc", - patches = ["//bazel:grpc_deps_1_15_1.patch"], patch_args = ["-p1"], + patches = [ + "//bazel:grpc_deps_1_15_1.patch", + "//bazel:grpc_deps_1_15_1_02.patch", + ], strip_prefix = "grpc-" + _GRPC_VERSION, urls = ["https://github.com/grpc/grpc/archive/v" + _GRPC_VERSION + ".tar.gz"], ) diff --git a/bazel/grpc_deps_1_15_1_02.patch b/bazel/grpc_deps_1_15_1_02.patch new file mode 100644 index 000000000..328134ea8 --- /dev/null +++ b/bazel/grpc_deps_1_15_1_02.patch @@ -0,0 +1,48 @@ +diff --git a/bazel/cc_grpc_library.bzl b/bazel/cc_grpc_library.bzl +index 3288565714..6bfcd653f5 100644 +--- a/bazel/cc_grpc_library.bzl ++++ b/bazel/cc_grpc_library.bzl +@@ -1,6 +1,6 @@ + """Generates and compiles C++ grpc stubs from proto_library rules.""" + +-load("//:bazel/generate_cc.bzl", "generate_cc") ++load("//bazel:generate_cc.bzl", "generate_cc") + + def cc_grpc_library(name, srcs, deps, proto_only, well_known_protos, generate_mocks = False, use_external = False, **kwargs): + """Generates C++ grpc classes from a .proto file. +diff --git a/bazel/generate_cc.bzl b/bazel/generate_cc.bzl +index ae747aa42c..2f14071f92 100644 +--- a/bazel/generate_cc.bzl ++++ b/bazel/generate_cc.bzl +@@ -83,7 +83,7 @@ _generate_cc = rule( + attrs = { + "srcs": attr.label_list( + mandatory = True, +- non_empty = True, ++ allow_empty = False, + providers = ["proto"], + ), + "plugin": attr.label( +diff --git a/bazel/grpc_build_system.bzl b/bazel/grpc_build_system.bzl +index 73147bf3ac..e8c392aa93 100644 +--- a/bazel/grpc_build_system.bzl ++++ b/bazel/grpc_build_system.bzl +@@ -24,6 +24,9 @@ + # + + # The set of pollers to test against if a test exercises polling ++ ++load("//bazel:cc_grpc_library.bzl", "cc_grpc_library") ++ + POLLERS = ['epollex', 'epollsig', 'epoll1', 'poll', 'poll-cv'] + + def if_not_windows(a): +@@ -95,8 +98,6 @@ def grpc_proto_plugin(name, srcs = [], deps = []): + deps = deps, + ) + +-load("//:bazel/cc_grpc_library.bzl", "cc_grpc_library") +- + def grpc_proto_library(name, srcs = [], deps = [], well_known_protos = False, + has_services = True, use_external = False, generate_mocks = False): + cc_grpc_library( diff --git a/bazel/protobuf_deps_3_6_1.patch b/bazel/protobuf_deps_3_6_1.patch new file mode 100644 index 000000000..1c85062d2 --- /dev/null +++ b/bazel/protobuf_deps_3_6_1.patch @@ -0,0 +1,24 @@ +diff --git a/protobuf.bzl b/protobuf.bzl +index 78f19c621..3b6d183b9 100644 +--- a/protobuf.bzl ++++ b/protobuf.bzl +@@ -130,7 +130,7 @@ proto_gen = rule( + "protoc": attr.label( + cfg = "host", + executable = True, +- single_file = True, ++ allow_single_file = True, + mandatory = True, + ), + "plugin": attr.label( +@@ -266,8 +266,8 @@ def internal_gen_well_known_protos_java(srcs): + Args: + srcs: the well known protos + """ +- root = Label("%s//protobuf_java" % (REPOSITORY_NAME)).workspace_root +- pkg = PACKAGE_NAME + "/" if PACKAGE_NAME else "" ++ root = Label("%s//protobuf_java" % (native.repository_name())).workspace_root ++ pkg = native.package_name() + "/" if native.package_name() else "" + if root == "": + include = " -I%ssrc " % pkg + else: diff --git a/grpc/BUILD b/grpc/BUILD new file mode 100644 index 000000000..e69de29bb diff --git a/grpc/README.md b/grpc/README.md index 544651d60..685003f92 100644 --- a/grpc/README.md +++ b/grpc/README.md @@ -22,6 +22,12 @@ the GRPC libraries for this to compile. This test will build using the 5. `cmake -DFLATBUFFERS_BUILD_GRPCTEST=ON -DGRPC_INSTALL_PATH=${GRPC_INSTALL_PATH} -DPROTOBUF_DOWNLOAD_PATH=${PROTOBUF_DOWNLOAD_PATH} ..` 6. `make` +For Bazel users: + +```shell +$bazel test src/compiler/... +``` + ## Running FlatBuffer gRPC tests ### Linux @@ -29,3 +35,9 @@ the GRPC libraries for this to compile. This test will build using the 1. `ln -s ${GRPC_INSTALL_PATH}/lib/libgrpc++_unsecure.so.6 ${GRPC_INSTALL_PATH}/lib/libgrpc++_unsecure.so.1` 2. `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${GRPC_INSTALL_PATH}/lib` 3. `make test ARGS=-V` + +For Bazel users: + +```shell +$bazel test tests/... +``` \ No newline at end of file diff --git a/grpc/src/compiler/BUILD b/grpc/src/compiler/BUILD index 96a785842..f15992109 100644 --- a/grpc/src/compiler/BUILD +++ b/grpc/src/compiler/BUILD @@ -104,4 +104,4 @@ cc_library( deps = [ "//:flatbuffers", ], -) \ No newline at end of file +) diff --git a/grpc/tests/BUILD b/grpc/tests/BUILD new file mode 100644 index 000000000..5d4023ec7 --- /dev/null +++ b/grpc/tests/BUILD @@ -0,0 +1,17 @@ +cc_test( + name = "grpc_test", + srcs = [ + "grpctest.cpp", + "message_builder_test.cpp", + ], + copts = ["-Itests"], + # This is required. + linkstatic = 1, + deps = [ + "//tests:monster_test_cc_fbs", + "//tests:monster_test_grpc", + "//tests:test_assert", + "//tests:test_builder", + "@com_github_grpc_grpc//:grpc++", + ], +) diff --git a/src/BUILD b/src/BUILD index cf1bd2f6c..86f0a7d60 100644 --- a/src/BUILD +++ b/src/BUILD @@ -55,8 +55,8 @@ cc_library( "idl_gen_php.cpp", "idl_gen_python.cpp", "idl_gen_rust.cpp", - "idl_gen_text.cpp", "idl_gen_swift.cpp", + "idl_gen_text.cpp", "util.cpp", ], hdrs = [ diff --git a/tests/BUILD b/tests/BUILD index 97ef67484..f66d4142d 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -9,6 +9,7 @@ cc_test( srcs = [ "evolution_test/evolution_v1_generated.h", "evolution_test/evolution_v2_generated.h", + "monster_test_bfbs_generated.h", "namespace_test/namespace_test1_generated.h", "namespace_test/namespace_test2_generated.h", "native_type_test_impl.cpp", @@ -19,7 +20,6 @@ cc_test( "test_builder.cpp", "test_builder.h", "union_vector/union_vector_generated.h", - "monster_test_bfbs_generated.h", ], copts = [ "-DFLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE", @@ -70,6 +70,45 @@ cc_test( # Test bzl rules load("//:build_defs.bzl", "flatbuffer_cc_library") +cc_library( + name = "test_assert", + srcs = ["test_assert.cpp"], + hdrs = ["test_assert.h"], + visibility = ["//grpc/tests:__subpackages__"], + deps = ["//:flatbuffers"], +) + +cc_library( + name = "test_builder", + srcs = ["test_builder.cpp"], + hdrs = ["test_builder.h"], + visibility = ["//grpc/tests:__subpackages__"], + deps = [ + ":monster_test_grpc", + ":test_assert", + "//:flatbuffers", + ], +) + +cc_library( + name = "monster_test_grpc", + srcs = [ + "monster_test.grpc.fb.cc", + "monster_test.grpc.fb.h", + "monster_test_generated.h", + ], + hdrs = [ + "monster_test.grpc.fb.h", + "monster_test_generated.h", + ], + includes = ["."], + visibility = ["//grpc/tests:__subpackages__"], + deps = [ + "//:flatbuffers", + "@com_github_grpc_grpc//:grpc++", + ], +) + flatbuffer_cc_library( name = "monster_test_cc_fbs", srcs = ["monster_test.fbs"], @@ -78,6 +117,7 @@ flatbuffer_cc_library( "include_test/include_test1.fbs", "include_test/sub/include_test2.fbs", ], + visibility = ["//grpc/tests:__subpackages__"], ) flatbuffer_cc_library(