erenon
erenon

Reputation: 19118

Do not mangle name of shared library with Bazel

Given a C++ application, built with Bazel, that depends on an external, system provided shared library:

cc_binary(
  name = 'app',
  srcs = ['app.cpp'],
  linkstatic = False,
  deps = ['@my_system//:system_lib'],
)

The WORKSPACE and BUILD.my_system files:

new_local_repository(
  name = 'my_system',
  build_file = 'BUILD.my_system',
  path = '/usr/lib/my_system/',
)
cc_import(
  name = 'system_lib',
  shared_library = 'system_lib.so',
  visibility = ['//visibility:public'],
)

This builds, but first copies the system provided lib to the cache, and links to that:

$ ldd bazel-bin/app/app
    system_lib.so => /home/erenon/bazel/proj/bazel-bin/app/../_solib_k8/_U@my_system_S_S_Csystem_Ulib___Uexternal_Smy_system/system_lib.so
    [...]

If I move app to an identical system that has /usr/lib/my_system/system_lib.so, it breaks, as it misses the cache. I'd like to package app in a way that it directly links to the original .so, without the intermediate cache copy or name mangling, i.e: I'd like to achieve:

$ ldd bazel-bin/app/app
    system_lib.so => /usr/lib/my_system/system_lib.so
    [...]

I tried cc_import.system_provided, but that appears only work for Windows lib/dlls.

Upvotes: 1

Views: 798

Answers (1)

otisonoza
otisonoza

Reputation: 1344

Wrap your library with another cc_library.
I can't explain why it works, but it does.

cc_library(
    name = "libsystem_lib",
    srcs = [ ":system_lib" ],
    hdrs = ...
)

Depend on this one instead.

Upvotes: 1

Related Questions