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