1 //===-- ClangHost.cpp -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "ClangHost.h"
11 
12 #include "clang/Basic/Version.h"
13 #include "clang/Config/config.h"
14 
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Threading.h"
19 
20 // Project includes
21 #include "lldb/Host/HostInfo.h"
22 #if !defined(_WIN32)
23 #include "lldb/Host/posix/HostInfoPosix.h"
24 #endif
25 #include "lldb/Utility/FileSpec.h"
26 #include "lldb/Utility/Log.h"
27 
28 #include <string>
29 
30 using namespace lldb_private;
31 
32 #if defined(_WIN32)
33 static bool ComputeClangDirectory(FileSpec &file_spec) { return false; }
34 #else
35 static bool DefaultComputeClangDirectory(FileSpec &file_spec) {
36   return HostInfoPosix::ComputePathRelativeToLibrary(
37       file_spec, (llvm::Twine("/lib") + CLANG_LIBDIR_SUFFIX + "/clang/" +
38                   CLANG_VERSION_STRING)
39                      .str());
40 }
41 
42 #if defined(__APPLE__)
43 
44 static bool VerifyClangPath(const llvm::Twine &clang_path) {
45   if (llvm::sys::fs::is_directory(clang_path))
46     return true;
47   Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
48   if (log)
49     log->Printf("VerifyClangPath(): "
50                 "failed to stat clang resource directory at \"%s\"",
51                 clang_path.str().c_str());
52   return false;
53 }
54 
55 bool lldb_private::ComputeClangDirectory(FileSpec &lldb_shlib_spec,
56                                          FileSpec &file_spec, bool verify) {
57   std::string raw_path = lldb_shlib_spec.GetPath();
58 
59   auto rev_it = llvm::sys::path::rbegin(raw_path);
60   auto r_end = llvm::sys::path::rend(raw_path);
61 
62   // Check for a Posix-style build of LLDB.
63   while (rev_it != r_end) {
64     if (*rev_it == "LLDB.framework")
65       break;
66     ++rev_it;
67   }
68 
69   if (rev_it == r_end)
70     return DefaultComputeClangDirectory(file_spec);
71 
72   // Inside Xcode and in Xcode toolchains LLDB is always in lockstep
73   // with the Swift compiler, so it can reuse its Clang resource
74   // directory. This allows LLDB and the Swift compiler to share the
75   // same Clang module cache.
76   llvm::SmallString<256> clang_path;
77   const char *swift_clang_resource_dir = "usr/lib/swift/clang";
78   auto parent = std::next(rev_it);
79   if (parent != r_end && *parent == "SharedFrameworks") {
80     // This is the top-level LLDB in the Xcode.app bundle.
81     // E.g., "Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A"
82     raw_path.resize(parent - r_end);
83     llvm::sys::path::append(clang_path, raw_path,
84                             "Developer/Toolchains/XcodeDefault.xctoolchain",
85                             swift_clang_resource_dir);
86     if (!verify || VerifyClangPath(clang_path)) {
87       file_spec.SetFile(clang_path.c_str(), true, FileSpec::Style::native);
88       return true;
89     }
90   } else if (parent != r_end && *parent == "PrivateFrameworks" &&
91              std::distance(parent, r_end) > 2) {
92     ++parent;
93     ++parent;
94     if (*parent == "System") {
95       // This is LLDB inside an Xcode toolchain.
96       // E.g., "Xcode.app/Contents/Developer/Toolchains/"               \
97       //       "My.xctoolchain/System/Library/PrivateFrameworks/LLDB.framework"
98       raw_path.resize(parent - r_end);
99       llvm::sys::path::append(clang_path, raw_path, swift_clang_resource_dir);
100       if (!verify || VerifyClangPath(clang_path)) {
101         file_spec.SetFile(clang_path.c_str(), true, FileSpec::Style::native);
102         return true;
103       }
104       raw_path = lldb_shlib_spec.GetPath();
105     }
106     raw_path.resize(rev_it - r_end);
107   } else {
108     raw_path.resize(rev_it - r_end);
109   }
110 
111   // Fall back to the Clang resource directory inside the framework.
112   raw_path.append("LLDB.framework/Resources/Clang");
113   file_spec.SetFile(raw_path.c_str(), true, FileSpec::Style::native);
114   return true;
115 }
116 
117 static bool ComputeClangDirectory(FileSpec &file_spec) {
118   if (FileSpec lldb_file_spec = HostInfo::GetShlibDir())
119     return ComputeClangDirectory(lldb_file_spec, file_spec, true);
120   return false;
121 }
122 #else  // __APPLE__
123 
124 // All non-Apple posix systems.
125 static bool ComputeClangDirectory(FileSpec &file_spec) {
126   return DefaultComputeClangDirectory(file_spec);
127 }
128 #endif // __APPLE__
129 #endif // _WIN32
130 
131 FileSpec lldb_private::GetClangResourceDir() {
132   static FileSpec g_cached_resource_dir;
133   static llvm::once_flag g_once_flag;
134   llvm::call_once(g_once_flag, []() {
135     ::ComputeClangDirectory(g_cached_resource_dir);
136     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
137     if (log)
138       log->Printf("GetClangResourceDir() => '%s'",
139                   g_cached_resource_dir.GetPath().c_str());
140   });
141   return g_cached_resource_dir;
142 }
143