aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/objc/J2ObjcSrcsProvider.java
blob: 4f540793ecb2fba3abe291722a28ca7ac62bf196 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 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.rules.objc;

import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.rules.objc.J2ObjcSource.SourceType;

/**
 * This provider is exported by java_library rules to supply J2ObjC-translated ObjC sources to
 * objc_binary for compilation and linking.
 */
@Immutable
public final class J2ObjcSrcsProvider implements TransitiveInfoProvider {
  private final NestedSet<J2ObjcSource> srcs;
  private final NestedSet<String> entryClasses;
  private final boolean hasProtos;

  /**
   * A builder for J2ObjCSrcsProvider.
   */
  public static class Builder {
    private final NestedSetBuilder<J2ObjcSource> srcsBuilder = NestedSetBuilder.stableOrder();
    private final NestedSetBuilder<String> entryClassesBuilder = NestedSetBuilder.stableOrder();
    private boolean hasProtos = false;

    /**
     * Constructs a new, empty J2ObjCSrcsProvider builder.
     */
    public Builder() {}

    /**
     * Transitively adds the given {@link J2ObjcSrcsProvider}
     * and all its properties to this builder.
     *
     * @param provider the J2ObjcSrcsProvider to add
     * @return this builder
     */
    public Builder addTransitive(J2ObjcSrcsProvider provider) {
      srcsBuilder.addTransitive(provider.getSrcs());
      entryClassesBuilder.addTransitive(provider.getEntryClasses());
      hasProtos |= provider.hasProtos();
      return this;
    }

    /**
     * Transitively adds all the J2ObjcSrcsProviders and all their properties
     * that can be reached through the "deps", "exports" and "runtime_deps" attributes.
     *
     * @param ruleContext the rule context
     * @return this builder
     */
    public Builder addTransitiveJ2ObjcSrcs(RuleContext ruleContext) {
      return addTransitiveJ2ObjcSrcs(ruleContext, "deps")
          .addTransitiveJ2ObjcSrcs(ruleContext, "exports")
          .addTransitiveJ2ObjcSrcs(ruleContext, "runtime_deps");
    }

    /**
     * Transitively adds the J2ObjCSrcsProviders of a given attribute to this Builder.
     *
     * @param ruleContext the rule context
     * @param attribute the attribute to which to add sources
     * @return this builder
     */
    public Builder addTransitiveJ2ObjcSrcs(RuleContext ruleContext, String attribute) {
      if (ruleContext.attributes().has(attribute, BuildType.LABEL_LIST)) {
        for (J2ObjcSrcsProvider provider :
            ruleContext.getPrerequisites(attribute, Mode.TARGET, J2ObjcSrcsProvider.class)) {
          addTransitive(provider);
        }
      }

      return this;
    }

    /**
     * Adds the given {@link J2ObjcSource} to this builder.
     *
     * @param source the source to add
     * @return this builder
     */
    public Builder addSource(J2ObjcSource source) {
      srcsBuilder.add(source);
      hasProtos |= source.getSourceType() == SourceType.PROTO;
      return this;
    }

    /**
     * Adds the given entry classes to this builder. See {@link #getEntryClasses()}.
     *
     * @param entryClasses the entry classes to add
     * @return this builder
     */
    public Builder addEntryClasses(Iterable<String> entryClasses) {
      entryClassesBuilder.addAll(entryClasses);
      return this;
    }

    /**
     * Builds a J2ObjCSrcsProvider from the information in this builder.
     *
     * @return the J2ObjCSrcsProvider to be built
     */
    public J2ObjcSrcsProvider build() {
      return new J2ObjcSrcsProvider(srcsBuilder.build(), entryClassesBuilder.build(), hasProtos);
    }
  }

  /**
   * Constructs a new J2ObjCSrcsProvider that contains all the information
   * that can be transitively reached through the "deps" attribute of the given rule context.
   *
   * @param ruleContext the rule context in which to look for deps
   */
  public static J2ObjcSrcsProvider buildFrom(RuleContext ruleContext) {
    return new Builder().addTransitiveJ2ObjcSrcs(ruleContext).build();
  }

  /**
   * Constructs a {@link J2ObjcSrcsProvider} to supply J2ObjC-translated ObjC sources to
   * objc_binary for compilation and linking.
   *
   * @param srcs a nested set of {@link J2ObjcSource}s containing translated source files
   * @param entryClasses a set of names of Java classes to used as entry point for J2ObjC dead code
   *     analysis. The Java class names should be in canonical format as defined by the Java
   *     Language Specification.
   * @param hasProtos whether the translated files in this provider have J2ObjC proto files
   */
  private J2ObjcSrcsProvider(NestedSet<J2ObjcSource> srcs, NestedSet<String> entryClasses,
      boolean hasProtos) {
    this.srcs = srcs;
    this.entryClasses = entryClasses;
    this.hasProtos = hasProtos;
  }

  public NestedSet<J2ObjcSource> getSrcs() {
    return srcs;
  }

  /**
   * Returns a set of entry classes specified on attribute entry_classes of j2objc_library targets
   * transitively.
   */
  public NestedSet<String> getEntryClasses() {
    return entryClasses;
  }

  /**
   * Returns whether the translated source files in the provider has proto files.
   */
  public boolean hasProtos() {
    return hasProtos;
  }
}