Commit 800f8d66 authored by Fahrzin Hemmati's avatar Fahrzin Hemmati Committed by Fahrzin Hemmati

Handle srcs in generated files by cd'ing in and out

parent 25625b95
...@@ -82,13 +82,31 @@ def _proto_gen_impl(ctx): ...@@ -82,13 +82,31 @@ def _proto_gen_impl(ctx):
import_flags += dep.proto.import_flags import_flags += dep.proto.import_flags
deps += dep.proto.deps deps += dep.proto.deps
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 = [] 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: if ctx.attr.gen_cc:
args += ["--cpp_out=" + gen_dir] args += ["--cpp_out=$(realpath %s)" % gen_dir]
if ctx.attr.gen_py: if ctx.attr.gen_py:
args += ["--python_out=" + gen_dir] args += ["--python_out=$(realpath %s)" % gen_dir]
inputs = srcs + deps inputs = [src] + deps
if ctx.executable.plugin: if ctx.executable.plugin:
plugin = ctx.executable.plugin plugin = ctx.executable.plugin
lang = ctx.attr.plugin_language lang = ctx.attr.plugin_language
...@@ -97,19 +115,35 @@ def _proto_gen_impl(ctx): ...@@ -97,19 +115,35 @@ def _proto_gen_impl(ctx):
if not lang: if not lang:
fail("cannot infer the target language of plugin", "plugin_language") fail("cannot infer the target language of plugin", "plugin_language")
if in_gen_dir:
outdir = "."
else:
outdir = gen_dir outdir = gen_dir
if ctx.attr.plugin_options: if ctx.attr.plugin_options:
outdir = ",".join(ctx.attr.plugin_options) + ":" + outdir outdir = ",".join(ctx.attr.plugin_options) + ":" + outdir
args += ["--plugin=protoc-gen-%s=%s" % (lang, plugin.path)] args += ["--plugin=protoc-gen-%s=$(realpath %s)" % (lang, plugin.path)]
args += ["--%s_out=%s" % (lang, outdir)] args += ["--%s_out=%s" % (lang, outdir)]
inputs += [plugin] inputs += [plugin]
if args: 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( ctx.action(
inputs=inputs, inputs=inputs + [ctx.executable.protoc],
outputs=ctx.outputs.outs, outputs=[out],
arguments=args + import_flags + [s.path for s in srcs], command=command,
executable=ctx.executable.protoc,
mnemonic="ProtoCompile", mnemonic="ProtoCompile",
use_default_shell_env=True, use_default_shell_env=True,
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment