in src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java [508:735]
public LibraryToLink createLibraryLinkerInput(
Object actionsObject,
Object featureConfigurationObject,
Object ccToolchainProviderObject,
Object staticLibraryObject,
Object picStaticLibraryObject,
Object dynamicLibraryObject,
Object interfaceLibraryObject,
Object picObjectFiles, // Sequence<Artifact> expected
Object objectFiles, // Sequence<Artifact> expected
boolean alwayslink,
String dynamicLibraryPath,
String interfaceLibraryPath,
Object mustKeepDebugForStarlark,
StarlarkThread thread)
throws EvalException {
if (checkObjectsBound(mustKeepDebugForStarlark)) {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
}
StarlarkActionFactory starlarkActionFactory =
nullIfNone(actionsObject, StarlarkActionFactory.class);
FeatureConfigurationForStarlark featureConfiguration =
nullIfNone(featureConfigurationObject, FeatureConfigurationForStarlark.class);
CcToolchainProvider ccToolchainProvider =
nullIfNone(ccToolchainProviderObject, CcToolchainProvider.class);
Artifact staticLibrary = nullIfNone(staticLibraryObject, Artifact.class);
Artifact picStaticLibrary = nullIfNone(picStaticLibraryObject, Artifact.class);
Artifact dynamicLibrary = nullIfNone(dynamicLibraryObject, Artifact.class);
Artifact interfaceLibrary = nullIfNone(interfaceLibraryObject, Artifact.class);
boolean mustKeepDebug =
convertFromNoneable(mustKeepDebugForStarlark, /* defaultValue= */ false);
if (checkObjectsBound(picObjectFiles, objectFiles) && !isBuiltIn(thread)) {
if (!starlarkActionFactory
.getActionConstructionContext()
.getConfiguration()
.getFragment(CppConfiguration.class)
.experimentalStarlarkCcImport()) {
throw Starlark.errorf(
"Cannot use objects/pic_objects without --experimental_starlark_cc_import");
}
}
ImmutableList<Artifact> picObjects = asArtifactImmutableList(picObjectFiles);
ImmutableList<Artifact> nopicObjects = asArtifactImmutableList(objectFiles);
StringBuilder extensionErrorsBuilder = new StringBuilder();
String extensionErrorMessage = "does not have any of the allowed extensions";
PathFragment dynamicLibraryPathFragment = null;
if (!Strings.isNullOrEmpty(dynamicLibraryPath)) {
dynamicLibraryPathFragment = PathFragment.create(dynamicLibraryPath);
validateSymlinkPath(
"dynamic_library_symlink_path",
dynamicLibraryPathFragment,
Link.ONLY_SHARED_LIBRARY_FILETYPES,
extensionErrorsBuilder);
}
PathFragment interfaceLibraryPathFragment = null;
if (!Strings.isNullOrEmpty(interfaceLibraryPath)) {
interfaceLibraryPathFragment = PathFragment.create(interfaceLibraryPath);
validateSymlinkPath(
"interface_library_symlink_path",
interfaceLibraryPathFragment,
Link.ONLY_INTERFACE_LIBRARY_FILETYPES,
extensionErrorsBuilder);
}
Artifact notNullArtifactForIdentifier = null;
if (staticLibrary != null) {
String filename = staticLibrary.getFilename();
if (!Link.ARCHIVE_FILETYPES.matches(filename)
&& (!alwayslink || !Link.LINK_LIBRARY_FILETYPES.matches(filename))) {
String extensions = Link.ARCHIVE_FILETYPES.toString();
if (alwayslink) {
extensions += ", " + Link.LINK_LIBRARY_FILETYPES;
}
extensionErrorsBuilder.append(
String.format("'%s' %s %s", filename, extensionErrorMessage, extensions));
extensionErrorsBuilder.append(LINE_SEPARATOR.value());
}
notNullArtifactForIdentifier = staticLibrary;
}
if (picStaticLibrary != null) {
String filename = picStaticLibrary.getFilename();
if (!Link.ARCHIVE_FILETYPES.matches(filename)
&& (!alwayslink || !Link.LINK_LIBRARY_FILETYPES.matches(filename))) {
String extensions = Link.ARCHIVE_FILETYPES.toString();
if (alwayslink) {
extensions += ", " + Link.LINK_LIBRARY_FILETYPES;
}
extensionErrorsBuilder.append(
String.format("'%s' %s %s", filename, extensionErrorMessage, extensions));
extensionErrorsBuilder.append(LINE_SEPARATOR.value());
}
notNullArtifactForIdentifier = picStaticLibrary;
}
if (dynamicLibrary != null) {
String filename = dynamicLibrary.getFilename();
if (!Link.ONLY_SHARED_LIBRARY_FILETYPES.matches(filename)) {
extensionErrorsBuilder.append(
String.format(
"'%s' %s %s", filename, extensionErrorMessage, Link.ONLY_SHARED_LIBRARY_FILETYPES));
extensionErrorsBuilder.append(LINE_SEPARATOR.value());
}
notNullArtifactForIdentifier = dynamicLibrary;
}
if (interfaceLibrary != null) {
String filename = interfaceLibrary.getFilename();
if (!FileTypeSet.of(CppFileTypes.INTERFACE_SHARED_LIBRARY, CppFileTypes.UNIX_SHARED_LIBRARY)
.matches(filename)) {
extensionErrorsBuilder.append(
String.format(
"'%s' %s %s",
filename, extensionErrorMessage, Link.ONLY_INTERFACE_LIBRARY_FILETYPES));
extensionErrorsBuilder.append(LINE_SEPARATOR.value());
}
notNullArtifactForIdentifier = interfaceLibrary;
}
if (dynamicLibrary != null || interfaceLibrary != null) {
String library = (dynamicLibrary != null) ? "dynamic" : "interface";
if (ccToolchainProvider == null) {
throw Starlark.errorf(
"If you pass '%s_library', you must also pass a 'cc_toolchain'", library);
}
if (featureConfiguration == null) {
throw Starlark.errorf(
"If you pass '%s_library', you must also pass a 'feature_configuration'", library);
}
}
if (nopicObjects != null && staticLibrary == null) {
throw Starlark.errorf("If you pass 'objects' you must also pass a 'static_library'");
}
if (picObjects != null && picStaticLibrary == null) {
throw Starlark.errorf("If you pass 'pic_objects' you must also pass a 'pic_static_library'");
}
if (notNullArtifactForIdentifier == null) {
throw Starlark.errorf("Must pass at least one artifact");
}
String extensionErrors = extensionErrorsBuilder.toString();
if (!extensionErrors.isEmpty()) {
throw Starlark.errorf("%s", extensionErrors);
}
Artifact resolvedSymlinkDynamicLibrary = null;
Artifact resolvedSymlinkInterfaceLibrary = null;
if (dynamicLibrary != null
&& !featureConfiguration
.getFeatureConfiguration()
.isEnabled(CppRuleClasses.TARGETS_WINDOWS)) {
resolvedSymlinkDynamicLibrary = dynamicLibrary;
if (dynamicLibraryPathFragment != null) {
if (dynamicLibrary.getRootRelativePath().getPathString().startsWith("_solib_")) {
throw Starlark.errorf(
"dynamic_library must not be a symbolic link in the solib directory. Got '%s'",
dynamicLibrary.getRootRelativePath());
}
dynamicLibrary =
SolibSymlinkAction.getDynamicLibrarySymlink(
starlarkActionFactory.asActionRegistry(starlarkActionFactory),
starlarkActionFactory.getActionConstructionContext(),
ccToolchainProvider.getSolibDirectory(),
dynamicLibrary,
dynamicLibraryPathFragment);
} else {
dynamicLibrary =
SolibSymlinkAction.getDynamicLibrarySymlink(
starlarkActionFactory.asActionRegistry(starlarkActionFactory),
starlarkActionFactory.getActionConstructionContext(),
ccToolchainProvider.getSolibDirectory(),
dynamicLibrary,
/* preserveName= */ true,
/* prefixConsumer= */ true);
}
}
if (interfaceLibrary != null
&& !featureConfiguration
.getFeatureConfiguration()
.isEnabled(CppRuleClasses.TARGETS_WINDOWS)) {
resolvedSymlinkInterfaceLibrary = interfaceLibrary;
if (interfaceLibraryPathFragment != null) {
if (interfaceLibrary.getRootRelativePath().getPathString().startsWith("_solib_")) {
throw Starlark.errorf(
"interface_library must not be a symbolic link in the solib directory. Got '%s'",
interfaceLibrary.getRootRelativePath());
}
interfaceLibrary =
SolibSymlinkAction.getDynamicLibrarySymlink(
/* actionRegistry= */ starlarkActionFactory.asActionRegistry(starlarkActionFactory),
/* actionConstructionContext= */ starlarkActionFactory
.getActionConstructionContext(),
ccToolchainProvider.getSolibDirectory(),
interfaceLibrary,
interfaceLibraryPathFragment);
} else {
interfaceLibrary =
SolibSymlinkAction.getDynamicLibrarySymlink(
/* actionRegistry= */ starlarkActionFactory.asActionRegistry(starlarkActionFactory),
/* actionConstructionContext= */ starlarkActionFactory
.getActionConstructionContext(),
ccToolchainProvider.getSolibDirectory(),
interfaceLibrary,
/* preserveName= */ true,
/* prefixConsumer= */ true);
}
}
if (staticLibrary == null
&& picStaticLibrary == null
&& dynamicLibrary == null
&& interfaceLibrary == null) {
throw Starlark.errorf(
"Must pass at least one of the following parameters: static_library, pic_static_library, "
+ "dynamic_library and interface_library.");
}
return LibraryToLink.builder()
.setLibraryIdentifier(CcLinkingOutputs.libraryIdentifierOf(notNullArtifactForIdentifier))
.setStaticLibrary(staticLibrary)
.setPicStaticLibrary(picStaticLibrary)
.setDynamicLibrary(dynamicLibrary)
.setResolvedSymlinkDynamicLibrary(resolvedSymlinkDynamicLibrary)
.setInterfaceLibrary(interfaceLibrary)
.setResolvedSymlinkInterfaceLibrary(resolvedSymlinkInterfaceLibrary)
.setObjectFiles(nopicObjects)
.setPicObjectFiles(picObjects)
.setAlwayslink(alwayslink)
.setMustKeepDebug(mustKeepDebug)
.build();
}