Skip to content

Commit

Permalink
Add feature to produce serialized diagnostics files
Browse files Browse the repository at this point in the history
Using the `serialized_diagnostics_file` feature will add the `--serialized-diagnostics` flag to C/C++/Objective-C/Objective-C++ compiles, causing a declared`.dia` file output to be produced.

Closes bazelbuild#15191.

Closes bazelbuild#15403.

PiperOrigin-RevId: 452024270
Change-Id: I8d28e6ceb729d90c3200ddd2ba96cdb09ac14afc
  • Loading branch information
brentleyjones authored and Copybara-Service committed May 31, 2022
1 parent 3649324 commit c8ea368
Show file tree
Hide file tree
Showing 15 changed files with 253 additions and 9 deletions.
Expand Up @@ -27,6 +27,7 @@ public enum ArtifactCategory {
INTERFACE_LIBRARY("lib", ".ifso", ".tbd", ".if.lib", ".lib"),
PIC_FILE("", ".pic"),
INCLUDED_FILE_LIST("", ".d"),
SERIALIZED_DIAGNOSTICS_FILE("", ".dia"),
OBJECT_FILE("", ".o", ".obj"),
PIC_OBJECT_FILE("", ".pic.o"),
CPP_MODULE("", ".pcm"),
Expand Down
Expand Up @@ -1552,8 +1552,8 @@ private Artifact createCompileActionTemplate(
SpecialArtifact outputFiles =
CppHelper.getCompileOutputTreeArtifact(
actionConstructionContext, label, sourceArtifact, outputName, usePic);
// Dotd file output is specified in the execution phase.
builder.setOutputs(outputFiles, /* dotdFile= */ null);
// Dotd and dia file outputs are specified in the execution phase.
builder.setOutputs(outputFiles, /* dotdFile= */ null, /* diagnosticsFile= */ null);
builder.setVariables(
setupCompileBuildVariables(
builder,
Expand All @@ -1575,11 +1575,18 @@ private Artifact createCompileActionTemplate(
CppHelper.getDotdOutputTreeArtifact(
actionConstructionContext, label, sourceArtifact, outputName, usePic);
}
SpecialArtifact diagnosticsTreeArtifact = null;
if (builder.serializedDiagnosticsFilesEnabled()) {
diagnosticsTreeArtifact =
CppHelper.getDiagnosticsOutputTreeArtifact(
actionConstructionContext, label, sourceArtifact, outputName, usePic);
}
CppCompileActionTemplate actionTemplate =
new CppCompileActionTemplate(
sourceArtifact,
outputFiles,
dotdTreeArtifact,
diagnosticsTreeArtifact,
builder,
ccToolchain,
outputCategories,
Expand Down Expand Up @@ -1639,6 +1646,10 @@ private CcToolchainVariables setupCompileBuildVariables(
if (builder.getDotdFile() != null) {
dotdFileExecPath = builder.getDotdFile().getExecPathString();
}
String diagnosticsFileExecPath = null;
if (builder.getDiagnosticsFile() != null) {
diagnosticsFileExecPath = builder.getDiagnosticsFile().getExecPathString();
}
if (needsFdoBuildVariables && fdoContext.hasArtifacts(cppConfiguration)) {
// This modifies the passed-in builder, which is a surprising side-effect, and makes it unsafe
// to call this method multiple times for the same builder.
Expand Down Expand Up @@ -1714,6 +1725,7 @@ private CcToolchainVariables setupCompileBuildVariables(
/* thinLtoOutputObjectFile= */ null,
getCopts(builder.getSourceFile(), sourceLabel),
dotdFileExecPath,
diagnosticsFileExecPath,
usePic,
ccCompilationContext.getExternalIncludeDirs(),
additionalBuildVariables);
Expand Down
Expand Up @@ -320,6 +320,7 @@ public CcToolchainVariables getCompileBuildVariables(
usePic,
/* fdoStamp= */ null,
/* dotdFileExecPath= */ null,
/* diagnosticsFileExecPath= */ null,
variablesExtensions,
/* additionalBuildVariables= */ ImmutableMap.of(),
/* directModuleMaps= */ ImmutableList.of(),
Expand Down
Expand Up @@ -44,6 +44,8 @@ public enum CompileBuildVariables {
OUTPUT_FILE("output_file"),
/** Variable for the dependency file path */
DEPENDENCY_FILE("dependency_file"),
/** Variable for the serialized diagnostics file path */
SERIALIZED_DIAGNOSTICS_FILE("serialized_diagnostics_file"),
/** Variable for the module file name. */
MODULE_NAME("module_name"),
/**
Expand Down Expand Up @@ -151,6 +153,7 @@ public static CcToolchainVariables setupVariablesOrReportRuleError(
boolean usePic,
String fdoStamp,
String dotdFileExecPath,
String diagnosticsFileExecPath,
ImmutableList<VariablesExtension> variablesExtensions,
ImmutableMap<String, String> additionalBuildVariables,
Iterable<Artifact> directModuleMaps,
Expand Down Expand Up @@ -184,6 +187,7 @@ public static CcToolchainVariables setupVariablesOrReportRuleError(
usePic,
fdoStamp,
dotdFileExecPath,
diagnosticsFileExecPath,
variablesExtensions,
additionalBuildVariables,
directModuleMaps,
Expand Down Expand Up @@ -219,6 +223,7 @@ public static CcToolchainVariables setupVariablesOrThrowEvalException(
boolean usePic,
String fdoStamp,
String dotdFileExecPath,
String diagnosticsFileExecPath,
ImmutableList<VariablesExtension> variablesExtensions,
ImmutableMap<String, String> additionalBuildVariables,
Iterable<Artifact> directModuleMaps,
Expand Down Expand Up @@ -252,6 +257,7 @@ public static CcToolchainVariables setupVariablesOrThrowEvalException(
usePic,
fdoStamp,
dotdFileExecPath,
diagnosticsFileExecPath,
variablesExtensions,
additionalBuildVariables,
directModuleMaps,
Expand Down Expand Up @@ -281,6 +287,7 @@ private static CcToolchainVariables setupVariables(
boolean usePic,
String fdoStamp,
String dotdFileExecPath,
String diagnosticsFileExecPath,
ImmutableList<VariablesExtension> variablesExtensions,
ImmutableMap<String, String> additionalBuildVariables,
Iterable<Artifact> directModuleMaps,
Expand Down Expand Up @@ -319,6 +326,7 @@ private static CcToolchainVariables setupVariables(
thinLtoOutputObjectFile,
userCompileFlags,
dotdFileExecPath,
diagnosticsFileExecPath,
usePic,
ImmutableList.of(),
ImmutableMap.of());
Expand All @@ -338,6 +346,7 @@ public static void setupSpecificVariables(
String thinLtoOutputObjectFile,
Iterable<String> userCompileFlags,
String dotdFileExecPath,
String diagnosticsFileExecPath,
boolean usePic,
ImmutableList<PathFragment> externalIncludeDirs,
Map<String, String> additionalBuildVariables) {
Expand All @@ -357,6 +366,12 @@ public static void setupSpecificVariables(
buildVariables.addStringVariable(DEPENDENCY_FILE.getVariableName(), dotdFileExecPath);
}

// Set diagnostics_file to enable <object>.dia file generation.
if (diagnosticsFileExecPath != null) {
buildVariables.addStringVariable(
SERIALIZED_DIAGNOSTICS_FILE.getVariableName(), diagnosticsFileExecPath);
}

if (gcnoFile != null) {
buildVariables.addStringVariable(GCOV_GCNO_FILE.getVariableName(), gcnoFile);
}
Expand Down
Expand Up @@ -225,6 +225,7 @@ public class CppCompileAction extends AbstractAction implements IncludeScannable
* not needed during actual execution.
* @param outputFile the object file that is written as result of the compilation
* @param dotdFile the .d file that is generated as a side-effect of compilation
* @param diagnosticsFile the .dia file that is generated as a side-effect of compilation
* @param gcnoFile the coverage notes that are written in coverage mode, can be null
* @param dwoFile the .dwo output file where debug information is stored for Fission builds (null
* if Fission mode is disabled)
Expand Down Expand Up @@ -253,6 +254,7 @@ public class CppCompileAction extends AbstractAction implements IncludeScannable
NestedSet<Artifact> additionalPrunableHeaders,
Artifact outputFile,
@Nullable Artifact dotdFile,
@Nullable Artifact diagnosticsFile,
@Nullable Artifact gcnoFile,
@Nullable Artifact dwoFile,
@Nullable Artifact ltoIndexingFile,
Expand All @@ -272,7 +274,14 @@ public class CppCompileAction extends AbstractAction implements IncludeScannable
NestedSetBuilder.fromNestedSet(mandatoryInputs)
.addTransitive(inputsForInvalidation)
.build(),
collectOutputs(outputFile, dotdFile, gcnoFile, dwoFile, ltoIndexingFile, additionalOutputs),
collectOutputs(
outputFile,
dotdFile,
diagnosticsFile,
gcnoFile,
dwoFile,
ltoIndexingFile,
additionalOutputs),
env);
Preconditions.checkNotNull(outputFile);
this.outputFile = outputFile;
Expand All @@ -293,7 +302,13 @@ public class CppCompileAction extends AbstractAction implements IncludeScannable
Preconditions.checkNotNull(additionalIncludeScanningRoots);
this.compileCommandLine =
buildCommandLine(
sourceFile, coptsFilter, actionName, dotdFile, featureConfiguration, variables);
sourceFile,
coptsFilter,
actionName,
dotdFile,
diagnosticsFile,
featureConfiguration,
variables);
this.executionInfo = executionInfo;
this.actionName = actionName;
this.featureConfiguration = featureConfiguration;
Expand All @@ -317,6 +332,7 @@ public class CppCompileAction extends AbstractAction implements IncludeScannable
private static ImmutableSet<Artifact> collectOutputs(
@Nullable Artifact outputFile,
@Nullable Artifact dotdFile,
@Nullable Artifact diagnosticsFile,
@Nullable Artifact gcnoFile,
@Nullable Artifact dwoFile,
@Nullable Artifact ltoIndexingFile,
Expand All @@ -333,6 +349,9 @@ private static ImmutableSet<Artifact> collectOutputs(
if (dotdFile != null) {
outputs.add(dotdFile);
}
if (diagnosticsFile != null) {
outputs.add(diagnosticsFile);
}
if (dwoFile != null) {
outputs.add(dwoFile);
}
Expand All @@ -347,6 +366,7 @@ static CompileCommandLine buildCommandLine(
CoptsFilter coptsFilter,
String actionName,
Artifact dotdFile,
Artifact diagnosticsFile,
FeatureConfiguration featureConfiguration,
CcToolchainVariables variables) {
return CompileCommandLine.builder(sourceFile, coptsFilter, actionName, dotdFile)
Expand Down
Expand Up @@ -58,6 +58,7 @@ public class CppCompileActionBuilder {
private Artifact dwoFile;
private Artifact ltoIndexingFile;
private Artifact dotdFile;
private Artifact diagnosticsFile;
private Artifact gcnoFile;
private CcCompilationContext ccCompilationContext = CcCompilationContext.EMPTY;
private final List<String> pluginOpts = new ArrayList<>();
Expand Down Expand Up @@ -306,6 +307,7 @@ public CppCompileAction buildAndVerify() throws UnconfiguredActionConfigExceptio
prunableHeaders,
outputFile,
dotdFile,
diagnosticsFile,
gcnoFile,
dwoFile,
ltoIndexingFile,
Expand Down Expand Up @@ -465,9 +467,15 @@ public boolean dotdFilesEnabled() {
&& !featureConfiguration.isEnabled(CppRuleClasses.PARSE_SHOWINCLUDES);
}

public CppCompileActionBuilder setOutputs(Artifact outputFile, Artifact dotdFile) {
public boolean serializedDiagnosticsFilesEnabled() {
return featureConfiguration.isEnabled(CppRuleClasses.SERIALIZED_DIAGNOSTICS_FILE);
}

public CppCompileActionBuilder setOutputs(
Artifact outputFile, Artifact dotdFile, Artifact diagnosticsFile) {
this.outputFile = outputFile;
this.dotdFile = dotdFile;
this.diagnosticsFile = diagnosticsFile;
return this;
}

Expand All @@ -492,6 +500,15 @@ public CppCompileActionBuilder setOutputs(
} else {
dotdFile = null;
}
if (serializedDiagnosticsFilesEnabled()) {
String diagnosticsFileName =
CppHelper.getDiagnosticsFileName(ccToolchain, outputCategory, outputName);
diagnosticsFile =
CppHelper.getCompileOutputArtifact(
actionConstructionContext, label, diagnosticsFileName, configuration);
} else {
diagnosticsFile = null;
}
return this;
}

Expand All @@ -517,6 +534,10 @@ public Artifact getDotdFile() {
return this.dotdFile;
}

public Artifact getDiagnosticsFile() {
return this.diagnosticsFile;
}

public CppCompileActionBuilder setGcnoFile(Artifact gcnoFile) {
this.gcnoFile = gcnoFile;
return this;
Expand Down
Expand Up @@ -47,6 +47,7 @@ public final class CppCompileActionTemplate extends ActionKeyCacher
private final SpecialArtifact sourceTreeArtifact;
private final SpecialArtifact outputTreeArtifact;
private final SpecialArtifact dotdTreeArtifact;
private final SpecialArtifact diagnosticsTreeArtifact;
private final CcToolchainProvider toolchain;
private final ImmutableList<ArtifactCategory> categories;
private final ActionOwner actionOwner;
Expand All @@ -59,6 +60,7 @@ public final class CppCompileActionTemplate extends ActionKeyCacher
* @param sourceTreeArtifact the TreeArtifact that contains source files to compile.
* @param outputTreeArtifact the TreeArtifact that contains compilation outputs.
* @param dotdTreeArtifact the TreeArtifact that contains dotd files.
* @param diagnosticsTreeArtifact the TreeArtifact that contains serialized diagnostics files.
* @param cppCompileActionBuilder An almost completely configured {@link CppCompileActionBuilder}
* without the input and output files set. It is used as a template to instantiate expanded
* {CppCompileAction}s.
Expand All @@ -71,6 +73,7 @@ public final class CppCompileActionTemplate extends ActionKeyCacher
SpecialArtifact sourceTreeArtifact,
SpecialArtifact outputTreeArtifact,
SpecialArtifact dotdTreeArtifact,
SpecialArtifact diagnosticsTreeArtifact,
CppCompileActionBuilder cppCompileActionBuilder,
CcToolchainProvider toolchain,
ImmutableList<ArtifactCategory> categories,
Expand All @@ -79,6 +82,7 @@ public final class CppCompileActionTemplate extends ActionKeyCacher
this.sourceTreeArtifact = sourceTreeArtifact;
this.outputTreeArtifact = outputTreeArtifact;
this.dotdTreeArtifact = dotdTreeArtifact;
this.diagnosticsTreeArtifact = diagnosticsTreeArtifact;
this.toolchain = toolchain;
this.categories = categories;
this.actionOwner = checkNotNull(actionOwner, outputTreeArtifact);
Expand Down Expand Up @@ -136,9 +140,19 @@ public ImmutableList<CppCompileAction> generateActionsForInputArtifacts(
TreeFileArtifact.createTemplateExpansionOutput(
dotdTreeArtifact, outputName + ".d", artifactOwner);
}
TreeFileArtifact diagnosticsFileArtifact = null;
if (diagnosticsTreeArtifact != null) {
diagnosticsFileArtifact =
TreeFileArtifact.createTemplateExpansionOutput(
diagnosticsTreeArtifact, outputName + ".dia", artifactOwner);
}
expandedActions.add(
createAction(
inputTreeFileArtifact, outputTreeFileArtifact, dotdFileArtifact, privateHeaders));
inputTreeFileArtifact,
outputTreeFileArtifact,
dotdFileArtifact,
diagnosticsFileArtifact,
privateHeaders));
}

return expandedActions.build();
Expand All @@ -156,6 +170,7 @@ protected void computeKey(
cppCompileActionBuilder.getCoptsFilter(),
CppActionNames.CPP_COMPILE,
dotdTreeArtifact,
diagnosticsTreeArtifact,
cppCompileActionBuilder.getFeatureConfiguration(),
cppCompileActionBuilder.getVariables());
CppCompileAction.computeKey(
Expand Down Expand Up @@ -186,13 +201,14 @@ private CppCompileAction createAction(
TreeFileArtifact sourceTreeFileArtifact,
TreeFileArtifact outputTreeFileArtifact,
@Nullable Artifact dotdFileArtifact,
@Nullable Artifact diagnosticsFileArtifact,
NestedSet<Artifact> privateHeaders)
throws ActionExecutionException {
CppCompileActionBuilder builder =
new CppCompileActionBuilder(cppCompileActionBuilder)
.setAdditionalPrunableHeaders(privateHeaders)
.setSourceFile(sourceTreeFileArtifact)
.setOutputs(outputTreeFileArtifact, dotdFileArtifact);
.setOutputs(outputTreeFileArtifact, dotdFileArtifact, diagnosticsFileArtifact);

CcToolchainVariables.Builder buildVariables =
CcToolchainVariables.builder(cppCompileActionBuilder.getVariables());
Expand All @@ -207,6 +223,11 @@ private CppCompileAction createAction(
CompileBuildVariables.DEPENDENCY_FILE.getVariableName(),
dotdFileArtifact.getExecPathString());
}
if (diagnosticsFileArtifact != null) {
buildVariables.overrideStringVariable(
CompileBuildVariables.SERIALIZED_DIAGNOSTICS_FILE.getVariableName(),
diagnosticsFileArtifact.getExecPathString());
}

builder.setVariables(buildVariables.build());

Expand Down

0 comments on commit c8ea368

Please sign in to comment.