Skip to content

Commit

Permalink
Add versioned soname support to interface_library
Browse files Browse the repository at this point in the history
  • Loading branch information
m3rcuriel committed Aug 14, 2022
1 parent 61c433e commit 8e9315e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
.add(
attr("interface_library", LABEL)
.allowedFileTypes(
CppFileTypes.INTERFACE_SHARED_LIBRARY, CppFileTypes.UNIX_SHARED_LIBRARY))
CppFileTypes.INTERFACE_SHARED_LIBRARY,
CppFileTypes.UNIX_SHARED_LIBRARY,
CppFileTypes.VERSIONED_SHARED_LIBRARY))
/*<!-- #BLAZE_RULE($cc_import).ATTRIBUTE(hdrs) -->
The list of header files published by
this precompiled library to be directly included by sources in dependent rules.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,9 @@ public LibraryToLink createLibraryLinkerInput(
}
if (interfaceLibrary != null) {
String filename = interfaceLibrary.getFilename();
if (!FileTypeSet.of(CppFileTypes.INTERFACE_SHARED_LIBRARY, CppFileTypes.UNIX_SHARED_LIBRARY)
if (!FileTypeSet.of(CppFileTypes.INTERFACE_SHARED_LIBRARY,
CppFileTypes.UNIX_SHARED_LIBRARY,
CppFileTypes.VERSIONED_SHARED_LIBRARY)
.matches(filename)) {
extensionErrorsBuilder.append(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ private Link() {} // uninstantiable
FileTypeSet.of(CppFileTypes.SHARED_LIBRARY, CppFileTypes.VERSIONED_SHARED_LIBRARY);

public static final FileTypeSet ONLY_INTERFACE_LIBRARY_FILETYPES =
FileTypeSet.of(CppFileTypes.INTERFACE_SHARED_LIBRARY);
FileTypeSet.of(CppFileTypes.INTERFACE_SHARED_LIBRARY,
CppFileTypes.UNIX_SHARED_LIBRARY,
CppFileTypes.VERSIONED_SHARED_LIBRARY);

public static final FileTypeSet ARCHIVE_LIBRARY_FILETYPES =
FileTypeSet.of(
Expand Down
19 changes: 17 additions & 2 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,10 @@ def _build_precompiled_files(ctx):
shared_libraries,
)

def _is_versioned_shared_library_extension_valid(shared_library_name):
def _is_versioned_library_extension_valid(shared_library_name, exts):
# validate agains the regex "^.+\\.((so)|(dylib))(\\.\\d\\w*)+$",
# must match VERSIONED_SHARED_LIBRARY.
for ext in (".so.", ".dylib."):
for ext in exts:
name, _, version = shared_library_name.rpartition(ext)
if name and version:
version_parts = version.split(".")
Expand All @@ -532,6 +532,9 @@ def _is_versioned_shared_library_extension_valid(shared_library_name):
return True
return False

def _is_versioned_shared_library_extension_valid(shared_library_name):
return _is_versioned_library_extension_valid(shared_library_name, [".so.", ".dylib."])

def _is_shared_library_extension_valid(shared_library_name):
if (shared_library_name.endswith(".so") or
shared_library_name.endswith(".dll") or
Expand All @@ -540,6 +543,17 @@ def _is_shared_library_extension_valid(shared_library_name):

return _is_versioned_shared_library_extension_valid(shared_library_name)

def _is_interface_library_extension_valid(interface_library_name):
if (interface_library_name.endswith(".ifso") or
interface_library_name.endswith(".tbd") or
interface_library_name.endswith(".dll.a") or
interface_library_name.endswith(".dylib") or
interface_library_name.endswith(".so") or
interface_library_name.endswith(".lib")):
return True

return _is_versioned_library_extension_valid(interface_library_name, [".so.", ".dylib."])

def _get_providers(deps, provider):
providers = []
for dep in deps:
Expand Down Expand Up @@ -932,6 +946,7 @@ cc_helper = struct(
extensions = extensions,
build_precompiled_files = _build_precompiled_files,
is_shared_library_extension_valid = _is_shared_library_extension_valid,
is_interface_library_extension_valid = _is_interface_library_extension_valid,
get_providers = _get_providers,
is_compilation_outputs_empty = _is_compilation_outputs_empty,
matches_extension = _matches_extension,
Expand Down
8 changes: 5 additions & 3 deletions src/main/starlark/builtins_bzl/common/cc/cc_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def _perform_error_checks(
not cc_helper.is_shared_library_extension_valid(shared_library_artifact.basename)):
fail("'shared_library' does not produce any cc_import shared_library files (expected .so, .dylib or .dll)")

if (interface_library_artifact != None and
not cc_helper.is_interface_library_extension_valid(interface_library_artifact.basename)):
fail("'interface_library' does not produce any cc_import interface_library files (expected .so, .dylib, .ifso)")

def _create_archive_action(
ctx,
feature_configuration,
Expand Down Expand Up @@ -194,9 +198,7 @@ cc_import = rule(
"static_library": attr.label(allow_single_file = [".a", ".lib"]),
"pic_static_library": attr.label(allow_single_file = [".pic.a", ".pic.lib"]),
"shared_library": attr.label(allow_single_file = True),
"interface_library": attr.label(
allow_single_file = [".ifso", ".tbd", ".lib", ".so", ".dylib"],
),
"interface_library": attr.label(allow_single_file = True),
"pic_objects": attr.label_list(
allow_files = [".o", ".pic.o"],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,31 @@ public void testCcImportWithInterfaceSharedLibrary() throws Exception {
.containsExactly("bin _solib_k8/_U_S_Sb_Cfoo___Ub/libfoo.so");
}

@Test
public void testCcImportWithVersionedInterfaceSharedLibrary() throws Exception {
useConfiguration("--cpu=k8");
ConfiguredTarget target =
scratchConfiguredTarget(
"a",
"foo",
starlarkImplementationLoadStatement,
"cc_import(name = 'foo', interface_library = 'libfoo.so.1ab2.1_a2', system_provided = 1)");
Artifact library =
target
.get(CcInfo.PROVIDER)
.getCcLinkingContext()
.getLibraries()
.getSingleton()
.getResolvedSymlinkInterfaceLibrary();
assertThat(artifactsToStrings(ImmutableList.of(library))).containsExactly("src a/libfoo.so.1ab2.1_a2");
Iterable<Artifact> dynamicLibrariesForRuntime =
target
.get(CcInfo.PROVIDER)
.getCcLinkingContext()
.getDynamicLibrariesForRuntime(/* linkingStatically= */ false);
assertThat(artifactsToStrings(dynamicLibrariesForRuntime)).isEmpty();
}

@Test
public void testCcImportWithBothStaticAndSharedLibraries() throws Exception {
useConfiguration("--cpu=k8");
Expand Down

0 comments on commit 8e9315e

Please sign in to comment.