Skip to content

Commit

Permalink
Add feature to produce serialized diagnostics files (bazelbuild#15600)
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.

(cherry picked from commit c8ea368)
  • Loading branch information
brentleyjones committed May 31, 2022
1 parent 4d900ce commit b703cb9
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 9 deletions.
Expand Up @@ -27,6 +27,7 @@ public enum ArtifactCategory {
INTERFACE_LIBRARY("lib", ".ifso", ".tbd", ".if.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 @@ -1540,8 +1540,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 @@ -1563,11 +1563,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 @@ -1627,6 +1634,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 @@ -1705,6 +1716,7 @@ private CcToolchainVariables setupCompileBuildVariables(
/* thinLtoOutputObjectFile= */ null,
getCopts(builder.getSourceFile(), sourceLabel),
dotdFileExecPath,
diagnosticsFileExecPath,
usePic,
ccCompilationContext.getExternalIncludeDirs(),
additionalBuildVariables);
Expand Down
Expand Up @@ -305,6 +305,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 @@ -222,6 +222,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 @@ -250,6 +251,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 @@ -269,7 +271,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 @@ -290,7 +299,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 @@ -314,6 +329,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 @@ -330,6 +346,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 @@ -344,6 +363,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 @@ -292,6 +293,7 @@ public CppCompileAction buildAndVerify(Consumer<String> errorCollector) {
prunableHeaders,
outputFile,
dotdFile,
diagnosticsFile,
gcnoFile,
dwoFile,
ltoIndexingFile,
Expand Down Expand Up @@ -446,9 +448,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 @@ -475,6 +483,16 @@ public CppCompileActionBuilder setOutputs(
} else {
dotdFile = null;
}
if (serializedDiagnosticsFilesEnabled()) {
String diagnosticsFileName =
CppHelper.getDiagnosticsFileName(
ruleErrorConsumer, ccToolchain, outputCategory, outputName);
diagnosticsFile =
CppHelper.getCompileOutputArtifact(
actionConstructionContext, label, diagnosticsFileName, configuration);
} else {
diagnosticsFile = null;
}
return this;
}

Expand All @@ -500,6 +518,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 @@ -48,6 +48,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 Iterable<ArtifactCategory> categories;
private final ActionOwner actionOwner;
Expand All @@ -60,6 +61,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 @@ -72,6 +74,7 @@ public final class CppCompileActionTemplate extends ActionKeyCacher
SpecialArtifact sourceTreeArtifact,
SpecialArtifact outputTreeArtifact,
SpecialArtifact dotdTreeArtifact,
SpecialArtifact diagnosticsTreeArtifact,
CppCompileActionBuilder cppCompileActionBuilder,
CcToolchainProvider toolchain,
Iterable<ArtifactCategory> categories,
Expand All @@ -80,6 +83,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 @@ -137,9 +141,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));
} catch (EvalException e) {
throw new ActionTemplateExpansionException(e);
}
Expand All @@ -160,6 +174,7 @@ protected void computeKey(
cppCompileActionBuilder.getCoptsFilter(),
CppActionNames.CPP_COMPILE,
dotdTreeArtifact,
diagnosticsTreeArtifact,
cppCompileActionBuilder.getFeatureConfiguration(),
cppCompileActionBuilder.getVariables());
CppCompileAction.computeKey(
Expand Down Expand Up @@ -190,12 +205,13 @@ private CppCompileAction createAction(
Artifact sourceTreeFileArtifact,
Artifact outputTreeFileArtifact,
@Nullable Artifact dotdFileArtifact,
@Nullable Artifact diagnosticsFileArtifact,
NestedSet<Artifact> privateHeaders)
throws ActionTemplateExpansionException {
CppCompileActionBuilder builder = new CppCompileActionBuilder(cppCompileActionBuilder);
builder.setAdditionalPrunableHeaders(privateHeaders);
builder.setSourceFile(sourceTreeFileArtifact);
builder.setOutputs(outputTreeFileArtifact, dotdFileArtifact);
builder.setOutputs(outputTreeFileArtifact, dotdFileArtifact, diagnosticsFileArtifact);

CcToolchainVariables.Builder buildVariables =
CcToolchainVariables.builder(cppCompileActionBuilder.getVariables());
Expand All @@ -210,6 +226,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 b703cb9

Please sign in to comment.