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

2

7

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-
Congius answered 16/4, 2019 at 0:34 Comment(1)
Have a look here github.com/prysmaticlabs/prysm/pull/3840/filesWanyen
L
2

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/

Louisalouisburg answered 31/10, 2020 at 17:59 Comment(0)
R
0

I followed @Rotem Tamir's answer. I had to make a few changes to make it work for the latest version. I had to change the patch file as follows and put it in a specific directory: external/com_github_confluentinc_confluent_kafka_go_v2.patch.

--- kafka/BUILD.bazel
+++ kafka/BUILD.bazel
@@ -32,75 +32,76 @@
         "select_rdkafka.h",
         "time.go",
     ],
+    cdeps = ["@librdkafka//:librdkafka", "@zlib//:zlib"],
     cgo = True,
     clinkopts = select({
         "@io_bazel_rules_go//go/platform:windows": [
-            "kafka/librdkafka_vendor/librdkafka_windows.a -lws2_32 -lsecur32 -lcrypt32",
+            "-lws2_32 -lsecur32 -lcrypt32",
         ],
         "//conditions:default": [],
     }) + select({
         "@io_bazel_rules_go//go/platform:android_amd64": [
-            "kafka/librdkafka_vendor/librdkafka_glibc_linux_amd64.a -lm -ldl -lpthread -lrt",
+            "-lm -ldl -lpthread -lrt",
         ],
         "@io_bazel_rules_go//go/platform:android_arm64": [
-            "kafka/librdkafka_vendor/librdkafka_glibc_linux_arm64.a -lm -ldl -lpthread -lrt",
+            "-lm -ldl -lpthread -lrt",
         ],
         "@io_bazel_rules_go//go/platform:darwin_amd64": [
-            "kafka/librdkafka_vendor/librdkafka_darwin_amd64.a -lm -lsasl2 -ldl -lpthread -framework CoreFoundation -framework SystemConfiguration",
+            "-lm -lsasl2 -ldl -lpthread -framework CoreFoundation -framework SystemConfiguration",
         ],
         "@io_bazel_rules_go//go/platform:darwin_arm64": [
-            "kafka/librdkafka_vendor/librdkafka_darwin_arm64.a -lm -lsasl2 -ldl -lpthread -framework CoreFoundation -framework SystemConfiguration",
+            "-lm -lsasl2 -ldl -lpthread -framework CoreFoundation -framework SystemConfiguration",
         ],
         "@io_bazel_rules_go//go/platform:ios_amd64": [
-            "kafka/librdkafka_vendor/librdkafka_darwin_amd64.a -lm -lsasl2 -ldl -lpthread -framework CoreFoundation -framework SystemConfiguration",
+            "-lm -lsasl2 -ldl -lpthread -framework CoreFoundation -framework SystemConfiguration",
         ],
         "@io_bazel_rules_go//go/platform:ios_arm64": [
-            "kafka/librdkafka_vendor/librdkafka_darwin_arm64.a -lm -lsasl2 -ldl -lpthread -framework CoreFoundation -framework SystemConfiguration",
+            "-lm -lsasl2 -ldl -lpthread -framework CoreFoundation -framework SystemConfiguration",
         ],
         "@io_bazel_rules_go//go/platform:linux_amd64": [
-            "kafka/librdkafka_vendor/librdkafka_glibc_linux_amd64.a -lm -ldl -lpthread -lrt",
+            "-lm -ldl -lpthread -lrt",
         ],
         "@io_bazel_rules_go//go/platform:linux_arm64": [
-            "kafka/librdkafka_vendor/librdkafka_glibc_linux_arm64.a -lm -ldl -lpthread -lrt",
+            "-lm -ldl -lpthread -lrt",
         ],
         "//conditions:default": [],
     }),
     copts = select({
         "@io_bazel_rules_go//go/platform:android_amd64": [
-            "-DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB",
+            "-DLIBRDKAFKA_STATICLIB",
         ],
         "@io_bazel_rules_go//go/platform:android_arm64": [
-            "-DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB",
+            "-DLIBRDKAFKA_STATICLIB",
         ],
         "@io_bazel_rules_go//go/platform:darwin_amd64": [
-            "-DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB",
+            "-DLIBRDKAFKA_STATICLIB",
         ],
         "@io_bazel_rules_go//go/platform:darwin_arm64": [
-            "-DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB",
+            "-DLIBRDKAFKA_STATICLIB",
         ],
         "@io_bazel_rules_go//go/platform:ios_amd64": [
-            "-DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB",
+            "-DLIBRDKAFKA_STATICLIB",
         ],
         "@io_bazel_rules_go//go/platform:ios_arm64": [
-            "-DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB",
+            "-DLIBRDKAFKA_STATICLIB",
         ],
         "@io_bazel_rules_go//go/platform:linux_amd64": [
-            "-DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB",
+            "-DLIBRDKAFKA_STATICLIB",
         ],
         "@io_bazel_rules_go//go/platform:linux_arm64": [
-            "-DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB",
+            "-DLIBRDKAFKA_STATICLIB",
         ],
         "@io_bazel_rules_go//go/platform:windows_386": [
-            "-DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB",
+            "-DLIBRDKAFKA_STATICLIB",
         ],
         "@io_bazel_rules_go//go/platform:windows_amd64": [
-            "-DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB",
+            "-DLIBRDKAFKA_STATICLIB",
         ],
         "@io_bazel_rules_go//go/platform:windows_arm": [
-            "-DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB",
+            "-DLIBRDKAFKA_STATICLIB",
         ],
         "@io_bazel_rules_go//go/platform:windows_arm64": [
-            "-DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB",
+            "-DLIBRDKAFKA_STATICLIB",
         ],
         "//conditions:default": [],
     }),

Rattling answered 18/11, 2023 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.