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 #include "lldb/Host/FileSystem.h"
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)
ComputeClangDirectory(FileSpec & file_spec)33 static bool ComputeClangDirectory(FileSpec &file_spec) { return false; }
34 #else
DefaultComputeClangDirectory(FileSpec & file_spec)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
VerifyClangPath(const llvm::Twine & clang_path)44 static bool VerifyClangPath(const llvm::Twine &clang_path) {
45 if (FileSystem::Instance().IsDirectory(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
ComputeClangDirectory(FileSpec & lldb_shlib_spec,FileSpec & file_spec,bool verify)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(), FileSpec::Style::native);
88 FileSystem::Instance().Resolve(file_spec);
89 return true;
90 }
91 } else if (parent != r_end && *parent == "PrivateFrameworks" &&
92 std::distance(parent, r_end) > 2) {
93 ++parent;
94 ++parent;
95 if (*parent == "System") {
96 // This is LLDB inside an Xcode toolchain.
97 // E.g., "Xcode.app/Contents/Developer/Toolchains/" \
98 // "My.xctoolchain/System/Library/PrivateFrameworks/LLDB.framework"
99 raw_path.resize(parent - r_end);
100 llvm::sys::path::append(clang_path, raw_path, swift_clang_resource_dir);
101 if (!verify || VerifyClangPath(clang_path)) {
102 file_spec.SetFile(clang_path.c_str(), FileSpec::Style::native);
103 FileSystem::Instance().Resolve(file_spec);
104 return true;
105 }
106 raw_path = lldb_shlib_spec.GetPath();
107 }
108 raw_path.resize(rev_it - r_end);
109 } else {
110 raw_path.resize(rev_it - r_end);
111 }
112
113 // Fall back to the Clang resource directory inside the framework.
114 raw_path.append("LLDB.framework/Resources/Clang");
115 file_spec.SetFile(raw_path.c_str(), FileSpec::Style::native);
116 FileSystem::Instance().Resolve(file_spec);
117 return true;
118 }
119
ComputeClangDirectory(FileSpec & file_spec)120 static bool ComputeClangDirectory(FileSpec &file_spec) {
121 if (FileSpec lldb_file_spec = HostInfo::GetShlibDir())
122 return ComputeClangDirectory(lldb_file_spec, file_spec, true);
123 return false;
124 }
125 #else // __APPLE__
126
127 // All non-Apple posix systems.
ComputeClangDirectory(FileSpec & file_spec)128 static bool ComputeClangDirectory(FileSpec &file_spec) {
129 return DefaultComputeClangDirectory(file_spec);
130 }
131 #endif // __APPLE__
132 #endif // _WIN32
133
GetClangResourceDir()134 FileSpec lldb_private::GetClangResourceDir() {
135 static FileSpec g_cached_resource_dir;
136 static llvm::once_flag g_once_flag;
137 llvm::call_once(g_once_flag, []() {
138 ::ComputeClangDirectory(g_cached_resource_dir);
139 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
140 if (log)
141 log->Printf("GetClangResourceDir() => '%s'",
142 g_cached_resource_dir.GetPath().c_str());
143 });
144 return g_cached_resource_dir;
145 }
146