aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java
blob: ab011ac8b012f71c3f2cb2c36b5579685972437c (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright 2014 Google Inc. 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.cpp;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Functions;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.ActionOwner;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.Root;
import com.google.devtools.build.lib.analysis.AnalysisEnvironment;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.CppCompileAction.DotdFile;
import com.google.devtools.build.lib.rules.cpp.CppCompileAction.IncludeResolver;
import com.google.devtools.build.lib.util.FileType;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * Builder class to construct C++ compile actions.
 */
public class CppCompileActionBuilder {
  public static final UUID GUID = UUID.fromString("cee5db0a-d2ad-4c69-9b81-97c936a29075");

  private final ActionOwner owner;
  private final List<String> features = new ArrayList<>();
  private CcToolchainFeatures.FeatureConfiguration featureConfiguration;
  private CcToolchainFeatures.Variables variables;
  private final Artifact sourceFile;
  private final Label sourceLabel;
  private final NestedSetBuilder<Artifact> mandatoryInputsBuilder;
  private NestedSetBuilder<Artifact> pluginInputsBuilder;
  private Artifact optionalSourceFile;
  private Artifact outputFile;
  private PathFragment tempOutputFile;
  private DotdFile dotdFile;
  private Artifact gcnoFile;
  private final BuildConfiguration configuration;
  private CppCompilationContext context = CppCompilationContext.EMPTY;
  private final List<String> copts = new ArrayList<>();
  private final List<String> pluginOpts = new ArrayList<>();
  private final List<Pattern> nocopts = new ArrayList<>();
  private AnalysisEnvironment analysisEnvironment;
  private ImmutableList<PathFragment> extraSystemIncludePrefixes = ImmutableList.of();
  private String fdoBuildStamp;
  private boolean usePic; 
  private IncludeResolver includeResolver = CppCompileAction.VOID_INCLUDE_RESOLVER;
  private UUID actionClassId = GUID;
  private Class<? extends CppCompileActionContext> actionContext;
  private CppConfiguration cppConfiguration;
  private ImmutableMap<Artifact, IncludeScannable> lipoScannableMap;
  private RuleContext ruleContext = null;
  // New fields need to be added to the copy constructor.

  /**
   * Creates a builder from a rule. This also uses the configuration and
   * artifact factory from the rule.
   */
  public CppCompileActionBuilder(RuleContext ruleContext, Artifact sourceFile, Label sourceLabel) {
    this.owner = ruleContext.getActionOwner();
    this.actionContext = CppCompileActionContext.class;
    this.cppConfiguration = ruleContext.getFragment(CppConfiguration.class);
    this.analysisEnvironment = ruleContext.getAnalysisEnvironment();
    this.sourceFile = sourceFile;
    this.sourceLabel = sourceLabel;
    this.configuration = ruleContext.getConfiguration();
    this.mandatoryInputsBuilder = NestedSetBuilder.stableOrder();
    this.pluginInputsBuilder = NestedSetBuilder.stableOrder();
    this.lipoScannableMap = getLipoScannableMap(ruleContext);
    this.ruleContext = ruleContext;

    features.addAll(ruleContext.getFeatures());
  }

  private static ImmutableMap<Artifact, IncludeScannable> getLipoScannableMap(
      RuleContext ruleContext) {
    if (!ruleContext.getFragment(CppConfiguration.class).isLipoOptimization()) {
      return null;
    }

    LipoContextProvider provider = ruleContext.getPrerequisite(
        ":lipo_context_collector", Mode.DONT_CHECK, LipoContextProvider.class);
    return provider.getIncludeScannables();
  }

  /**
   * Creates a builder for an owner that is not required to be rule.
   * 
   * <p>If errors are found when creating the {@code CppCompileAction}, builders constructed
   * this way will throw a runtime exception.
   */
  @VisibleForTesting
  public CppCompileActionBuilder(
      ActionOwner owner, AnalysisEnvironment analysisEnvironment, Artifact sourceFile,
      Label sourceLabel, BuildConfiguration configuration) {
    this.owner = owner;
    this.actionContext = CppCompileActionContext.class;
    this.cppConfiguration = configuration.getFragment(CppConfiguration.class);
    this.analysisEnvironment = analysisEnvironment;
    this.sourceFile = sourceFile;
    this.sourceLabel = sourceLabel;
    this.configuration = configuration;
    this.mandatoryInputsBuilder = NestedSetBuilder.stableOrder();
    this.pluginInputsBuilder = NestedSetBuilder.stableOrder();
    this.lipoScannableMap = ImmutableMap.of();
  }

  /**
   * Creates a builder that is a copy of another builder.
   */
  public CppCompileActionBuilder(CppCompileActionBuilder other) {
    this.owner = other.owner;
    this.features.addAll(other.features);
    this.featureConfiguration = other.featureConfiguration;
    this.sourceFile = other.sourceFile;
    this.sourceLabel = other.sourceLabel;
    this.mandatoryInputsBuilder = NestedSetBuilder.<Artifact>stableOrder()
        .addTransitive(other.mandatoryInputsBuilder.build());
    this.pluginInputsBuilder = NestedSetBuilder.<Artifact>stableOrder()
        .addTransitive(other.pluginInputsBuilder.build());
    this.optionalSourceFile = other.optionalSourceFile;
    this.outputFile = other.outputFile;
    this.tempOutputFile = other.tempOutputFile;
    this.dotdFile = other.dotdFile;
    this.gcnoFile = other.gcnoFile;
    this.configuration = other.configuration;
    this.context = other.context;
    this.copts.addAll(other.copts);
    this.pluginOpts.addAll(other.pluginOpts);
    this.nocopts.addAll(other.nocopts);
    this.analysisEnvironment = other.analysisEnvironment;
    this.extraSystemIncludePrefixes = ImmutableList.copyOf(other.extraSystemIncludePrefixes);
    this.includeResolver = other.includeResolver;
    this.actionClassId = other.actionClassId;
    this.actionContext = other.actionContext;
    this.cppConfiguration = other.cppConfiguration;
    this.fdoBuildStamp = other.fdoBuildStamp;
    this.usePic = other.usePic;
    this.lipoScannableMap = other.lipoScannableMap;
    this.ruleContext = other.ruleContext;
  }

  public PathFragment getTempOutputFile() {
    return tempOutputFile;
  }

  public Artifact getSourceFile() {
    return sourceFile;
  }

  public CppCompilationContext getContext() {
    return context;
  }

  public NestedSet<Artifact> getMandatoryInputs() {
    return mandatoryInputsBuilder.build();
  }

  /**
   * Returns the .dwo output file that matches the specified .o output file. If Fission mode
   * isn't enabled for this build, this is null (we don't produce .dwo files in that case).
   */
  private static Artifact getDwoFile(RuleContext ruleContext, Artifact outputFile,
      CppConfiguration cppConfiguration) {

    // Only create .dwo's for .o compilations (i.e. not .ii or .S).
    boolean isObjectOutput = CppFileTypes.OBJECT_FILE.matches(outputFile.getExecPath())
        || CppFileTypes.PIC_OBJECT_FILE.matches(outputFile.getExecPath());

    // Note configurations can be null for tests.
    if (cppConfiguration != null && cppConfiguration.useFission() && isObjectOutput) {
      return ruleContext.getRelatedArtifact(outputFile.getRootRelativePath(), ".dwo");
    } else {
      return null;
    }
  }

  private static Predicate<String> getNocoptPredicate(Collection<Pattern> patterns) {
    final ImmutableList<Pattern> finalPatterns = ImmutableList.copyOf(patterns);
    if (finalPatterns.isEmpty()) {
      return Predicates.alwaysTrue();
    } else {
      return new Predicate<String>() {
        @Override
        public boolean apply(String option) {
          for (Pattern pattern : finalPatterns) {
            if (pattern.matcher(option).matches()) {
              return false;
            }
          }

          return true;
        }
      };
    }
  }

  private Iterable<IncludeScannable> getLipoScannables(NestedSet<Artifact> realMandatoryInputs) {
    return lipoScannableMap == null ? ImmutableList.<IncludeScannable>of() : Iterables.filter(
        Iterables.transform(
            Iterables.filter(
                FileType.filter(
                    realMandatoryInputs,
                    CppFileTypes.C_SOURCE, CppFileTypes.CPP_SOURCE,
                    CppFileTypes.ASSEMBLER_WITH_C_PREPROCESSOR),
                Predicates.not(Predicates.equalTo(getSourceFile()))),
            Functions.forMap(lipoScannableMap, null)),
        Predicates.notNull());
  }

  /**
   * Builds the Action as configured and returns the to be generated Artifact.
   *
   * <p>This method may be called multiple times to create multiple compile
   * actions (usually after calling some setters to modify the generated
   * action).
   */
  public CppCompileAction build() {
    // Configuration can be null in tests.
    NestedSetBuilder<Artifact> realMandatoryInputsBuilder = NestedSetBuilder.compileOrder();
    realMandatoryInputsBuilder.addTransitive(mandatoryInputsBuilder.build());
    if (tempOutputFile == null && configuration != null
        && !configuration.getFragment(CppConfiguration.class).shouldScanIncludes()) {
      realMandatoryInputsBuilder.addTransitive(context.getDeclaredIncludeSrcs());
    }
    realMandatoryInputsBuilder.addTransitive(context.getAdditionalInputs());
    realMandatoryInputsBuilder.addTransitive(pluginInputsBuilder.build());
    realMandatoryInputsBuilder.add(sourceFile);
    boolean fake = tempOutputFile != null;

    // Copying the collections is needed to make the builder reusable.
    if (fake) {
      return new FakeCppCompileAction(owner, ImmutableList.copyOf(features), featureConfiguration,
          variables, sourceFile, sourceLabel, realMandatoryInputsBuilder.build(), outputFile,
          tempOutputFile, dotdFile, configuration, cppConfiguration, context, actionContext,
          ImmutableList.copyOf(copts), ImmutableList.copyOf(pluginOpts),
          getNocoptPredicate(nocopts), extraSystemIncludePrefixes, fdoBuildStamp, ruleContext,
          usePic);
    } else {
      NestedSet<Artifact> realMandatoryInputs = realMandatoryInputsBuilder.build();

      return new CppCompileAction(owner, ImmutableList.copyOf(features), featureConfiguration,
          variables, sourceFile, sourceLabel, realMandatoryInputs, outputFile, dotdFile,
          gcnoFile, getDwoFile(ruleContext, outputFile, cppConfiguration),
          optionalSourceFile, configuration, cppConfiguration, context,
          actionContext, ImmutableList.copyOf(copts),
          ImmutableList.copyOf(pluginOpts),
          getNocoptPredicate(nocopts),
          extraSystemIncludePrefixes, fdoBuildStamp,
          includeResolver, getLipoScannables(realMandatoryInputs), actionClassId,
          usePic, ruleContext);
    }
  }
  
  /**
   * Sets the feature configuration to be used for the action. 
   */
  public CppCompileActionBuilder setFeatureConfiguration(
      FeatureConfiguration featureConfiguration) {
    this.featureConfiguration = featureConfiguration;
    return this;
  }
  
  /**
   * Sets the feature build variables to be used for the action. 
   */
  public CppCompileActionBuilder setVariables(CcToolchainFeatures.Variables variables) {
    this.variables = variables;
    return this;
  }

  public CppCompileActionBuilder setIncludeResolver(IncludeResolver includeResolver) {
    this.includeResolver = includeResolver;
    return this;
  }

  public CppCompileActionBuilder setCppConfiguration(CppConfiguration cppConfiguration) {
    this.cppConfiguration = cppConfiguration;
    return this;
  }

  public CppCompileActionBuilder setActionContext(
      Class<? extends CppCompileActionContext> actionContext) {
    this.actionContext = actionContext;
    return this;
  }

  public CppCompileActionBuilder setActionClassId(UUID uuid) {
    this.actionClassId = uuid;
    return this;
  }

  public CppCompileActionBuilder setExtraSystemIncludePrefixes(
      Collection<PathFragment> extraSystemIncludePrefixes) {
    this.extraSystemIncludePrefixes = ImmutableList.copyOf(extraSystemIncludePrefixes);
    return this;
  }

  public CppCompileActionBuilder addPluginInput(Artifact artifact) {
    pluginInputsBuilder.add(artifact);
    return this;
  }

  public CppCompileActionBuilder clearPluginInputs() {
    pluginInputsBuilder = NestedSetBuilder.stableOrder();
    return this;
  }

  /**
   * Set an optional source file (usually with metadata of the main source file). The optional
   * source file can only be set once, whether via this method or through the constructor
   * {@link #CppCompileActionBuilder(CppCompileActionBuilder)}.
   */
  public CppCompileActionBuilder addOptionalSourceFile(Artifact artifact) {
    Preconditions.checkState(optionalSourceFile == null, "%s %s", optionalSourceFile, artifact);
    optionalSourceFile = artifact;
    return this;
  }

  public CppCompileActionBuilder addMandatoryInputs(Iterable<Artifact> artifacts) {
    mandatoryInputsBuilder.addAll(artifacts);
    return this;
  }

  public CppCompileActionBuilder addTransitiveMandatoryInputs(NestedSet<Artifact> artifacts) {
    mandatoryInputsBuilder.addTransitive(artifacts);
    return this;
  }

  public CppCompileActionBuilder setOutputFile(Artifact outputFile) {
    this.outputFile = outputFile;
    return this;
  }

  /**
   * The temp output file is not an artifact, since it does not appear in the outputs of the
   * action.
   *
   * <p>This is theoretically a problem if that file already existed before, since then Blaze
   * does not delete it before executing the rule, but 1. that only applies for local
   * execution which does not happen very often and 2. it is only a problem if the compiler is
   * affected by the presence of this file, which it should not be.
   */
  public CppCompileActionBuilder setTempOutputFile(PathFragment tempOutputFile) {
    this.tempOutputFile = tempOutputFile;
    return this;
  }

  @VisibleForTesting
  public CppCompileActionBuilder setDotdFileForTesting(Artifact dotdFile) {
    this.dotdFile = new DotdFile(dotdFile);
    return this;
  }

  public CppCompileActionBuilder setDotdFile(PathFragment outputName, String extension) {
    if (CppFileTypes.mustProduceDotdFile(outputName.toString())) {
      if (configuration.getFragment(CppConfiguration.class).getInmemoryDotdFiles()) {
        // Just set the path, no artifact is constructed
        PathFragment file = FileSystemUtils.replaceExtension(outputName, extension);
        Root root = configuration.getBinDirectory();
        dotdFile = new DotdFile(root.getExecPath().getRelative(file));
      } else {
        dotdFile = new DotdFile(ruleContext.getRelatedArtifact(outputName, extension));
      }
    } else {
      dotdFile = null;
    }
    return this;
  }

  public CppCompileActionBuilder setGcnoFile(Artifact gcnoFile) {
    this.gcnoFile = gcnoFile;
    return this;
  }

  public CppCompileActionBuilder addCopt(String copt) {
    copts.add(copt);
    return this;
  }

  public CppCompileActionBuilder addPluginOpt(String opt) {
    pluginOpts.add(opt);
    return this;
  }

  public CppCompileActionBuilder clearPluginOpts() {
    pluginOpts.clear();
    return this;
  }

  public CppCompileActionBuilder addCopts(Iterable<? extends String> copts) {
    Iterables.addAll(this.copts, copts);
    return this;
  }

  public CppCompileActionBuilder addCopts(int position, Iterable<? extends String> copts) {
    this.copts.addAll(position, ImmutableList.copyOf(copts));
    return this;
  }

  public CppCompileActionBuilder addNocopts(Pattern nocopts) {
    this.nocopts.add(nocopts);
    return this;
  }

  public CppCompileActionBuilder setContext(CppCompilationContext context) {
    this.context = context;
    return this;
  }

  public CppCompileActionBuilder setFdoBuildStamp(String fdoBuildStamp) {
    this.fdoBuildStamp = fdoBuildStamp;
    return this;
  }

  /**
   * Sets whether the CompileAction should use pic mode.
   */
  public CppCompileActionBuilder setPicMode(boolean usePic) {
    this.usePic = usePic;
    return this;
  }
}