Skip to content

Commit cd59e93

Browse files
authored
add a static_suffix attr (#1372)
Some third party cmake packages append a suffix to static libs eg. cares: ``` out_shared_libs = [ "libcares.so", ], out_static_libs = [ "libcares_static.a", ] ``` `cmake` will treat these as separate libs, creating separate `CcInfo` `<LibraryToLink>`'s for each: ``` <LinkerInput(libraries=[<LibraryToLink(static_library=libcares_static.a)>, <LibraryToLink(dynamic_library=libcares.so)>]> ``` This creates a different `linking_context` compared to packages that have matching filenames, and can lead to changes in linking behavior when used as a dep. This change introduces a new attribute `static_suffix` that provides a hint to allow association of the suffix-appended static lib with the shared lib: ``` static_suffix = "_static", ``` results in ``` <LinkerInput(libraries=[<LibraryToLink(static_library=libcares_static.a, dynamic_library=libcares.so)>]> ```
1 parent cc26e67 commit cd59e93

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

foreign_cc/private/cc_toolchain_util.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _configure_features(ctx, cc_toolchain):
6767
def _create_libraries_to_link(ctx, files):
6868
libs = []
6969

70-
static_map = _files_map(_filter(files.static_libraries or [], _is_position_independent, True))
70+
static_map = _files_map(_filter(files.static_libraries or [], _is_position_independent, True), suffix = ctx.attr.static_suffix)
7171
pic_static_map = _files_map(_filter(files.static_libraries or [], _is_position_independent, False))
7272
shared_map = _files_map(files.shared_libraries or [])
7373
interface_map = _files_map(files.interface_libraries or [])
@@ -106,10 +106,10 @@ def _filter(list_, predicate, inverse):
106106
result.append(elem)
107107
return result
108108

109-
def _files_map(files_list):
109+
def _files_map(files_list, suffix = ""):
110110
by_names_map = {}
111111
for file_ in files_list:
112-
name_ = _file_name_no_ext(file_.basename)
112+
name_ = _removesuffix(_file_name_no_ext(file_.basename), suffix)
113113
value = by_names_map.get(name_)
114114
if value:
115115
fail("Can not have libraries with the same name in the same category")
@@ -390,3 +390,6 @@ def _prefix(text, from_str, prefix):
390390
def _file_name_no_ext(basename):
391391
(before, _separator, _after) = basename.rpartition(".")
392392
return before
393+
394+
def _removesuffix(s, sub):
395+
return s[:-len(sub)] if sub and s.endswith(sub) else s

foreign_cc/private/framework.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
210210
mandatory = False,
211211
default = False,
212212
),
213+
"static_suffix": attr.string(
214+
doc = (
215+
"Optional suffix used by static libs." +
216+
"Ensures correct association of static and shared libs."
217+
),
218+
mandatory = False,
219+
),
213220
"targets": attr.string_list(
214221
doc = (
215222
"A list of targets with in the foreign build system to produce. An empty string (`\"\"`) will result in " +

0 commit comments

Comments
 (0)