bazel-contrib / bazel-contrib/rules_foreign_cc
how to use output binary file in genrule?
- Dominant language
- Starlark
- Stars
- 737
- Forks
- 270
- PR merge metrics
- No merged PRs in 30d
Description
I use cmake macro to build protobuf successfully, and get a binary file: protoc
```
# file: protobuf/BUILD/bazel
cmake(
name = "protobuf_impl",
build_args = [
"--parallel=`nproc`",
],
cache_entries = {
"protobuf_WITH_ZLIB": "OFF",
"protobuf_BUILD_TESTS": "OFF",
"protobuf_BUILD_SHARED_LIBS": "ON",
},
working_directory = "cmake",
lib_source = ":srcs",
out_include_dir = "include",
out_lib_dir = "lib64",
out_shared_libs = ["libprotobuf.so"],
out_binaries = ["protoc"]
)
```

I try to use protoc in genrule as following but failed, how to get the correct path of protoc?
```
# file: myprotobuf.bzl
def cpp_proto(name, src):
native.genrule(
name = "%s-gen" % name,
outs = ["%s.pb.cc" % name, "%s.pb.h" % name],
cmd = "$(locations @protobuf//:protobuf_impl)\\bin\\protoc --cpp_out=$(GENDIR) $<", # failed
srcs = [src],
tools = ["@protobuf//:protobuf_impl"],
)
native.cc_library(
name = name,
srcs = ["%s.pb.cc" % name],
hdrs = ["%s.pb.h" % name],
)
```
Contributor guide
Research direction
Start with protobuf/BUILD/bazel and myprotobuf.bzl, then inspect how the cmake rule exposes the out_binaries entry and how native.genrule expands locations for tools. Verify the generated protoc path and invoke it from the genrule; done means the .pb.cc and .pb.h outputs are generated successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100