Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bazel rules_go: linking go binary against a static c++ library (.a file) produced by another target in the workspace

I'm using the confluent-kafka-go library inside my go binary, and this library needs to be linked against librdkafka. Other targets in my project use librdkakfa, so I have produced the static librdkafka.a and librdkafka++.a using rules_foreign_cc's cmake_external rule:

   //this is my "third_party/kafka/BUILD" file:

load("@rules_foreign_cc//tools/build_defs:cmake.bzl", "cmake_external")

    cmake_external(
        name = "librdkafka",
        cache_entries = {
            "RDKAFKA_BUILD_STATIC": "ON",
            "WITH_ZSTD": "OFF",
            "WITH_SSL": "OFF",
            "WITH_SASL": "OFF",
            "ENABLE_LZ4_EXT": "OFF",
            "WITH_LIBDL": "OFF",
        },
        lib_source = "@kafka//:all",
        static_libraries = [
            "librdkafka++.a",
            "librdkafka.a",
        ],
        visibility = ["//visibility:public"],
    ) 

Which produces the librdkafka libraries and headers just fine:

 $ bazel build //third_party/kafka:librdkafka 
INFO: Analysed target //third_party/kafka:librdkafka (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //third_party/kafka:librdkafka up-to-date:
  bazel-genfiles/third_party/kafka/librdkafka/include
  bazel-genfiles/third_party/kafka/librdkafka/lib/librdkafka++.a
  bazel-genfiles/third_party/kafka/librdkafka/lib/librdkafka.a
  bazel-genfiles/third_party/kafka/copy_librdkafka/librdkafka
  bazel-genfiles/third_party/kafka/librdkafka/logs/CMake_script.sh
  bazel-genfiles/third_party/kafka/librdkafka/logs/CMake.log
  bazel-genfiles/third_party/kafka/librdkafka/logs/wrapper_script.sh
INFO: Elapsed time: 0.187s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action

Now I'm trying to link against librdkafka.a (I don't need the librdkafka++.a file, only the c version) inside my go_binary rule:

    go_binary(
        name = "foo",
        srcs = ["foo.go"],
        cdeps = [
            "//third_party/kafka:librdkafka",
        ],
        cgo = "True",
        static = "on",
        visibility = ["//visibility:public"],
        deps = [
            ":ox_go_proto",
  "@com_github_confluentinc_confluent_kafka_go//kafka:go_default_library",
  "@com_github_golang_protobuf//proto:go_default_library",
        ],
    )

but I get hundreds of undefined rdkafka references when I run bazel build //foo:foo (my go binary)

unction _cgo_52d112b951a8_Cfunc_rd_kafka_AdminOptions_destroy: error: undefined reference to 'rd_kafka_AdminOptions_destroy'
bazel-out/k8-fastbuild/bin/external/com_github_confluentinc_confluent_kafka_go/kafka/_objs/go_default_library%linux_amd64%cgo_c_lib/adminapi.cgo2.pic.o:adminapi.cgo2.c:function _cgo_52d112b951a8_Cfunc_rd_kafka_AlterConfigs: error: undefined reference to 'rd_kafka_AlterConfigs'
bazel-out/k8-fastbuild/bin/external/com_github_confluentinc_confluent_kafka_go/kafka/_objs/go_default_library%linux_amd64%cgo_c_lib/adminapi.cgo2.pic.o:adminapi.cgo2.c:function _cgo_52d112b951a8_Cfunc_rd_kafka_AlterConfigs_result_resources: error: undefined reference to 'rd_kafka_AlterConfigs_result_resources'
bazel-out/k8-fastbuild/bin/external/com_github_confluentinc_confluent_kafka_go/kafka/_objs/go_default_library%linux_amd64%cgo_c_lib/adminapi.cgo2.pic.o:adminapi.cgo2.c:function _cgo_52d112b951a8_Cfunc_rd_kafka_ConfigEntry_is_read_only: error: undefined reference to 'rd_kafka_ConfigEntry_is_read_only'
bazel-out/k8-fastbuild/bin/external/com_github_confluentinc_confluent_kafka_go/kafka/_objs/go_default_library%linux_amd64%cgo_c_lib/adminapi.cgo2.pic.o:adminapi.cgo2.c:function _cgo_52d112b951a8_Cfunc_rd_kafka_ConfigEntry_is_sensitive: error: undefined reference to 'rd_kafka_ConfigEntry_is_sensitive'
bazel-out/k8-fastbuild/bin/external/com_github_confluentinc_confluent_kafka_go/kafka/_objs/go_default_library%linux_amd64%cgo_c_lib/adminapi.cgo2.pic.o:adminapi.cgo2.c:function _cgo_52d112b951a8_Cfunc_rd_kafka_ConfigEntry_is_synonym: error: undefined reference to 'rd_kafka_ConfigEntry_is_synonym'
bazel-out/k8-
like image 474
mancini0 Avatar asked Apr 16 '19 00:04

mancini0


1 Answers

You’re close, but you need to add the cdeps to the confluent Kafka go library, and not to your go_binary target. I’m assuming you are using Gazelle and rules_go to pull in external deps. You need to use go_repository “patch” attribute to pass a patch file to sort that out in the generated BUILD file.

I’ve documented this in a recent blog post:

https://rotemtam.com/2020/10/30/bazel-building-cgo-bindings/

like image 150
Rotem Tamir Avatar answered Jan 04 '23 14:01

Rotem Tamir