52
52
import com .google .devtools .build .lib .rules .cpp .CcToolchainFeatures .WithFeatureSet ;
53
53
import com .google .devtools .build .lib .rules .cpp .CcToolchainVariables .Expandable ;
54
54
import com .google .devtools .build .lib .rules .cpp .CcToolchainVariables .StringValueParser ;
55
+ import com .google .devtools .build .lib .rules .cpp .CppActionConfigs .CppPlatform ;
55
56
import com .google .devtools .build .lib .rules .cpp .LinkerInputs .LibraryToLink ;
56
57
import com .google .devtools .build .lib .skyframe .serialization .ObjectCodec ;
57
58
import com .google .devtools .build .lib .skyframe .serialization .autocodec .AutoCodec ;
71
72
import com .google .devtools .build .lib .util .Pair ;
72
73
import com .google .devtools .build .lib .util .StringUtil ;
73
74
import com .google .devtools .build .lib .vfs .PathFragment ;
75
+ import com .google .devtools .build .lib .view .config .crosstool .CrosstoolConfig .CToolchain ;
74
76
import java .util .ArrayList ;
75
77
import java .util .Arrays ;
78
+ import java .util .HashSet ;
76
79
import java .util .List ;
80
+ import java .util .Locale ;
77
81
import java .util .Map ;
78
82
import java .util .Map .Entry ;
83
+ import java .util .Set ;
79
84
import javax .annotation .Nullable ;
80
85
81
86
/** A module that contains Skylark utilities for C++ support. */
@@ -985,22 +990,104 @@ public CcToolchainConfigInfo ccToolchainConfigInfoFromSkylark(
985
990
for (Object feature : features ) {
986
991
featureBuilder .add (featureFromSkylark ((SkylarkInfo ) feature ));
987
992
}
993
+ ImmutableList <Feature > featureList = featureBuilder .build ();
994
+
995
+ ImmutableSet <String > featureNames =
996
+ featureList .stream ()
997
+ .map (feature -> feature .getName ())
998
+ .collect (ImmutableSet .toImmutableSet ());
988
999
989
1000
ImmutableList .Builder <ActionConfig > actionConfigBuilder = ImmutableList .builder ();
990
1001
for (Object actionConfig : actionConfigs ) {
991
1002
actionConfigBuilder .add (actionConfigFromSkylark ((SkylarkInfo ) actionConfig ));
992
1003
}
1004
+ ImmutableList <ActionConfig > actionConfigList = actionConfigBuilder .build ();
993
1005
994
1006
ImmutableList .Builder <ArtifactNamePattern > artifactNamePatternBuilder = ImmutableList .builder ();
995
1007
for (Object artifactNamePattern : artifactNamePatterns ) {
996
1008
artifactNamePatternBuilder .add (
997
1009
artifactNamePatternFromSkylark ((SkylarkInfo ) artifactNamePattern ));
998
1010
}
1011
+ getLegacyArtifactNamePatterns (artifactNamePatternBuilder );
999
1012
1013
+ // Pairs (toolName, toolPath)
1000
1014
ImmutableList .Builder <Pair <String , String >> toolPathPairs = ImmutableList .builder ();
1001
1015
for (Object toolPath : toolPaths ) {
1002
1016
toolPathPairs .add (toolPathFromSkylark ((SkylarkInfo ) toolPath ));
1003
1017
}
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
+ }
1004
1091
1005
1092
ImmutableList .Builder <Pair <String , String >> makeVariablePairs = ImmutableList .builder ();
1006
1093
for (Object makeVariable : makeVariables ) {
@@ -1012,8 +1099,8 @@ public CcToolchainConfigInfo ccToolchainConfigInfoFromSkylark(
1012
1099
boolean hasDynamicLinkingModeFlags = dynamicModeFlags != null ;
1013
1100
1014
1101
return new CcToolchainConfigInfo (
1015
- actionConfigBuilder . build () ,
1016
- featureBuilder . build () ,
1102
+ actionConfigList ,
1103
+ featureList ,
1017
1104
artifactNamePatternBuilder .build (),
1018
1105
ImmutableList .copyOf (cxxBuiltInIncludeDirectories ),
1019
1106
toolchainIdentifier ,
@@ -1033,7 +1120,7 @@ public CcToolchainConfigInfo ccToolchainConfigInfoFromSkylark(
1033
1120
supportsFission ,
1034
1121
supportsDsym ,
1035
1122
needsPic ,
1036
- toolPathPairs . build () ,
1123
+ toolPathList ,
1037
1124
ImmutableList .copyOf (compilerFlags ),
1038
1125
ImmutableList .copyOf (cxxFlags ),
1039
1126
ImmutableList .copyOf (unfilteredCxxFlags ),
@@ -1523,4 +1610,29 @@ private static ImmutableList<SkylarkInfo> getSkylarkProviderListFromSkylarkField
1523
1610
}
1524
1611
return compilationModeFlagsBuilder .build ();
1525
1612
}
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
+ }
1526
1638
}
0 commit comments