Skip to content

Commit

Permalink
Add toolchains data to RuleClass and RuleContext.
Browse files Browse the repository at this point in the history
Also expose both sides to Skylark.
Part of bazelbuild#2219.

Change-Id: I4d749dd9981fe33f75310acb0ec3927cff6f28fe
PiperOrigin-RevId: 156340638
  • Loading branch information
katre authored and dslomov committed May 19, 2017
1 parent 67cea8f commit eca2840
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 132 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ java_library(
":util",
":vfs",
"//src/main/java/com/google/devtools/build/lib/actions",
"//src/main/java/com/google/devtools/build/lib/analysis/platform",
"//src/main/java/com/google/devtools/build/lib/buildeventstream/proto:build_event_stream_java_proto",
"//src/main/java/com/google/devtools/build/lib/causes",
"//src/main/java/com/google/devtools/build/lib/cmdline",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public ImmutableList<TransitiveInfoCollection> getFiles() {
private final ErrorReporter reporter;
private final ImmutableBiMap<String, Class<? extends TransitiveInfoProvider>>
skylarkProviderRegistry;
private final ToolchainContext toolchainContext;

private ActionOwner actionOwner;

Expand Down Expand Up @@ -197,6 +198,8 @@ private RuleContext(
this.skylarkProviderRegistry = builder.skylarkProviderRegistry;
this.hostConfiguration = builder.hostConfiguration;
reporter = builder.reporter;
// TODO(katre): Populate the actual selected toolchains.
this.toolchainContext = new ToolchainContext(null);
}

private ImmutableSet<String> getEnabledFeatures() {
Expand Down Expand Up @@ -1121,6 +1124,10 @@ public String expandSingleMakeVariable(String attrName, String expression) {
}
}

public ToolchainContext getToolchainContext() {
return toolchainContext;
}

private void checkAttribute(String attributeName, Mode mode) {
Attribute attributeDefinition = attributes.getAttributeDefinition(attributeName);
if (attributeDefinition == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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.analysis;

import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
import com.google.devtools.build.lib.packages.ClassObjectConstructor;
import com.google.devtools.build.lib.syntax.SkylarkDict;
import java.util.Map;
import javax.annotation.Nullable;

/** Contains toolchain-related information needed for a {@link RuleContext}. */
public class ToolchainContext {
private final ImmutableMap<ClassObjectConstructor.Key, ToolchainInfo> toolchains;

public ToolchainContext(@Nullable Map<ClassObjectConstructor.Key, ToolchainInfo> toolchains) {
this.toolchains =
toolchains == null
? ImmutableMap.<ClassObjectConstructor.Key, ToolchainInfo>of()
: ImmutableMap.copyOf(toolchains);
}

public SkylarkDict<ClassObjectConstructor.Key, ToolchainInfo> collectToolchains() {
return SkylarkDict.<ClassObjectConstructor.Key, ToolchainInfo>copyOf(null, toolchains);
}
}
44 changes: 29 additions & 15 deletions src/main/java/com/google/devtools/build/lib/packages/RuleClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ public Transition buildTransitionFor(Rule rule) {
private boolean supportsConstraintChecking = true;

private final Map<String, Attribute> attributes = new LinkedHashMap<>();
private final List<ClassObjectConstructor.Key> requiredToolchains = new ArrayList<>();

/**
* Constructs a new {@code RuleClassBuilder} using all attributes from all
Expand Down Expand Up @@ -618,6 +619,7 @@ public RuleClass build(String name) {
ruleDefinitionEnvironmentHashCode,
configurationFragmentPolicy.build(),
supportsConstraintChecking,
requiredToolchains,
attributes.values().toArray(new Attribute[0]));
}

Expand Down Expand Up @@ -998,6 +1000,11 @@ public Builder setOptionReferenceFunctionForConfigSettingOnly(
return this;
}

public Builder addRequiredToolchain(ClassObjectConstructor.Key toolchain) {
this.requiredToolchains.add(toolchain);
return this;
}

/**
* Returns an Attribute.Builder object which contains a replica of the
* same attribute in the parent rule if exists.
Expand Down Expand Up @@ -1118,26 +1125,27 @@ public Attribute.Builder<?> copy(String name) {
*/
private final boolean supportsConstraintChecking;

private final ImmutableList<ClassObjectConstructor.Key> requiredToolchains;

/**
* Constructs an instance of RuleClass whose name is 'name', attributes
* are 'attributes'. The {@code srcsAllowedFiles} determines which types of
* files are allowed as parameters to the "srcs" attribute; rules are always
* allowed. For the "deps" attribute, there are four cases:
* Constructs an instance of RuleClass whose name is 'name', attributes are 'attributes'. The
* {@code srcsAllowedFiles} determines which types of files are allowed as parameters to the
* "srcs" attribute; rules are always allowed. For the "deps" attribute, there are four cases:
*
* <ul>
* <li>if the parameter is a file, it is allowed if its file type is given
* in {@code depsAllowedFiles},
* <li>if the parameter is a rule and the rule class is accepted by
* {@code depsAllowedRules}, then it is allowed,
* <li>if the parameter is a rule and the rule class is not accepted by
* {@code depsAllowedRules}, but accepted by
* {@code depsAllowedRulesWithWarning}, then it is allowed, but
* triggers a warning;
* <li>if the parameter is a file, it is allowed if its file type is given in {@code
* depsAllowedFiles},
* <li>if the parameter is a rule and the rule class is accepted by {@code depsAllowedRules},
* then it is allowed,
* <li>if the parameter is a rule and the rule class is not accepted by {@code
* depsAllowedRules}, but accepted by {@code depsAllowedRulesWithWarning}, then it is
* allowed, but triggers a warning;
* <li>all other parameters trigger an error.
* </ul>
*
* <p>The {@code depsAllowedRules} predicate should have a {@code toString}
* method which returns a plain English enumeration of the allowed rule class
* names, if it does not allow all rule classes.
* <p>The {@code depsAllowedRules} predicate should have a {@code toString} method which returns a
* plain English enumeration of the allowed rule class names, if it does not allow all rule
* classes.
*/
@VisibleForTesting
RuleClass(
Expand Down Expand Up @@ -1165,6 +1173,7 @@ public Attribute.Builder<?> copy(String name) {
String ruleDefinitionEnvironmentHashCode,
ConfigurationFragmentPolicy configurationFragmentPolicy,
boolean supportsConstraintChecking,
List<ClassObjectConstructor.Key> requiredToolchains,
Attribute... attributes) {
this.name = name;
this.isSkylark = isSkylark;
Expand Down Expand Up @@ -1193,6 +1202,7 @@ public Attribute.Builder<?> copy(String name) {
this.outputsDefaultExecutable = outputsDefaultExecutable;
this.configurationFragmentPolicy = configurationFragmentPolicy;
this.supportsConstraintChecking = supportsConstraintChecking;
this.requiredToolchains = ImmutableList.copyOf(requiredToolchains);

// Create the index and collect non-configurable attributes.
int index = 0;
Expand Down Expand Up @@ -2002,6 +2012,10 @@ public boolean outputsDefaultExecutable() {
return outputsDefaultExecutable;
}

public ImmutableList<ClassObjectConstructor.Key> getRequiredToolchains() {
return requiredToolchains;
}

public static boolean isThirdPartyPackage(PackageIdentifier packageIdentifier) {
if (!packageIdentifier.getRepository().isMain()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
package com.google.devtools.build.lib.packages;

import com.google.devtools.build.lib.packages.ClassObjectConstructor.Key;
import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;

/**
* Constructor that can be used to generate toolchains. The key returned by {@link #getKey()} is a
* serializable representation of the constructor and serves to identify toolchains of the same
* type.
*/
public interface ToolchainConstructor {
public interface ToolchainConstructor extends SkylarkValue {

Key getKey();
}
Loading

0 comments on commit eca2840

Please sign in to comment.