Skip to content

Commit d2704da

Browse files
Add feature to produce indexstore files for CC builds
1 parent b6bb4c1 commit d2704da

File tree

14 files changed

+201
-5
lines changed

14 files changed

+201
-5
lines changed

src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public enum ArtifactCategory {
2525
DYNAMIC_LIBRARY("lib", ".so", ".dylib", ".dll", ".wasm"),
2626
EXECUTABLE("", "", ".exe", ".wasm"),
2727
INTERFACE_LIBRARY("lib", ".ifso", ".tbd", ".if.lib", ".lib"),
28+
INDEXSTORE_FILE("", ".indexstore"),
2829
PIC_FILE("", ".pic"),
2930
INCLUDED_FILE_LIST("", ".d"),
3031
SERIALIZED_DIAGNOSTICS_FILE("", ".dia"),

src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,8 +1468,8 @@ private Artifact createCompileActionTemplate(
14681468
SpecialArtifact outputFiles =
14691469
CppHelper.getCompileOutputTreeArtifact(
14701470
actionConstructionContext, label, sourceArtifact, outputName, usePic);
1471-
// Dotd and dia file outputs are specified in the execution phase.
1472-
builder.setOutputs(outputFiles, /* dotdFile= */ null, /* diagnosticsFile= */ null);
1471+
// Dotd, dia and indexstore file outputs are specified in the execution phase.
1472+
builder.setOutputs(outputFiles, /* dotdFile= */ null, /* diagnosticsFile= */ null, /* indexstoreFiles */ null);
14731473
builder.setVariables(
14741474
setupCompileBuildVariables(
14751475
builder,
@@ -1497,12 +1497,19 @@ private Artifact createCompileActionTemplate(
14971497
CppHelper.getDiagnosticsOutputTreeArtifact(
14981498
actionConstructionContext, label, sourceArtifact, outputName, usePic);
14991499
}
1500+
SpecialArtifact indexstoreTreeArtifact = null;
1501+
if (builder.indexstoreFilesEnabled()) {
1502+
indexstoreTreeArtifact =
1503+
CppHelper.getIndexstoreOutputTreeArtifact(
1504+
actionConstructionContext, label, sourceArtifact, outputName);
1505+
}
15001506
CppCompileActionTemplate actionTemplate =
15011507
new CppCompileActionTemplate(
15021508
sourceArtifact,
15031509
outputFiles,
15041510
dotdTreeArtifact,
15051511
diagnosticsTreeArtifact,
1512+
indexstoreTreeArtifact,
15061513
builder,
15071514
ccToolchain,
15081515
outputCategories,
@@ -1574,6 +1581,10 @@ private CcToolchainVariables setupCompileBuildVariables(
15741581
if (builder.getDiagnosticsFile() != null) {
15751582
diagnosticsFileExecPath = builder.getDiagnosticsFile().getExecPathString();
15761583
}
1584+
String indexstoreFilesExecPath = null;
1585+
if (builder.getIndexstoreFiles() != null) {
1586+
indexstoreFilesExecPath = builder.getIndexstoreFiles().getExecPathString();
1587+
}
15771588
if (needsFdoBuildVariables && fdoContext.hasArtifacts(cppConfiguration)) {
15781589
// This modifies the passed-in builder, which is a surprising side-effect, and makes it unsafe
15791590
// to call this method multiple times for the same builder.
@@ -1650,6 +1661,7 @@ private CcToolchainVariables setupCompileBuildVariables(
16501661
getCopts(builder.getSourceFile(), sourceLabel),
16511662
dotdFileExecPath,
16521663
diagnosticsFileExecPath,
1664+
indexstoreFilesExecPath,
16531665
usePic,
16541666
ccCompilationContext.getExternalIncludeDirs(),
16551667
additionalBuildVariables);

src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ public CcToolchainVariables getCompileBuildVariables(
360360
/* fdoStamp= */ null,
361361
/* dotdFileExecPath= */ null,
362362
/* diagnosticsFileExecPath= */ null,
363+
/* indexstoreFilesExecPath= */ null,
363364
variablesExtensions,
364365
/* additionalBuildVariables= */ ImmutableMap.of(),
365366
/* directModuleMaps= */ ImmutableList.of(),

src/main/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariables.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public enum CompileBuildVariables {
4646
DEPENDENCY_FILE("dependency_file"),
4747
/** Variable for the serialized diagnostics file path */
4848
SERIALIZED_DIAGNOSTICS_FILE("serialized_diagnostics_file"),
49+
/** Variable for the indexstore file paths */
50+
INDEXSTORE_FILES("indexstore_files"),
4951
/** Variable for the module file name. */
5052
MODULE_NAME("module_name"),
5153
/**
@@ -154,6 +156,7 @@ public static CcToolchainVariables setupVariablesOrReportRuleError(
154156
String fdoStamp,
155157
String dotdFileExecPath,
156158
String diagnosticsFileExecPath,
159+
String indexstoreFilesExecPath,
157160
ImmutableList<VariablesExtension> variablesExtensions,
158161
ImmutableMap<String, String> additionalBuildVariables,
159162
Iterable<Artifact> directModuleMaps,
@@ -188,6 +191,7 @@ public static CcToolchainVariables setupVariablesOrReportRuleError(
188191
fdoStamp,
189192
dotdFileExecPath,
190193
diagnosticsFileExecPath,
194+
indexstoreFilesExecPath,
191195
variablesExtensions,
192196
additionalBuildVariables,
193197
directModuleMaps,
@@ -224,6 +228,7 @@ public static CcToolchainVariables setupVariablesOrThrowEvalException(
224228
String fdoStamp,
225229
String dotdFileExecPath,
226230
String diagnosticsFileExecPath,
231+
String indexstoreFilesExecPath,
227232
ImmutableList<VariablesExtension> variablesExtensions,
228233
ImmutableMap<String, String> additionalBuildVariables,
229234
Iterable<Artifact> directModuleMaps,
@@ -258,6 +263,7 @@ public static CcToolchainVariables setupVariablesOrThrowEvalException(
258263
fdoStamp,
259264
dotdFileExecPath,
260265
diagnosticsFileExecPath,
266+
indexstoreFilesExecPath,
261267
variablesExtensions,
262268
additionalBuildVariables,
263269
directModuleMaps,
@@ -288,6 +294,7 @@ private static CcToolchainVariables setupVariables(
288294
String fdoStamp,
289295
String dotdFileExecPath,
290296
String diagnosticsFileExecPath,
297+
String indexstoreFilesExecPath,
291298
ImmutableList<VariablesExtension> variablesExtensions,
292299
ImmutableMap<String, String> additionalBuildVariables,
293300
Iterable<Artifact> directModuleMaps,
@@ -327,6 +334,7 @@ private static CcToolchainVariables setupVariables(
327334
userCompileFlags,
328335
dotdFileExecPath,
329336
diagnosticsFileExecPath,
337+
indexstoreFilesExecPath,
330338
usePic,
331339
ImmutableList.of(),
332340
ImmutableMap.of());
@@ -347,6 +355,7 @@ public static void setupSpecificVariables(
347355
Iterable<String> userCompileFlags,
348356
String dotdFileExecPath,
349357
String diagnosticsFileExecPath,
358+
String indexstoreFilesExecPath,
350359
boolean usePic,
351360
ImmutableList<PathFragment> externalIncludeDirs,
352361
Map<String, String> additionalBuildVariables) {
@@ -372,6 +381,12 @@ public static void setupSpecificVariables(
372381
SERIALIZED_DIAGNOSTICS_FILE.getVariableName(), diagnosticsFileExecPath);
373382
}
374383

384+
// Set indexstore_files to enable <object>.indexstore files generation.
385+
if (indexstoreFilesExecPath != null) {
386+
buildVariables.addStringVariable(
387+
INDEXSTORE_FILES.getVariableName(), indexstoreFilesExecPath);
388+
}
389+
375390
if (gcnoFile != null) {
376391
buildVariables.addStringVariable(GCOV_GCNO_FILE.getVariableName(), gcnoFile);
377392
}

src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ public class CppCompileAction extends AbstractAction implements IncludeScannable
223223
* @param outputFile the object file that is written as result of the compilation
224224
* @param dotdFile the .d file that is generated as a side-effect of compilation
225225
* @param diagnosticsFile the .dia file that is generated as a side-effect of compilation
226+
* @param indexstoreFiles the .indexstore files that are generated as a side-effect of compilation
226227
* @param gcnoFile the coverage notes that are written in coverage mode, can be null
227228
* @param dwoFile the .dwo output file where debug information is stored for Fission builds (null
228229
* if Fission mode is disabled)
@@ -252,6 +253,7 @@ public class CppCompileAction extends AbstractAction implements IncludeScannable
252253
Artifact outputFile,
253254
@Nullable Artifact dotdFile,
254255
@Nullable Artifact diagnosticsFile,
256+
@Nullable Artifact indexstoreFiles,
255257
@Nullable Artifact gcnoFile,
256258
@Nullable Artifact dwoFile,
257259
@Nullable Artifact ltoIndexingFile,
@@ -275,6 +277,7 @@ public class CppCompileAction extends AbstractAction implements IncludeScannable
275277
Preconditions.checkNotNull(outputFile, "outputFile"),
276278
dotdFile,
277279
diagnosticsFile,
280+
indexstoreFiles,
278281
gcnoFile,
279282
dwoFile,
280283
ltoIndexingFile,
@@ -327,6 +330,7 @@ private static ImmutableSet<Artifact> collectOutputs(
327330
Artifact outputFile,
328331
@Nullable Artifact dotdFile,
329332
@Nullable Artifact diagnosticsFile,
333+
@Nullable Artifact indexstoreFiles,
330334
@Nullable Artifact gcnoFile,
331335
@Nullable Artifact dwoFile,
332336
@Nullable Artifact ltoIndexingFile,
@@ -343,6 +347,9 @@ private static ImmutableSet<Artifact> collectOutputs(
343347
if (diagnosticsFile != null) {
344348
outputs.add(diagnosticsFile);
345349
}
350+
if (indexstoreFiles != null) {
351+
outputs.add(indexstoreFiles);
352+
}
346353
if (dwoFile != null) {
347354
outputs.add(dwoFile);
348355
}

src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class CppCompileActionBuilder {
6060
private Artifact ltoIndexingFile;
6161
private Artifact dotdFile;
6262
private Artifact diagnosticsFile;
63+
private Artifact indexstoreFiles;
6364
private Artifact gcnoFile;
6465
private CcCompilationContext ccCompilationContext = CcCompilationContext.EMPTY;
6566
private final List<String> pluginOpts = new ArrayList<>();
@@ -320,6 +321,7 @@ public CppCompileAction buildAndVerify() throws UnconfiguredActionConfigExceptio
320321
outputFile,
321322
dotdFile,
322323
diagnosticsFile,
324+
indexstoreFiles,
323325
gcnoFile,
324326
dwoFile,
325327
ltoIndexingFile,
@@ -493,12 +495,17 @@ public boolean serializedDiagnosticsFilesEnabled() {
493495
return featureConfiguration.isEnabled(CppRuleClasses.SERIALIZED_DIAGNOSTICS_FILE);
494496
}
495497

498+
public boolean indexstoreFilesEnabled() {
499+
return featureConfiguration.isEnabled(CppRuleClasses.INDEXSTORE_FILES);
500+
}
501+
496502
@CanIgnoreReturnValue
497503
public CppCompileActionBuilder setOutputs(
498-
Artifact outputFile, Artifact dotdFile, Artifact diagnosticsFile) {
504+
Artifact outputFile, Artifact dotdFile, Artifact diagnosticsFile, Artifact indexstoreFiles) {
499505
this.outputFile = outputFile;
500506
this.dotdFile = dotdFile;
501507
this.diagnosticsFile = diagnosticsFile;
508+
this.indexstoreFiles = indexstoreFiles;
502509
return this;
503510
}
504511

@@ -533,6 +540,15 @@ public CppCompileActionBuilder setOutputs(
533540
} else {
534541
diagnosticsFile = null;
535542
}
543+
if (indexstoreFilesEnabled()) {
544+
String indexstoreFilesName =
545+
CppHelper.getIndexstoreFilesName(ccToolchain, outputCategory, outputName);
546+
indexstoreFiles =
547+
CppHelper.getCompileOutputArtifact(
548+
actionConstructionContext, label, indexstoreFilesName, configuration);
549+
} else {
550+
indexstoreFiles = null;
551+
}
536552
return this;
537553
}
538554

@@ -564,6 +580,10 @@ public Artifact getDiagnosticsFile() {
564580
return this.diagnosticsFile;
565581
}
566582

583+
public Artifact getIndexstoreFiles() {
584+
return this.indexstoreFiles;
585+
}
586+
567587
@CanIgnoreReturnValue
568588
public CppCompileActionBuilder setGcnoFile(Artifact gcnoFile) {
569589
this.gcnoFile = gcnoFile;

src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionTemplate.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public final class CppCompileActionTemplate extends ActionKeyCacher
4848
private final SpecialArtifact outputTreeArtifact;
4949
private final SpecialArtifact dotdTreeArtifact;
5050
private final SpecialArtifact diagnosticsTreeArtifact;
51+
private final SpecialArtifact indexstoreTreeArtifact;
5152
private final CcToolchainProvider toolchain;
5253
private final ImmutableList<ArtifactCategory> categories;
5354
private final ActionOwner actionOwner;
@@ -61,6 +62,7 @@ public final class CppCompileActionTemplate extends ActionKeyCacher
6162
* @param outputTreeArtifact the TreeArtifact that contains compilation outputs.
6263
* @param dotdTreeArtifact the TreeArtifact that contains dotd files.
6364
* @param diagnosticsTreeArtifact the TreeArtifact that contains serialized diagnostics files.
65+
* @param indexstoreTreeArtifact the TreeArtifact that contains indexstore files.
6466
* @param cppCompileActionBuilder An almost completely configured {@link CppCompileActionBuilder}
6567
* without the input and output files set. It is used as a template to instantiate expanded
6668
* {CppCompileAction}s.
@@ -74,6 +76,7 @@ public final class CppCompileActionTemplate extends ActionKeyCacher
7476
SpecialArtifact outputTreeArtifact,
7577
SpecialArtifact dotdTreeArtifact,
7678
SpecialArtifact diagnosticsTreeArtifact,
79+
SpecialArtifact indexstoreTreeArtifact,
7780
CppCompileActionBuilder cppCompileActionBuilder,
7881
CcToolchainProvider toolchain,
7982
ImmutableList<ArtifactCategory> categories,
@@ -83,6 +86,7 @@ public final class CppCompileActionTemplate extends ActionKeyCacher
8386
this.outputTreeArtifact = outputTreeArtifact;
8487
this.dotdTreeArtifact = dotdTreeArtifact;
8588
this.diagnosticsTreeArtifact = diagnosticsTreeArtifact;
89+
this.indexstoreTreeArtifact = indexstoreTreeArtifact;
8690
this.toolchain = toolchain;
8791
this.categories = categories;
8892
this.actionOwner = checkNotNull(actionOwner, outputTreeArtifact);
@@ -146,12 +150,19 @@ public ImmutableList<CppCompileAction> generateActionsForInputArtifacts(
146150
TreeFileArtifact.createTemplateExpansionOutput(
147151
diagnosticsTreeArtifact, outputName + ".dia", artifactOwner);
148152
}
153+
TreeFileArtifact indexstoreFilesArtifact = null;
154+
if (indexstoreTreeArtifact != null) {
155+
indexstoreFilesArtifact =
156+
TreeFileArtifact.createTemplateExpansionOutput(
157+
indexstoreTreeArtifact, outputName + ".indexstore", artifactOwner);
158+
}
149159
expandedActions.add(
150160
createAction(
151161
inputTreeFileArtifact,
152162
outputTreeFileArtifact,
153163
dotdFileArtifact,
154164
diagnosticsFileArtifact,
165+
indexstoreFilesArtifact,
155166
privateHeaders));
156167
}
157168

@@ -201,13 +212,14 @@ private CppCompileAction createAction(
201212
TreeFileArtifact outputTreeFileArtifact,
202213
@Nullable Artifact dotdFileArtifact,
203214
@Nullable Artifact diagnosticsFileArtifact,
215+
@Nullable Artifact indexstoreFilesArtifact,
204216
NestedSet<Artifact> privateHeaders)
205217
throws ActionExecutionException {
206218
CppCompileActionBuilder builder =
207219
new CppCompileActionBuilder(cppCompileActionBuilder)
208220
.setAdditionalPrunableHeaders(privateHeaders)
209221
.setSourceFile(sourceTreeFileArtifact)
210-
.setOutputs(outputTreeFileArtifact, dotdFileArtifact, diagnosticsFileArtifact);
222+
.setOutputs(outputTreeFileArtifact, dotdFileArtifact, diagnosticsFileArtifact, indexstoreFilesArtifact);
211223

212224
CcToolchainVariables.Builder buildVariables =
213225
CcToolchainVariables.builder(cppCompileActionBuilder.getVariables());
@@ -227,6 +239,11 @@ private CppCompileAction createAction(
227239
CompileBuildVariables.SERIALIZED_DIAGNOSTICS_FILE.getVariableName(),
228240
diagnosticsFileArtifact.getExecPathString());
229241
}
242+
if (indexstoreFilesArtifact != null) {
243+
buildVariables.overrideStringVariable(
244+
CompileBuildVariables.INDEXSTORE_FILES.getVariableName(),
245+
indexstoreFilesArtifact.getExecPathString());
246+
}
230247

231248
builder.setVariables(buildVariables.build());
232249

src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class CppHelper {
7373
static final PathFragment PIC_DOTD_FILES = PathFragment.create("_pic_dotd");
7474
static final PathFragment DIA_FILES = PathFragment.create("_dia");
7575
static final PathFragment PIC_DIA_FILES = PathFragment.create("_pic_dia");
76+
static final PathFragment INDEXSTORE_FILES = PathFragment.create("_indexstore");
7677

7778
// TODO(bazel-team): should this use Link.SHARED_LIBRARY_FILETYPES?
7879
public static final FileTypeSet SHARED_LIBRARY_FILETYPES =
@@ -307,6 +308,13 @@ private static PathFragment getDiagnosticsDirectory(
307308
ruleLabel, usePic ? PIC_DIA_FILES : DIA_FILES, siblingRepositoryLayout);
308309
}
309310

311+
/** Returns the directory where indexstore files are created. */
312+
private static PathFragment getIndexstoreDirectory(
313+
Label ruleLabel, boolean siblingRepositoryLayout) {
314+
return AnalysisUtils.getUniqueDirectory(
315+
ruleLabel, INDEXSTORE_FILES, siblingRepositoryLayout);
316+
}
317+
310318
public static Artifact getLinkedArtifact(
311319
Label label,
312320
ActionConstructionContext actionConstructionContext,
@@ -583,6 +591,23 @@ public static SpecialArtifact getDiagnosticsOutputTreeArtifact(
583591
sourceTreeArtifact.getRoot());
584592
}
585593

594+
/**
595+
* Returns the corresponding indexstore files TreeArtifact given the source
596+
* TreeArtifact.
597+
*/
598+
public static SpecialArtifact getIndexstoreOutputTreeArtifact(
599+
ActionConstructionContext actionConstructionContext,
600+
Label label,
601+
Artifact sourceTreeArtifact,
602+
String outputName) {
603+
return actionConstructionContext.getTreeArtifact(
604+
getIndexstoreDirectory(
605+
label,
606+
actionConstructionContext.getConfiguration().isSiblingRepositoryLayout())
607+
.getRelative(outputName),
608+
sourceTreeArtifact.getRoot());
609+
}
610+
586611
public static String getArtifactNameForCategory(
587612
CcToolchainProvider toolchain,
588613
ArtifactCategory category,
@@ -618,6 +643,18 @@ static String getDiagnosticsFileName(
618643
toolchain, ArtifactCategory.SERIALIZED_DIAGNOSTICS_FILE, baseName);
619644
}
620645

646+
static String getIndexstoreFilesName(
647+
CcToolchainProvider toolchain, ArtifactCategory outputCategory, String outputName)
648+
throws RuleErrorException {
649+
String baseName =
650+
outputCategory == ArtifactCategory.OBJECT_FILE
651+
|| outputCategory == ArtifactCategory.PROCESSED_HEADER
652+
? outputName
653+
: getArtifactNameForCategory(toolchain, outputCategory, outputName);
654+
return getArtifactNameForCategory(
655+
toolchain, ArtifactCategory.INDEXSTORE_FILE, baseName);
656+
}
657+
621658
/**
622659
* Returns true if the build implied by the given config and toolchain uses --start-lib/--end-lib
623660
* ld options.

0 commit comments

Comments
 (0)