Skip to content

Commit

Permalink
C++: Deletes CcLinkParamsInfo
Browse files Browse the repository at this point in the history
This class is not necessary anymore. We can use CcLinkParamsStoreImpl directly
which has been renamed to CcLinkParamsStore.

RELNOTES:none
PiperOrigin-RevId: 196656488
  • Loading branch information
oquenchil authored and Copybara-Service committed May 15, 2018
1 parent c4f2d80 commit 6b3e482
Show file tree
Hide file tree
Showing 25 changed files with 331 additions and 354 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.TargetUtils;
import com.google.devtools.build.lib.rules.cpp.AbstractCcLinkParamsStore;
import com.google.devtools.build.lib.rules.cpp.CcLinkParams;
import com.google.devtools.build.lib.rules.cpp.CcLinkParamsInfo;
import com.google.devtools.build.lib.rules.cpp.CcLinkParamsStore;
import com.google.devtools.build.lib.rules.cpp.CcLinkingInfo;
import com.google.devtools.build.lib.rules.java.DeployArchiveBuilder;
Expand Down Expand Up @@ -568,16 +568,16 @@ public void addProviders(
RuleConfiguredTargetBuilder ruleBuilder) {
// TODO(plf): Figure out whether we can remove support for C++ dependencies in Bazel.
CcLinkingInfo.Builder ccLinkingInfoBuilder = CcLinkingInfo.Builder.create();
ccLinkingInfoBuilder.setCcLinkParamsInfo(
new CcLinkParamsInfo(
new CcLinkParamsStore() {
ccLinkingInfoBuilder.setCcLinkParamsStore(
new CcLinkParamsStore(
new AbstractCcLinkParamsStore() {
@Override
protected void collect(
CcLinkParams.Builder builder, boolean linkingStatically, boolean linkShared) {
builder.addTransitiveTargets(
javaCommon.targetsTreatedAsDeps(ClasspathType.BOTH),
JavaCcLinkParamsProvider.TO_LINK_PARAMS,
CcLinkParamsInfo.TO_LINK_PARAMS);
CcLinkParamsStore.TO_LINK_PARAMS);
}
}));
ruleBuilder.addNativeDeclaredProvider(ccLinkingInfoBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.rules.cpp.CcLinkParamsStore;
import com.google.devtools.build.lib.rules.cpp.AbstractCcLinkParamsStore;
import com.google.devtools.build.lib.rules.python.PyCommon;
import com.google.devtools.build.lib.rules.python.PythonConfiguration;
import com.google.devtools.build.lib.rules.python.PythonSemantics;
Expand Down Expand Up @@ -128,7 +128,7 @@ public Artifact getPythonTemplateMainArtifact(RuleContext ruleContext, Artifact
public Artifact createExecutable(
RuleContext ruleContext,
PyCommon common,
CcLinkParamsStore ccLinkParamsStore,
AbstractCcLinkParamsStore ccLinkParamsStore,
NestedSet<PathFragment> imports)
throws InterruptedException {
String main = common.determineMainExecutableSource(/*withWorkspaceName=*/ true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.rules.cpp.AbstractCcLinkParamsStore;
import com.google.devtools.build.lib.rules.cpp.CcLinkParamsStore;
import com.google.devtools.build.lib.rules.cpp.CcLinkParamsStore.CcLinkParamsStoreImpl;

/** A target that provides C++ libraries to be linked into Android targets. */
@AutoValue
@Immutable
public abstract class AndroidCcLinkParamsProvider implements TransitiveInfoProvider {
public static AndroidCcLinkParamsProvider create(CcLinkParamsStore store) {
return new AutoValue_AndroidCcLinkParamsProvider(new CcLinkParamsStoreImpl(store));
public static AndroidCcLinkParamsProvider create(AbstractCcLinkParamsStore store) {
return new AutoValue_AndroidCcLinkParamsProvider(new CcLinkParamsStore(store));
}

public abstract CcLinkParamsStore getLinkParams();
public abstract AbstractCcLinkParamsStore getLinkParams();

public static final Function<TransitiveInfoCollection, CcLinkParamsStore> TO_LINK_PARAMS =
public static final Function<TransitiveInfoCollection, AbstractCcLinkParamsStore> TO_LINK_PARAMS =
(TransitiveInfoCollection input) -> {
AndroidCcLinkParamsProvider provider = input.getProvider(AndroidCcLinkParamsProvider.class);
return provider == null ? null : provider.getLinkParams();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.TriState;
import com.google.devtools.build.lib.rules.android.ZipFilterBuilder.CheckHashMismatchMode;
import com.google.devtools.build.lib.rules.cpp.AbstractCcLinkParamsStore;
import com.google.devtools.build.lib.rules.cpp.CcLinkParams;
import com.google.devtools.build.lib.rules.cpp.CcLinkParamsInfo;
import com.google.devtools.build.lib.rules.cpp.CcLinkParamsStore;
import com.google.devtools.build.lib.rules.java.ClasspathConfiguredFragment;
import com.google.devtools.build.lib.rules.java.JavaCcLinkParamsProvider;
Expand Down Expand Up @@ -800,14 +800,14 @@ public boolean isNeverLink() {
return asNeverLink;
}

public CcLinkParamsStore getCcLinkParamsStore() {
public AbstractCcLinkParamsStore getCcLinkParamsStore() {
return getCcLinkParamsStore(
javaCommon.targetsTreatedAsDeps(ClasspathType.BOTH), ImmutableList.<String>of());
}

public static CcLinkParamsStore getCcLinkParamsStore(
public static AbstractCcLinkParamsStore getCcLinkParamsStore(
final Iterable<? extends TransitiveInfoCollection> deps, final Collection<String> linkOpts) {
return new CcLinkParamsStore() {
return new AbstractCcLinkParamsStore() {
@Override
protected void collect(
CcLinkParams.Builder builder, boolean linkingStatically, boolean linkShared) {
Expand All @@ -818,7 +818,7 @@ protected void collect(
// Link in Android-specific C++ code (e.g., android_libraries) in the transitive closure
AndroidCcLinkParamsProvider.TO_LINK_PARAMS,
// Link in non-language-specific C++ code in the transitive closure
CcLinkParamsInfo.TO_LINK_PARAMS);
CcLinkParamsStore.TO_LINK_PARAMS);
builder.addLinkOpts(linkOpts);
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.rules.cpp;

import com.google.common.base.Preconditions;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;

/**
* A cache of C link parameters.
*
* <p>The cache holds instances of {@link com.google.devtools.build.lib.rules.cpp.CcLinkParams} for
* combinations of linkingStatically and linkShared. If a requested value is not available in the
* cache, it is computed and then stored.
*
* <p>Typically this class is used on targets that may be linked in as C libraries as in the
* following example:
*
* <pre>
* class SomeTarget implements CcLinkParamsStore {
* private final AbstractCcLinkParamsStore ccLinkParamsStore = new AbstractCcLinkParamsStore() {
* @Override
* protected void collect(CcLinkParams.Builder builder, boolean linkingStatically,
* boolean linkShared) {
* builder.add[...]
* }
* };
*
* @Override
* public CcLinkParams getCcLinkParams(boolean linkingStatically, boolean linkShared) {
* return ccLinkParamsStore.get(linkingStatically, linkShared);
* }
* }
* </pre>
*/
public abstract class AbstractCcLinkParamsStore {
protected CcLinkParams staticSharedParams;
protected CcLinkParams staticNoSharedParams;
protected CcLinkParams noStaticSharedParams;
protected CcLinkParams noStaticNoSharedParams;

private CcLinkParams compute(boolean linkingStatically, boolean linkShared) {
CcLinkParams.Builder builder = CcLinkParams.builder(linkingStatically, linkShared);
collect(builder, linkingStatically, linkShared);
return builder.build();
}

/**
* Returns {@link com.google.devtools.build.lib.rules.cpp.CcLinkParams} for a combination of
* parameters.
*
* <p>The {@link com.google.devtools.build.lib.rules.cpp.CcLinkParams} instance is computed lazily
* and cached.
*/
public synchronized CcLinkParams get(boolean linkingStatically, boolean linkShared) {
CcLinkParams result = lookup(linkingStatically, linkShared);
if (result == null) {
result = compute(linkingStatically, linkShared);
put(linkingStatically, linkShared, result);
}
return result;
}

private CcLinkParams lookup(boolean linkingStatically, boolean linkShared) {
if (linkingStatically) {
return linkShared ? staticSharedParams : staticNoSharedParams;
} else {
return linkShared ? noStaticSharedParams : noStaticNoSharedParams;
}
}

private void put(boolean linkingStatically, boolean linkShared, CcLinkParams params) {
Preconditions.checkNotNull(params);
if (linkingStatically) {
if (linkShared) {
staticSharedParams = params;
} else {
staticNoSharedParams = params;
}
} else {
if (linkShared) {
noStaticSharedParams = params;
} else {
noStaticNoSharedParams = params;
}
}
}

/**
* Hook for building the actual link params.
*
* <p>Users should override this method and call methods of the builder to
* set up the actual CcLinkParams objects.
*
* <p>Implementations of this method must not fail or try to report errors on the
* configured target.
*/
protected abstract void collect(CcLinkParams.Builder builder, boolean linkingStatically,
boolean linkShared);

@AutoCodec
@VisibleForSerialization
static class EmptyCcLinkParamsStore extends AbstractCcLinkParamsStore {
public static final ObjectCodec<EmptyCcLinkParamsStore> CODEC =
new AbstractCcLinkParamsStore_EmptyCcLinkParamsStore_AutoCodec();

@Override
protected void collect(
CcLinkParams.Builder builder, boolean linkingStatically, boolean linkShared) {}
}

/** An empty CcLinkParamStore. */
public static final AbstractCcLinkParamsStore EMPTY = new EmptyCcLinkParamsStore();
}

Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ private static CcLinkingInfo overrideRunfilesProvider(
CcLinkingInfo ccLinkingInfo =
(CcLinkingInfo) linkingInfo.getProviders().getProvider(CcLinkingInfo.PROVIDER.getKey());
CcLinkingInfo.Builder ccLinkingInfoBuilder = CcLinkingInfo.Builder.create();
ccLinkingInfoBuilder.setCcLinkParamsInfo(ccLinkingInfo.getCcLinkParamsInfo());
ccLinkingInfoBuilder.setCcLinkParamsStore(ccLinkingInfo.getCcLinkParamsStore());
ccLinkingInfoBuilder.setCcExecutionDynamicLibrariesInfo(
ccLinkingInfo.getCcExecutionDynamicLibrariesInfo());
ccLinkingInfoBuilder.setCcRunfiles(new CcRunfiles(staticRunfiles, sharedRunfiles));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public CcLinkParams build() {
nonCodeInputs);
}

public boolean add(CcLinkParamsStore store) {
public boolean add(AbstractCcLinkParamsStore store) {
if (store != null) {
CcLinkParams args = store.get(linkingStatically, linkShared);
addTransitiveArgs(args);
Expand All @@ -239,14 +239,17 @@ public Builder addTransitiveTargets(Iterable<? extends TransitiveInfoCollection>
/**
* Includes link parameters from a dependency target.
*
* <p>The target should implement {@link CcLinkParamsInfo}. If it does not,
* the method does not do anything.
* <p>The target should implement {@link CcLinkParamsStore}. If it does not, the method does not
* do anything.
*/
public Builder addTransitiveTarget(TransitiveInfoCollection target) {
CcLinkingInfo ccLinkingInfo = target.get(CcLinkingInfo.PROVIDER);
CcLinkParamsInfo ccLinkParamsInfo =
ccLinkingInfo == null ? null : ccLinkingInfo.getCcLinkParamsInfo();
return addTransitiveProvider(ccLinkParamsInfo);
CcLinkParamsStore ccLinkParamsStore =
ccLinkingInfo == null ? null : ccLinkingInfo.getCcLinkParamsStore();
if (ccLinkParamsStore != null) {
add(ccLinkParamsStore);
}
return this;
}

/**
Expand All @@ -255,31 +258,23 @@ public Builder addTransitiveTarget(TransitiveInfoCollection target) {
* added.
*/
@SafeVarargs
public final Builder addTransitiveTarget(TransitiveInfoCollection target,
Function<TransitiveInfoCollection, CcLinkParamsStore> firstMapping,
public final Builder addTransitiveTarget(
TransitiveInfoCollection target,
Function<TransitiveInfoCollection, AbstractCcLinkParamsStore> firstMapping,
@SuppressWarnings("unchecked") // Java arrays don't preserve generic arguments.
Function<TransitiveInfoCollection, CcLinkParamsStore>... remainingMappings) {
Function<TransitiveInfoCollection, AbstractCcLinkParamsStore>... remainingMappings) {
if (add(firstMapping.apply(target))) {
return this;
}
for (Function<TransitiveInfoCollection, CcLinkParamsStore> mapping : remainingMappings) {
for (Function<TransitiveInfoCollection, AbstractCcLinkParamsStore> mapping :
remainingMappings) {
if (add(mapping.apply(target))) {
return this;
}
}
return this;
}

/**
* Includes link parameters from a CcLinkParamsInfo provider.
*/
public Builder addTransitiveProvider(CcLinkParamsInfo provider) {
if (provider != null) {
add(provider.getCcLinkParamsStore());
}
return this;
}

/**
* Includes link parameters from the given targets. Each target is checked for the given
* mappings in the order specified, and the first mapping that returns a non-null result is
Expand All @@ -288,9 +283,9 @@ public Builder addTransitiveProvider(CcLinkParamsInfo provider) {
@SafeVarargs
public final Builder addTransitiveTargets(
Iterable<? extends TransitiveInfoCollection> targets,
Function<TransitiveInfoCollection, CcLinkParamsStore> firstMapping,
@SuppressWarnings("unchecked") // Java arrays don't preserve generic arguments.
Function<TransitiveInfoCollection, CcLinkParamsStore>... remainingMappings) {
Function<TransitiveInfoCollection, AbstractCcLinkParamsStore> firstMapping,
@SuppressWarnings("unchecked") // Java arrays don't preserve generic arguments.
Function<TransitiveInfoCollection, AbstractCcLinkParamsStore>... remainingMappings) {
for (TransitiveInfoCollection target : targets) {
addTransitiveTarget(target, firstMapping, remainingMappings);
}
Expand Down Expand Up @@ -387,7 +382,7 @@ public Builder addNonCodeInputs(Iterable<Artifact> nonCodeInputs) {
public Builder addCcLibrary(RuleContext context) {
addTransitiveTargets(
context.getPrerequisites("deps", Mode.TARGET),
CcLinkParamsInfo.TO_LINK_PARAMS,
CcLinkParamsStore.TO_LINK_PARAMS,
CcSpecificLinkParamsProvider.TO_LINK_PARAMS);
return this;
}
Expand Down
Loading

0 comments on commit 6b3e482

Please sign in to comment.