1 //===-- ClangHost.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ClangHost.h"
12 #include "clang/Basic/Version.h"
13 #include "clang/Config/config.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Threading.h"
20 #include "lldb/Host/FileSystem.h"
21 #include "lldb/Host/HostInfo.h"
23 #include "lldb/Host/posix/HostInfoPosix.h"
25 #include "lldb/Utility/FileSpec.h"
26 #include "lldb/Utility/Log.h"
30 using namespace lldb_private;
33 static bool ComputeClangDirectory(FileSpec &file_spec) { return false; }
35 static bool DefaultComputeClangDirectory(FileSpec &file_spec) {
36 return HostInfoPosix::ComputePathRelativeToLibrary(
37 file_spec, (llvm::Twine("/lib") + CLANG_LIBDIR_SUFFIX + "/clang/" +
42 #if defined(__APPLE__)
44 static bool VerifyClangPath(const llvm::Twine &clang_path) {
45 if (FileSystem::Instance().IsDirectory(clang_path))
47 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
49 log->Printf("VerifyClangPath(): "
50 "failed to stat clang resource directory at \"%s\"",
51 clang_path.str().c_str());
55 bool lldb_private::ComputeClangDirectory(FileSpec &lldb_shlib_spec,
56 FileSpec &file_spec, bool verify) {
57 std::string raw_path = lldb_shlib_spec.GetPath();
59 auto rev_it = llvm::sys::path::rbegin(raw_path);
60 auto r_end = llvm::sys::path::rend(raw_path);
62 // Check for a Posix-style build of LLDB.
63 while (rev_it != r_end) {
64 if (*rev_it == "LLDB.framework")
70 return DefaultComputeClangDirectory(file_spec);
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);
91 } else if (parent != r_end && *parent == "PrivateFrameworks" &&
92 std::distance(parent, r_end) > 2) {
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);
106 raw_path = lldb_shlib_spec.GetPath();
108 raw_path.resize(rev_it - r_end);
110 raw_path.resize(rev_it - r_end);
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);
120 static bool ComputeClangDirectory(FileSpec &file_spec) {
121 if (FileSpec lldb_file_spec = HostInfo::GetShlibDir())
122 return ComputeClangDirectory(lldb_file_spec, file_spec, true);
127 // All non-Apple posix systems.
128 static bool ComputeClangDirectory(FileSpec &file_spec) {
129 return DefaultComputeClangDirectory(file_spec);
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);
141 log->Printf("GetClangResourceDir() => '%s'",
142 g_cached_resource_dir.GetPath().c_str());
144 return g_cached_resource_dir;