Skip to content

Commit

Permalink
Add constraint_setting and constraint_value rules, to enable defining
Browse files Browse the repository at this point in the history
platform-related constraints and values.

Part of ongoing work on bazelbuild#2219.
  • Loading branch information
katre committed Feb 24, 2017
1 parent 5dbb23b commit 2726d2d
Show file tree
Hide file tree
Showing 13 changed files with 409 additions and 6 deletions.
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
/.project
/.settings
/WORKSPACE.user.bzl
/bazel-bazel
/bazel-bin
/bazel-genfiles
/bazel-io_bazel
/bazel-out
/bazel-testlogs
/bazel-*
/bazel.iml
/output/
3 changes: 3 additions & 0 deletions src/main/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ filegroup(
"//src/main/java/com/google/devtools/build/lib/rules/genquery:srcs",
"//src/main/java/com/google/devtools/build/lib/rules/genrule:srcs",
"//src/main/java/com/google/devtools/build/lib/rules/objc:srcs",
"//src/main/java/com/google/devtools/build/lib/rules/platform:srcs",
"//src/main/java/com/google/devtools/build/lib/sandbox:srcs",
"//src/main/java/com/google/devtools/build/lib/ssd:srcs",
"//src/main/java/com/google/devtools/build/lib/standalone:srcs",
Expand Down Expand Up @@ -580,6 +581,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/rules/genquery",
"//src/main/java/com/google/devtools/build/lib/rules/genrule",
"//src/main/java/com/google/devtools/build/lib/rules/objc",
"//src/main/java/com/google/devtools/build/lib/rules/platform",
"//src/main/java/com/google/devtools/build/skyframe",
"//src/main/java/com/google/devtools/common/options",
"//src/main/protobuf:crosstool_config_java_proto",
Expand Down Expand Up @@ -1128,6 +1130,7 @@ filegroup(
"//src/main/java/com/google/devtools/build/lib/rules/genquery:srcs",
"//src/main/java/com/google/devtools/build/lib/rules/genrule:srcs",
"//src/main/java/com/google/devtools/build/lib/rules/objc:srcs",
"//src/main/java/com/google/devtools/build/lib/rules/platform:srcs",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@
import com.google.devtools.build.lib.rules.objc.ObjcRuleClasses;
import com.google.devtools.build.lib.rules.objc.ObjcXcodeprojRule;
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.proto.BazelProtoLibraryRule;
import com.google.devtools.build.lib.rules.proto.ProtoConfiguration;
import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainRule;
Expand Down Expand Up @@ -273,6 +275,7 @@ public static void setup(ConfiguredRuleClassProvider.Builder builder) {
CORE_RULES.init(builder);
CORE_WORKSPACE_RULES.init(builder);
BASIC_RULES.init(builder);
PLATFORM_RULES.init(builder);
PROTO_RULES.init(builder);
SH_RULES.init(builder);
CPP_RULES.init(builder);
Expand Down Expand Up @@ -334,6 +337,20 @@ public ImmutableList<RuleSet> requires() {
}
};

public static final RuleSet PLATFORM_RULES =
new RuleSet() {
@Override
public void init(Builder builder) {
builder.addRuleDefinition(new ConstraintSettingRule());
builder.addRuleDefinition(new ConstraintValueRule());
}

@Override
public ImmutableList<RuleSet> requires() {
return ImmutableList.of(CORE_RULES);
}
};

public static final RuleSet BASIC_RULES =
new RuleSet() {
@Override
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/google/devtools/build/lib/rules/platform/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Description:
# Rules to define Platforms and related Constraints.

package(
default_visibility = ["//src:__subpackages__"],
)

java_library(
name = "platform",
srcs = glob(["*.java"]),
deps = [
"//src/main/java/com/google/devtools/build/lib:build-base",
"//src/main/java/com/google/devtools/build/lib:concurrent",
"//src/main/java/com/google/devtools/build/lib:packages-internal",
"//src/main/java/com/google/devtools/build/lib:util",
"//third_party:auto_value",
"//third_party:guava",
],
)

filegroup(
name = "srcs",
testonly = 0, # All srcs should be not test only, overwrite package default.
srcs = glob(["**"]),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.FileProvider;
import com.google.devtools.build.lib.analysis.FilesToRunProvider;
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.rules.RuleConfiguredTargetFactory;

/**
* Defines a category of constraint that can be fulfilled by a constraint_value rule in a platform
* definition.
*/
public class ConstraintSetting implements RuleConfiguredTargetFactory {

@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException {

return new RuleConfiguredTargetBuilder(ruleContext)
.addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY)
.addProvider(FileProvider.class, FileProvider.EMPTY)
.addProvider(FilesToRunProvider.class, FilesToRunProvider.EMPTY)
.addProvider(
ConstraintSettingProvider.class,
ConstraintSettingProvider.create(ruleContext.getLabel()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.devtools.build.lib.analysis.TransitiveInfoProvider;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;

/** Provider for a platform constraint setting that is available to be fulfilled. */
@AutoValue
@Immutable
public abstract class ConstraintSettingProvider implements TransitiveInfoProvider {
public abstract Label constraint();

public static ConstraintSettingProvider create(Label constraint) {
return new AutoValue_ConstraintSettingProvider(constraint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 static com.google.devtools.build.lib.packages.Attribute.attr;

import com.google.common.collect.ImmutableList;
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.packages.RuleClass;
import com.google.devtools.build.lib.syntax.Type;

/** Rule definition for {@link ConstraintSetting}. */
public class ConstraintSettingRule implements RuleDefinition {
public static final String RULE_NAME = "constraint_setting";

@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
return builder
.override(
attr("tags", Type.STRING_LIST)
// No need to show up in ":all", etc. target patterns.
.value(ImmutableList.of("manual"))
.nonconfigurable("low-level attribute, used in platform configuration"))
.exemptFromConstraintChecking("this rule *defines* a constraint")
.build();
}

@Override
public RuleDefinition.Metadata getMetadata() {
return RuleDefinition.Metadata.builder()
.name(RULE_NAME)
.ancestors(BaseRuleClasses.RuleBase.class)
.factoryClass(ConstraintSetting.class)
.build();
}
}
/*<!-- #BLAZE_RULE (NAME = constraint_setting, TYPE = OTHER, FAMILY = Platform)[GENERIC_RULE] -->
<p>This rule defines a type of constraint that can be used to define an execution platform.</p>
<!-- #END_BLAZE_RULE -->*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.FileProvider;
import com.google.devtools.build.lib.analysis.FilesToRunProvider;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
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.rules.RuleConfiguredTargetFactory;

/** Defines a potential value of a constraint. */
public class ConstraintValue implements RuleConfiguredTargetFactory {

@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException {

ConstraintSettingProvider constraint =
ruleContext.getPrerequisite(
ConstraintValueRule.PARENT_CONSTRAINT_ATTR,
Mode.DONT_CHECK,
ConstraintSettingProvider.class);

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()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.devtools.build.lib.analysis.TransitiveInfoProvider;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;

/** Provider for a platform constraint value that fulfills a {@link ConstraintSettingProvider}. */
@AutoValue
@Immutable
public abstract class ConstraintValueProvider implements TransitiveInfoProvider {
public abstract ConstraintSettingProvider constraint();

public abstract Label value();

public static ConstraintValueProvider create(ConstraintSettingProvider constraint, Label value) {
return new AutoValue_ConstraintValueProvider(constraint, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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 static com.google.devtools.build.lib.packages.Attribute.attr;

import com.google.common.collect.ImmutableList;
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;
import com.google.devtools.build.lib.util.FileTypeSet;

/** Rule definition for {@link ConstraintValue}. */
public class ConstraintValueRule implements RuleDefinition {
public static final String RULE_NAME = "constraint_value";
public static final String PARENT_CONSTRAINT_ATTR = "constraint";

@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
return builder
.override(
attr("tags", Type.STRING_LIST)
// No need to show up in ":all", etc. target patterns.
.value(ImmutableList.of("manual"))
.nonconfigurable("low-level attribute, used in platform configuration"))

/* <!-- #BLAZE_RULE(constraint_value).ATTRIBUTE(constraint) -->
The constraint_setting rule this value is applied to.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(
attr(PARENT_CONSTRAINT_ATTR, BuildType.LABEL)
.mandatory()
.allowedFileTypes(FileTypeSet.NO_FILE)
.mandatoryNativeProviders(
ImmutableList.<Class<? extends TransitiveInfoProvider>>of(
ConstraintSettingProvider.class)))
.exemptFromConstraintChecking("this rule *defines* a constraint")
.build();
}

@Override
public Metadata getMetadata() {
return Metadata.builder()
.name(RULE_NAME)
.ancestors(BaseRuleClasses.RuleBase.class)
.factoryClass(ConstraintValue.class)
.build();
}
}
/*<!-- #BLAZE_RULE (NAME = constraint_value, TYPE = OTHER, FAMILY = Platform)[GENERIC_RULE] -->
<p>This rule defines a specific value of a constraint, which can be used to define execution platforms.
<!-- #END_BLAZE_RULE -->*/
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ filegroup(
"//src/test/java/com/google/devtools/build/lib/skylark:srcs",
"//src/test/java/com/google/devtools/build/lib/skyframe:srcs",
"//src/test/java/com/google/devtools/build/lib/rules/android:srcs",
"//src/test/java/com/google/devtools/build/lib/rules/platform:srcs",
"//src/test/java/com/google/devtools/build/lib/rules/repository:srcs",
"//src/test/java/com/google/devtools/build/lib/bazel/repository:srcs",
"//src/test/java/com/google/devtools/build/lib/buildeventstream/transports:srcs",
Expand Down
Loading

0 comments on commit 2726d2d

Please sign in to comment.