aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/analysis/RuleDefinition.java
blob: 1e27956ed206e806849534a1b009723a3d8e9d65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2014 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.analysis;

import com.google.auto.value.AutoValue;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * This class is a common ancestor for every rule object.
 *
 * <p>Implementors are also required to have the {@link Metadata} annotation
 * set.
 */
public interface RuleDefinition {
  /**
   * This method should return a RuleClass object that represents the rule. The usual pattern is
   * that various setter methods are called on the builder object passed in as the argument, then
   * the object that is built by the builder is returned.
   *
   * @param builder A {@link com.google.devtools.build.lib.packages.RuleClass.Builder} object
   *     already preloaded with the attributes of the ancestors specified in the {@link
   *     Metadata} annotation.
   * @param environment The services Blaze provides to rule definitions.
   *
   * @return the {@link RuleClass} representing the rule.
   */
  RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment environment);

  /**
   * Returns metadata for this rule.
   */
  Metadata getMetadata();

  /**
   * Value class that contains the name, type, ancestors of a rule, as well as a reference to the
   * configured target factory.
   */
  @AutoValue
  public abstract static class Metadata {
    /**
     * The name of the rule, as it appears in the BUILD file. If it starts with
     * '$', the rule will be hidden from users and will only be usable from
     * inside Blaze.
     */
    public abstract String name();

    /**
     * The type of the rule. It can be an abstract rule, a normal rule or a test
     * rule. If the rule type is abstract, the configured class must not be set.
     */
    public abstract RuleClassType type();

    /**
     * The {@link RuleConfiguredTargetFactory} class that implements this rule. If the rule is
     * abstract, this must not be set.
     */
    public abstract Class<? extends RuleConfiguredTargetFactory> factoryClass();

    /**
     * The list of other rule classes this rule inherits from.
     */
    public abstract List<Class<? extends RuleDefinition>> ancestors();

    public static Builder builder() {
      return new AutoValue_RuleDefinition_Metadata.Builder()
          .type(RuleClassType.NORMAL)
          .factoryClass(RuleConfiguredTargetFactory.class)
          .ancestors(Collections.<Class<? extends RuleDefinition>>emptyList());
    }

    public static Metadata empty() {
      return builder().build();
    }

    /**
     * Builder class for the Metadata class.
     */
    @AutoValue.Builder
    public abstract static class Builder {
      public abstract Builder name(String s);
      public abstract Builder type(RuleClassType type);
      public abstract Builder factoryClass(Class<? extends RuleConfiguredTargetFactory> factory);
      public abstract Builder ancestors(List<Class<? extends RuleDefinition>> ancestors);
      @SafeVarargs
      public final Builder ancestors(Class<? extends RuleDefinition>... ancstrs) {
        return ancestors(Arrays.asList(ancstrs));
      }
      public abstract Metadata build();
    }
  }
}