|
| 1 | +load("@bazel_tools//tools/jdk:toolchain_utils.bzl", "find_java_runtime_toolchain", "find_java_toolchain") |
| 2 | + |
| 3 | +# Taken from bazelbuild/rules_go license: Apache 2 |
| 4 | +# https://github.com/bazelbuild/rules_go/blob/528f6faf83f85c23da367d61f784893d1b3bd72b/proto/compiler.bzl#L94 |
| 5 | +# replaced `prefix = paths.join(..` with `prefix = "/".join(..` |
| 6 | +def _proto_path(src, proto): |
| 7 | + """proto_path returns the string used to import the proto. This is the proto |
| 8 | + source path within its repository, adjusted by import_prefix and |
| 9 | + strip_import_prefix. |
| 10 | +
|
| 11 | + Args: |
| 12 | + src: the proto source File. |
| 13 | + proto: the ProtoInfo provider. |
| 14 | +
|
| 15 | + Returns: |
| 16 | + An import path string. |
| 17 | + """ |
| 18 | + if not hasattr(proto, "proto_source_root"): |
| 19 | + # Legacy path. Remove when Bazel minimum version >= 0.21.0. |
| 20 | + path = src.path |
| 21 | + root = src.root.path |
| 22 | + ws = src.owner.workspace_root |
| 23 | + if path.startswith(root): |
| 24 | + path = path[len(root):] |
| 25 | + if path.startswith("/"): |
| 26 | + path = path[1:] |
| 27 | + if path.startswith(ws): |
| 28 | + path = path[len(ws):] |
| 29 | + if path.startswith("/"): |
| 30 | + path = path[1:] |
| 31 | + return path |
| 32 | + |
| 33 | + if proto.proto_source_root == ".": |
| 34 | + # true if proto sources were generated |
| 35 | + prefix = src.root.path + "/" |
| 36 | + elif proto.proto_source_root.startswith(src.root.path): |
| 37 | + # sometimes true when import paths are adjusted with import_prefix |
| 38 | + prefix = proto.proto_source_root + "/" |
| 39 | + else: |
| 40 | + # usually true when paths are not adjusted |
| 41 | + prefix = "/".join([src.root.path, proto.proto_source_root]) + "/" |
| 42 | + if not src.path.startswith(prefix): |
| 43 | + # sometimes true when importing multiple adjusted protos |
| 44 | + return src.path |
| 45 | + return src.path[len(prefix):] |
| 46 | + |
| 47 | +def _reactive_grpc_library_impl(ctx): |
| 48 | + proto = ctx.attr.proto[ProtoInfo] |
| 49 | + descriptor_set_in = proto.transitive_descriptor_sets |
| 50 | + |
| 51 | + gensrcjar = ctx.actions.declare_file("%s-proto-gensrc.jar" % ctx.label.name) |
| 52 | + |
| 53 | + args = ctx.actions.args() |
| 54 | + args.add(ctx.executable.reactive_plugin.path, format = "--plugin=protoc-gen-reactive-grpc-plugin=%s") |
| 55 | + args.add("--reactive-grpc-plugin_out=:{0}".format(gensrcjar.path)) |
| 56 | + args.add_joined("--descriptor_set_in", descriptor_set_in, join_with = ":") |
| 57 | + for src in proto.check_deps_sources.to_list(): |
| 58 | + args.add(_proto_path(src, proto)) |
| 59 | + |
| 60 | + ctx.actions.run( |
| 61 | + inputs = descriptor_set_in, |
| 62 | + tools = [ctx.executable.reactive_plugin], |
| 63 | + outputs = [gensrcjar], |
| 64 | + executable = ctx.executable._protoc, |
| 65 | + arguments = [args], |
| 66 | + ) |
| 67 | + |
| 68 | + deps = [java_common.make_non_strict(dep[JavaInfo]) for dep in ctx.attr.deps] |
| 69 | + deps += [dep[JavaInfo] for dep in ctx.attr.reactive_deps] |
| 70 | + |
| 71 | + java_info = java_common.compile( |
| 72 | + ctx, |
| 73 | + deps = deps, |
| 74 | + host_javabase = find_java_runtime_toolchain(ctx, ctx.attr._host_javabase), |
| 75 | + java_toolchain = find_java_toolchain(ctx, ctx.attr._java_toolchain), |
| 76 | + output = ctx.outputs.jar, |
| 77 | + output_source_jar = ctx.outputs.srcjar, |
| 78 | + source_jars = [gensrcjar], |
| 79 | + ) |
| 80 | + |
| 81 | + return [java_info] |
| 82 | + |
| 83 | +_reactive_grpc_library = rule( |
| 84 | + attrs = { |
| 85 | + "proto": attr.label( |
| 86 | + mandatory = True, |
| 87 | + providers = [ProtoInfo], |
| 88 | + ), |
| 89 | + "deps": attr.label_list( |
| 90 | + mandatory = True, |
| 91 | + allow_empty = False, |
| 92 | + providers = [JavaInfo], |
| 93 | + ), |
| 94 | + "_protoc": attr.label( |
| 95 | + default = Label("@com_google_protobuf//:protoc"), |
| 96 | + executable = True, |
| 97 | + cfg = "host", |
| 98 | + ), |
| 99 | + "reactive_deps": attr.label_list( |
| 100 | + mandatory = True, |
| 101 | + allow_empty = False, |
| 102 | + providers = [JavaInfo], |
| 103 | + ), |
| 104 | + "reactive_plugin": attr.label( |
| 105 | + mandatory = True, |
| 106 | + executable = True, |
| 107 | + cfg = "host", |
| 108 | + ), |
| 109 | + "_java_toolchain": attr.label( |
| 110 | + default = Label("@bazel_tools//tools/jdk:current_java_toolchain"), |
| 111 | + ), |
| 112 | + "_host_javabase": attr.label( |
| 113 | + cfg = "host", |
| 114 | + default = Label("@bazel_tools//tools/jdk:current_host_java_runtime"), |
| 115 | + ), |
| 116 | + }, |
| 117 | + fragments = ["java"], |
| 118 | + outputs = { |
| 119 | + "jar": "lib%{name}.jar", |
| 120 | + "srcjar": "lib%{name}-src.jar", |
| 121 | + }, |
| 122 | + provides = [JavaInfo], |
| 123 | + implementation = _reactive_grpc_library_impl, |
| 124 | +) |
| 125 | + |
| 126 | +def reactor_grpc_library(**kwargs): |
| 127 | + _reactive_grpc_library( |
| 128 | + reactive_plugin = "@com_salesforce_servicelibs_reactive_grpc//reactor/reactor-grpc:reactor_grpc_bin", |
| 129 | + reactive_deps = [ |
| 130 | + "@com_salesforce_servicelibs_reactive_grpc//reactor/reactor-grpc-stub", |
| 131 | + "@io_projectreactor_reactor_core", |
| 132 | + ], |
| 133 | + **kwargs |
| 134 | + ) |
| 135 | + |
| 136 | +def rx_grpc_library(**kwargs): |
| 137 | + _reactive_grpc_library( |
| 138 | + reactive_plugin = "@com_salesforce_servicelibs_reactive_grpc//rx-java/rxgrpc:rxgrpc_bin", |
| 139 | + reactive_deps = [ |
| 140 | + "@com_salesforce_servicelibs_reactive_grpc//rx-java/rxgrpc-stub", |
| 141 | + "@com_salesforce_servicelibs_reactive_grpc//common/reactive-grpc-common", |
| 142 | + "@io_reactivex_rxjava2_rxjava", |
| 143 | + ], |
| 144 | + **kwargs |
| 145 | + ) |
0 commit comments