diff --git a/rules/scala_proto.bzl b/rules/scala_proto.bzl index 28cb5c63..485af14f 100644 --- a/rules/scala_proto.bzl +++ b/rules/scala_proto.bzl @@ -11,6 +11,7 @@ scala_proto_library = rule( { "deps": attr.label_list( doc = "The proto_library targets you wish to generate Scala from", + providers = [ProtoInfo], ), "_zipper": attr.label(cfg = "host", default = "@bazel_tools//tools/zip:zipper", executable = True), }, diff --git a/rules/scala_proto/private/ScalaProtoWorker.scala b/rules/scala_proto/private/ScalaProtoWorker.scala index 195e0f9a..4db11809 100644 --- a/rules/scala_proto/private/ScalaProtoWorker.scala +++ b/rules/scala_proto/private/ScalaProtoWorker.scala @@ -3,7 +3,7 @@ package annex.scala.proto import higherkindness.rules_scala.common.args.implicits._ import higherkindness.rules_scala.common.worker.WorkerMain import java.io.File -import java.nio.file.{Files, Paths} +import java.nio.file.Files import java.util.Collections import net.sourceforge.argparse4j.ArgumentParsers import net.sourceforge.argparse4j.impl.Arguments @@ -21,6 +21,13 @@ object ScalaProtoWorker extends WorkerMain[Unit] { .help("Output dir") .metavar("output_dir") .`type`(Arguments.fileType.verifyCanCreate) + parser + .addArgument("--proto_paths") + .help("Paths to be passed to protoc as --proto_path arguments") + .metavar("proto_paths") + .nargs("*") + .`type`(Arguments.fileType.verifyCanRead.verifyIsDirectory) + .setDefault_(Collections.emptyList) parser .addArgument("sources") .help("Source files") @@ -36,11 +43,14 @@ object ScalaProtoWorker extends WorkerMain[Unit] { protected[this] def work(ctx: Unit, args: Array[String]): Unit = { val namespace = argParser.parseArgs(args) val sources = namespace.getList[File]("sources").asScala.toList + val protoPaths = namespace.getList[File]("proto_paths").asScala.toList val scalaOut = namespace.get[File]("output_dir").toPath Files.createDirectories(scalaOut) - val params = s"--scala_out=$scalaOut" :: sources.map(_.getPath) + val params = s"--scala_out=$scalaOut" :: + sources.map(_.getPath) ::: + protoPaths.map(dir => s"--proto_path=${dir.getPath}") ProtocBridge.runWithGenerators( protoc = a => com.github.os72.protocjar.Protoc.runProtoc(a.toArray), diff --git a/rules/scala_proto/private/core.bzl b/rules/scala_proto/private/core.bzl index 36044ea0..86f8bdbc 100644 --- a/rules/scala_proto/private/core.bzl +++ b/rules/scala_proto/private/core.bzl @@ -10,13 +10,9 @@ load( scala_proto_library_private_attributes = {} def scala_proto_library_implementation(ctx): - proto_deps = [dep for dep in ctx.attr.deps if ProtoInfo in dep] - if proto_deps != ctx.attr.deps: - fail("disallowed non proto deps in %s" % ctx.attr.deps) + protos = [dep[ProtoInfo] for dep in ctx.attr.deps] - protos = [dep[ProtoInfo] for dep in proto_deps] - - transitive_sources = depset(transitive = [proto.transitive_sources for proto in protos]) + sources = depset(direct = [source for proto in protos for source in proto.direct_sources]) transitive_proto_path = depset(transitive = [proto.transitive_proto_path for proto in protos]) compiler = ctx.toolchains["@rules_scala_annex//rules/scala_proto:compiler_toolchain_type"] @@ -32,7 +28,8 @@ def scala_proto_library_implementation(ctx): args = ctx.actions.args() args.add("--output_dir", gendir.path) - args.add_all("--", transitive_sources) + args.add_all("--proto_paths", transitive_proto_path) + args.add_all("--", sources) args.set_param_file_format("multiline") args.use_param_file("@%s", use_always = True) @@ -43,7 +40,7 @@ def scala_proto_library_implementation(ctx): ctx.actions.run( mnemonic = "ScalaProtoCompile", - inputs = depset(direct = [], transitive = [transitive_sources]), + inputs = depset(transitive = [proto.transitive_sources for proto in protos]), outputs = [gendir], executable = compiler.compiler.files_to_run.executable, tools = compiler_inputs, diff --git a/tests/proto/BUILD b/tests/proto/BUILD index 8f10081b..c6950ef3 100644 --- a/tests/proto/BUILD +++ b/tests/proto/BUILD @@ -27,3 +27,30 @@ scala_library( "@annex_test//:com_thesamet_scalapb_scalapb_runtime_2_12", ], ) + +proto_library( + name = "one_proto", + srcs = ["one.proto"], + visibility = ["//visibility:public"], + deps = [ + ":zero_proto", + ], +) + +scala_proto_library( + name = "one_scala_proto", + deps = [ + ":one_proto", + ], +) + +scala_library( + name = "one_scala", + srcs = [":one_scala_proto"], + deps = [ + ":zero_scala", + "@annex_test//:com_google_protobuf_protobuf_java", + "@annex_test//:com_thesamet_scalapb_lenses_2_12", + "@annex_test//:com_thesamet_scalapb_scalapb_runtime_2_12", + ], +) diff --git a/tests/proto/expected_one b/tests/proto/expected_one new file mode 100644 index 00000000..dfd1a417 --- /dev/null +++ b/tests/proto/expected_one @@ -0,0 +1,10 @@ +-rw---- 2.0 fat 0 bx 0% stor 20100101.000000 META-INF/ +-rw---- 2.0 fat 48 b- 0% stor 20100101.000000 META-INF/MANIFEST.MF +-rw---- 1.0 fat 0 b- 0% stor 20100101.000000 anx/ +-rw---- 1.0 fat 0 b- 0% stor 20100101.000000 anx/proto/ +-rw---- 1.0 fat 0 b- 0% stor 20100101.000000 anx/proto/one/ +-rw---- 2.0 fat 14827 b- 0% stor 20100101.000002 anx/proto/one/One$.class +-rw---- 2.0 fat 3097 b- 0% stor 20100101.000002 anx/proto/one/One$OneLens.class +-rw---- 2.0 fat 18959 b- 0% stor 20100101.000002 anx/proto/one/One.class +-rw---- 2.0 fat 6376 b- 0% stor 20100101.000002 anx/proto/one/OneProto$.class +-rw---- 2.0 fat 1937 b- 0% stor 20100101.000002 anx/proto/one/OneProto.class diff --git a/tests/proto/expected b/tests/proto/expected_zero similarity index 100% rename from tests/proto/expected rename to tests/proto/expected_zero diff --git a/tests/proto/one.proto b/tests/proto/one.proto new file mode 100644 index 00000000..5a405b47 --- /dev/null +++ b/tests/proto/one.proto @@ -0,0 +1,9 @@ +syntax = "proto2"; + +option java_package = "anx.proto"; + +import "proto/zero.proto"; + +message One { + optional Zero zero = 1; +} diff --git a/tests/proto/test b/tests/proto/test index 8effff76..437638dd 100755 --- a/tests/proto/test +++ b/tests/proto/test @@ -4,6 +4,11 @@ [[ "$(bazel version | head -n1 | cut -d: -f2 | tr -d ' ')" =~ 0.15.*|0.16.* ]] || bazel build :zero_scala_proto bazel build :zero_scala -diff expected <( +diff expected_zero <( zipinfo -m -T --h-t "$(bazel info bazel-bin)/proto/zero_scala.jar" ) + +bazel build :one_scala +diff expected_one <( + zipinfo -m -T --h-t "$(bazel info bazel-bin)/proto/one_scala.jar" +)