aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/objc/ProtocolBuffers2Support.java
blob: 7e13b359217483ee28ee59d7a1b91afbf97ed881 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2016 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.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.FileWriteAction;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;

/**
 * Support for generating Objective C proto static libraries that registers actions which generate
 * and compile the Objective C protos by using the deprecated ProtocolBuffers2 library and compiler.
 *
 * <p>Methods on this class can be called in any order without impacting the result.
 */
final class ProtocolBuffers2Support {

  private static final String UNIQUE_DIRECTORY_NAME = "_generated_protos";

  private final RuleContext ruleContext;
  private final ProtoAttributes attributes;

  /**
   * Creates a new proto support for the ProtocolBuffers2 library.
   *
   * @param ruleContext context this proto library is constructed in
   */
  public ProtocolBuffers2Support(RuleContext ruleContext) {
    this.ruleContext = ruleContext;
    this.attributes = new ProtoAttributes(ruleContext);
  }

  /**
   * Register the proto generation actions. These actions generate the ObjC/CPP code to be compiled
   * by this rule.
   */
  public ProtocolBuffers2Support registerGenerationActions() throws InterruptedException {
    ruleContext.registerAction(
        FileWriteAction.create(
            ruleContext,
            getProtoInputsFile(),
            getProtoInputsFileContents(
                attributes.filterWellKnownProtos(attributes.getProtoFiles())),
            false));

    ruleContext.registerAction(
        ObjcRuleClasses.spawnOnDarwinActionBuilder()
            .setMnemonic("GenObjcPB2Protos")
            .addInput(attributes.getProtoCompiler())
            .addInputs(attributes.getProtoCompilerSupport())
            .addInput(getProtoInputsFile())
            .addTransitiveInputs(attributes.getProtoFiles())
            .addInputs(attributes.getOptionsFile().asSet())
            .addOutputs(getGeneratedProtoOutputs(getHeaderExtension()))
            .addOutputs(getGeneratedProtoOutputs(getSourceExtension()))
            .setExecutable(PathFragment.create("/usr/bin/python"))
            .addCommandLine(getGenerationCommandLine())
            .build(ruleContext));
    return this;
  }

  /**
   * Registers the actions that will compile the generated code.
   */
  public ProtocolBuffers2Support registerCompilationActions()
      throws RuleErrorException, InterruptedException {
    CompilationSupport compilationSupport =
        new CompilationSupport.Builder()
            .setRuleContext(ruleContext)
            .doNotUseDeps()
            .doNotUsePch()
            .build();

    compilationSupport.registerCompileAndArchiveActions(getCommon());
    return this;
  }

  /** Adds the generated files to the set of files to be output when this rule is built. */
  public ProtocolBuffers2Support addFilesToBuild(NestedSetBuilder<Artifact> filesToBuild)
      throws InterruptedException {
    filesToBuild
        .addAll(getGeneratedProtoOutputs(getHeaderExtension()))
        .addAll(getGeneratedProtoOutputs(getSourceExtension()))
        .addAll(getCompilationArtifacts().getArchive().asSet());
    return this;
  }

  /** Returns the ObjcProvider for this target. */
  public ObjcProvider getObjcProvider() {
    return getCommon().getObjcProvider();
  }

  private String getHeaderExtension() {
    return ".pb" + (attributes.usesObjcHeaderNames() ? "objc.h" : ".h");
  }

  private String getSourceExtension() {
    return ".pb.m";
  }

  private ObjcCommon getCommon() {
    return new ObjcCommon.Builder(ruleContext)
        .setIntermediateArtifacts(new IntermediateArtifacts(ruleContext, ""))
        .setHasModuleMap()
        .setCompilationArtifacts(getCompilationArtifacts())
        .addIncludes(getIncludes())
        .addDepObjcProviders(
            ruleContext.getPrerequisites(
                ObjcRuleClasses.PROTO_LIB_ATTR, Mode.TARGET, ObjcProvider.SKYLARK_CONSTRUCTOR))
        .build();
  }

  private CompilationArtifacts getCompilationArtifacts() {
    Iterable<Artifact> generatedSources = getGeneratedProtoOutputs(getSourceExtension());
    return new CompilationArtifacts.Builder()
        .setIntermediateArtifacts(new IntermediateArtifacts(ruleContext, ""))
        .addAdditionalHdrs(getGeneratedProtoOutputs(getHeaderExtension()))
        .addAdditionalHdrs(generatedSources)
        .addNonArcSrcs(generatedSources)
        .build();
  }

  private Artifact getProtoInputsFile() {
    return ruleContext.getUniqueDirectoryArtifact(
        "_protos", "_proto_input_files", ruleContext.getConfiguration().getGenfilesDirectory());
  }

  private String getProtoInputsFileContents(Iterable<Artifact> outputProtos) {
    // Sort the file names to make the remote action key independent of the precise deps structure.
    // compile_protos.py will sort the input list anyway.
    Iterable<Artifact> sorted = Ordering.natural().immutableSortedCopy(outputProtos);
    return Artifact.joinExecPaths("\n", sorted);
  }

  private CustomCommandLine getGenerationCommandLine() {
    CustomCommandLine.Builder commandLineBuilder =
        new CustomCommandLine.Builder()
            .addExecPath(attributes.getProtoCompiler())
            .add("--input-file-list")
            .addExecPath(getProtoInputsFile())
            .add("--output-dir")
            .addDynamicString(getWorkspaceRelativeOutputDir().getSafePathString())
            .add("--working-dir")
            .add(".");

    if (attributes.getOptionsFile().isPresent()) {
      commandLineBuilder
          .add("--compiler-options-path")
          .addExecPath(attributes.getOptionsFile().get());
    }

    if (attributes.usesObjcHeaderNames()) {
      commandLineBuilder.add("--use-objc-header-names");
    }
    return commandLineBuilder.build();
  }

  public ImmutableSet<PathFragment> getIncludes() {
    ImmutableSet.Builder<PathFragment> searchPathEntriesBuilder =
        new ImmutableSet.Builder<PathFragment>().add(getWorkspaceRelativeOutputDir());

    if (attributes.needsPerProtoIncludes()) {
      PathFragment generatedProtoDir = PathFragment.create(
          getWorkspaceRelativeOutputDir(), ruleContext.getLabel().getPackageFragment());

      searchPathEntriesBuilder
          .add(generatedProtoDir)
          .addAll(
              Iterables.transform(
                  getGeneratedProtoOutputs(getHeaderExtension()),
                  input -> input.getExecPath().getParentDirectory()));
    }

    return searchPathEntriesBuilder.build();
  }

  private PathFragment getWorkspaceRelativeOutputDir() {
    // Generate sources in a package-and-rule-scoped directory; adds both the
    // package-and-rule-scoped directory and the header-containing-directory to the include path
    // of dependers.
    PathFragment rootRelativeOutputDir = ruleContext.getUniqueDirectory(UNIQUE_DIRECTORY_NAME);

    return PathFragment.create(
        ruleContext.getBinOrGenfilesDirectory().getExecPath(), rootRelativeOutputDir);
  }

  private Iterable<Artifact> getGeneratedProtoOutputs(String extension) {
    ImmutableList.Builder<Artifact> builder = new ImmutableList.Builder<>();
    for (Artifact protoFile : attributes.filterWellKnownProtos(attributes.getProtoFiles())) {
      String protoFileName = FileSystemUtils.removeExtension(protoFile.getFilename());
      String generatedOutputName = attributes.getGeneratedProtoFilename(protoFileName, false);

      PathFragment generatedFilePath = PathFragment.create(
          protoFile.getRootRelativePath().getParentDirectory(),
          PathFragment.create(generatedOutputName));

      PathFragment outputFile = FileSystemUtils.appendExtension(generatedFilePath, extension);

      if (outputFile != null) {
        builder.add(
            ruleContext.getUniqueDirectoryArtifact(
                UNIQUE_DIRECTORY_NAME, outputFile, ruleContext.getBinOrGenfilesDirectory()));
      }
    }
    return builder.build();
  }
}