rules_foreign_cc icon indicating copy to clipboard operation
rules_foreign_cc copied to clipboard

how to use output binary file in genrule?

Open MaleicAcid opened this issue 1 year ago • 1 comments

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"]
)

image

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],
  )

MaleicAcid avatar Aug 24 '22 10:08 MaleicAcid

See the example of building gn

https://github.com/bazelbuild/rules_foreign_cc/blob/b136e6c52da63da300b0f588c8a214d97b0d15cd/examples/third_party/gn/BUILD.gn.bazel#L45

rules_foreign_cc rules (eg cmake()) create an output group for each binary. A binary can be referenced individually by extracting the binary from the cmake() target using a filegroup

jheaff1 avatar Aug 25 '22 17:08 jheaff1