Skip to content

Commit

Permalink
C++: Remove whitelist flag for new API
Browse files Browse the repository at this point in the history
    Remove the flag for parts of the API that are stable.

    bazelbuild/bazel#4570

    RELNOTES: None.
    PiperOrigin-RevId: 227516883
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent 68c227c commit dc12090
Show file tree
Hide file tree
Showing 3 changed files with 430 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ public CcLinkingInfo getCcLinkingInfo() {
return ccLinkingInfo;
}

public CcLinkingContext getCcLinkingContext() {
return LibraryToLinkWrapper.fromCcLinkingInfo(ccLinkingInfo);
}

public static CcInfo merge(Collection<CcInfo> ccInfos) {
ImmutableList.Builder<CcCompilationContext> ccCompilationContexts = ImmutableList.builder();
ImmutableList.Builder<CcLinkingInfo> ccLinkingInfos = ImmutableList.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,18 @@ protected Iterable<String> userFlagsToIterable(CppConfiguration cppConfiguration
}

/**
* This method returns a {@link LibraryToLinkWrapper} object that will be used to contain linking
* artifacts and information for a single library that will later be used by a linking action.
* This method returns either {@link LibraryToLink} object (old API) or a {@link
* LibraryToLinkWrapper} object (new API) that will be used to contain linking artifacts and
* information for a single library that will later be used by a linking action.
*
* <p>This method supports the old and the new API. Attributes of the old API are marked
* deprecated. They cannot be mixed. If using the old API a {@link CcLinkingInfo} object is
* returned, if using the new API then a {@link
* com.google.devtools.build.lib.rules.cpp.LibraryToLinkWrapper.CcLinkingContext}
*
* @param skylarkRuleContextObject deprecated
* @param libraryObject deprecated
* @param skylarkArtifactCategoryObject deprecated
* @param actionsObject SkylarkActionFactory
* @param featureConfigurationObject FeatureConfiguration
* @param staticLibraryObject Artifact
Expand All @@ -378,6 +387,9 @@ protected Iterable<String> userFlagsToIterable(CppConfiguration cppConfiguration
*/
@Override
public Object createLibraryLinkerInput(
Object skylarkRuleContextObject,
Object libraryObject,
Object skylarkArtifactCategoryObject,
Object actionsObject,
Object featureConfigurationObject,
Object ccToolchainProviderObject,
Expand All @@ -389,6 +401,10 @@ public Object createLibraryLinkerInput(
Location location,
Environment environment)
throws EvalException, InterruptedException {
SkylarkRuleContext skylarkRuleContext =
nullIfNone(skylarkRuleContextObject, SkylarkRuleContext.class);
Artifact library = nullIfNone(libraryObject, Artifact.class);
String skylarkArtifactCategory = nullIfNone(skylarkArtifactCategoryObject, String.class);
SkylarkActionFactory skylarkActionFactory =
nullIfNone(actionsObject, SkylarkActionFactory.class);
FeatureConfiguration featureConfiguration =
Expand All @@ -400,49 +416,101 @@ public Object createLibraryLinkerInput(
Artifact dynamicLibrary = nullIfNone(dynamicLibraryObject, Artifact.class);
Artifact interfaceLibrary = nullIfNone(interfaceLibraryObject, Artifact.class);

Artifact notNullArtifactForIdentifier = null;
if (staticLibrary != null) {
notNullArtifactForIdentifier = staticLibrary;
} else if (picStaticLibrary != null) {
notNullArtifactForIdentifier = picStaticLibrary;
} else if (dynamicLibrary != null) {
notNullArtifactForIdentifier = dynamicLibrary;
} else if (interfaceLibrary != null) {
notNullArtifactForIdentifier = interfaceLibrary;
} else {
throw new EvalException(location, "Must pass at least one artifact");
}

Artifact resolvedSymlinkDynamicLibrary = null;
Artifact resolvedSymlinkInterfaceLibrary = null;
if (!featureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS)) {
if (dynamicLibrary != null) {
resolvedSymlinkDynamicLibrary = dynamicLibrary;
dynamicLibrary =
SolibSymlinkAction.getDynamicLibrarySymlink(
/* actionRegistry= */ skylarkActionFactory.asActionRegistry(
location, skylarkActionFactory),
/* actionConstructionContext= */ skylarkActionFactory
.getActionConstructionContext(),
ccToolchainProvider.getSolibDirectory(),
dynamicLibrary,
/* preserveName= */ true,
/* prefixConsumer= */ true,
/* configuration= */ null);
String parameters =
"actions*, feature_configuration*, "
+ "cc_toolchain*, static_library, pic_static_library, dynamic_library, "
+ "interface_library and alwayslink. Those with * are required.";
String oldAndNewApiMixError =
"Do not mix parameter of old and new API. Old API parameters are: ctx*, library* and "
+ "artifact_category*. New API parameters are: "
+ parameters;

if (skylarkRuleContext != null || library != null || skylarkArtifactCategory != null) {
CcCommon.checkLocationWhitelisted(
environment.getSemantics(),
location,
environment.getGlobals().getLabel().getPackageIdentifier().toString());
if (skylarkRuleContext == null
|| library == null
|| skylarkArtifactCategory == null
|| skylarkActionFactory != null
|| featureConfiguration != null
|| ccToolchainProvider != null
|| staticLibrary != null
|| picStaticLibrary != null
|| dynamicLibrary != null
|| interfaceLibrary != null) {
throw new EvalException(location, oldAndNewApiMixError);
}
if (interfaceLibrary != null) {
resolvedSymlinkInterfaceLibrary = interfaceLibrary;
interfaceLibrary =
SolibSymlinkAction.getDynamicLibrarySymlink(
/* actionRegistry= */ skylarkActionFactory.asActionRegistry(
location, skylarkActionFactory),
/* actionConstructionContext= */ skylarkActionFactory
.getActionConstructionContext(),
ccToolchainProvider.getSolibDirectory(),
interfaceLibrary,
/* preserveName= */ true,
/* prefixConsumer= */ true,
/* configuration= */ null);
CcCommon.checkRuleWhitelisted(skylarkRuleContext);
ArtifactCategory artifactCategory =
ArtifactCategory.fromString(
skylarkArtifactCategory,
skylarkRuleContext.getRuleContext().getRule().getLocation(),
"artifact_category");
return LinkerInputs.opaqueLibraryToLink(
library, artifactCategory, CcLinkingOutputs.libraryIdentifierOf(library));
}

if (skylarkActionFactory != null
|| featureConfiguration != null
|| ccToolchainProvider != null
|| staticLibrary != null
|| picStaticLibrary != null
|| dynamicLibrary != null
|| interfaceLibrary != null) {
if (skylarkActionFactory == null
|| featureConfiguration == null
|| ccToolchainProvider == null
|| skylarkRuleContext != null
|| library != null
|| skylarkArtifactCategory != null) {
throw new EvalException(location, oldAndNewApiMixError);
}
Artifact notNullArtifactForIdentifier = null;
if (staticLibrary != null) {
notNullArtifactForIdentifier = staticLibrary;
} else if (picStaticLibrary != null) {
notNullArtifactForIdentifier = picStaticLibrary;
} else if (dynamicLibrary != null) {
notNullArtifactForIdentifier = dynamicLibrary;
} else if (interfaceLibrary != null) {
notNullArtifactForIdentifier = interfaceLibrary;
} else {
throw new EvalException(location, "Must pass at least one artifact");
}

Artifact resolvedSymlinkDynamicLibrary = null;
Artifact resolvedSymlinkInterfaceLibrary = null;
if (!featureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS)) {
if (dynamicLibrary != null) {
resolvedSymlinkDynamicLibrary = dynamicLibrary;
dynamicLibrary =
SolibSymlinkAction.getDynamicLibrarySymlink(
/* actionRegistry= */ skylarkActionFactory.asActionRegistry(
location, skylarkActionFactory),
/* actionConstructionContext= */ skylarkActionFactory
.getActionConstructionContext(),
ccToolchainProvider.getSolibDirectory(),
dynamicLibrary,
/* preserveName= */ true,
/* prefixConsumer= */ true,
/* configuration= */ null);
}
if (interfaceLibrary != null) {
resolvedSymlinkInterfaceLibrary = interfaceLibrary;
interfaceLibrary =
SolibSymlinkAction.getDynamicLibrarySymlink(
/* actionRegistry= */ skylarkActionFactory.asActionRegistry(
location, skylarkActionFactory),
/* actionConstructionContext= */ skylarkActionFactory
.getActionConstructionContext(),
ccToolchainProvider.getSolibDirectory(),
interfaceLibrary,
/* preserveName= */ true,
/* prefixConsumer= */ true,
/* configuration= */ null);
}
}

return LibraryToLinkWrapper.builder()
Expand All @@ -456,10 +524,7 @@ public Object createLibraryLinkerInput(
.setAlwayslink(alwayslink)
.build();
}
throw new EvalException(
location,
"Must pass parameters: static_library, pic_static_library, dynamic_library, "
+ "interface_library and alwayslink.");
throw new EvalException(location, "Must pass parameters: " + parameters);
}

@Override
Expand Down Expand Up @@ -641,18 +706,73 @@ private static SkylarkNestedSet toNestedSetOfStrings(Object obj, String fieldNam

@Override
public Object createCcLinkingInfo(
Object skylarkRuleContextObject,
Object staticModeParamsForDynamicLibraryObject,
Object staticModeParamsForExecutableObject,
Object dynamicModeParamsForDynamicLibraryObject,
Object dynamicModeParamsForExecutableObject,
Object librariesToLinkObject,
Object userLinkFlagsObject,
Location location,
Environment environment)
throws EvalException, InterruptedException {
SkylarkRuleContext skylarkRuleContext =
nullIfNone(skylarkRuleContextObject, SkylarkRuleContext.class);
CcLinkParams staticModeParamsForDynamicLibrary =
nullIfNone(staticModeParamsForDynamicLibraryObject, CcLinkParams.class);
CcLinkParams staticModeParamsForExecutable =
nullIfNone(staticModeParamsForExecutableObject, CcLinkParams.class);
CcLinkParams dynamicModeParamsForDynamicLibrary =
nullIfNone(dynamicModeParamsForDynamicLibraryObject, CcLinkParams.class);
CcLinkParams dynamicModeParamsForExecutable =
nullIfNone(dynamicModeParamsForExecutableObject, CcLinkParams.class);
@SuppressWarnings("unchecked")
SkylarkList<LibraryToLinkWrapper> librariesToLink =
nullIfNone(librariesToLinkObject, SkylarkList.class);
@SuppressWarnings("unchecked")
SkylarkList<String> userLinkFlags = nullIfNone(userLinkFlagsObject, SkylarkList.class);

String oldAndNewApiMixError =
"Do not mix parameter of old and new API. Old API parameters are: ctx, "
+ "static_mode_params_for_dynamic_library, "
+ "static_mode_params_for_executable, "
+ "dynamic_mode_params_for_dynamic_library and dynamic_mode_params_for_executable."
+ " New API parameters are: libraries_to_link and user_link_flags.";

if (skylarkRuleContext != null
|| staticModeParamsForDynamicLibrary != null
|| staticModeParamsForExecutable != null
|| dynamicModeParamsForDynamicLibrary != null
|| dynamicModeParamsForExecutable != null) {
CcCommon.checkLocationWhitelisted(
environment.getSemantics(),
location,
environment.getGlobals().getLabel().getPackageIdentifier().toString());
if (skylarkRuleContext == null
|| staticModeParamsForDynamicLibrary == null
|| staticModeParamsForExecutable == null
|| dynamicModeParamsForDynamicLibrary == null
|| dynamicModeParamsForExecutable == null
|| librariesToLink != null
|| userLinkFlags != null) {
throw new EvalException(location, oldAndNewApiMixError);
}
CcLinkingInfo.Builder ccLinkingInfoBuilder = CcLinkingInfo.Builder.create();
ccLinkingInfoBuilder
.setStaticModeParamsForDynamicLibrary(staticModeParamsForDynamicLibrary)
.setStaticModeParamsForExecutable(staticModeParamsForExecutable)
.setDynamicModeParamsForDynamicLibrary(dynamicModeParamsForDynamicLibrary)
.setDynamicModeParamsForExecutable(dynamicModeParamsForExecutable);
return ccLinkingInfoBuilder.build();
}
if (librariesToLink != null || userLinkFlags != null) {
if (staticModeParamsForDynamicLibrary != null
|| staticModeParamsForExecutable != null
|| dynamicModeParamsForDynamicLibrary != null
|| dynamicModeParamsForExecutable != null
|| dynamicModeParamsForExecutable != null) {
throw new EvalException(location, oldAndNewApiMixError);
}
CcLinkingContext.Builder ccLinkingContextBuilder = CcLinkingContext.builder();
if (librariesToLink != null) {
ccLinkingContextBuilder.addLibraries(
Expand Down
Loading

0 comments on commit dc12090

Please sign in to comment.