Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScalaPB deps toolchain #1104

Merged
merged 7 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions docs/scala_proto_library.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ To configure ScalaPB options, configure a different `scala_proto_toolchain` and

```python
load("@io_bazel_rules_scala//scala_proto:scala_proto_toolchain.bzl", "scala_proto_toolchain")
load("@io_bazel_rules_scala//scala:providers.bzl", "declare_deps_provider")

scala_proto_toolchain(
name = "scala_proto_toolchain_configuration",
with_grpc = False,
with_flat_package = False,
with_single_line_to_string = False,
visibility = ["//visibility:public"],
dep_providers = [":my_grpc_deps", ":my_compile_deps"],
)

toolchain(
Expand All @@ -49,6 +51,20 @@ toolchain(
toolchain_type = "@io_bazel_rules_scala//scala_proto:toolchain_type",
visibility = ["//visibility:public"],
)

declare_deps_provider(
name = "my_compile_deps",
deps_id = "scalapb_compile_deps",
deps = ["@dep1", "@dep2"],
visibility = ["//visibility:public"],
)

declare_deps_provider(
name = "my_grpc_deps",
deps_id = "scalapb_grpc_deps",
deps = ["@dep3", "@dep4"],
visibility = ["//visibility:public"],
)
```

### Toolchain Attributes
Expand All @@ -63,6 +79,5 @@ toolchain(
| code_generator | `Label, optional (has default)` <br> Which code generator to use. A sensible default is provided.
| named_generators | `String dict, optional` <br>
| extra_generator_dependencies | `List of labels, optional` <br>
| grpc_deps | `List of labels, optional (has default)` <br> gRPC dependencies. A sensible default is provided.
| implicit_compile_deps | `List of labels, optional (has default)` <br> ScalaPB dependencies. A sensible default is provided.
| scalac | `Label, optional (has default)` <br> Target for scalac. A sensible default is provided.
| dep_providers | `List of labels, optional (has default)` <br> allows inject gRPC (deps_id - `scalapb_grpc_deps`) and ScalaPB (deps_id `scalapb_compile_deps`) dependencies
61 changes: 53 additions & 8 deletions scala_proto/BUILD
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
load("@rules_java//java:defs.bzl", "java_library")
load("//scala_proto:scala_proto_toolchain.bzl", "scala_proto_toolchain")
load("//scala_proto:default_dep_sets.bzl", "DEFAULT_SCALAPB_COMPILE_DEPS", "DEFAULT_SCALAPB_GRPC_DEPS")
load(
"//scala_proto:scala_proto_toolchain.bzl",
"export_scalapb_toolchain_deps",
"scala_proto_deps_toolchain",
"scala_proto_toolchain",
)
load(
"//scala_proto:default_dep_sets.bzl",
"DEFAULT_SCALAPB_COMPILE_DEPS",
"DEFAULT_SCALAPB_GRPC_DEPS",
)
load("//scala:providers.bzl", "declare_deps_provider")

toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)

toolchain_type(
name = "deps_toolchain_type",
visibility = ["//visibility:public"],
)

scala_proto_deps_toolchain(
name = "default_deps_toolchain_impl",
visibility = ["//visibility:public"],
)

scala_proto_toolchain(
name = "default_toolchain_impl",
visibility = ["//visibility:public"],
Expand All @@ -22,6 +42,12 @@ toolchain(
visibility = ["//visibility:public"],
)

toolchain(
name = "default_deps_toolchain",
toolchain = ":default_deps_toolchain_impl",
toolchain_type = ":deps_toolchain_type",
)

scala_proto_toolchain(
name = "enable_all_options_toolchain_impl",
visibility = ["//visibility:public"],
Expand All @@ -38,14 +64,33 @@ toolchain(
visibility = ["//visibility:public"],
)

java_library(
name = "default_scalapb_compile_dependencies",
declare_deps_provider(
name = "scalapb_compile_deps_provider",
deps_id = "scalapb_compile_deps",
visibility = ["//visibility:public"],
deps = DEFAULT_SCALAPB_COMPILE_DEPS,
)

declare_deps_provider(
name = "scalapb_grpc_deps_provider",
deps_id = "scalapb_grpc_deps",
visibility = ["//visibility:public"],
deps = DEFAULT_SCALAPB_GRPC_DEPS,
)

declare_deps_provider(
name = "scalapb_worker_deps_provider",
deps_id = "scalapb_worker_deps",
visibility = ["//visibility:public"],
exports = DEFAULT_SCALAPB_COMPILE_DEPS,
deps = [
"//external:io_bazel_rules_scala/dependency/com_google_protobuf/protobuf_java",
"//external:io_bazel_rules_scala/dependency/proto/protoc_bridge",
"//external:io_bazel_rules_scala/dependency/proto/scalapb_plugin",
],
)

java_library(
name = "default_scalapb_grpc_dependencies",
export_scalapb_toolchain_deps(
name = "scalapb_worker_deps",
deps_id = "scalapb_worker_deps",
visibility = ["//visibility:public"],
exports = DEFAULT_SCALAPB_GRPC_DEPS,
)
2 changes: 2 additions & 0 deletions scala_proto/private/scala_proto_default_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,5 @@ def scala_proto_default_repositories(
name = "io_bazel_rules_scala/dependency/proto/opencensus_contrib_grpc_metrics",
actual = "@scala_proto_rules_opencensus_contrib_grpc_metrics//jar",
)

native.register_toolchains("@io_bazel_rules_scala//scala_proto:default_deps_toolchain")
31 changes: 21 additions & 10 deletions scala_proto/private/scalapb_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("//scala/private:common.bzl", "write_manifest_file")
load("//scala/private:dependency.bzl", "legacy_unclear_dependency_info_for_protobuf_scrooge")
load("//scala/private:rule_impls.bzl", "compile_scala")
load("//scala_proto/private:proto_to_scala_src.bzl", "proto_to_scala_src")
load("//scala/private/toolchain_deps:toolchain_deps.bzl", "find_deps_info_on", "java_info_for_deps")

ScalaPBAspectInfo = provider(fields = [
"proto_info",
Expand Down Expand Up @@ -139,13 +140,23 @@ def _scalapb_aspect_impl(target, ctx):
compile_protos = sorted(target_ti.direct_sources)
transitive_protos = sorted(target_ti.transitive_sources.to_list())

toolchain = ctx.toolchains["@io_bazel_rules_scala//scala_proto:toolchain_type"]
deps_toolchain_type_label = "@io_bazel_rules_scala//scala_proto:deps_toolchain_type"
toolchain_type_label = "@io_bazel_rules_scala//scala_proto:toolchain_type"
toolchain = ctx.toolchains[toolchain_type_label]
flags = []
imps = [j[JavaInfo] for j in ctx.attr._implicit_compile_deps]

compile_deps = find_deps_info_on(
ctx,
deps_toolchain_type_label,
"scalapb_compile_deps",
).deps

imps = [dep[JavaInfo] for dep in compile_deps]

if toolchain.with_grpc:
grpc_deps = find_deps_info_on(ctx, deps_toolchain_type_label, "scalapb_grpc_deps").deps
flags.append("grpc")
imps.extend([j[JavaInfo] for j in ctx.attr._grpc_deps])
imps.extend([dep[JavaInfo] for dep in grpc_deps])

if toolchain.with_flat_package:
flags.append("flat_package")
Expand Down Expand Up @@ -233,16 +244,16 @@ scalapb_aspect = aspect(
[ScalaPBImport],
],
attrs = {
"_protoc": attr.label(executable = True, cfg = "host", default = "@com_google_protobuf//:protoc"),
"_implicit_compile_deps": attr.label_list(cfg = "target", default = [
"//external:io_bazel_rules_scala/dependency/proto/implicit_compile_deps",
]),
"_grpc_deps": attr.label_list(cfg = "target", default = [
"//external:io_bazel_rules_scala/dependency/proto/grpc_deps",
]),
"_protoc": attr.label(
executable = True,
cfg = "exec",
default = "@com_google_protobuf//:protoc",
),
},
incompatible_use_toolchain_transition = True,
toolchains = [
"@io_bazel_rules_scala//scala:toolchain_type",
"@io_bazel_rules_scala//scala_proto:toolchain_type",
"@io_bazel_rules_scala//scala_proto:deps_toolchain_type",
],
)
39 changes: 38 additions & 1 deletion scala_proto/scala_proto_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
load("//scala_proto:default_dep_sets.bzl", "DEFAULT_SCALAPB_COMPILE_DEPS", "DEFAULT_SCALAPB_GRPC_DEPS")
load("@io_bazel_rules_scala//scala:providers.bzl", "DepsInfo")
load("//scala/private/toolchain_deps:toolchain_deps.bzl", "expose_toolchain_deps")

def _scala_proto_toolchain_impl(ctx):
toolchain = platform_common.ToolchainInfo(
Expand Down Expand Up @@ -28,7 +30,7 @@ scala_proto_toolchain = rule(
"blacklisted_protos": attr.label_list(default = []),
"code_generator": attr.label(
executable = True,
cfg = "host",
cfg = "exec",
default = Label("@io_bazel_rules_scala//src/scala/scripts:scalapb_worker"),
allow_files = True,
),
Expand All @@ -43,3 +45,38 @@ scala_proto_toolchain = rule(
),
},
)

def _scala_proto_deps_toolchain(ctx):
toolchain = platform_common.ToolchainInfo(
dep_providers = ctx.attr.dep_providers,
)
return [toolchain]

scala_proto_deps_toolchain = rule(
_scala_proto_deps_toolchain,
attrs = {
"dep_providers": attr.label_list(
default = [
"@io_bazel_rules_scala//scala_proto:scalapb_compile_deps_provider",
"@io_bazel_rules_scala//scala_proto:scalapb_grpc_deps_provider",
"@io_bazel_rules_scala//scala_proto:scalapb_worker_deps_provider",
],
cfg = "target",
providers = [DepsInfo],
),
},
)

def _export_scalapb_toolchain_deps(ctx):
return expose_toolchain_deps(ctx, "@io_bazel_rules_scala//scala_proto:deps_toolchain_type")

export_scalapb_toolchain_deps = rule(
_export_scalapb_toolchain_deps,
attrs = {
"deps_id": attr.string(
mandatory = True,
),
},
incompatible_use_toolchain_transition = True,
toolchains = ["@io_bazel_rules_scala//scala_proto:deps_toolchain_type"],
)
10 changes: 1 addition & 9 deletions src/scala/scripts/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,10 @@ scala_library(
scala_library(
name = "scalapb_worker_lib",
srcs = ["ScalaPBWorker.scala"],
unused_dependency_checker_ignored_targets = [
"//external:io_bazel_rules_scala/dependency/com_google_protobuf/protobuf_java",
],
visibility = ["//visibility:public"],
runtime_deps = [
"//external:io_bazel_rules_scala/dependency/com_google_protobuf/protobuf_java",
],
deps = [
":scala_proto_request_extractor",
"//external:io_bazel_rules_scala/dependency/com_google_protobuf/protobuf_java",
"//external:io_bazel_rules_scala/dependency/proto/protoc_bridge",
"//external:io_bazel_rules_scala/dependency/proto/scalapb_plugin",
"//scala_proto:scalapb_worker_deps",
"//src/java/io/bazel/rulesscala/io_utils",
"//src/java/io/bazel/rulesscala/jar",
"//src/java/io/bazel/rulesscala/worker",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ scala_library(
srcs = ["ExtraProtobufGenerator.scala"],
visibility = ["//visibility:public"],
deps = [
"//external:io_bazel_rules_scala/dependency/com_google_protobuf/protobuf_java",
"//external:io_bazel_rules_scala/dependency/proto/protoc_bridge",
"//external:io_bazel_rules_scala/dependency/proto/scalapb_plugin",
"//scala_proto:scalapb_worker_deps",
],
)