Skip to content

Commit

Permalink
Add "toolchains" attribute to Skylark's rule definitions.
Browse files Browse the repository at this point in the history
Part of bazelbuild#2219.

Change-Id: I4d749dd9981fe33f75310acb0ec3927cff6f28fe
  • Loading branch information
katre committed Apr 28, 2017
1 parent 1a832bc commit fe87e51
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
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<SkylarkExportable> 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(SkylarkExportable 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,6 +1125,8 @@ public Attribute.Builder<?> copy(String name) {
*/
private final boolean supportsConstraintChecking;

private final ImmutableList<SkylarkExportable> requiredToolchains;

/**
* Constructs an instance of RuleClass whose name is 'name', attributes
* are 'attributes'. The {@code srcsAllowedFiles} determines which types of
Expand Down Expand Up @@ -1165,6 +1174,7 @@ public Attribute.Builder<?> copy(String name) {
String ruleDefinitionEnvironmentHashCode,
ConfigurationFragmentPolicy configurationFragmentPolicy,
boolean supportsConstraintChecking,
List<SkylarkExportable> requiredToolchains,
Attribute... attributes) {
this.name = name;
this.isSkylark = isSkylark;
Expand Down Expand Up @@ -1193,6 +1203,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 +2013,10 @@ public boolean outputsDefaultExecutable() {
return outputsDefaultExecutable;
}

public ImmutableList<SkylarkExportable> 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 @@ -357,6 +357,17 @@ public ClassObjectConstructor invoke(Location location) {
+ "<br/><br/>"
+ "This should only be used for testing the analysis-time behavior of "
+ "Skylark rules. This flag may be removed in the future."
),
@Param(
name = "toolchains",
type = SkylarkList.class,
generic1 = SkylarkExportable.class,
defaultValue = "[]",
doc =
"<i>(Experimental)</i><br/><br/>"
+ "If set, the set of toolchains this rule requires. Toolchains will be "
+ "found by checking the current platform, and provided to the rule "
+ "implementation via <code>ctx.toolchain</code>."
)
},
useAst = true,
Expand All @@ -376,6 +387,7 @@ public BaseFunction invoke(
SkylarkList fragments,
SkylarkList hostFragments,
Boolean skylarkTestable,
SkylarkList<SkylarkExportable> toolchains,
FuncallExpression ast,
Environment funcallEnv)
throws EvalException, ConversionException {
Expand Down Expand Up @@ -434,6 +446,11 @@ public BaseFunction invoke(
hostFragments.getContents(String.class, "host_fragments"));
builder.setConfiguredTargetFunction(implementation);
builder.setRuleDefinitionEnvironment(funcallEnv);

for (SkylarkExportable toolchain : toolchains) {
builder.addRequiredToolchain(toolchain);
}

return new RuleFunction(builder, type, attributes, ast.getLocation());
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ private static RuleClass newRuleClass(
.setMissingFragmentPolicy(missingFragmentPolicy)
.build(),
supportsConstraintChecking,
ImmutableList.<SkylarkExportable>of(),
attributes);
}

Expand Down

0 comments on commit fe87e51

Please sign in to comment.