abseil / abseil-cpp

Abseil Common Libraries (C++)
https://abseil.io
Apache License 2.0
14.73k stars 2.58k forks source link

Have Bazel rules for creating shared libraries #1746

Open lano1106 opened 3 weeks ago

lano1106 commented 3 weeks ago

Describe the issue

it is possible to build shared libraries with cmake with BUILD_SHARED_LIBS=ON

AFAIK, it is not possible to create them with Bazel. According to https://ltekieli.com/linux-shared-libraries-with-cmake-and-bazel/ it requires additional cc_shared_library rules to setup up the needed dynamic deps.

The reason why it is important to have this feature setup, it is to allow to build other projects linking with abseil that are exclusively using bazel.

For example, https://github.com/google/tcmalloc is such project.

It currently statically links with abseil. This makes it impossible to build tcmalloc as a shared library and use the resulting so lib file in a project also linking with cmake generated Abseil libraries without breaking the ODR rule.

Being able to build Abseil shared libraries within Bazel is a prerequisite to be able to build tcmalloc shared libraries that can be used with projects already dynamically linking with Abseil.

Steps to reproduce the problem

there is no cc_shared_library rules in abseil BUILD files or if it is possible to create shared libraries, it is not well documented.

I could not find anything on the topic.

What version of Abseil are you using?

20240722 LTS

What operating system and version are you using?

ArchLinux kernel 6.10.4

What compiler and version are you using?

gcc version 14.2.1 20240805 (GCC)

What build system are you using?

bazel 7.2.1

Additional context

No response

lano1106 commented 3 weeks ago

I have tried this:

cc_shared_library(
    name = "tcmalloc_shared",
    shared_lib_name = "libtcmalloc.so",
    user_link_flags = ["-Wl,-O1,--sort-common,--as-needed", "-Wl,-soname,libtcmalloc.so"],
    dynamic_deps = [
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/debugging:leak_check",
        "@com_google_absl//absl/debugging:stacktrace",
        "@com_google_absl//absl/debugging:symbolize",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
    ],
    deps = [":tcmalloc"],
)

this does not work at all. I receive a bunch of cryptic errors with bazel build --copt "-march=sapphirerapids" --copt "-O3" --copt "-flto" --linkopt "-O3" --linkopt "-flto=auto" //tcmalloc:tcmalloc_shared :

ERROR: /nas/cloud/dev/tcmalloc/tcmalloc/BUILD:1498:18: in dynamic_deps attribute of cc_shared_library rule //tcmalloc:tcmalloc_shared: '@@abseil-cpp~//absl/base:base' does not have mandatory providers: 'CcSharedLibraryInfo'
ERROR: /nas/cloud/dev/tcmalloc/tcmalloc/BUILD:1498:18: in dynamic_deps attribute of cc_shared_library rule //tcmalloc:tcmalloc_shared: '@@abseil-cpp~//absl/debugging:leak_check' does not have mandatory providers: 'CcSharedLibraryInfo'
ERROR: /nas/cloud/dev/tcmalloc/tcmalloc/BUILD:1498:18: in dynamic_deps attribute of cc_shared_library rule //tcmalloc:tcmalloc_shared: '@@abseil-cpp~//absl/debugging:stacktrace' does not have mandatory providers: 'CcSharedLibraryInfo'
ERROR: /nas/cloud/dev/tcmalloc/tcmalloc/BUILD:1498:18: in dynamic_deps attribute of cc_shared_library rule //tcmalloc:tcmalloc_shared: '@@abseil-cpp~//absl/debugging:symbolize' does not have mandatory providers: 'CcSharedLibraryInfo'
ERROR: /nas/cloud/dev/tcmalloc/tcmalloc/BUILD:1498:18: in dynamic_deps attribute of cc_shared_library rule //tcmalloc:tcmalloc_shared: '@@abseil-cpp~//absl/status:status' does not have mandatory providers: 'CcSharedLibraryInfo'
ERROR: /nas/cloud/dev/tcmalloc/tcmalloc/BUILD:1498:18: in dynamic_deps attribute of cc_shared_library rule //tcmalloc:tcmalloc_shared: '@@abseil-cpp~//absl/status:statusor' does not have mandatory providers: 'CcSharedLibraryInfo'
ERROR: /nas/cloud/dev/tcmalloc/tcmalloc/BUILD:1498:18: in dynamic_deps attribute of cc_shared_library rule //tcmalloc:tcmalloc_shared: '@@abseil-cpp~//absl/strings:strings' does not have mandatory providers: 'CcSharedLibraryInfo'
ERROR: /nas/cloud/dev/tcmalloc/tcmalloc/BUILD:1498:18: in dynamic_deps attribute of cc_shared_library rule //tcmalloc:tcmalloc_shared: '@@abseil-cpp~//absl/time:time' does not have mandatory providers: 'CcSharedLibraryInfo'
ERROR: /nas/cloud/dev/tcmalloc/tcmalloc/BUILD:1498:18: Analysis of target '//tcmalloc:tcmalloc_shared' failed
ERROR: Analysis of target '//tcmalloc:tcmalloc_shared' failed; build aborted

I have come to the conclusion (maybe incorrectly) that abseil bazel files would need to be modified to support such usage...

lano1106 commented 3 weeks ago

I have found this page about bazel C++ rules: https://bazel.build/reference/be/c-cpp#cc_shared_library

I don't know if Bazel is also a Google creation but IMHO, the reference page lack some details in order to be able to correctly understand how to use these rules appropriately... I feel like I am shooting in the dark with my attempts to use the system to make it do what I want to achieve...