Skip to content

Commit

Permalink
Expose platform-related providers to Skylark.
Browse files Browse the repository at this point in the history
Change-Id: I7615d3e6e33e0c48f18b2506a135f45ce3705a38
PiperOrigin-RevId: 152256914
  • Loading branch information
katre authored and hlopko committed Apr 6, 2017
1 parent 2fcd79a commit 96580a4
Show file tree
Hide file tree
Showing 16 changed files with 492 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
import com.google.devtools.build.lib.rules.objc.XcTestAppProvider;
import com.google.devtools.build.lib.rules.platform.ConstraintSettingRule;
import com.google.devtools.build.lib.rules.platform.ConstraintValueRule;
import com.google.devtools.build.lib.rules.platform.PlatformCommon;
import com.google.devtools.build.lib.rules.platform.PlatformRule;
import com.google.devtools.build.lib.rules.proto.BazelProtoLibraryRule;
import com.google.devtools.build.lib.rules.proto.ProtoConfiguration;
Expand Down Expand Up @@ -353,6 +354,8 @@ public void init(Builder builder) {
builder.addRuleDefinition(new ConstraintSettingRule());
builder.addRuleDefinition(new ConstraintValueRule());
builder.addRuleDefinition(new PlatformRule());

builder.addSkylarkAccessibleTopLevels("platform_common", new PlatformCommon());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ java_library(
deps = [
"//src/main/java/com/google/devtools/build/lib:build-base",
"//src/main/java/com/google/devtools/build/lib:packages",
"//src/main/java/com/google/devtools/build/lib:skylarkinterface",
"//third_party:auto_value",
"//third_party:guava",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.RunfilesProvider;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;

/**
Expand All @@ -37,9 +36,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
.addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY)
.addProvider(FileProvider.class, FileProvider.EMPTY)
.addProvider(FilesToRunProvider.class, FilesToRunProvider.EMPTY)
.addProvider(
ConstraintSettingProvider.class,
ConstraintSettingProvider.create(ruleContext.getLabel()))
.addNativeDeclaredProvider(ConstraintSettingInfo.create(ruleContext.getLabel()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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.rules.platform;

import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.ClassObjectConstructor;
import com.google.devtools.build.lib.packages.NativeClassObjectConstructor;
import com.google.devtools.build.lib.packages.SkylarkClassObject;
import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier;
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
import com.google.devtools.build.lib.util.Preconditions;

/** Provider for a platform constraint setting that is available to be fulfilled. */
@SkylarkModule(
name = "ConstraintSettingInfo",
doc = "A specific constraint setting that may be used to define a platform.",
category = SkylarkModuleCategory.PROVIDER
)
@AutoValue
@Immutable
public abstract class ConstraintSettingInfo extends SkylarkClassObject {

/** Name used in Skylark for accessing this provider. */
static final String SKYLARK_NAME = "ConstraintSettingInfo";

/** Skylark constructor and identifier for this provider. */
static final ClassObjectConstructor SKYLARK_CONSTRUCTOR =
new NativeClassObjectConstructor(SKYLARK_NAME) {};

/** Identifier used to retrieve this provider from rules which export it. */
public static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER =
SkylarkProviderIdentifier.forKey(SKYLARK_CONSTRUCTOR.getKey());

ConstraintSettingInfo() {
super(SKYLARK_CONSTRUCTOR, ImmutableMap.<String, Object>of());
}

@SkylarkCallable(
name = "label",
doc = "The label used to identify this constraint setting.",
structField = true
)
public abstract Label label();

/** Retrieves and casts the provider from the given target. */
public static ConstraintSettingInfo fromTarget(TransitiveInfoCollection target) {
Object provider = target.get(SKYLARK_IDENTIFIER);
if (provider == null) {
return null;
}
Preconditions.checkState(provider instanceof ConstraintSettingInfo);
return (ConstraintSettingInfo) provider;
}

/** Retrieves and casts the providers from the given targets. */
public static Iterable<ConstraintSettingInfo> fromTargets(
Iterable<? extends TransitiveInfoCollection> targets) {
return Iterables.transform(
targets,
new Function<TransitiveInfoCollection, ConstraintSettingInfo>() {
@Override
public ConstraintSettingInfo apply(TransitiveInfoCollection target) {
return fromTarget(target);
}
});
}

/** Returns a new {@link ConstraintSettingInfo} with the given data. */
public static ConstraintSettingInfo create(Label constraintSetting) {
return new AutoValue_ConstraintSettingInfo(constraintSetting);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,16 @@ public class ConstraintValue implements RuleConfiguredTargetFactory {
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException {

ConstraintSettingProvider constraint =
ruleContext.getPrerequisite(
ConstraintValueRule.CONSTRAINT_SETTING_ATTR,
Mode.DONT_CHECK,
ConstraintSettingProvider.class);
ConstraintSettingInfo constraint =
ConstraintSettingInfo.fromTarget(
ruleContext.getPrerequisite(
ConstraintValueRule.CONSTRAINT_SETTING_ATTR, Mode.DONT_CHECK));

return new RuleConfiguredTargetBuilder(ruleContext)
.addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY)
.addProvider(FileProvider.class, FileProvider.EMPTY)
.addProvider(FilesToRunProvider.class, FilesToRunProvider.EMPTY)
.addProvider(
ConstraintValueProvider.class,
ConstraintValueProvider.create(constraint, ruleContext.getLabel()))
.addNativeDeclaredProvider(ConstraintValueInfo.create(constraint, ruleContext.getLabel()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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.rules.platform;

import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.ClassObjectConstructor;
import com.google.devtools.build.lib.packages.NativeClassObjectConstructor;
import com.google.devtools.build.lib.packages.SkylarkClassObject;
import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier;
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
import com.google.devtools.build.lib.util.Preconditions;

/** Provider for a platform constraint value that fulfills a {@link ConstraintSettingInfo}. */
@SkylarkModule(
name = "ConstraintValueProvider",
doc = "A value for a constraint setting that can be used to define a platform.",
category = SkylarkModuleCategory.PROVIDER
)
@AutoValue
@Immutable
public abstract class ConstraintValueInfo extends SkylarkClassObject {

/** Name used in Skylark for accessing this provider. */
static final String SKYLARK_NAME = "ConstraintValueInfo";

/** Skylark constructor and identifier for this provider. */
static final ClassObjectConstructor SKYLARK_CONSTRUCTOR =
new NativeClassObjectConstructor(SKYLARK_NAME) {};

/** Identifier used to retrieve this provider from rules which export it. */
public static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER =
SkylarkProviderIdentifier.forKey(SKYLARK_CONSTRUCTOR.getKey());

ConstraintValueInfo() {
super(SKYLARK_CONSTRUCTOR, ImmutableMap.<String, Object>of());
}

@SkylarkCallable(
name = "constraint",
doc = "The constraint setting that this value fulfills.",
structField = true
)
public abstract ConstraintSettingInfo constraint();

@SkylarkCallable(
name = "label",
doc = "The label used to identify this constraint value.",
structField = true
)
public abstract Label label();

/** Retrieves and casts the provider from the given target. */
public static ConstraintValueInfo fromTarget(TransitiveInfoCollection target) {
Object provider = target.get(SKYLARK_IDENTIFIER);
if (provider == null) {
return null;
}
Preconditions.checkState(provider instanceof ConstraintValueInfo);
return (ConstraintValueInfo) provider;
}

/** Retrieves and casts the providers from the given targets. */
public static Iterable<ConstraintValueInfo> fromTargets(
Iterable<? extends TransitiveInfoCollection> targets) {
return Iterables.transform(
targets,
new Function<TransitiveInfoCollection, ConstraintValueInfo>() {
@Override
public ConstraintValueInfo apply(TransitiveInfoCollection target) {
return fromTarget(target);
}
});
}

/** Returns a new {@link ConstraintValueInfo} with the given data. */
public static ConstraintValueInfo create(ConstraintSettingInfo constraint, Label value) {
return new AutoValue_ConstraintValueInfo(constraint, value);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.syntax.Type;
Expand Down Expand Up @@ -48,9 +47,7 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
.mandatory()
.allowedRuleClasses(ConstraintSettingRule.RULE_NAME)
.allowedFileTypes(FileTypeSet.NO_FILE)
.mandatoryNativeProviders(
ImmutableList.<Class<? extends TransitiveInfoProvider>>of(
ConstraintSettingProvider.class)))
.mandatoryProviders(ImmutableList.of(ConstraintSettingInfo.SKYLARK_IDENTIFIER)))
.removeAttribute("deps")
.removeAttribute("data")
.exemptFromConstraintChecking("this rule *defines* a constraint")
Expand Down
Loading

0 comments on commit 96580a4

Please sign in to comment.