-
Notifications
You must be signed in to change notification settings - Fork 4k
/
CompileBuildVariables.java
517 lines (487 loc) · 18.8 KB
/
CompileBuildVariables.java
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// 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.cpp;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.RuleErrorConsumer;
import com.google.devtools.build.lib.analysis.config.BuildOptions;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.CcToolchainVariables.VariablesExtension;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.List;
import java.util.Map;
import net.starlark.java.eval.EvalException;
/** Enum covering all build variables we create for all various {@link CppCompileAction}. */
public enum CompileBuildVariables {
/** Variable for the path to the source file being compiled. */
SOURCE_FILE("source_file"),
/**
* Variable for all flags coming from copt rule attribute, and from --copt, --cxxopt, or
* --conlyopt options.
*/
USER_COMPILE_FLAGS("user_compile_flags"),
/** Variable for the path to the compilation output file. */
OUTPUT_FILE("output_file"),
/** Variable for the dependency file path */
DEPENDENCY_FILE("dependency_file"),
/** Variable for the module file name. */
MODULE_NAME("module_name"),
/**
* Variable for the collection of include paths.
*
* @see CcCompilationContext#getIncludeDirs().
*/
INCLUDE_PATHS("include_paths"),
/**
* Variable for the collection of quote include paths.
*
* @see CcCompilationContext#getQuoteIncludeDirs().
*/
QUOTE_INCLUDE_PATHS("quote_include_paths"),
/**
* Variable for the collection of system include paths.
*
* @see CcCompilationContext#getSystemIncludeDirs().
*/
SYSTEM_INCLUDE_PATHS("system_include_paths"),
/**
* Variable for the collection of external include paths.
*
* @see CcCompilationContext#getExternalIncludeDirs().
*/
EXTERNAL_INCLUDE_PATHS("external_include_paths"),
/**
* Variable for the collection of framework include paths.
*
* @see CcCompilationContext#getFrameworkIncludeDirs().
*/
FRAMEWORK_PATHS("framework_include_paths"),
/** Variable for the module map file name. */
MODULE_MAP_FILE("module_map_file"),
/** Variable for the dependent module map file name. */
DEPENDENT_MODULE_MAP_FILES("dependent_module_map_files"),
/** Variable for the collection of module files. */
MODULE_FILES("module_files"),
/** Variable for the collection of macros defined for preprocessor. */
PREPROCESSOR_DEFINES("preprocessor_defines"),
/** Variable for the gcov coverage file path. */
GCOV_GCNO_FILE("gcov_gcno_file"),
/**
* Variable for the minimized LTO indexing bitcode file, used by the LTO indexing action. This
* file was generated by CppCompile actions. For efficiency, it contains minimal information that
* is required by the LTO indexing action.
*/
LTO_INDEXING_BITCODE_FILE("lto_indexing_bitcode_file"),
/**
* Variable for the LTO index file, used by the LTO backend action. This file was generated by the
* LTO indexing action.
*/
THINLTO_INDEX("thinlto_index"),
/** Variable for the bitcode file that is input to LTO backend. */
THINLTO_INPUT_BITCODE_FILE("thinlto_input_bitcode_file"),
/** Variable for the object file that is output by LTO backend. */
THINLTO_OUTPUT_OBJECT_FILE("thinlto_output_object_file"),
/** Variable marking fission is used. */
IS_USING_FISSION("is_using_fission"),
/** Variable for the per object debug info file. */
PER_OBJECT_DEBUG_INFO_FILE("per_object_debug_info_file"),
/** Variable present when the output is compiled as position independent. */
PIC("pic"),
/** Variable marking that we are generating preprocessed sources (from --save_temps). */
OUTPUT_PREPROCESS_FILE("output_preprocess_file"),
/** Variable marking that we are generating assembly source (from --save_temps). */
OUTPUT_ASSEMBLY_FILE("output_assembly_file"),
/** Path to the fdo instrument artifact */
FDO_INSTRUMENT_PATH("fdo_instrument_path"),
/** Path to the fdo profile artifact */
FDO_PROFILE_PATH("fdo_profile_path"),
/** Path to the context sensitive fdo instrument artifact */
CS_FDO_INSTRUMENT_PATH("cs_fdo_instrument_path"),
/** Path to the cache prefetch profile artifact */
FDO_PREFETCH_HINTS_PATH("fdo_prefetch_hints_path"),
/** Path to the Propeller Optimize compiler profile artifact */
PROPELLER_OPTIMIZE_CC_PATH("propeller_optimize_cc_path"),
/** Path to the Propeller Optimize linker profile artifact */
PROPELLER_OPTIMIZE_LD_PATH("propeller_optimize_ld_path"),
/** Variable for includes that compiler needs to include into sources. */
INCLUDES("includes");
private final String variableName;
CompileBuildVariables(String variableName) {
this.variableName = variableName;
}
public static CcToolchainVariables setupVariablesOrReportRuleError(
RuleErrorConsumer ruleErrorConsumer,
FeatureConfiguration featureConfiguration,
CcToolchainProvider ccToolchainProvider,
BuildOptions buildOptions,
CppConfiguration cppConfiguration,
String sourceFile,
String outputFile,
String gcnoFile,
boolean isUsingFission,
String dwoFile,
String ltoIndexingFile,
ImmutableList<String> includes,
Iterable<String> userCompileFlags,
CppModuleMap cppModuleMap,
boolean usePic,
String fdoStamp,
String dotdFileExecPath,
ImmutableList<VariablesExtension> variablesExtensions,
ImmutableMap<String, String> additionalBuildVariables,
Iterable<Artifact> directModuleMaps,
NestedSet<PathFragment> includeDirs,
NestedSet<PathFragment> quoteIncludeDirs,
NestedSet<PathFragment> systemIncludeDirs,
NestedSet<PathFragment> frameworkIncludeDirs,
Iterable<String> defines,
Iterable<String> localDefines) {
try {
if (usePic
&& !featureConfiguration.isEnabled(CppRuleClasses.PIC)
&& !featureConfiguration.isEnabled(CppRuleClasses.SUPPORTS_PIC)) {
throw new EvalException(CcCommon.PIC_CONFIGURATION_ERROR);
}
return setupVariables(
featureConfiguration,
ccToolchainProvider.getBuildVariables(buildOptions, cppConfiguration),
sourceFile,
outputFile,
gcnoFile,
isUsingFission,
dwoFile,
ltoIndexingFile,
/* thinLtoIndex= */ null,
/* thinLtoInputBitcodeFile= */ null,
/* thinLtoOutputObjectFile= */ null,
includes,
userCompileFlags,
cppModuleMap,
usePic,
fdoStamp,
dotdFileExecPath,
variablesExtensions,
additionalBuildVariables,
directModuleMaps,
getSafePathStrings(includeDirs),
getSafePathStrings(quoteIncludeDirs),
getSafePathStrings(systemIncludeDirs),
getSafePathStrings(frameworkIncludeDirs),
defines,
localDefines);
} catch (EvalException e) {
ruleErrorConsumer.ruleError(e.getMessage());
return CcToolchainVariables.EMPTY;
}
}
public static CcToolchainVariables setupVariablesOrThrowEvalException(
FeatureConfiguration featureConfiguration,
CcToolchainProvider ccToolchainProvider,
BuildOptions buildOptions,
CppConfiguration cppConfiguration,
String sourceFile,
String outputFile,
String gcnoFile,
boolean isUsingFission,
String dwoFile,
String ltoIndexingFile,
String thinLtoIndex,
String thinLtoInputBitcodeFile,
String thinLtoOutputObjectFile,
ImmutableList<String> includes,
Iterable<String> userCompileFlags,
CppModuleMap cppModuleMap,
boolean usePic,
String fdoStamp,
String dotdFileExecPath,
ImmutableList<VariablesExtension> variablesExtensions,
ImmutableMap<String, String> additionalBuildVariables,
Iterable<Artifact> directModuleMaps,
NestedSet<String> includeDirs,
NestedSet<String> quoteIncludeDirs,
NestedSet<String> systemIncludeDirs,
NestedSet<String> frameworkIncludeDirs,
Iterable<String> defines,
Iterable<String> localDefines)
throws EvalException {
if (usePic
&& !featureConfiguration.isEnabled(CppRuleClasses.PIC)
&& !featureConfiguration.isEnabled(CppRuleClasses.SUPPORTS_PIC)) {
throw new EvalException(CcCommon.PIC_CONFIGURATION_ERROR);
}
return setupVariables(
featureConfiguration,
ccToolchainProvider.getBuildVariables(buildOptions, cppConfiguration),
sourceFile,
outputFile,
gcnoFile,
isUsingFission,
dwoFile,
ltoIndexingFile,
thinLtoIndex,
thinLtoInputBitcodeFile,
thinLtoOutputObjectFile,
includes,
userCompileFlags,
cppModuleMap,
usePic,
fdoStamp,
dotdFileExecPath,
variablesExtensions,
additionalBuildVariables,
directModuleMaps,
includeDirs,
quoteIncludeDirs,
systemIncludeDirs,
frameworkIncludeDirs,
defines,
localDefines);
}
private static CcToolchainVariables setupVariables(
FeatureConfiguration featureConfiguration,
CcToolchainVariables parent,
String sourceFile,
String outputFile,
String gcnoFile,
boolean isUsingFission,
String dwoFile,
String ltoIndexingFile,
String thinLtoIndex,
String thinLtoInputBitcodeFile,
String thinLtoOutputObjectFile,
ImmutableList<String> includes,
Iterable<String> userCompileFlags,
CppModuleMap cppModuleMap,
boolean usePic,
String fdoStamp,
String dotdFileExecPath,
ImmutableList<VariablesExtension> variablesExtensions,
ImmutableMap<String, String> additionalBuildVariables,
Iterable<Artifact> directModuleMaps,
NestedSet<String> includeDirs,
NestedSet<String> quoteIncludeDirs,
NestedSet<String> systemIncludeDirs,
NestedSet<String> frameworkIncludeDirs,
Iterable<String> defines,
Iterable<String> localDefines) {
CcToolchainVariables.Builder buildVariables = CcToolchainVariables.builder(parent);
setupCommonVariablesInternal(
buildVariables,
featureConfiguration,
includes,
cppModuleMap,
fdoStamp,
variablesExtensions,
additionalBuildVariables,
directModuleMaps,
includeDirs,
quoteIncludeDirs,
systemIncludeDirs,
frameworkIncludeDirs,
defines,
localDefines);
setupSpecificVariables(
buildVariables,
sourceFile,
outputFile,
gcnoFile,
dwoFile,
isUsingFission,
ltoIndexingFile,
thinLtoIndex,
thinLtoInputBitcodeFile,
thinLtoOutputObjectFile,
userCompileFlags,
dotdFileExecPath,
usePic,
ImmutableList.of(),
ImmutableMap.of());
return buildVariables.build();
}
public static void setupSpecificVariables(
CcToolchainVariables.Builder buildVariables,
String sourceFile,
String outputFile,
String gcnoFile,
String dwoFile,
boolean isUsingFission,
String ltoIndexingFile,
String thinLtoIndex,
String thinLtoInputBitcodeFile,
String thinLtoOutputObjectFile,
Iterable<String> userCompileFlags,
String dotdFileExecPath,
boolean usePic,
ImmutableList<PathFragment> externalIncludeDirs,
Map<String, String> additionalBuildVariables) {
buildVariables.addStringSequenceVariable(
USER_COMPILE_FLAGS.getVariableName(), userCompileFlags);
if (sourceFile != null) {
buildVariables.addStringVariable(SOURCE_FILE.getVariableName(), sourceFile);
}
if (outputFile != null) {
buildVariables.addStringVariable(OUTPUT_FILE.getVariableName(), outputFile);
}
// Set dependency_file to enable <object>.d file generation.
if (dotdFileExecPath != null) {
buildVariables.addStringVariable(DEPENDENCY_FILE.getVariableName(), dotdFileExecPath);
}
if (gcnoFile != null) {
buildVariables.addStringVariable(GCOV_GCNO_FILE.getVariableName(), gcnoFile);
}
if (dwoFile != null) {
buildVariables.addStringVariable(PER_OBJECT_DEBUG_INFO_FILE.getVariableName(), dwoFile);
}
if (isUsingFission) {
buildVariables.addStringVariable(IS_USING_FISSION.getVariableName(), "");
}
if (ltoIndexingFile != null) {
buildVariables.addStringVariable(
LTO_INDEXING_BITCODE_FILE.getVariableName(), ltoIndexingFile);
}
if (thinLtoIndex != null) {
buildVariables.addStringVariable(THINLTO_INDEX.getVariableName(), thinLtoIndex);
}
if (thinLtoInputBitcodeFile != null) {
buildVariables.addStringVariable(
THINLTO_INPUT_BITCODE_FILE.getVariableName(), thinLtoInputBitcodeFile);
}
if (thinLtoOutputObjectFile != null) {
buildVariables.addStringVariable(
THINLTO_OUTPUT_OBJECT_FILE.getVariableName(), thinLtoOutputObjectFile);
}
if (usePic) {
buildVariables.addStringVariable(PIC.getVariableName(), "");
}
if (!externalIncludeDirs.isEmpty()) {
buildVariables.addStringSequenceVariable(
EXTERNAL_INCLUDE_PATHS.getVariableName(),
Iterables.transform(externalIncludeDirs, PathFragment::getSafePathString));
}
buildVariables.addAllStringVariables(additionalBuildVariables);
}
public static void setupCommonVariables(
CcToolchainVariables.Builder buildVariables,
FeatureConfiguration featureConfiguration,
List<String> includes,
CppModuleMap cppModuleMap,
String fdoStamp,
List<VariablesExtension> variablesExtensions,
Map<String, String> additionalBuildVariables,
Iterable<Artifact> directModuleMaps,
List<PathFragment> includeDirs,
List<PathFragment> quoteIncludeDirs,
List<PathFragment> systemIncludeDirs,
List<PathFragment> frameworkIncludeDirs,
Iterable<String> defines,
Iterable<String> localDefines) {
setupCommonVariablesInternal(
buildVariables,
featureConfiguration,
includes,
cppModuleMap,
fdoStamp,
variablesExtensions,
additionalBuildVariables,
directModuleMaps,
getSafePathStrings(includeDirs),
getSafePathStrings(quoteIncludeDirs),
getSafePathStrings(systemIncludeDirs),
getSafePathStrings(frameworkIncludeDirs),
defines,
localDefines);
}
private static void setupCommonVariablesInternal(
CcToolchainVariables.Builder buildVariables,
FeatureConfiguration featureConfiguration,
List<String> includes,
CppModuleMap cppModuleMap,
String fdoStamp,
List<VariablesExtension> variablesExtensions,
Map<String, String> additionalBuildVariables,
Iterable<Artifact> directModuleMaps,
NestedSet<String> includeDirs,
NestedSet<String> quoteIncludeDirs,
NestedSet<String> systemIncludeDirs,
NestedSet<String> frameworkIncludeDirs,
Iterable<String> defines,
Iterable<String> localDefines) {
Preconditions.checkNotNull(directModuleMaps);
Preconditions.checkNotNull(includeDirs);
Preconditions.checkNotNull(quoteIncludeDirs);
Preconditions.checkNotNull(systemIncludeDirs);
Preconditions.checkNotNull(frameworkIncludeDirs);
Preconditions.checkNotNull(defines);
Preconditions.checkNotNull(localDefines);
if (featureConfiguration.isEnabled(CppRuleClasses.MODULE_MAPS) && cppModuleMap != null) {
buildVariables.addStringVariable(MODULE_NAME.getVariableName(), cppModuleMap.getName());
buildVariables.addStringVariable(
MODULE_MAP_FILE.getVariableName(), cppModuleMap.getArtifact().getExecPathString());
buildVariables.addStringSequenceVariable(
DEPENDENT_MODULE_MAP_FILES.getVariableName(),
Iterables.transform(directModuleMaps, Artifact::getExecPathString));
}
if (featureConfiguration.isEnabled(CppRuleClasses.USE_HEADER_MODULES)) {
// Module inputs will be set later when the action is executed.
buildVariables.addStringSequenceVariable(MODULE_FILES.getVariableName(), ImmutableSet.of());
}
buildVariables.addStringSequenceVariable(INCLUDE_PATHS.getVariableName(), includeDirs);
buildVariables.addStringSequenceVariable(
QUOTE_INCLUDE_PATHS.getVariableName(), quoteIncludeDirs);
buildVariables.addStringSequenceVariable(
SYSTEM_INCLUDE_PATHS.getVariableName(), systemIncludeDirs);
if (!includes.isEmpty()) {
buildVariables.addStringSequenceVariable(INCLUDES.getVariableName(), includes);
}
buildVariables.addStringSequenceVariable(
FRAMEWORK_PATHS.getVariableName(), frameworkIncludeDirs);
Iterable<String> allDefines;
if (fdoStamp != null) {
// Stamp FDO builds with FDO subtype string
allDefines =
Iterables.concat(
defines,
localDefines,
ImmutableList.of(CppConfiguration.FDO_STAMP_MACRO + "=\"" + fdoStamp + "\""));
} else {
allDefines = Iterables.concat(defines, localDefines);
}
buildVariables.addStringSequenceVariable(PREPROCESSOR_DEFINES.getVariableName(), allDefines);
buildVariables.addAllStringVariables(additionalBuildVariables);
for (VariablesExtension extension : variablesExtensions) {
extension.addVariables(buildVariables);
}
}
/** Get the safe path strings for a list of paths to use in the build variables. */
private static NestedSet<String> getSafePathStrings(NestedSet<PathFragment> paths) {
return getSafePathStrings(paths.toList());
}
/** Get the safe path strings for a list of paths to use in the build variables. */
private static NestedSet<String> getSafePathStrings(List<PathFragment> paths) {
// Using ImmutableSet first to remove duplicates, then NestedSet for smaller memory footprint.
return NestedSetBuilder.wrap(
Order.STABLE_ORDER,
Iterables.transform(ImmutableSet.copyOf(paths), PathFragment::getSafePathString));
}
public String getVariableName() {
return variableName;
}
}