10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
13
- #include " clang/Frontend/Utils.h"
14
13
#include " clang/Basic/FileManager.h"
15
14
#include " clang/Basic/SourceManager.h"
16
15
#include " clang/Frontend/DependencyOutputOptions.h"
17
16
#include " clang/Frontend/FrontendDiagnostic.h"
17
+ #include " clang/Frontend/Utils.h"
18
18
#include " clang/Lex/DirectoryLookup.h"
19
19
#include " clang/Lex/ModuleMap.h"
20
20
#include " clang/Lex/PPCallbacks.h"
23
23
#include " llvm/ADT/StringSet.h"
24
24
#include " llvm/Support/FileSystem.h"
25
25
#include " llvm/Support/Path.h"
26
+ #include " llvm/Support/VirtualFileSystem.h"
26
27
#include " llvm/Support/raw_ostream.h"
27
28
#include < optional>
29
+ #include < system_error>
28
30
29
31
using namespace clang ;
30
32
@@ -236,6 +238,7 @@ void DependencyFileGenerator::attachToPreprocessor(Preprocessor &PP) {
236
238
PP.SetSuppressIncludeNotFoundError (true );
237
239
238
240
DependencyCollector::attachToPreprocessor (PP);
241
+ FS = PP.getFileManager ().getVirtualFileSystemPtr ();
239
242
}
240
243
241
244
bool DependencyFileGenerator::sawDependency (StringRef Filename, bool FromModule,
@@ -312,11 +315,22 @@ void DependencyFileGenerator::finishedMainFile(DiagnosticsEngine &Diags) {
312
315
// / https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
313
316
// / https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
314
317
// / for Windows file-naming info.
315
- static void PrintFilename (raw_ostream &OS, StringRef Filename,
318
+ static void printFilename (raw_ostream &OS, llvm::vfs::FileSystem *FS,
319
+ StringRef Filename,
316
320
DependencyOutputFormat OutputFormat) {
317
321
// Convert filename to platform native path
318
322
llvm::SmallString<256 > NativePath;
319
323
llvm::sys::path::native (Filename.str (), NativePath);
324
+ // Resolve absolute path. Make and Ninja canonicalize paths
325
+ // without checking for symbolic links in the path, for performance concerns.
326
+ // If there is something like `/bin/../lib64` -> `/usr/lib64`
327
+ // (where `/bin` links to `/usr/bin`), Make will see them as `/lib64`.
328
+ if (FS != nullptr && llvm::sys::path::is_absolute (NativePath)) {
329
+ llvm::SmallString<256 > NativePathTmp = NativePath;
330
+ std::error_code EC = FS->getRealPath (NativePathTmp, NativePath);
331
+ if (EC)
332
+ NativePath = NativePathTmp;
333
+ }
320
334
321
335
if (OutputFormat == DependencyOutputFormat::NMake) {
322
336
// Add quotes if needed. These are the characters listed as "special" to
@@ -400,7 +414,7 @@ void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) {
400
414
Columns = 2 ;
401
415
}
402
416
OS << ' ' ;
403
- PrintFilename (OS, File, OutputFormat);
417
+ printFilename (OS, FS. get () , File, OutputFormat);
404
418
Columns += N + 1 ;
405
419
}
406
420
OS << ' \n ' ;
@@ -411,7 +425,7 @@ void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) {
411
425
for (auto I = Files.begin (), E = Files.end (); I != E; ++I) {
412
426
if (Index++ == InputFileIndex)
413
427
continue ;
414
- PrintFilename (OS, *I, OutputFormat);
428
+ printFilename (OS, FS. get () , *I, OutputFormat);
415
429
OS << " :\n " ;
416
430
}
417
431
}
0 commit comments