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.
Adding new ToolchainsFunction to report all available toolchains to B…
…azel. Part of bazelbuild#2219. Change-Id: I76f3ebe88bd687c8b9ec47b91743a1cb724ec16f
- Loading branch information
Showing
10 changed files
with
392 additions
and
0 deletions.
There are no files selected for viewing
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
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
143 changes: 143 additions & 0 deletions
143
src/main/java/com/google/devtools/build/lib/skyframe/ToolchainsFunction.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,143 @@ | ||
// Copyright 2017 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
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.TransitiveInfoCollection; | ||
import com.google.devtools.build.lib.analysis.config.BuildConfiguration; | ||
import com.google.devtools.build.lib.analysis.platform.ToolchainInfo; | ||
import com.google.devtools.build.lib.cmdline.Label; | ||
import com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.ConfiguredValueCreationException; | ||
import com.google.devtools.build.lib.util.Preconditions; | ||
import com.google.devtools.build.skyframe.SkyFunction; | ||
import com.google.devtools.build.skyframe.SkyFunctionException; | ||
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 {@link ToolchainInfo} providers available for toolchain | ||
* resolution. | ||
*/ | ||
public class ToolchainsFunction implements SkyFunction { | ||
|
||
/** Ignores the key and returns all known instances of {@link ToolchainInfo}. */ | ||
@Nullable | ||
@Override | ||
public SkyValue compute(SkyKey skyKey, Environment env) | ||
throws SkyFunctionException, InterruptedException { | ||
|
||
BuildConfiguration configuration = (BuildConfiguration) skyKey.argument(); | ||
|
||
// Find toolchains passed in via command-line. | ||
List<Label> extraToolchainLabels = PrecomputedValue.EXTRA_TOOLCHAINS.get(env); | ||
ImmutableList<ToolchainInfo> extraToolchains = | ||
resolveToolchainLabels(env, configuration, extraToolchainLabels); | ||
if (extraToolchains == null) { | ||
return null; | ||
} | ||
|
||
// TODO(katre): Add a way to register toolchains in WORKSPACE and resolve them here. | ||
|
||
return ToolchainsValue.create(extraToolchains); | ||
} | ||
|
||
private ImmutableList<ToolchainInfo> resolveToolchainLabels( | ||
Environment env, final BuildConfiguration configuration, List<Label> labels) | ||
throws InterruptedException, ToolchainsFunctionException { | ||
List<SkyKey> keys = | ||
Lists.transform( | ||
labels, | ||
new Function<Label, SkyKey>() { | ||
@Override | ||
public SkyKey apply(Label label) { | ||
return SkyKey.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<ToolchainInfo> 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(); | ||
ToolchainInfo toolchainInfo = toolchain(target); | ||
if (toolchainInfo == null) { | ||
throw new ToolchainsFunctionException(new InvalidTargetException(toolchainLabel)); | ||
} | ||
toolchains.add(toolchainInfo); | ||
} catch (ConfiguredValueCreationException e) { | ||
throw new ToolchainsFunctionException(e); | ||
} | ||
} | ||
return toolchains.build(); | ||
} | ||
|
||
/** Retrieves and casts the provider from the given target. */ | ||
public static ToolchainInfo toolchain(TransitiveInfoCollection target) { | ||
Object provider = target.get(ToolchainInfo.SKYLARK_IDENTIFIER); | ||
if (provider == null) { | ||
return null; | ||
} | ||
Preconditions.checkState(provider instanceof ToolchainInfo); | ||
return (ToolchainInfo) provider; | ||
} | ||
|
||
@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 ToolchainInfo} 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; | ||
} | ||
} | ||
|
||
/** Used to indicate errors during the computation of an {@link ToolchainsValue}. */ | ||
private static final class ToolchainsFunctionException extends SkyFunctionException { | ||
public ToolchainsFunctionException(ConfiguredValueCreationException e) { | ||
super(e, Transience.PERSISTENT); | ||
} | ||
|
||
public ToolchainsFunctionException(InvalidTargetException e) { | ||
super(e, Transience.PERSISTENT); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/google/devtools/build/lib/skyframe/ToolchainsValue.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,39 @@ | ||
// Copyright 2017 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
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.ToolchainInfo; | ||
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 ToolchainsValue implements SkyValue { | ||
|
||
/** Returns the {@link SkyKey} for {@link ToolchainsValue}s. */ | ||
public static SkyKey key(BuildConfiguration configuration) { | ||
return SkyKey.create(SkyFunctions.TOOLCHAINS, configuration); | ||
} | ||
|
||
public static ToolchainsValue create(Iterable<ToolchainInfo> toolchains) { | ||
return new AutoValue_ToolchainsValue(ImmutableList.copyOf(toolchains)); | ||
} | ||
|
||
public abstract ImmutableList<ToolchainInfo> toolchains(); | ||
} |
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.