Resolving full target name in custom build rule

766 views
Skip to first unread message

ker...@google.com

unread,
Jul 8, 2015, 10:02:40 PM7/8/15
to blaze...@google.com, bazel-...@googlegroups.com
Within a custom build rule, how can you resolve a relative target name such as :foo to a full target name such as //bar/baz:foo?

For example, if //bar/baz/BUILD contains

py_library(name='foo')
my_custom_rule(arg=':foo')

how can my_custom_rule convert ':foo' into '//bar/baz:foo'?

- Kerrick

Nathan Harmata

unread,
Jul 8, 2015, 10:14:16 PM7/8/15
to ker...@google.com, blaze-devel, bazel-...@googlegroups.com

Kerrick Staley

unread,
Jul 8, 2015, 10:29:59 PM7/8/15
to Nathan Harmata, blaze-devel, bazel-...@googlegroups.com
So some code like the following?

def ResolveLabel(label):
  package = label.split(':')[0]
  if not package.startswith('//'):
    package = PACKAGE_NAME + '/' + package
  if ':' in label:
    target = label.split(':')[-1]
  else:
    target = label.split('/')[-1]
  return package + ':' + target

- Kerrick

Nathan Harmata

unread,
Jul 8, 2015, 10:34:09 PM7/8/15
to Kerrick Staley, blaze-devel, bazel-...@googlegroups.com
Sure, some string manipulation like that is the right approach.

Though may I ask what you are trying to accomplish? I can't think of a good reason off the top of my head why you would need to do something like this.

Laurent Le Brun

unread,
Jul 9, 2015, 4:35:07 AM7/9/15
to Nathan Harmata, Kerrick Staley, blaze-devel, bazel-...@googlegroups.com
If you are using a Skylark macro, see absolute_label.bzl:

load("/tools/build_defs/absolute_label", "absolute_label")

If you have a build extension, see
devtools/buildifier/build_defs/label_with_target
but please note that go/deprecate-preprocessing
> --
>

Laurent Le Brun

unread,
Jul 9, 2015, 4:57:58 AM7/9/15
to Nathan Harmata, Kerrick Staley, bazel-...@googlegroups.com
Sorry, the file isn't shipped yet with Bazel:

def absolute_label(label):
"""Expands a label to be of the full form //package:foo.

absolute_label("//pkg:foo") = "//pkg:foo"
absolute_label("//pkg:gws") = "//pkg:gws"
absolute_label("//pkg") = "//pkg:gws"
absolute_label(":foo") = "//current_package:foo"
"""
if label.startswith("//") and not (":" in label):
label += ":" + label[label.rfind("/")+1:]
elif label.startswith(":"):
label = "//" + PACKAGE_NAME + label
return label
--
Laurent

Kerrick Staley

unread,
Jul 10, 2015, 2:20:25 PM7/10/15
to Laurent Le Brun, Nathan Harmata, bazel-...@googlegroups.com
I ended up with this implementation:
def _AbsoluteLabel(label):
  if ':' in label:
    package, target = label.split(':')
  elif '/' in label:
    package = label
    target = label.split('/')[-1]
  else:
    package = ''
    target = label
  if not package.startswith('//'):
    package = '//' + PACKAGE_NAME + ('/' + package if package else '')
  return package + ':' + target

The absolute_label function in that file doesn't seem to handle labels without slashes or a colon, e.g. "foo.py" (and looking at its tests, this seems intentional). I think these should resolve to e.g. //PACKAGE_NAME:foo.py.

- Kerrick
Reply all
Reply to author
Forward
0 new messages