This project has a bunch of proto files. Their CMakeLists.txt file runs protoc to create the pb.* srcs, and then it creates an umbrella header file called "msgs.hh" which simply has an include line for every proto header that was generated.
I'm trying to generate that header similarly in Bazel and for some reason I'm struggling.
I could simply hard-code all the files, but that wouldn't be as robust, and there are hundreds of proto files in this particular project, so I'd rather it be automated.
I'm trying to do essentially this in my BUILD file:
------------------
proto_library(
name = "my_proto",
srcs = ["my.proto"],
)
cc_proto_library(
name = "my_cc_proto",
deps = [":my_proto"],
)
genrule(
name = "msgs_hh",
srcs = [":my_cc_proto"],
outs = ["msgs.hh"],
cmd = """
echo '// Automatically generated' >> $@
for src in "$(SRCS)"
do
if [[ $${src} == *".h" ]]; then
echo '#include <ignition/msgs/'"$$(basename $$src)"'>' >> $@
fi
done
""",
visibility = ["//visibility:private"],
)
--------------------
Is it not correct so use ":my_cc_proto" as srcs? If not, then is there a better way I can achieve what I'm trying to do?
Any help appreciated!
Thanks
Kevin
| To add a bit more context, when I try to build the msgs_hh target, it doesn't appear to be compiling the protos. Also, I added print statements in the cmd, and $(SRCS) is an empty list. |
--
You received this message because you are subscribed to a topic in the Google Groups "bazel-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bazel-discuss/lWpZKTE4i1s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/8b270132-b273-4181-8801-8b42fa270fc4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
| I should also add that I'm using the latest version of bazel from HEAD. |
To unsubscribe from this group and all its topics, 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/8b270132-b273-4181-8801-8b42fa270fc4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
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-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/a58ab959-7952-0b57-f699-3caeafede57a%40mixmax.com.
| Thank you, thank you! That was immensely helpful. I wrote up a little bazel skylark rule based on your feedback. Here is what I came up with: def _glob_cc_proto_hdr_impl(ctx): output_hdr = ctx.outputs.out output_content = "\n".join(ctx.attr.hdr_lines) + "\n" for cc_proto in ctx.attr.cc_protos: path_prefix = ctx.genfiles_dir.path + "/" + cc_proto.label.workspace_root + "/" src_hdrs = sorted([hdr.path.replace(path_prefix, "") for hdr in cc_proto.cc.transitive_hdrs if hdr.path.startswith(path_prefix)]) output_content += "\n".join(["#include <" + hdr + ">" for hdr in src_hdrs]) + "\n" ctx.file_action(output=output_hdr, content=output_content) glob_cc_proto_hdr = rule( implementation=_glob_cc_proto_hdr_impl, attrs={ "hdr_lines": attr.string_list(default=[]), "cc_protos": attr.label_list(mandatory=True), "out": attr.output(mandatory=True), }, executable=False, ) I have 2 remaining questions:
Thanks again for your help! Kevin |
| Correction: It creates the generated header in the bazel-bin directory (not bazel-out). |
| Ah blurgh, and I just realized you can't use a *.bzl file from a BUILD file if that BUILD file is being used for an external repo. In my case, my build file is being passed to a "new_git_respository" rule. And when I try to build, it gives me this error: ERROR: /home/kevin/Projects/first_commit/WORKSPACE:55:1: error loading package '@ign_msgs_archive//': Extension file not found. Unable to load package for '@ign_msgs_archive//bzl:glob_cc_proto_hdr.bzl': BUILD file not found on package path and referenced by '//external:ign_msgs_factory'. It's searching for glob_cc_proto_hdr.bzl under the @ign_msgs_archive// workspace, which obviously fails. |
| It works! Thank you again so much for the help. I was really bashing my head against the wall. I used attr.output and it worked fine. I just needed to add output_to_genfiles=True to my rule. |