1313// limitations under the License.
1414package com .google .devtools .build .lib .skyframe ;
1515
16- import static com .google .common .collect .ImmutableList .builder ;
1716import static com .google .common .collect .ImmutableList .toImmutableList ;
1817import static com .google .common .collect .ImmutableSet .toImmutableSet ;
1918import static java .util .stream .Collectors .joining ;
4039import com .google .devtools .build .lib .skyframe .PlatformLookupUtil .InvalidPlatformException ;
4140import com .google .devtools .build .lib .skyframe .RegisteredExecutionPlatformsFunction .InvalidExecutionPlatformLabelException ;
4241import com .google .devtools .build .lib .skyframe .RegisteredToolchainsFunction .InvalidToolchainLabelException ;
43- import com .google .devtools .build .lib .skyframe .SingleToolchainResolutionFunction .NoToolchainFoundException ;
4442import com .google .devtools .build .lib .skyframe .SingleToolchainResolutionValue .SingleToolchainResolutionKey ;
4543import com .google .devtools .build .lib .skyframe .ToolchainTypeLookupUtil .InvalidToolchainTypeException ;
4644import com .google .devtools .build .skyframe .SkyFunction ;
4745import com .google .devtools .build .skyframe .SkyFunctionException ;
4846import com .google .devtools .build .skyframe .SkyKey ;
49- import com .google .devtools .build .skyframe .SkyframeIterableResult ;
47+ import com .google .devtools .build .skyframe .SkyframeLookupResult ;
5048import java .util .ArrayList ;
5149import java .util .List ;
5250import java .util .Map ;
@@ -92,15 +90,15 @@ public UnloadedToolchainContext compute(SkyKey skyKey, Environment env)
9290 // Load the configured target for the toolchain types to ensure that they are valid and
9391 // resolve aliases.
9492 ImmutableMap <Label , ToolchainTypeInfo > resolvedToolchainTypeInfos =
95- loadToolchainTypes (
93+ loadToolchainTypeInfos (
9694 env ,
9795 configuration ,
9896 key .toolchainTypes ().stream ()
9997 .map (ToolchainTypeRequirement ::toolchainType )
10098 .collect (toImmutableSet ()));
10199 builder .setRequestedLabelToToolchainType (resolvedToolchainTypeInfos );
102- ImmutableSet <ToolchainTypeRequirement > resolvedToolchainTypes =
103- loadToolchainTypeRequirements (resolvedToolchainTypeInfos , key .toolchainTypes ());
100+ ImmutableSet <ToolchainType > resolvedToolchainTypes =
101+ loadToolchainTypes (resolvedToolchainTypeInfos , key .toolchainTypes ());
104102
105103 // Create keys for all platforms that will be used, and validate them early.
106104 PlatformKeys platformKeys =
@@ -152,11 +150,24 @@ public UnloadedToolchainContext compute(SkyKey skyKey, Environment env)
152150 }
153151 }
154152
153+ @ AutoValue
154+ abstract static class ToolchainType {
155+ abstract ToolchainTypeRequirement toolchainTypeRequirement ();
156+
157+ abstract ToolchainTypeInfo toolchainTypeInfo ();
158+
159+ static ToolchainType create (
160+ ToolchainTypeRequirement toolchainTypeRequirement , ToolchainTypeInfo toolchainTypeInfo ) {
161+ return new AutoValue_ToolchainResolutionFunction_ToolchainType (
162+ toolchainTypeRequirement , toolchainTypeInfo );
163+ }
164+ }
165+
155166 /**
156167 * Returns a map from the requested toolchain type Label (after any alias chains) to the {@link
157168 * ToolchainTypeInfo} provider.
158169 */
159- private static ImmutableMap <Label , ToolchainTypeInfo > loadToolchainTypes (
170+ private static ImmutableMap <Label , ToolchainTypeInfo > loadToolchainTypeInfos (
160171 Environment environment ,
161172 BuildConfigurationValue configuration ,
162173 ImmutableSet <Label > toolchainTypeLabels )
@@ -182,16 +193,17 @@ private static ImmutableMap<Label, ToolchainTypeInfo> loadToolchainTypes(
182193 /**
183194 * Returns a map from the actual post-alias Label to the ToolchainTypeRequirement for that type.
184195 */
185- private ImmutableSet <ToolchainTypeRequirement > loadToolchainTypeRequirements (
196+ private ImmutableSet <ToolchainType > loadToolchainTypes (
186197 ImmutableMap <Label , ToolchainTypeInfo > resolvedToolchainTypeInfos ,
187198 ImmutableSet <ToolchainTypeRequirement > toolchainTypes ) {
188- ImmutableSet .Builder <ToolchainTypeRequirement > resolved = new ImmutableSet .Builder <>();
199+ ImmutableSet .Builder <ToolchainType > resolved = new ImmutableSet .Builder <>();
189200
190201 for (ToolchainTypeRequirement toolchainTypeRequirement : toolchainTypes ) {
191202 // Find the actual Label.
192203 Label toolchainTypeLabel = toolchainTypeRequirement .toolchainType ();
193- if (resolvedToolchainTypeInfos .containsKey (toolchainTypeLabel )) {
194- toolchainTypeLabel = resolvedToolchainTypeInfos .get (toolchainTypeLabel ).typeLabel ();
204+ ToolchainTypeInfo toolchainTypeInfo = resolvedToolchainTypeInfos .get (toolchainTypeLabel );
205+ if (toolchainTypeInfo != null ) {
206+ toolchainTypeLabel = toolchainTypeInfo .typeLabel ();
195207 }
196208
197209 // If the labels don't match, re-build the TTR.
@@ -200,7 +212,7 @@ private ImmutableSet<ToolchainTypeRequirement> loadToolchainTypeRequirements(
200212 toolchainTypeRequirement .toBuilder ().toolchainType (toolchainTypeLabel ).build ();
201213 }
202214
203- resolved .add (toolchainTypeRequirement );
215+ resolved .add (ToolchainType . create ( toolchainTypeRequirement , toolchainTypeInfo ) );
204216 }
205217 return resolved .build ();
206218 }
@@ -388,7 +400,7 @@ private static boolean filterPlatform(
388400 private static void determineToolchainImplementations (
389401 Environment environment ,
390402 BuildConfigurationKey configurationKey ,
391- ImmutableSet <ToolchainTypeRequirement > toolchainTypes ,
403+ ImmutableSet <ToolchainType > toolchainTypes ,
392404 Optional <ConfiguredTargetKey > forcedExecutionPlatform ,
393405 UnloadedToolchainContextImpl .Builder builder ,
394406 PlatformKeys platformKeys ,
@@ -399,43 +411,43 @@ private static void determineToolchainImplementations(
399411
400412 // Find the toolchains for the requested toolchain types.
401413 List <SingleToolchainResolutionKey > registeredToolchainKeys = new ArrayList <>();
402- for (ToolchainTypeRequirement toolchainType : toolchainTypes ) {
414+ for (ToolchainType toolchainType : toolchainTypes ) {
403415 registeredToolchainKeys .add (
404416 SingleToolchainResolutionValue .key (
405417 configurationKey ,
406- toolchainType ,
418+ toolchainType .toolchainTypeRequirement (),
419+ toolchainType .toolchainTypeInfo (),
407420 platformKeys .targetPlatformKey (),
408421 platformKeys .executionPlatformKeys (),
409422 debugTarget ));
410423 }
411424
412- SkyframeIterableResult results =
413- environment .getOrderedValuesAndExceptions (registeredToolchainKeys );
425+ SkyframeLookupResult results = environment .getValuesAndExceptions (registeredToolchainKeys );
414426 boolean valuesMissing = false ;
415427
416428 // Determine the potential set of toolchains.
417429 Table <ConfiguredTargetKey , ToolchainTypeInfo , Label > resolvedToolchains =
418430 HashBasedTable .create ();
419431 ImmutableSet .Builder <ToolchainTypeInfo > requiredToolchainTypesBuilder = ImmutableSet .builder ();
420432 List <Label > missingToolchains = new ArrayList <>();
421- while (results .hasNext ()) {
422- try {
423- SingleToolchainResolutionValue singleToolchainResolutionValue =
424- (SingleToolchainResolutionValue )
425- results .nextOrThrow (
426- NoToolchainFoundException .class , InvalidToolchainLabelException .class );
433+ for (SingleToolchainResolutionKey key : registeredToolchainKeys ) {
434+ SingleToolchainResolutionValue singleToolchainResolutionValue =
435+ (SingleToolchainResolutionValue )
436+ results .getOrThrow (key , InvalidToolchainLabelException .class );
427437 if (singleToolchainResolutionValue == null ) {
428438 valuesMissing = true ;
429439 continue ;
430440 }
431441
442+ if (singleToolchainResolutionValue .availableToolchainLabels ().isEmpty ()) {
443+ // Save the missing type and continue looping to check for more.
444+ // TODO(katre): Handle mandatory/optional.
445+ missingToolchains .add (key .toolchainType ().toolchainType ());
446+ } else {
432447 ToolchainTypeInfo requiredToolchainType = singleToolchainResolutionValue .toolchainType ();
433448 requiredToolchainTypesBuilder .add (requiredToolchainType );
434449 resolvedToolchains .putAll (
435450 findPlatformsAndLabels (requiredToolchainType , singleToolchainResolutionValue ));
436- } catch (NoToolchainFoundException e ) {
437- // Save the missing type and continue looping to check for more.
438- missingToolchains .add (e .missingToolchainType ().toolchainType ());
439451 }
440452 }
441453
@@ -458,9 +470,15 @@ private static void determineToolchainImplementations(
458470 platformKeys .executionPlatformKeys (),
459471 resolvedToolchains );
460472
473+ ImmutableSet <ToolchainTypeRequirement > toolchainTypeRequirements =
474+ toolchainTypes .stream ()
475+ .map (ToolchainType ::toolchainTypeRequirement )
476+ .collect (toImmutableSet ());
461477 if (selectedExecutionPlatformKey .isEmpty ()) {
462478 throw new NoMatchingPlatformException (
463- toolchainTypes , platformKeys .executionPlatformKeys (), platformKeys .targetPlatformKey ());
479+ toolchainTypeRequirements ,
480+ platformKeys .executionPlatformKeys (),
481+ platformKeys .targetPlatformKey ());
464482 }
465483
466484 Map <ConfiguredTargetKey , PlatformInfo > platforms =
@@ -471,7 +489,7 @@ private static void determineToolchainImplementations(
471489 throw new ValueMissingException ();
472490 }
473491
474- builder .setToolchainTypes (toolchainTypes );
492+ builder .setToolchainTypes (toolchainTypeRequirements );
475493 builder .setExecutionPlatform (platforms .get (selectedExecutionPlatformKey .get ()));
476494 builder .setTargetPlatform (platforms .get (platformKeys .targetPlatformKey ()));
477495
0 commit comments