forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add skyfunction to return all registered toolchain labels.
Part of bazelbuild#2219. Change-Id: I7293fd13bd8e0931f92afd051e18a9e7ce63762d
- Loading branch information
Showing
6 changed files
with
368 additions
and
0 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
src/main/java/com/google/devtools/build/lib/skyframe/RegisteredToolchainsFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.google.devtools.build.lib.skyframe; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Lists; | ||
import com.google.devtools.build.lib.analysis.ConfiguredTarget; | ||
import com.google.devtools.build.lib.analysis.config.BuildConfiguration; | ||
import com.google.devtools.build.lib.analysis.platform.DeclaredToolchainInfo; | ||
import com.google.devtools.build.lib.analysis.platform.ToolchainInfo; | ||
import com.google.devtools.build.lib.cmdline.Label; | ||
import com.google.devtools.build.lib.rules.ExternalPackageUtil; | ||
import com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.ConfiguredValueCreationException; | ||
import com.google.devtools.build.lib.syntax.EvalException; | ||
import com.google.devtools.build.skyframe.LegacySkyKey; | ||
import com.google.devtools.build.skyframe.SkyFunction; | ||
import com.google.devtools.build.skyframe.SkyFunctionException; | ||
import com.google.devtools.build.skyframe.SkyFunctionException.Transience; | ||
import com.google.devtools.build.skyframe.SkyKey; | ||
import com.google.devtools.build.skyframe.SkyValue; | ||
import com.google.devtools.build.skyframe.ValueOrException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* {@link SkyFunction} that returns all registered toolchains available for toolchain resolution. | ||
*/ | ||
public class RegisteredToolchainsFunction implements SkyFunction { | ||
|
||
@Nullable | ||
@Override | ||
public SkyValue compute(SkyKey skyKey, Environment env) | ||
throws SkyFunctionException, InterruptedException { | ||
|
||
BuildConfiguration configuration = (BuildConfiguration) skyKey.argument(); | ||
|
||
// Get the registered toolchains. | ||
List<Label> registeredToolchainLabels = ExternalPackageUtil.getRegisteredToolchainLabels(env); | ||
if (registeredToolchainLabels == null) { | ||
return null; | ||
} | ||
|
||
// Load the configured target for each, and get the declared toolchain providers. | ||
ImmutableList<DeclaredToolchainInfo> registeredToolchains = | ||
configureRegisteredToolchains(env, configuration, registeredToolchainLabels); | ||
if (env.valuesMissing()) { | ||
return null; | ||
} | ||
|
||
return RegisteredToolchainsValue.create(registeredToolchains); | ||
} | ||
|
||
private ImmutableList<DeclaredToolchainInfo> configureRegisteredToolchains( | ||
Environment env, final BuildConfiguration configuration, List<Label> labels) | ||
throws InterruptedException, RegisteredToolchainsFunctionException { | ||
List<SkyKey> keys = | ||
Lists.transform( | ||
labels, | ||
new Function<Label, SkyKey>() { | ||
@Override | ||
public SkyKey apply(Label label) { | ||
return LegacySkyKey.create( | ||
SkyFunctions.CONFIGURED_TARGET, new ConfiguredTargetKey(label, configuration)); | ||
} | ||
}); | ||
|
||
Map<SkyKey, ValueOrException<ConfiguredValueCreationException>> values = | ||
env.getValuesOrThrow(keys, ConfiguredValueCreationException.class); | ||
if (env.valuesMissing()) { | ||
return null; | ||
} | ||
ImmutableList.Builder<DeclaredToolchainInfo> toolchains = new ImmutableList.Builder<>(); | ||
for (SkyKey key : keys) { | ||
ConfiguredTargetKey configuredTargetKey = (ConfiguredTargetKey) key.argument(); | ||
Label toolchainLabel = configuredTargetKey.getLabel(); | ||
try { | ||
ConfiguredTarget target = | ||
((ConfiguredTargetValue) values.get(key).get()).getConfiguredTarget(); | ||
DeclaredToolchainInfo toolchainInfo = target.getProvider(DeclaredToolchainInfo.class); | ||
if (toolchainInfo == null) { | ||
throw new RegisteredToolchainsFunctionException( | ||
new InvalidTargetException(toolchainLabel), Transience.PERSISTENT); | ||
} | ||
toolchains.add(toolchainInfo); | ||
} catch (ConfiguredValueCreationException e) { | ||
throw new RegisteredToolchainsFunctionException(e, Transience.PERSISTENT); | ||
} | ||
} | ||
return toolchains.build(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String extractTag(SkyKey skyKey) { | ||
return null; | ||
} | ||
|
||
/** | ||
* Used to indicate that the given {@link Label} represents a {@link ConfiguredTarget} which is | ||
* not a valid {@link DeclaredToolchainInfo} provider. | ||
*/ | ||
public static final class InvalidTargetException extends Exception { | ||
|
||
private final Label invalidLabel; | ||
|
||
public InvalidTargetException(Label invalidLabel) { | ||
super(String.format("target '%s' does not provide a toolchain", invalidLabel)); | ||
this.invalidLabel = invalidLabel; | ||
} | ||
|
||
public Label getInvalidLabel() { | ||
return invalidLabel; | ||
} | ||
} | ||
|
||
public static class RegisteredToolchainsFunctionException extends SkyFunctionException { | ||
|
||
public RegisteredToolchainsFunctionException(EvalException cause, Transience transience) { | ||
super(cause, transience); | ||
} | ||
|
||
public RegisteredToolchainsFunctionException( | ||
InvalidTargetException cause, Transience transience) { | ||
super(cause, transience); | ||
} | ||
|
||
public RegisteredToolchainsFunctionException( | ||
ConfiguredValueCreationException cause, Transience persistent) { | ||
super(cause, persistent); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/google/devtools/build/lib/skyframe/RegisteredToolchainsValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.google.devtools.build.lib.skyframe; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.devtools.build.lib.analysis.config.BuildConfiguration; | ||
import com.google.devtools.build.lib.analysis.platform.DeclaredToolchainInfo; | ||
import com.google.devtools.build.skyframe.LegacySkyKey; | ||
import com.google.devtools.build.skyframe.SkyKey; | ||
import com.google.devtools.build.skyframe.SkyValue; | ||
|
||
/** | ||
* A value which represents every toolchain known to Bazel and available for toolchain resolution. | ||
*/ | ||
@AutoValue | ||
public abstract class RegisteredToolchainsValue implements SkyValue { | ||
|
||
/** Returns the {@link SkyKey} for {@link RegisteredToolchainsValue}s. */ | ||
public static SkyKey key(BuildConfiguration configuration) { | ||
return LegacySkyKey.create(SkyFunctions.REGISTERED_TOOLCHAINS, configuration); | ||
} | ||
|
||
public static RegisteredToolchainsValue create( | ||
Iterable<DeclaredToolchainInfo> registeredToolchains) { | ||
return new AutoValue_RegisteredToolchainsValue(ImmutableList.copyOf(registeredToolchains)); | ||
} | ||
|
||
public abstract ImmutableList<DeclaredToolchainInfo> registeredToolchains(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.