Skip to content

Commit

Permalink
Remove unused fields from CcCommon
Browse files Browse the repository at this point in the history
Remove getToolchain, getFdoContext from CcModule. They were unused.
Remove CcCommon(RuleContext, CcToolchainProvider) constructor. Because it set nothing.
Remove CcCommon.getPrivateHeaders,getSources,mapToListOfPairs. They were unused.
Remove SHARED_LIBRARY_FILETYPES from CppHelper. It was unused.

PiperOrigin-RevId: 584564680
Change-Id: I36ab454408bd94b414f9a77783b7a158df14fe84
  • Loading branch information
comius authored and copybara-github committed Nov 22, 2023
1 parent fe68cfe commit e52779c
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.rules.cpp.CcCompilationHelper.SourceCategory;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.CollidingProvidesException;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.Link.LinkTargetType;
Expand Down Expand Up @@ -139,81 +138,8 @@ public String getRepresentation() {

private final RuleContext ruleContext;

private final CcToolchainProvider ccToolchain;
private final FdoContext fdoContext;

public CcCommon(RuleContext ruleContext) throws RuleErrorException {
this(
ruleContext,
Preconditions.checkNotNull(
CppHelper.getToolchainUsingDefaultCcToolchainAttribute(ruleContext)));
}

public CcCommon(RuleContext ruleContext, CcToolchainProvider ccToolchain) {
public CcCommon(RuleContext ruleContext) {
this.ruleContext = ruleContext;
this.fdoContext = ccToolchain.getFdoContext();
this.ccToolchain = ccToolchain;
}

/**
* Returns a list of ({@link Artifact}, {@link Label}) pairs. Each pair represents an input source
* file and the label of the rule that generates it (or the label of the source file itself if it
* is an input file).
*/
List<Pair<Artifact, Label>> getPrivateHeaders() {
Map<Artifact, Label> map = Maps.newLinkedHashMap();
Iterable<? extends TransitiveInfoCollection> providers =
ruleContext.getPrerequisitesIf("srcs", FileProvider.class);
for (TransitiveInfoCollection provider : providers) {
for (Artifact artifact :
provider.getProvider(FileProvider.class).getFilesToBuild().toList()) {
// TODO(bazel-team): We currently do not produce an error for duplicate headers and other
// non-source artifacts with different labels, as that would require cleaning up the code
// base without significant benefit; we should eventually make this consistent one way or
// the other.
if (CppFileTypes.CPP_HEADER.matches(artifact.getExecPath())) {
map.put(artifact, provider.getLabel());
}
}
}
return mapToListOfPairs(map);
}

/**
* Returns a list of ({@link Artifact}, {@link Label}) pairs. Each pair represents an input source
* file and the label of the rule that generates it (or the label of the source file itself if it
* is an input file).
*/
List<Pair<Artifact, Label>> getSources() {
Map<Artifact, Label> map = Maps.newLinkedHashMap();
Iterable<? extends TransitiveInfoCollection> providers =
ruleContext.getPrerequisitesIf("srcs", FileProvider.class);
for (TransitiveInfoCollection provider : providers) {
for (Artifact artifact :
provider.getProvider(FileProvider.class).getFilesToBuild().toList()) {
if (!CppFileTypes.CPP_HEADER.matches(artifact.getExecPath())) {
Label oldLabel = map.put(artifact, provider.getLabel());
if (SourceCategory.CC_AND_OBJC.getSourceTypes().matches(artifact.getExecPathString())
&& oldLabel != null
&& !oldLabel.equals(provider.getLabel())) {
ruleContext.attributeError(
"srcs",
String.format(
"Artifact '%s' is duplicated (through '%s' and '%s')",
artifact.getExecPathString(), oldLabel, provider.getLabel()));
}
}
}
}
return mapToListOfPairs(map);
}

private List<Pair<Artifact, Label>> mapToListOfPairs(Map<Artifact, Label> map) {
ImmutableList.Builder<Pair<Artifact, Label>> result = ImmutableList.builder();
for (Map.Entry<Artifact, Label> entry : map.entrySet()) {
result.add(Pair.of(entry.getKey(), entry.getValue()));
}
return result.build();
}

/**
Expand Down Expand Up @@ -262,16 +188,6 @@ public List<Pair<Artifact, Label>> getHeaders() {
return getHeaders(ruleContext);
}

/** Returns the C++ toolchain provider. */
public CcToolchainProvider getToolchain() {
return ccToolchain;
}

/** Returns the C++ FDO optimization support provider. */
public FdoContext getFdoContext() {
return fdoContext;
}

public static void reportInvalidOptions(
RuleContext ruleContext, CppConfiguration cppConfiguration, CcToolchainProvider ccToolchain) {
if (cppConfiguration.getLibcTopLabel() != null && ccToolchain.getDefaultSysroot() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2534,7 +2534,7 @@ public Tuple compile(

SourceCategory sourceCategory =
(language == Language.CPP) ? SourceCategory.CC : SourceCategory.CC_AND_OBJC;
CcCommon common = new CcCommon(actions.getRuleContext(), ccToolchainProvider);
CcCommon common = new CcCommon(actions.getRuleContext());
BuildConfigurationValue configuration =
actions.getActionConstructionContext().getConfiguration();
CcCompilationHelper helper =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,8 @@ public String getPackageHeadersCheckingModeForStarlarkAspect(
parameters = {
@Param(name = "ctx", positional = false, named = true),
})
public CcCommon createCommon(StarlarkRuleContext starlarkRuleContext) throws EvalException {
try {
return new CcCommon(starlarkRuleContext.getRuleContext());
} catch (RuleErrorException e) {
throw new EvalException(e);
}
public CcCommon createCommon(StarlarkRuleContext starlarkRuleContext) {
return new CcCommon(starlarkRuleContext.getRuleContext());
}

@StarlarkMethod(name = "launcher_provider", documented = false, structField = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.Link.LinkTargetType;
import com.google.devtools.build.lib.server.FailureDetails.FailAction.Code;
import com.google.devtools.build.lib.util.FileTypeSet;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.ArrayList;
Expand All @@ -76,10 +75,6 @@ public class CppHelper {
public static final PathFragment SHARED_NONLTO_BACKEND_ROOT_PREFIX =
PathFragment.create("shared.nonlto");

// TODO(bazel-team): should this use Link.SHARED_LIBRARY_FILETYPES?
public static final FileTypeSet SHARED_LIBRARY_FILETYPES =
FileTypeSet.of(CppFileTypes.SHARED_LIBRARY, CppFileTypes.VERSIONED_SHARED_LIBRARY);

/** Base label of the c++ toolchain category. */
public static final String TOOLCHAIN_TYPE_LABEL = "//tools/cpp:toolchain_type";

Expand Down

0 comments on commit e52779c

Please sign in to comment.