Skip to content

Commit

Permalink
Adding a SkyFunction to perform toolchain resolution.
Browse files Browse the repository at this point in the history
Part of bazelbuild#2219.

Change-Id: I339009c13639144ca756eb07c520df7d430a64e3
  • Loading branch information
katre committed May 1, 2017
1 parent 7afafd3 commit 79e6574
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public final class SkyFunctions {
public static final SkyFunctionName LOCAL_REPOSITORY_LOOKUP =
SkyFunctionName.create("LOCAL_REPOSITORY_LOOKUP");
public static final SkyFunctionName TOOLCHAINS = SkyFunctionName.create("TOOLCHAINS");
public static final SkyFunctionName TOOLCHAIN_RESOLUTION = SkyFunctionName.create("TOOLCHAIN_RESOLUTION");

public static Predicate<SkyKey> isSkyFunction(final SkyFunctionName functionName) {
return new Predicate<SkyKey>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ private ImmutableMap<SkyFunctionName, SkyFunction> skyFunctions(
new ActionTemplateExpansionFunction(removeActionsAfterEvaluation));
map.put(SkyFunctions.LOCAL_REPOSITORY_LOOKUP, new LocalRepositoryLookupFunction());
map.put(SkyFunctions.TOOLCHAINS, new ToolchainsFunction());
map.put(SkyFunctions.TOOLCHAIN_RESOLUTION, new ToolchainResolutionFunction());
map.putAll(extraSkyFunctions);
return map.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.google.devtools.build.lib.skyframe;

import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
import com.google.devtools.build.lib.packages.NoSuchThingException;
import com.google.devtools.build.lib.skyframe.ToolchainResolutionValue.ToolchainResolutionKey;
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 java.util.List;
import javax.annotation.Nullable;

/** {@link SkyFunction} which performs toolchain resolution for a class of rules. */
public class ToolchainResolutionFunction implements SkyFunction {

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env)
throws SkyFunctionException, InterruptedException {

ToolchainResolutionKey key = (ToolchainResolutionKey) skyKey.argument();

// Get all toolchains.
ToolchainsValue toolchains =
(ToolchainsValue) env.getValue(ToolchainsValue.key(key.configuration()));
if (toolchains == null) {
return null;
}

// Find the right one.
ToolchainInfo toolchain =
resolveConstraints(key.targetPlatform(), key.execPlatform(), toolchains.toolchains());
return ToolchainResolutionValue.create(toolchain);
}

// TODO(katre): Implement real resolution.
private ToolchainInfo resolveConstraints(
PlatformInfo targetPlatform, PlatformInfo execPlatform, List<ToolchainInfo> toolchains)
throws ToolchainResolutionFunctionException {
if (toolchains.isEmpty()) {
throw new ToolchainResolutionFunctionException(new NoToolchainFoundException());
}
return Iterables.getFirst(toolchains, null);
}

@Nullable
@Override
public String extractTag(SkyKey skyKey) {
return null;
}

/** Used to indicate that a toolchain was not found for the current request. */
public static final class NoToolchainFoundException extends NoSuchThingException {
public NoToolchainFoundException() {
super("no matching toolchain found");
}
}

/** Used to indicate errors during the computation of an {@link ToolchainResolutionValue}. */
private static final class ToolchainResolutionFunctionException extends SkyFunctionException {
public ToolchainResolutionFunctionException(NoToolchainFoundException e) {
super(e, Transience.PERSISTENT);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
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 the selected toolchain for a specific target and execution platform. */
@AutoValue
public abstract class ToolchainResolutionValue implements SkyValue {

// A key representing the input data.

public static SkyKey key(
BuildConfiguration configuration, PlatformInfo targetPlatform, PlatformInfo execPlatform) {
return SkyKey.create(
SkyFunctions.TOOLCHAIN_RESOLUTION,
ToolchainResolutionKey.create(configuration, targetPlatform, execPlatform));
}

@AutoValue
public abstract static class ToolchainResolutionKey {
public abstract BuildConfiguration configuration();

public abstract PlatformInfo targetPlatform();

public abstract PlatformInfo execPlatform();
// TODO(katre): toolchain type.

public static ToolchainResolutionKey create(
BuildConfiguration configuration, PlatformInfo targetPlatform, PlatformInfo execPlatform) {
return new AutoValue_ToolchainResolutionValue_ToolchainResolutionKey(
configuration, targetPlatform, execPlatform);
}
}

public static ToolchainResolutionValue create(ToolchainInfo toolchain) {
return new AutoValue_ToolchainResolutionValue(toolchain);
}

public abstract ToolchainInfo toolchain();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// 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 static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.skyframe.EvaluationResultSubjectFactory.assertThatEvaluationResult;

import com.google.common.collect.ImmutableList;
import com.google.common.testing.EqualsTester;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils;
import com.google.devtools.build.lib.skylark.util.SkylarkTestCase;
import com.google.devtools.build.skyframe.EvaluationResult;
import com.google.devtools.build.skyframe.SkyKey;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link ToolchainResolutionValue} and {@link ToolchainResolutionFunction}. */
@RunWith(JUnit4.class)
public class ToolchainResolutionFunctionTest extends SkylarkTestCase {

private ToolchainInfo toolchain1;
private ToolchainInfo toolchain2;

@Before
public void createToolchains() throws Exception {
scratch.file(
"test/toolchain.bzl",
"def _impl(ctx):",
" toolchain = platform_common.toolchain(",
" exec_compatible_with = ctx.attr.exec_compatible_with,",
" target_compatible_with = ctx.attr.target_compatible_with,",
" foo = ctx.attr.foo,",
" bar = ctx.attr.bar,",
" )",
" return [toolchain]",
"test_toolchain = rule(",
" implementation = _impl,",
" attrs = {",
" 'exec_compatible_with': attr.label_list(",
" providers = [platform_common.ConstraintValueInfo]),",
" 'target_compatible_with': attr.label_list(",
" providers = [platform_common.ConstraintValueInfo]),",
" 'foo': attr.string(),",
" 'bar': attr.string(),",
" }",
")");
scratch.file(
"test/BUILD",
"load(':toolchain.bzl', 'test_toolchain')",
"constraint_setting(name = 'os')",
"constraint_value(name = 'linux',",
" constraint_setting = ':os')",
"constraint_value(name = 'mac',",
" constraint_setting = ':os')",
"filegroup(name = 'not_a_toolchain')",
"test_toolchain(",
" name = 'toolchain_1',",
" exec_compatible_with = [",
" ':linux',",
" ],",
" target_compatible_with = [",
" ':mac',",
" ],",
" foo = 'val1a',",
" bar = 'val1b',",
")",
"test_toolchain(",
" name = 'toolchain_2',",
" exec_compatible_with = [",
" ':mac',",
" ],",
" target_compatible_with = [",
" ':linux',",
" ],",
" foo = 'val2a',",
" bar = 'val2b',",
")");

Label toolchain1Label = makeLabel("//test:toolchain_1");
toolchain1 =
(ToolchainInfo) getConfiguredTarget(toolchain1Label).get(ToolchainInfo.SKYLARK_IDENTIFIER);
Label toolchain2Label = makeLabel("//test:toolchain_2");
toolchain2 =
(ToolchainInfo) getConfiguredTarget(toolchain2Label).get(ToolchainInfo.SKYLARK_IDENTIFIER);

// Add the toolchains.
PrecomputedValue.EXTRA_TOOLCHAINS.set(
getSkyframeExecutor().injectable(), ImmutableList.of(toolchain1Label, toolchain2Label));
}

private EvaluationResult<ToolchainResolutionValue> eval(SkyKey key) throws InterruptedException {
return SkyframeExecutorTestUtils.evaluate(
getSkyframeExecutor(), key, /*keepGoing=*/ false, reporter);
}

// TODO(katre): Current toolchain resolution does not actually check the constraints, it just returns the first toolchain available.
@Test
public void testResolution() throws Exception {

SkyKey key =
ToolchainResolutionValue.key(
targetConfig, PlatformInfo.builder().build(), PlatformInfo.builder().build());
EvaluationResult<ToolchainResolutionValue> result = eval(key);

assertThatEvaluationResult(result).hasNoError();

ToolchainResolutionValue toolchainResolutionValue = result.get(key);
assertThat(toolchainResolutionValue.toolchain()).isEqualTo(toolchain1);
}

@Test
public void testResolution_noneFound() throws Exception {
// Clear the toolchains.
PrecomputedValue.EXTRA_TOOLCHAINS.set(
getSkyframeExecutor().injectable(), ImmutableList.<Label>of());

SkyKey key =
ToolchainResolutionValue.key(
targetConfig, PlatformInfo.builder().build(), PlatformInfo.builder().build());
EvaluationResult<ToolchainResolutionValue> result = eval(key);

assertThatEvaluationResult(result)
.hasErrorEntryForKeyThat(key)
.hasExceptionThat()
.hasMessageThat()
.contains("no matching toolchain found");
}

@Test
public void testEqualsAndHashCode() {
new EqualsTester()
.addEqualityGroup(
ToolchainResolutionValue.create(toolchain1),
ToolchainResolutionValue.create(toolchain1))
.addEqualityGroup(
ToolchainResolutionValue.create(toolchain2),
ToolchainResolutionValue.create(toolchain2));
}
}

0 comments on commit 79e6574

Please sign in to comment.