Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1 | // Copyright 2014 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.analysis; |
| 16 | |
| 17 | import com.google.devtools.build.lib.packages.RuleClass; |
Googler | 5850503 | 2015-03-19 16:12:34 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType; |
| 19 | import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory; |
| 20 | |
| 21 | import java.util.Arrays; |
| 22 | import java.util.Collections; |
| 23 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * This class is a common ancestor for every rule object. |
| 27 | * |
Ulf Adams | 3ab82f7 | 2015-09-04 12:10:53 +0000 | [diff] [blame^] | 28 | * <p>Implementors are also required to have the {@link Metadata} annotation |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | * set. |
| 30 | */ |
| 31 | public interface RuleDefinition { |
| 32 | /** |
| 33 | * This method should return a RuleClass object that represents the rule. The usual pattern is |
| 34 | * that various setter methods are called on the builder object passed in as the argument, then |
| 35 | * the object that is built by the builder is returned. |
| 36 | * |
| 37 | * @param builder A {@link com.google.devtools.build.lib.packages.RuleClass.Builder} object |
| 38 | * already preloaded with the attributes of the ancestors specified in the {@link |
Ulf Adams | 3ab82f7 | 2015-09-04 12:10:53 +0000 | [diff] [blame^] | 39 | * Metadata} annotation. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | * @param environment The services Blaze provides to rule definitions. |
| 41 | * |
| 42 | * @return the {@link RuleClass} representing the rule. |
| 43 | */ |
| 44 | RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment environment); |
Googler | 5850503 | 2015-03-19 16:12:34 +0000 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Returns metadata for this rule. |
| 48 | */ |
| 49 | Metadata getMetadata(); |
| 50 | |
| 51 | /** |
| 52 | * Value class that contains the name, type, ancestors of a rule, as well as a reference to the |
| 53 | * configured target factory. |
| 54 | */ |
| 55 | public abstract static class Metadata { |
| 56 | /** |
| 57 | * The name of the rule, as it appears in the BUILD file. If it starts with |
| 58 | * '$', the rule will be hidden from users and will only be usable from |
| 59 | * inside Blaze. |
| 60 | */ |
| 61 | public abstract String name(); |
| 62 | |
| 63 | /** |
| 64 | * The type of the rule. It can be an abstract rule, a normal rule or a test |
| 65 | * rule. If the rule type is abstract, the configured class must not be set. |
| 66 | */ |
| 67 | public abstract RuleClassType type(); |
| 68 | |
| 69 | /** |
| 70 | * The {@link RuleConfiguredTargetFactory} class that implements this rule. If the rule is |
| 71 | * abstract, this must not be set. |
| 72 | */ |
| 73 | public abstract Class<? extends RuleConfiguredTargetFactory> factoryClass(); |
| 74 | |
| 75 | /** |
| 76 | * The list of other rule classes this rule inherits from. |
| 77 | */ |
| 78 | public abstract List<Class<? extends RuleDefinition>> ancestors(); |
| 79 | |
| 80 | public static Builder builder() { |
Han-Wen Nienhuys | abcdb67 | 2015-04-07 18:25:18 +0000 | [diff] [blame] | 81 | return new AutoValueRuleDefinitionMetadata.Builder() |
Googler | 5850503 | 2015-03-19 16:12:34 +0000 | [diff] [blame] | 82 | .type(RuleClassType.NORMAL) |
| 83 | .factoryClass(RuleConfiguredTargetFactory.class) |
| 84 | .ancestors(Collections.<Class<? extends RuleDefinition>>emptyList()); |
| 85 | } |
| 86 | |
| 87 | public static Metadata empty() { |
| 88 | return builder().build(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Builder class for the Metadata class. |
| 93 | */ |
| 94 | public abstract static class Builder { |
| 95 | public abstract Builder name(String s); |
| 96 | public abstract Builder type(RuleClassType type); |
| 97 | public abstract Builder factoryClass(Class<? extends RuleConfiguredTargetFactory> factory); |
| 98 | public abstract Builder ancestors(List<Class<? extends RuleDefinition>> ancestors); |
Ulf Adams | 0820711 | 2015-04-23 15:49:34 +0000 | [diff] [blame] | 99 | @SafeVarargs |
| 100 | public final Builder ancestors(Class<? extends RuleDefinition>... ancstrs) { |
Googler | 5850503 | 2015-03-19 16:12:34 +0000 | [diff] [blame] | 101 | return ancestors(Arrays.asList(ancstrs)); |
| 102 | } |
| 103 | public abstract Metadata build(); |
| 104 | } |
| 105 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 106 | } |