Clean up Javascript-related Bazel rules. (#1138)

This commit is contained in:
Eryu Xia 2021-09-21 23:16:28 -07:00 committed by GitHub
parent 32fe12459b
commit 78ddf996d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 2 additions and 650 deletions

View File

@ -2,15 +2,6 @@ workspace(name = "com_github_grpc_grpc_web")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_closure",
sha256 = "a4b6be2187fc4ba3b8815a29d833d744dbe889e9b03a4fd27234921339775a5c",
strip_prefix = "rules_closure-1ea537ad44baa7cce8c2c7a12b74375155ec6978",
urls = [
"https://github.com/bazelbuild/rules_closure/archive/1ea537ad44baa7cce8c2c7a12b74375155ec6978.zip",
],
)
http_archive(
name = "bazel_skylib",
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
@ -38,16 +29,6 @@ http_archive(
],
)
load("@io_bazel_rules_closure//closure:repositories.bzl", "rules_closure_dependencies", "rules_closure_toolchains")
rules_closure_dependencies()
rules_closure_toolchains()
load("//bazel:repositories.bzl", "grpc_web_toolchains")
grpc_web_toolchains()
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
grpc_deps()

View File

@ -1,33 +0,0 @@
## Copyright 2020 Google LLC
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## https://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
load("//bazel:defs.bzl", "grpc_web_toolchain")
# The toolchain type used to distinguish proto toolchains.
toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)
grpc_web_toolchain(
name = "closure_toolchain_impl",
generator = "//javascript/net/grpc/web:protoc-gen-grpc-web",
runtime_library = "//javascript/net/grpc/web:closure_grpcweb_runtime",
)
toolchain(
name = "closure_toolchain",
toolchain = ":closure_toolchain_impl",
toolchain_type = ":toolchain_type",
)

View File

@ -1,20 +0,0 @@
## Copyright 2020 Google LLC
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## https://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
"""Compatibility-export for `closure_grpc_web_library`."""
load("//bazel/private/rules:closure_grpc_web_library.bzl", _closure_grpc_web_library = "closure_grpc_web_library")
# TODO(yannic): Deprecate and remove.
closure_grpc_web_library = _closure_grpc_web_library

View File

@ -1,21 +0,0 @@
## Copyright 2020 Google LLC
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## https://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
"""Starlark rules for using gRPC-Web."""
load("//bazel/private/rules:closure_grpc_web_library.bzl", _closure_grpc_web_library = "closure_grpc_web_library")
load("//bazel/private/rules:grpc_web_toolchain.bzl", _grpc_web_toolchain = "grpc_web_toolchain")
closure_grpc_web_library = _closure_grpc_web_library
grpc_web_toolchain = _grpc_web_toolchain

View File

@ -1 +0,0 @@
# Intentionally left empty (for now).

View File

@ -1,197 +0,0 @@
# This rule was inspired by rules_closure`s implementation of
# |closure_proto_library|, licensed under Apache 2.
# https://github.com/bazelbuild/rules_closure/blob/3555e5ba61fdcc17157dd833eaf7d19b313b1bca/closure/protobuf/closure_proto_library.bzl
"""Starlark rules for using gRPC-Web with Bazel and `rules_closure`."""
load("@io_bazel_rules_closure//closure/compiler:closure_js_library.bzl", "create_closure_js_library")
load("@io_bazel_rules_closure//closure/private:defs.bzl", "CLOSURE_JS_TOOLCHAIN_ATTRS", "unfurl") # buildifier: disable=bzl-visibility
load("@io_bazel_rules_closure//closure/protobuf:closure_proto_library.bzl", "closure_proto_aspect")
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
# This was borrowed from Rules Go, licensed under Apache 2.
# https://github.com/bazelbuild/rules_go/blob/67f44035d84a352cffb9465159e199066ecb814c/proto/compiler.bzl#L72
def _proto_path(proto):
path = proto.path
root = proto.root.path
ws = proto.owner.workspace_root
if path.startswith(root):
path = path[len(root):]
if path.startswith("/"):
path = path[1:]
if path.startswith(ws):
path = path[len(ws):]
if path.startswith("/"):
path = path[1:]
return path
def _proto_include_path(proto):
path = proto.path[:-len(_proto_path(proto))]
if not path:
return "."
if path.endswith("/"):
path = path[:-1]
return path
def _proto_include_paths(protos):
return [_proto_include_path(proto) for proto in protos]
def _generate_closure_grpc_web_src_progress_message(proto):
# TODO(yannic): Add a better message?
return "Generating GRPC Web for %s" % proto
def _assert(condition, message):
if not condition:
fail(message)
def _generate_closure_grpc_web_srcs(
label,
actions,
protoc,
protoc_gen_grpc_web,
import_style,
mode,
sources,
transitive_sources):
args = actions.args()
args.add("--plugin", "protoc-gen-grpc-web=" + protoc_gen_grpc_web.executable.path)
args.add_all(_proto_include_paths(transitive_sources.to_list()), format_each = "-I%s")
args.add("--grpc-web_opt", "mode=" + mode)
if "es6" == import_style:
args.add("--grpc-web_opt", "import_style=experimental_closure_es6")
else:
args.add("--grpc-web_opt", "import_style=" + import_style)
root = None
files = []
es6_files = []
for src in sources:
basename = src.basename[:-(len(src.extension) + 1)]
js = actions.declare_file(basename + "_grpc_web_pb.js", sibling = src)
files.append(js)
_assert(
((root == None) or (root == js.root.path)),
"proto sources do not have the same root: '{}' != '{}'".format(root, js.root.path),
)
root = js.root.path
if "es6" == import_style:
es6 = actions.declare_file(basename + ".pb.grpc-web.js", sibling = src)
es6_files.append(es6)
_assert(root == es6.root.path, "ES6 file should have same root: '{}' != '{}'".format(root, es6.root.path))
_assert(root, "At least one source file is required")
args.add("--grpc-web_out", root)
args.add_all(sources)
actions.run(
tools = [protoc_gen_grpc_web],
inputs = transitive_sources,
outputs = files + es6_files,
executable = protoc,
arguments = [args],
progress_message =
_generate_closure_grpc_web_src_progress_message(str(label)),
)
return files, es6_files
_error_multiple_deps = "".join([
"'deps' attribute must contain exactly one label ",
"(we didn't name it 'dep' for consistency). ",
"We may revisit this restriction later.",
])
def _closure_grpc_web_library_impl(ctx):
if len(ctx.attr.deps) > 1:
# TODO(yannic): Revisit this restriction.
fail(_error_multiple_deps, "deps")
grpc_web_toolchain = ctx.toolchains["@com_github_grpc_grpc_web//bazel:toolchain_type"]
proto_info = ctx.attr.deps[0][ProtoInfo]
srcs, es6_srcs = _generate_closure_grpc_web_srcs(
label = ctx.label,
actions = ctx.actions,
protoc = ctx.executable._protoc,
protoc_gen_grpc_web = grpc_web_toolchain.generator,
import_style = ctx.attr.import_style,
mode = ctx.attr.mode,
sources = proto_info.direct_sources,
transitive_sources = proto_info.transitive_imports,
)
deps = unfurl(ctx.attr.deps, provider = "closure_js_library")
deps.append(grpc_web_toolchain.runtime_library)
library = create_closure_js_library(
ctx = ctx,
srcs = srcs + es6_srcs,
deps = deps,
suppress = [
"misplacedTypeAnnotation",
"unusedPrivateMembers",
"reportUnknownTypes",
"strictDependencies",
"extraRequire",
],
lenient = False,
)
# `rules_closure` still uses the legacy provider syntax.
# buildifier: disable=rule-impl-return
return struct(
exports = library.exports,
closure_js_library = library.closure_js_library,
# The usual suspects are exported as runfiles, in addition to raw source.
runfiles = ctx.runfiles(files = srcs),
)
closure_grpc_web_library = rule(
implementation = _closure_grpc_web_library_impl,
attrs = dict({
"deps": attr.label_list(
mandatory = True,
providers = [ProtoInfo, "closure_js_library"],
# The files generated by this aspect are required dependencies.
aspects = [closure_proto_aspect],
),
"import_style": attr.string(
default = "closure",
values = [
"closure",
# This is experimental and requires closure-js.
# We reserve the right to do breaking changes at any time.
"es6",
],
),
"mode": attr.string(
default = "grpcwebtext",
values = ["grpcwebtext", "grpcweb"],
),
# Internal only.
# TODO(yannic): Switch to using `proto_toolchain` after
# https://github.com/bazelbuild/rules_proto/pull/25 lands.
"_protoc": attr.label(
default = Label("@com_google_protobuf//:protoc"),
executable = True,
cfg = "host",
),
}, **CLOSURE_JS_TOOLCHAIN_ATTRS),
toolchains = [
"@com_github_grpc_grpc_web//bazel:toolchain_type",
],
# TODO(yannic): Remove after `--incompatible_override_toolchain_transition` is flipped.
# https://github.com/bazelbuild/bazel/issues/11584
incompatible_use_toolchain_transition = True,
)

View File

@ -1,42 +0,0 @@
## Copyright 2020 Google LLC
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## https://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
"""Contains the definition of `grpc_web_toolchain`."""
def _grpc_web_toolchain_impl(ctx):
return [
platform_common.ToolchainInfo(
generator = ctx.attr.generator.files_to_run,
runtime_library = ctx.attr.runtime_library,
),
]
grpc_web_toolchain = rule(
implementation = _grpc_web_toolchain_impl,
attrs = {
"generator": attr.label(
mandatory = True,
executable = True,
cfg = "exec",
),
"runtime_library": attr.label(
mandatory = False,
cfg = "target",
providers = ["closure_js_library"],
),
},
provides = [
platform_common.ToolchainInfo,
],
)

View File

@ -1,27 +0,0 @@
## Copyright 2020 Google LLC
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## https://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
"""To be documented."""
def grpc_web_dependencies():
"""An utility method to load all dependencies of `gRPC-Web`."""
fail("Loading dependencies through grpc_web_dependencies() is not supported yet.")
def grpc_web_toolchains():
"""An utility method to load all gRPC-Web toolchains."""
native.register_toolchains(
"//bazel:closure_toolchain",
)

View File

@ -1,4 +1,3 @@
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_library")
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
@ -11,231 +10,3 @@ cc_binary(
"@com_google_protobuf//:protoc_lib",
],
)
# Runtime for `import_style=closure` and `mode={grpcweb,grpcwebtext}`
closure_js_library(
name = "closure_grpcweb_runtime",
visibility = ["//visibility:public"],
exports = [
":abstractclientbase",
":clientreadablestream",
":error",
":grpcwebclientbase",
],
)
closure_js_library(
name = "abstractclientbase",
srcs = [
"abstractclientbase.js",
],
suppress = [
"reportUnknownTypes",
],
visibility = ["//visibility:public"],
deps = [
":clientreadablestream",
":error",
":methodtype",
":requester",
],
)
closure_js_library(
name = "calloptions",
srcs = [
"calloptions.js",
],
suppress = [
"reportUnknownTypes",
],
visibility = ["//visibility:public"],
)
closure_js_library(
name = "clientoptions",
srcs = [
"clientoptions.js",
],
visibility = ["//visibility:public"],
deps = [
":interceptor",
],
)
closure_js_library(
name = "clientreadablestream",
srcs = [
"clientreadablestream.js",
],
visibility = ["//visibility:public"],
)
closure_js_library(
name = "clientunarycallimpl",
srcs = [
"clientunarycallimpl.js",
],
visibility = ["//visibility:public"],
deps = [
":clientreadablestream",
],
)
closure_js_library(
name = "error",
srcs = [
"error.js",
],
visibility = ["//visibility:public"],
deps = [
":metadata",
],
)
closure_js_library(
name = "generictransportinterface",
srcs = [
"generictransportinterface.js",
],
visibility = ["//visibility:public"],
deps = [
"@io_bazel_rules_closure//closure/library/net:xhrio",
"@io_bazel_rules_closure//closure/library/net/streams:nodereadablestream",
],
)
closure_js_library(
name = "grpcwebclientbase",
srcs = [
"grpcwebclientbase.js",
],
suppress = [
"checkTypes",
"reportUnknownTypes",
],
visibility = ["//visibility:public"],
deps = [
":abstractclientbase",
":clientoptions",
":clientreadablestream",
":clientunarycallimpl",
":error",
":grpcwebclientreadablestream",
":interceptor",
":methodtype",
":requester",
":status",
":statuscode",
"@io_bazel_rules_closure//closure/library/crypt:base64",
"@io_bazel_rules_closure//closure/library/net:xhrio",
"@io_bazel_rules_closure//closure/library/net/rpc:httpcors",
],
)
closure_js_library(
name = "grpcwebclientreadablestream",
srcs = [
"grpcwebclientreadablestream.js",
],
suppress = [
"reportUnknownTypes",
],
visibility = ["//visibility:public"],
deps = [
":clientreadablestream",
":error",
":generictransportinterface",
":grpcwebstreamparser",
":status",
":statuscode",
"@io_bazel_rules_closure//closure/library/crypt:base64",
"@io_bazel_rules_closure//closure/library/events",
"@io_bazel_rules_closure//closure/library/net:errorcode",
"@io_bazel_rules_closure//closure/library/net:eventtype",
"@io_bazel_rules_closure//closure/library/net:xhrio",
"@io_bazel_rules_closure//closure/library/net:xmlhttp",
"@io_bazel_rules_closure//closure/library/string",
],
)
closure_js_library(
name = "grpcwebstreamparser",
srcs = [
"grpcwebstreamparser.js",
],
suppress = [
"checkTypes",
"reportUnknownTypes",
],
visibility = ["//visibility:public"],
deps = [
"@io_bazel_rules_closure//closure/library/asserts",
"@io_bazel_rules_closure//closure/library/net/streams:streamparser",
],
)
closure_js_library(
name = "interceptor",
srcs = [
"interceptor.js",
],
visibility = ["//visibility:public"],
deps = [
":clientreadablestream",
":requester",
],
)
closure_js_library(
name = "metadata",
srcs = [
"metadata.js",
],
visibility = ["//visibility:public"],
)
closure_js_library(
name = "methodtype",
srcs = [
"methodtype.js",
],
visibility = ["//visibility:public"],
)
closure_js_library(
name = "requester",
srcs = [
"methoddescriptor.js",
"methoddescriptorinterface.js",
"request.js",
"requestinternal.js",
"unaryresponse.js",
"unaryresponseinternal.js",
],
suppress = [
"reportUnknownTypes",
],
visibility = ["//visibility:public"],
deps = [
":calloptions",
":metadata",
":methodtype",
],
)
closure_js_library(
name = "status",
srcs = [
"status.js",
],
visibility = ["//visibility:public"],
)
closure_js_library(
name = "statuscode",
srcs = [
"statuscode.js",
],
visibility = ["//visibility:public"],
)

View File

@ -73,7 +73,6 @@ WORKDIR /github/grpc-web
# Copy only files necessary to build the protoc-gen-grpc-web first as an optimization because they
# are rarely updated compared with the javascript files.
COPY ./WORKSPACE ./WORKSPACE
COPY ./bazel ./bazel
COPY ./javascript/net/grpc/web/grpc_generator.cc javascript/net/grpc/web/grpc_generator.cc
COPY ./javascript/net/grpc/web/BUILD.bazel javascript/net/grpc/web/BUILD.bazel
@ -96,4 +95,4 @@ COPY ./src ./src
COPY ./test ./test
RUN /usr/local/bin/buildifier \
--mode=check --lint=warn --warnings=all -r ./WORKSPACE bazel javascript net
--mode=check --lint=warn --warnings=all -r ./WORKSPACE javascript net

View File

@ -1,8 +1,6 @@
load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library")
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_binary", "closure_js_library", "closure_proto_library")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//bazel:defs.bzl", "closure_grpc_web_library")
proto_library(
name = "echo_proto",
@ -44,57 +42,3 @@ cc_binary(
"@com_github_grpc_grpc//:grpc++",
],
)
# Client
closure_proto_library(
name = "echo_closure_proto",
deps = [
":echo_proto",
],
)
closure_grpc_web_library(
name = "echo_closure_grpc_web",
# ES6-modules are broken in OSS because of limitation in rules_closure.
# Also emits gRPC-Web bindings with `goog.provide`; We only use `es6` here
# because of `echo_es6_integration_test_bin`.
import_style = "es6",
deps = [
":echo_proto",
],
)
closure_js_library(
name = "echo_chat",
srcs = [
"echo_chat.js",
],
deps = [
":echo_closure_grpc_web",
":echo_closure_proto",
],
)
closure_js_binary(
name = "client",
entry_points = [
"/net/grpc/gateway/examples/echo/echo_chat.js",
],
deps = [
":echo_chat",
],
)
# ES6 import style integration test
closure_js_binary(
name = "echo_es6_integration_test_bin",
entry_points = [
"goog:proto.grpc.gateway.testing.EchoServiceClient",
"/net/grpc/gateway/examples/echo/echo.pb.grpc-web.js",
],
deps = [
":echo_closure_grpc_web",
],
)

View File

@ -16,8 +16,6 @@ This step downloads the necessary pre-requisites, and serves as the base docker
image for the subsequent docker images.
```sh
$ docker build -t grpcweb/common \
-f net/grpc/gateway/docker/common/Dockerfile .
$ docker build -t grpcweb/prereqs \
-f net/grpc/gateway/docker/prereqs/Dockerfile .
```

View File

@ -13,7 +13,7 @@ Run interop tests
```sh
$ cd grpc-web
$ docker-compose build common prereqs node-interop-server interop-client
$ docker-compose build prereqs node-interop-server interop-client
```