diff options
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/analysis/constraints/Environment.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/analysis/constraints/Environment.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/constraints/Environment.java b/src/main/java/com/google/devtools/build/lib/analysis/constraints/Environment.java new file mode 100644 index 0000000000..912ed723fe --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/analysis/constraints/Environment.java @@ -0,0 +1,71 @@ +// Copyright 2015 Google Inc. 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.constraints; + +import com.google.common.collect.ImmutableList; +import com.google.devtools.build.lib.actions.Artifact; +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.collect.nestedset.NestedSetBuilder; +import com.google.devtools.build.lib.collect.nestedset.Order; +import com.google.devtools.build.lib.packages.EnvironmentGroup; +import com.google.devtools.build.lib.packages.Package; +import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory; +import com.google.devtools.build.lib.syntax.Label; + +/** + * Implementation for the environment rule. + */ +public class Environment implements RuleConfiguredTargetFactory { + + @Override + public ConfiguredTarget create(RuleContext ruleContext) { + + // The main analysis work to do here is to simply fill in SupportedEnvironmentsProvider to + // pass the environment itself to depending rules. + // + // This will likely expand when we add support for environments fulfilling other environments. + Label label = ruleContext.getLabel(); + Package pkg = ruleContext.getRule().getPackage(); + + EnvironmentGroup group = null; + for (EnvironmentGroup pkgGroup : pkg.getTargets(EnvironmentGroup.class)) { + if (pkgGroup.getEnvironments().contains(label)) { + group = pkgGroup; + break; + } + } + + if (group == null) { + ruleContext.ruleError("no matching environment group from the same package"); + return null; + } + + return new RuleConfiguredTargetBuilder(ruleContext) + .addProvider(SupportedEnvironmentsProvider.class, + new SupportedEnvironments( + new EnvironmentCollection.Builder().put(group, label).build())) + .addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY) + .add(FileProvider.class, new FileProvider(ruleContext.getLabel(), + NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER))) + .add(FilesToRunProvider.class, new FilesToRunProvider(ruleContext.getLabel(), + ImmutableList.<Artifact>of(), null, null)) + .build(); + } +} |