Skip to content

Commit b189157

Browse files
scentiniCopybara-Service
authored and
Copybara-Service
committed
Make cc_common.create_cc_toolchain_config_info() deal with legacy feature behavior
After selecting the CToolchain, Bazel tempers with it by adding and moving features, action configs, and artifact name patterns (see CppToolchainInfo#addLegacyFeatures() ). What it does is : For every ArtifactCategory defined in, if the CToolchain does not contain an equivalent ArtifactNamePattern, we add it. if CToolchain does not contain "no_legacy_features" feature: - if it contains "legacy_compile_flags" feature, move it to the very top of the list of features (above both CToolchain features and legacy ones). - prepend a bunch of action_configs in front of the ones that CToolchain defines - prepend a bunch of features in front of the ones that CToolchain defines, IF they are not already present. - append a bunch of features to the list that CToolchain defines ,IF they are not already present. This cl introduces the above described behavior to cc_common.create_cc_toolchain_config_info(). Work towards bazelbuild#5380 RELNOTES: None. PiperOrigin-RevId: 221595847
1 parent cd2a036 commit b189157

File tree

7 files changed

+1546
-1144
lines changed

7 files changed

+1546
-1144
lines changed

src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.WithFeatureSet;
5353
import com.google.devtools.build.lib.rules.cpp.CcToolchainVariables.Expandable;
5454
import com.google.devtools.build.lib.rules.cpp.CcToolchainVariables.StringValueParser;
55+
import com.google.devtools.build.lib.rules.cpp.CppActionConfigs.CppPlatform;
5556
import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink;
5657
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
5758
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
@@ -71,11 +72,15 @@
7172
import com.google.devtools.build.lib.util.Pair;
7273
import com.google.devtools.build.lib.util.StringUtil;
7374
import com.google.devtools.build.lib.vfs.PathFragment;
75+
import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain;
7476
import java.util.ArrayList;
7577
import java.util.Arrays;
78+
import java.util.HashSet;
7679
import java.util.List;
80+
import java.util.Locale;
7781
import java.util.Map;
7882
import java.util.Map.Entry;
83+
import java.util.Set;
7984
import javax.annotation.Nullable;
8085

8186
/** A module that contains Skylark utilities for C++ support. */
@@ -985,22 +990,104 @@ public CcToolchainConfigInfo ccToolchainConfigInfoFromSkylark(
985990
for (Object feature : features) {
986991
featureBuilder.add(featureFromSkylark((SkylarkInfo) feature));
987992
}
993+
ImmutableList<Feature> featureList = featureBuilder.build();
994+
995+
ImmutableSet<String> featureNames =
996+
featureList.stream()
997+
.map(feature -> feature.getName())
998+
.collect(ImmutableSet.toImmutableSet());
988999

9891000
ImmutableList.Builder<ActionConfig> actionConfigBuilder = ImmutableList.builder();
9901001
for (Object actionConfig : actionConfigs) {
9911002
actionConfigBuilder.add(actionConfigFromSkylark((SkylarkInfo) actionConfig));
9921003
}
1004+
ImmutableList<ActionConfig> actionConfigList = actionConfigBuilder.build();
9931005

9941006
ImmutableList.Builder<ArtifactNamePattern> artifactNamePatternBuilder = ImmutableList.builder();
9951007
for (Object artifactNamePattern : artifactNamePatterns) {
9961008
artifactNamePatternBuilder.add(
9971009
artifactNamePatternFromSkylark((SkylarkInfo) artifactNamePattern));
9981010
}
1011+
getLegacyArtifactNamePatterns(artifactNamePatternBuilder);
9991012

1013+
// Pairs (toolName, toolPath)
10001014
ImmutableList.Builder<Pair<String, String>> toolPathPairs = ImmutableList.builder();
10011015
for (Object toolPath : toolPaths) {
10021016
toolPathPairs.add(toolPathFromSkylark((SkylarkInfo) toolPath));
10031017
}
1018+
ImmutableList<Pair<String, String>> toolPathList = toolPathPairs.build();
1019+
1020+
if (!featureNames.contains(CppRuleClasses.NO_LEGACY_FEATURES)) {
1021+
String gccToolPath = "DUMMY_GCC_TOOL";
1022+
String linkerToolPath = "DUMMY_LINKER_TOOL";
1023+
String arToolPath = "DUMMY_AR_TOOL";
1024+
String stripToolPath = "DUMMY_STRIP_TOOL";
1025+
for (Pair<String, String> tool : toolPathList) {
1026+
if (tool.first.equals(CppConfiguration.Tool.GCC.getNamePart())) {
1027+
gccToolPath = tool.second;
1028+
linkerToolPath =
1029+
skylarkRuleContext
1030+
.getRuleContext()
1031+
.getLabel()
1032+
.getPackageIdentifier()
1033+
.getPathUnderExecRoot()
1034+
.getRelative(PathFragment.create(tool.second))
1035+
.getPathString();
1036+
}
1037+
if (tool.first.equals(CppConfiguration.Tool.AR.getNamePart())) {
1038+
arToolPath = tool.second;
1039+
}
1040+
if (tool.first.equals(CppConfiguration.Tool.STRIP.getNamePart())) {
1041+
stripToolPath = tool.second;
1042+
}
1043+
}
1044+
1045+
ImmutableList.Builder<Feature> legacyFeaturesBuilder = ImmutableList.builder();
1046+
// TODO(b/30109612): Remove fragile legacyCompileFlags shuffle once there are no legacy
1047+
// crosstools.
1048+
// Existing projects depend on flags from legacy toolchain fields appearing first on the
1049+
// compile command line. 'legacy_compile_flags' feature contains all these flags, and so it
1050+
// needs to appear before other features from {@link CppActionConfigs}.
1051+
if (featureNames.contains(CppRuleClasses.LEGACY_COMPILE_FLAGS)) {
1052+
legacyFeaturesBuilder.add(
1053+
featureList.stream()
1054+
.filter(feature -> feature.getName().equals(CppRuleClasses.LEGACY_COMPILE_FLAGS))
1055+
.findFirst()
1056+
.get());
1057+
}
1058+
1059+
CppPlatform platform = targetLibc.equals("macos") ? CppPlatform.MAC : CppPlatform.LINUX;
1060+
for (CToolchain.Feature feature :
1061+
CppActionConfigs.getLegacyFeatures(
1062+
platform,
1063+
featureNames,
1064+
linkerToolPath,
1065+
// This should be toolchain-based, rather than feature based, because
1066+
// it controls whether or not to declare the feature at all.
1067+
supportsEmbeddedRuntimes,
1068+
supportsInterfaceSharedObjects)) {
1069+
legacyFeaturesBuilder.add(new Feature(feature));
1070+
}
1071+
legacyFeaturesBuilder.addAll(
1072+
featureList.stream()
1073+
.filter(feature -> !feature.getName().equals(CppRuleClasses.LEGACY_COMPILE_FLAGS))
1074+
.collect(ImmutableList.toImmutableList()));
1075+
for (CToolchain.Feature feature :
1076+
CppActionConfigs.getFeaturesToAppearLastInFeaturesList(featureNames)) {
1077+
legacyFeaturesBuilder.add(new Feature(feature));
1078+
}
1079+
1080+
featureList = legacyFeaturesBuilder.build();
1081+
1082+
ImmutableList.Builder<ActionConfig> legacyActionConfigBuilder = ImmutableList.builder();
1083+
for (CToolchain.ActionConfig actionConfig :
1084+
CppActionConfigs.getLegacyActionConfigs(
1085+
platform, gccToolPath, arToolPath, stripToolPath, supportsEmbeddedRuntimes)) {
1086+
legacyActionConfigBuilder.add(new ActionConfig(actionConfig));
1087+
}
1088+
legacyActionConfigBuilder.addAll(actionConfigList);
1089+
actionConfigList = legacyActionConfigBuilder.build();
1090+
}
10041091

10051092
ImmutableList.Builder<Pair<String, String>> makeVariablePairs = ImmutableList.builder();
10061093
for (Object makeVariable : makeVariables) {
@@ -1012,8 +1099,8 @@ public CcToolchainConfigInfo ccToolchainConfigInfoFromSkylark(
10121099
boolean hasDynamicLinkingModeFlags = dynamicModeFlags != null;
10131100

10141101
return new CcToolchainConfigInfo(
1015-
actionConfigBuilder.build(),
1016-
featureBuilder.build(),
1102+
actionConfigList,
1103+
featureList,
10171104
artifactNamePatternBuilder.build(),
10181105
ImmutableList.copyOf(cxxBuiltInIncludeDirectories),
10191106
toolchainIdentifier,
@@ -1033,7 +1120,7 @@ public CcToolchainConfigInfo ccToolchainConfigInfoFromSkylark(
10331120
supportsFission,
10341121
supportsDsym,
10351122
needsPic,
1036-
toolPathPairs.build(),
1123+
toolPathList,
10371124
ImmutableList.copyOf(compilerFlags),
10381125
ImmutableList.copyOf(cxxFlags),
10391126
ImmutableList.copyOf(unfilteredCxxFlags),
@@ -1523,4 +1610,29 @@ private static ImmutableList<SkylarkInfo> getSkylarkProviderListFromSkylarkField
15231610
}
15241611
return compilationModeFlagsBuilder.build();
15251612
}
1613+
1614+
private static void getLegacyArtifactNamePatterns(
1615+
ImmutableList.Builder<ArtifactNamePattern> patterns) {
1616+
Set<ArtifactCategory> definedCategories = new HashSet<>();
1617+
for (ArtifactNamePattern pattern : patterns.build()) {
1618+
try {
1619+
definedCategories.add(
1620+
ArtifactCategory.valueOf(
1621+
pattern.getArtifactCategory().getCategoryName().toUpperCase(Locale.ENGLISH)));
1622+
} catch (IllegalArgumentException e) {
1623+
// Invalid category name, will be detected later.
1624+
continue;
1625+
}
1626+
}
1627+
1628+
for (ArtifactCategory category : ArtifactCategory.values()) {
1629+
if (!definedCategories.contains(category)
1630+
&& category.getDefaultPrefix() != null
1631+
&& category.getDefaultExtension() != null) {
1632+
patterns.add(
1633+
new ArtifactNamePattern(
1634+
category, category.getDefaultPrefix(), category.getDefaultExtension()));
1635+
}
1636+
}
1637+
}
15261638
}

0 commit comments

Comments
 (0)