From 800f8d66bf30a9cc822e892102c86ec3e96c97df Mon Sep 17 00:00:00 2001 From: Fahrzin Hemmati Date: Thu, 30 Nov 2017 22:27:48 -0800 Subject: Handle srcs in generated files by cd'ing in and out --- protobuf.bzl | 90 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 28 deletions(-) diff --git a/protobuf.bzl b/protobuf.bzl index 78f19c62..2d8556f9 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -82,37 +82,71 @@ def _proto_gen_impl(ctx): import_flags += dep.proto.import_flags deps += dep.proto.deps - args = [] - if ctx.attr.gen_cc: - args += ["--cpp_out=" + gen_dir] - if ctx.attr.gen_py: - args += ["--python_out=" + gen_dir] - - inputs = srcs + deps - if ctx.executable.plugin: - plugin = ctx.executable.plugin - lang = ctx.attr.plugin_language - if not lang and plugin.basename.startswith('protoc-gen-'): - lang = plugin.basename[len('protoc-gen-'):] - if not lang: - fail("cannot infer the target language of plugin", "plugin_language") - - outdir = gen_dir - if ctx.attr.plugin_options: - outdir = ",".join(ctx.attr.plugin_options) + ":" + outdir - args += ["--plugin=protoc-gen-%s=%s" % (lang, plugin.path)] - args += ["--%s_out=%s" % (lang, outdir)] - inputs += [plugin] - - if args: + if not ctx.attr.gen_cc and not ctx.attr.gen_py and not ctx.executable.plugin: + return struct( + proto=struct( + srcs=srcs, + import_flags=import_flags, + deps=deps, + ), + ) + + for src, out in zip(srcs, ctx.outputs.outs): + args = [] + + in_gen_dir = src.root.path == gen_dir + if in_gen_dir: + import_flags_real = [] + for f in depset(import_flags): + path = f.replace('-I', '') + import_flags_real.append('-I$(realpath -s %s)' % path) + + if ctx.attr.gen_cc: + args += ["--cpp_out=$(realpath %s)" % gen_dir] + if ctx.attr.gen_py: + args += ["--python_out=$(realpath %s)" % gen_dir] + + inputs = [src] + deps + if ctx.executable.plugin: + plugin = ctx.executable.plugin + lang = ctx.attr.plugin_language + if not lang and plugin.basename.startswith('protoc-gen-'): + lang = plugin.basename[len('protoc-gen-'):] + if not lang: + fail("cannot infer the target language of plugin", "plugin_language") + + if in_gen_dir: + outdir = "." + else: + outdir = gen_dir + + if ctx.attr.plugin_options: + outdir = ",".join(ctx.attr.plugin_options) + ":" + outdir + args += ["--plugin=protoc-gen-%s=$(realpath %s)" % (lang, plugin.path)] + args += ["--%s_out=%s" % (lang, outdir)] + inputs += [plugin] + + if in_gen_dir: + orig_command = " ".join( + ["$(realpath %s)" % ctx.executable.protoc.path] + args + + import_flags_real + ["-I.", src.basename]) + command = ";".join([ + 'CMD="%s"' % orig_command, + "cd %s" % src.dirname, + "${CMD}", + "cd -", + "mv %s/%s %s" % (gen_dir, out.basename, out.path) + ]) + else: + command = " ".join( + [ctx.executable.protoc.path] + args + import_flags + [src.path]) ctx.action( - inputs=inputs, - outputs=ctx.outputs.outs, - arguments=args + import_flags + [s.path for s in srcs], - executable=ctx.executable.protoc, + inputs=inputs + [ctx.executable.protoc], + outputs=[out], + command=command, mnemonic="ProtoCompile", use_default_shell_env=True, - ) + ) return struct( proto=struct( -- cgit v1.2.3 From a6501e4a2b491ad6d27a8e47e93b74e3230a5406 Mon Sep 17 00:00:00 2001 From: Fahrzin Hemmati Date: Wed, 14 Mar 2018 15:23:23 -0700 Subject: Strip trailing / from path --- protobuf.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf.bzl b/protobuf.bzl index 2d8556f9..98e18108 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -94,7 +94,7 @@ def _proto_gen_impl(ctx): for src, out in zip(srcs, ctx.outputs.outs): args = [] - in_gen_dir = src.root.path == gen_dir + in_gen_dir = src.root.path == gen_dir.rstrip('/') if in_gen_dir: import_flags_real = [] for f in depset(import_flags): -- cgit v1.2.3 From b3c2ec70733e3945f0c13b8aa21b9060445b3a1f Mon Sep 17 00:00:00 2001 From: Fahrzin Hemmati Date: Wed, 14 Mar 2018 17:49:04 -0700 Subject: Handle multiple outs per input (for plugins) --- protobuf.bzl | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/protobuf.bzl b/protobuf.bzl index 98e18108..5c4a24d7 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -91,8 +91,10 @@ def _proto_gen_impl(ctx): ), ) - for src, out in zip(srcs, ctx.outputs.outs): + for src in srcs: args = [] + src_name = src.basename[:-len(".proto")] + outs = [out for out in ctx.outputs.outs if src_name in out.basename] in_gen_dir = src.root.path == gen_dir.rstrip('/') if in_gen_dir: @@ -126,27 +128,28 @@ def _proto_gen_impl(ctx): args += ["--%s_out=%s" % (lang, outdir)] inputs += [plugin] - if in_gen_dir: - orig_command = " ".join( - ["$(realpath %s)" % ctx.executable.protoc.path] + args + - import_flags_real + ["-I.", src.basename]) - command = ";".join([ - 'CMD="%s"' % orig_command, - "cd %s" % src.dirname, - "${CMD}", - "cd -", - "mv %s/%s %s" % (gen_dir, out.basename, out.path) - ]) - else: - command = " ".join( - [ctx.executable.protoc.path] + args + import_flags + [src.path]) - ctx.action( - inputs=inputs + [ctx.executable.protoc], - outputs=[out], - command=command, - mnemonic="ProtoCompile", - use_default_shell_env=True, - ) + for out in outs: + if in_gen_dir: + orig_command = " ".join( + ["$(realpath %s)" % ctx.executable.protoc.path] + args + + import_flags_real + ["-I.", src.basename]) + command = ";".join([ + 'CMD="%s"' % orig_command, + "cd %s" % src.dirname, + "${CMD}", + "cd -", + "mv %s/%s %s" % (gen_dir, out.basename, out.path) + ]) + else: + command = " ".join( + [ctx.executable.protoc.path] + args + import_flags + [src.path]) + ctx.action( + inputs=inputs + [ctx.executable.protoc], + outputs=[out], + command=command, + mnemonic="ProtoCompile", + use_default_shell_env=True, + ) return struct( proto=struct( -- cgit v1.2.3 From ae638966e544f5108d566e48cb8edd67d5953f13 Mon Sep 17 00:00:00 2001 From: Fahrzin Hemmati Date: Thu, 15 Mar 2018 00:37:31 -0700 Subject: Track outputs correctly --- protobuf.bzl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/protobuf.bzl b/protobuf.bzl index 5c4a24d7..ac3e862e 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -93,8 +93,6 @@ def _proto_gen_impl(ctx): for src in srcs: args = [] - src_name = src.basename[:-len(".proto")] - outs = [out for out in ctx.outputs.outs if src_name in out.basename] in_gen_dir = src.root.path == gen_dir.rstrip('/') if in_gen_dir: @@ -103,11 +101,16 @@ def _proto_gen_impl(ctx): path = f.replace('-I', '') import_flags_real.append('-I$(realpath -s %s)' % path) + outs = [] + use_grpc_plugin = (ctx.attr.plugin_language == "grpc") if ctx.attr.gen_cc: args += ["--cpp_out=$(realpath %s)" % gen_dir] + outs.extend(_CcOuts([src.basename], use_grpc_plugin=use_grpc_plugin)) if ctx.attr.gen_py: args += ["--python_out=$(realpath %s)" % gen_dir] + outs.extend(_PyOuts([src.basename], use_grpc_plugin=use_grpc_plugin)) + outs = [ctx.actions.declare_file(out, sibling=src) for out in outs] inputs = [src] + deps if ctx.executable.plugin: plugin = ctx.executable.plugin -- cgit v1.2.3 From d1403e58d82ff3a1024316ec2067fb98126e0659 Mon Sep 17 00:00:00 2001 From: Fahrzin Hemmati Date: Fri, 16 Mar 2018 13:23:34 -0700 Subject: Only use realpath when in gendir, and use the original ctx.action if not in gendir --- protobuf.bzl | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/protobuf.bzl b/protobuf.bzl index ac3e862e..2b9d65fb 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -102,12 +102,13 @@ def _proto_gen_impl(ctx): import_flags_real.append('-I$(realpath -s %s)' % path) outs = [] - use_grpc_plugin = (ctx.attr.plugin_language == "grpc") + use_grpc_plugin = (ctx.attr.plugin_language == "grpc" and ctx.attr.plugin) + path_tpl = "$(realpath %s)" if in_gen_dir else "%s" if ctx.attr.gen_cc: - args += ["--cpp_out=$(realpath %s)" % gen_dir] + args += [("--cpp_out=" + path_tpl) % gen_dir] outs.extend(_CcOuts([src.basename], use_grpc_plugin=use_grpc_plugin)) if ctx.attr.gen_py: - args += ["--python_out=$(realpath %s)" % gen_dir] + args += [("--python_out=" + path_tpl) % gen_dir] outs.extend(_PyOuts([src.basename], use_grpc_plugin=use_grpc_plugin)) outs = [ctx.actions.declare_file(out, sibling=src) for out in outs] @@ -120,19 +121,25 @@ def _proto_gen_impl(ctx): if not lang: fail("cannot infer the target language of plugin", "plugin_language") - if in_gen_dir: - outdir = "." - else: - outdir = gen_dir + outdir = "." if in_gen_dir else gen_dir if ctx.attr.plugin_options: outdir = ",".join(ctx.attr.plugin_options) + ":" + outdir - args += ["--plugin=protoc-gen-%s=$(realpath %s)" % (lang, plugin.path)] + args += [("--plugin=protoc-gen-%s=" + path_tpl) % (lang, plugin.path)] args += ["--%s_out=%s" % (lang, outdir)] inputs += [plugin] - for out in outs: - if in_gen_dir: + if not in_gen_dir: + ctx.action( + inputs=inputs, + outputs=outs, + arguments=args + import_flags + [src.path], + executable=ctx.executable.protoc, + mnemonic="ProtoCompile", + use_default_shell_env=True, + ) + else: + for out in outs: orig_command = " ".join( ["$(realpath %s)" % ctx.executable.protoc.path] + args + import_flags_real + ["-I.", src.basename]) @@ -143,16 +150,13 @@ def _proto_gen_impl(ctx): "cd -", "mv %s/%s %s" % (gen_dir, out.basename, out.path) ]) - else: - command = " ".join( - [ctx.executable.protoc.path] + args + import_flags + [src.path]) - ctx.action( - inputs=inputs + [ctx.executable.protoc], - outputs=[out], - command=command, - mnemonic="ProtoCompile", - use_default_shell_env=True, - ) + ctx.action( + inputs=inputs + [ctx.executable.protoc], + outputs=[out], + command=command, + mnemonic="ProtoCompile", + use_default_shell_env=True, + ) return struct( proto=struct( -- cgit v1.2.3 From 0d68b29ac49e48519afbaeaaa14c82a9e086b691 Mon Sep 17 00:00:00 2001 From: Fahrzin Hemmati Date: Mon, 26 Mar 2018 19:08:26 -0700 Subject: Add unittest build rules --- BUILD | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/BUILD b/BUILD index d6fb85d8..a85b1bf9 100644 --- a/BUILD +++ b/BUILD @@ -913,3 +913,30 @@ objc_library( non_arc_srcs = OBJC_SRCS, visibility = ["//visibility:public"], ) + +################################################################################ +# Test generated proto support +################################################################################ + +genrule( + name = "generated_protos", + srcs = ["src/google/protobuf/unittest_import.proto"], + outs = ["unittest_gen.proto"], + cmd = "cat $(SRCS) | sed 's|google/|src/google/|' > $(OUTS)" +) + +proto_library( + name = "generated_protos_proto", + srcs = ["unittest_gen.proto"], +) + + +py_proto_library( + name = "generated_protos_py", + srcs = [ + "unittest_gen.proto", + "src/google/protobuf/unittest_import_public.proto", + ], + default_runtime = "", + protoc = ":protoc", +) -- cgit v1.2.3 From 2cfa7612b2e8226c66977b7f91b5b65d874276bb Mon Sep 17 00:00:00 2001 From: Fahrzin Hemmati Date: Mon, 26 Mar 2018 19:09:08 -0700 Subject: Fix moving generated files onto themselves --- protobuf.bzl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/protobuf.bzl b/protobuf.bzl index 2b9d65fb..511df772 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -72,7 +72,7 @@ def _proto_gen_impl(ctx): deps = [] deps += ctx.files.srcs source_dir = _SourceDir(ctx) - gen_dir = _GenDir(ctx) + gen_dir = _GenDir(ctx).rstrip('/') if source_dir: import_flags = ["-I" + source_dir, "-I" + gen_dir] else: @@ -94,7 +94,7 @@ def _proto_gen_impl(ctx): for src in srcs: args = [] - in_gen_dir = src.root.path == gen_dir.rstrip('/') + in_gen_dir = src.root.path == gen_dir if in_gen_dir: import_flags_real = [] for f in depset(import_flags): @@ -148,8 +148,10 @@ def _proto_gen_impl(ctx): "cd %s" % src.dirname, "${CMD}", "cd -", - "mv %s/%s %s" % (gen_dir, out.basename, out.path) ]) + generated_out = '/'.join([gen_dir, out.basename]) + if generated_out != out.path: + command += ";mv %s %s" % (generated_out, out.path) ctx.action( inputs=inputs + [ctx.executable.protoc], outputs=[out], -- cgit v1.2.3 From 55962db9b78d0751e861e43660bde1f82eb911e7 Mon Sep 17 00:00:00 2001 From: Fahrzin Hemmati Date: Mon, 7 May 2018 17:36:34 -0700 Subject: Fix generated_protos_proto deps --- BUILD | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/BUILD b/BUILD index a85b1bf9..94ff203b 100644 --- a/BUILD +++ b/BUILD @@ -927,10 +927,12 @@ genrule( proto_library( name = "generated_protos_proto", - srcs = ["unittest_gen.proto"], + srcs = [ + "unittest_gen.proto", + "src/google/protobuf/unittest_import_public.proto", + ], ) - py_proto_library( name = "generated_protos_py", srcs = [ -- cgit v1.2.3