@@ -24,6 +24,26 @@ def _emit_params_file_action(ctx, path, mnemonic, cmds):
2424 return f
2525
2626
27+ def _get_relative_dirname (base , file ):
28+ """Return a dirname in the form of path segments relative to base.
29+ If the file.short_path is not within base, return empty list.
30+ Example: if base="foo/bar/baz.txt"
31+ and file.short_path="bar/baz.txt",
32+ return ["bar"].
33+ Args:
34+ base (string): the base dirname (ctx.label.package)
35+ file (File): the file to calculate relative dirname.
36+ Returns:
37+ (list<string>): path
38+ """
39+ path = file .short_path
40+ if not path .startswith (base ):
41+ return []
42+ path = path [len (base )+ 1 :] # remove trailing slash
43+ parts = path .split ("/" )
44+ return parts [:- 1 ]
45+
46+
2747def _get_offset_path (root , path ):
2848 """Adjust path relative to offset"""
2949
@@ -144,7 +164,6 @@ def _build_output_srcjar(run, builder):
144164 if run .data .verbose > 2 :
145165 print ("Copied jar %s srcjar to %s" % (protojar .path , srcjar .path ))
146166
147-
148167def _build_output_files (run , builder ):
149168 """Build a list of files we expect to be generated."""
150169
@@ -162,7 +181,9 @@ def _build_output_files(run, builder):
162181 if run .lang .output_file_style == 'capitalize' :
163182 base = _capitalize (base )
164183 for ext in exts :
165- pbfile = ctx .new_file (base + ext )
184+ path = _get_relative_dirname (ctx .label .package , file )
185+ path .append (base + ext )
186+ pbfile = ctx .new_file ("/" .join (path ))
166187 builder ["outputs" ] += [pbfile ]
167188
168189
0 commit comments