blob: 6d7cf9fb6edff06a06c75046b1c46f358aa19f45 [file] [log] [blame]
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001// 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
15package com.google.devtools.build.lib.analysis;
16
17import com.google.devtools.build.lib.packages.RuleClass;
Googler58505032015-03-19 16:12:34 +000018import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
19import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
20
21import java.util.Arrays;
22import java.util.Collections;
23import java.util.List;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024
25/**
26 * This class is a common ancestor for every rule object.
27 *
Ulf Adams3ab82f72015-09-04 12:10:53 +000028 * <p>Implementors are also required to have the {@link Metadata} annotation
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029 * set.
30 */
31public 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 Adams3ab82f72015-09-04 12:10:53 +000039 * Metadata} annotation.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040 * @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);
Googler58505032015-03-19 16:12:34 +000045
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 Nienhuysabcdb672015-04-07 18:25:18 +000081 return new AutoValueRuleDefinitionMetadata.Builder()
Googler58505032015-03-19 16:12:34 +000082 .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 Adams08207112015-04-23 15:49:34 +000099 @SafeVarargs
100 public final Builder ancestors(Class<? extends RuleDefinition>... ancstrs) {
Googler58505032015-03-19 16:12:34 +0000101 return ancestors(Arrays.asList(ancstrs));
102 }
103 public abstract Metadata build();
104 }
105 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100106}