tl;dr How do you write a Bazel rule to symlink to an existing target? I'd like the invocation to look like:
# this should create a symbolic link from my-binary to some/deep/dir/elsewhere/my-binary:my-binary.
symlink(
name = "my-binary",
src = "//some/deep/dir/elsewhere/my-binary:my-binary",
)
I've read previous related discussions [1] and the Skylark examples [2] but am currently unable to come up with a solution.
Background:
I'm converting a large project from using the standard Go tools ("go build"/"go install) to Bazel. The project has several tens of binaries scattered across its directory hierarchy and I'd like to symlink or copy these to a single "bin" directory in "bazel-bin". Collecting everything in a single directory greatly simplifies further tooling (e.g. it's easy to add to my $PATH) and emulates the "go install" step that other tooling relies on.
My current best effort is:
def _impl(ctx):
output = ctx.outputs.out
# go_binary rules have two outputs, a library and the binary itself.
# The following is a horrible hack to find the binary, not the library.
for f in ctx.attr.src.files:
if not f.path.endswith(".a"):
input = f
break
ctx.action(
inputs=[input],
outputs=[output],
mnemonic="Symlink",
command="mkdir -p %s && ln -s %s %s" % (output.dirname, input.path, output.path))
symlink = rule(
implementation=_impl,
attrs={
# We can't use single_file=True because we'd like to use this with
# go_binary targets.
"src": attr.label(mandatory=True, allow_files=True),
},
outputs={
"out": "%{name}",
},
)
This fails in a number of ways:
1) I get a warning about the name of the rule conflicting with the name of the output:
WARNING: BUILD.bazel:3:1: target 'my-binary' is both a rule and a file; please choose another name for the rule.
I'd really like to avoid stuttering the name of the symlink (i.e. I'd like the output file to be the name of the rule). Is there any way to do this and avoid the warning?
2) The generated symlink is relative and does not point to the actual location of the src:
$ ls -l bazel-bin/bin
lrwxr-xr-x 1 user wheel 71 Jan 4 14:23 my-binary -> bazel-out/local-fastbuild/bin/some/deep/dir/elsewhere/my-binary:my-binary
Bazel warns about this:
ERROR: BUILD.bazel:3:1: output 'bin/my-binary' is a dangling symbolic link.
How can I get either a correct relative path or an absolute path to the actual file on disk? None of the File properties [3] seem relevant, and the correct relative path will depend on where I invoke the symlink() rule.
3) Is there any nice way to work around the double outputs (binary and library) of rules_go's go_binary?
Thanks very much for any help!
Tom
[1] https://groups.google.com/forum/#!topic/bazel-discuss/EoiHjBPLNuY
[2] https://bazel.build/versions/master/docs/skylark/cookbook.html
[3] https://bazel.build/versions/master/docs/skylark/lib/File.html
--
This message is for the attention of the intended recipient(s) only. It may
contain confidential, proprietary and/or legally privileged information.
Use, disclosure and/or retransmission of information contained in this
email may be prohibited. If you are not an intended recipient, you are
kindly asked to notify the sender immediately (by reply e-mail) and to
permanently delete this message. Thank you.
--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/96dca868-3db2-4e5a-8cdd-bf02dea176dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/96dca868-3db2-4e5a-8cdd-bf02dea176dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/96dca868-3db2-4e5a-8cdd-bf02dea176dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/96dca868-3db2-4e5a-8cdd-bf02dea176dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
This message is for the attention of the intended recipient(s) only. It may contain confidential, proprietary and/or legally privileged information. Use, disclosure and/or retransmission of information contained in this email may be prohibited. If you are not an intended recipient, you are kindly asked to notify the sender immediately (by reply e-mail) and to permanently delete this message. Thank you.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/96dca868-3db2-4e5a-8cdd-bf02dea176dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
This message is for the attention of the intended recipient(s) only. It may contain confidential, proprietary and/or legally privileged information. Use, disclosure and/or retransmission of information contained in this email may be prohibited. If you are not an intended recipient, you are kindly asked to notify the sender immediately (by reply e-mail) and to permanently delete this message. Thank you.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/96dca868-3db2-4e5a-8cdd-bf02dea176dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
This message is for the attention of the intended recipient(s) only. It may contain confidential, proprietary and/or legally privileged information. Use, disclosure and/or retransmission of information contained in this email may be prohibited. If you are not an intended recipient, you are kindly asked to notify the sender immediately (by reply e-mail) and to permanently delete this message. Thank you.
This message is for the attention of the intended recipient(s) only. It may contain confidential, proprietary and/or legally privileged information. Use, disclosure and/or retransmission of information contained in this email may be prohibited. If you are not an intended recipient, you are kindly asked to notify the sender immediately (by reply e-mail) and to permanently delete this message. Thank you.