@@ -72,6 +72,7 @@ enum ResourceDirRecipeKind {
72
72
RDRK_InvokeCompiler,
73
73
};
74
74
75
+ static std::string OutputFileName = " -" ;
75
76
static ScanningMode ScanMode = ScanningMode::DependencyDirectivesScan;
76
77
static ScanningOutputFormat Format = ScanningOutputFormat::Make;
77
78
static ScanningOptimizations OptimizeArgs;
@@ -175,6 +176,9 @@ static void ParseArgs(int argc, char **argv) {
175
176
if (const llvm::opt::Arg *A = Args.getLastArg (OPT_module_files_dir_EQ))
176
177
ModuleFilesDir = A->getValue ();
177
178
179
+ if (const llvm::opt::Arg *A = Args.getLastArg (OPT_o))
180
+ OutputFileName = A->getValue ();
181
+
178
182
EagerLoadModules = Args.hasArg (OPT_eager_load_pcm);
179
183
180
184
if (const llvm::opt::Arg *A = Args.getLastArg (OPT_j)) {
@@ -419,6 +423,11 @@ class FullDeps {
419
423
}
420
424
421
425
void printFullOutput (raw_ostream &OS) {
426
+ // Skip sorting modules and constructing the JSON object if the output
427
+ // cannot be observed anyway. This makes timings less noisy.
428
+ if (&OS == &llvm::nulls ())
429
+ return ;
430
+
422
431
// Sort the modules by name to get a deterministic order.
423
432
std::vector<IndexedModuleID> ModuleIDs;
424
433
for (auto &&M : Modules)
@@ -849,8 +858,25 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
849
858
});
850
859
851
860
SharedStream Errs (llvm::errs ());
852
- // Print out the dependency results to STDOUT by default.
853
- SharedStream DependencyOS (llvm::outs ());
861
+
862
+ std::optional<llvm::raw_fd_ostream> FileOS;
863
+ llvm::raw_ostream &ThreadUnsafeDependencyOS = [&]() -> llvm::raw_ostream & {
864
+ if (OutputFileName == " -" )
865
+ return llvm::outs ();
866
+
867
+ if (OutputFileName == " /dev/null" )
868
+ return llvm::nulls ();
869
+
870
+ std::error_code EC;
871
+ FileOS.emplace (OutputFileName, EC);
872
+ if (EC) {
873
+ llvm::errs () << " Failed to open output file '" << OutputFileName
874
+ << " ': " << llvm::errorCodeToError (EC) << ' \n ' ;
875
+ std::exit (1 );
876
+ }
877
+ return *FileOS;
878
+ }();
879
+ SharedStream DependencyOS (ThreadUnsafeDependencyOS);
854
880
855
881
std::vector<tooling::CompileCommand> Inputs =
856
882
AdjustingCompilations->getAllCompileCommands ();
@@ -991,9 +1017,9 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
991
1017
HadErrors = true ;
992
1018
993
1019
if (Format == ScanningOutputFormat::Full)
994
- FD->printFullOutput (llvm::outs () );
1020
+ FD->printFullOutput (ThreadUnsafeDependencyOS );
995
1021
else if (Format == ScanningOutputFormat::P1689)
996
- PD.printDependencies (llvm::outs () );
1022
+ PD.printDependencies (ThreadUnsafeDependencyOS );
997
1023
998
1024
return HadErrors;
999
1025
}
0 commit comments