blob: 2096b2bd0ae059639170116ee43884e1941603d5 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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
Carmi Grushko97f81482016-04-08 14:14:56 +000017import com.google.auto.value.AutoValue;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.devtools.build.lib.packages.RuleClass;
Googler58505032015-03-19 16:12:34 +000019import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
20import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
21
22import java.util.Arrays;
23import java.util.Collections;
24import java.util.List;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025
26/**
27 * This class is a common ancestor for every rule object.
28 *
Ulf Adams3ab82f72015-09-04 12:10:53 +000029 * <p>Implementors are also required to have the {@link Metadata} annotation
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010030 * set.
31 */
32public interface RuleDefinition {
33 /**
34 * This method should return a RuleClass object that represents the rule. The usual pattern is
35 * that various setter methods are called on the builder object passed in as the argument, then
36 * the object that is built by the builder is returned.
37 *
38 * @param builder A {@link com.google.devtools.build.lib.packages.RuleClass.Builder} object
39 * already preloaded with the attributes of the ancestors specified in the {@link
Ulf Adams3ab82f72015-09-04 12:10:53 +000040 * Metadata} annotation.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041 * @param environment The services Blaze provides to rule definitions.
42 *
43 * @return the {@link RuleClass} representing the rule.
44 */
45 RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment environment);
Googler58505032015-03-19 16:12:34 +000046
47 /**
48 * Returns metadata for this rule.
49 */
50 Metadata getMetadata();
51
52 /**
53 * Value class that contains the name, type, ancestors of a rule, as well as a reference to the
54 * configured target factory.
55 */
Carmi Grushko97f81482016-04-08 14:14:56 +000056 @AutoValue
Googler58505032015-03-19 16:12:34 +000057 public abstract static class Metadata {
58 /**
59 * The name of the rule, as it appears in the BUILD file. If it starts with
60 * '$', the rule will be hidden from users and will only be usable from
61 * inside Blaze.
62 */
63 public abstract String name();
64
65 /**
66 * The type of the rule. It can be an abstract rule, a normal rule or a test
67 * rule. If the rule type is abstract, the configured class must not be set.
68 */
69 public abstract RuleClassType type();
70
71 /**
72 * The {@link RuleConfiguredTargetFactory} class that implements this rule. If the rule is
73 * abstract, this must not be set.
74 */
75 public abstract Class<? extends RuleConfiguredTargetFactory> factoryClass();
76
77 /**
78 * The list of other rule classes this rule inherits from.
79 */
80 public abstract List<Class<? extends RuleDefinition>> ancestors();
81
82 public static Builder builder() {
Carmi Grushko97f81482016-04-08 14:14:56 +000083 return new AutoValue_RuleDefinition_Metadata.Builder()
Googler58505032015-03-19 16:12:34 +000084 .type(RuleClassType.NORMAL)
85 .factoryClass(RuleConfiguredTargetFactory.class)
86 .ancestors(Collections.<Class<? extends RuleDefinition>>emptyList());
87 }
88
89 public static Metadata empty() {
90 return builder().build();
91 }
92
93 /**
94 * Builder class for the Metadata class.
95 */
Carmi Grushko97f81482016-04-08 14:14:56 +000096 @AutoValue.Builder
Googler58505032015-03-19 16:12:34 +000097 public abstract static class Builder {
98 public abstract Builder name(String s);
99 public abstract Builder type(RuleClassType type);
100 public abstract Builder factoryClass(Class<? extends RuleConfiguredTargetFactory> factory);
101 public abstract Builder ancestors(List<Class<? extends RuleDefinition>> ancestors);
Ulf Adams08207112015-04-23 15:49:34 +0000102 @SafeVarargs
103 public final Builder ancestors(Class<? extends RuleDefinition>... ancstrs) {
Googler58505032015-03-19 16:12:34 +0000104 return ancestors(Arrays.asList(ancstrs));
105 }
106 public abstract Metadata build();
107 }
108 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100109}