1 //===-- ClangHost.cpp -------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 #include "clang/Basic/Version.h"
12 #include "clang/Config/config.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/Threading.h"
19 #include "lldb/Host/Config.h"
20 #include "lldb/Host/FileSystem.h"
21 #include "lldb/Host/HostInfo.h"
22 #include "lldb/Utility/FileSpec.h"
23 #include "lldb/Utility/Log.h"
27 using namespace lldb_private;
29 static bool VerifyClangPath(const llvm::Twine &clang_path) {
30 if (FileSystem::Instance().IsDirectory(clang_path))
32 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
34 log->Printf("VerifyClangPath(): "
35 "failed to stat clang resource directory at \"%s\"",
36 clang_path.str().c_str());
41 /// This will compute the clang resource directory assuming that clang was
42 /// installed with the same prefix as lldb.
44 /// If verify is true, the first candidate resource directory will be returned.
45 /// This mode is only used for testing.
47 static bool DefaultComputeClangResourceDirectory(FileSpec &lldb_shlib_spec,
50 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
51 std::string raw_path = lldb_shlib_spec.GetPath();
52 llvm::StringRef parent_dir = llvm::sys::path::parent_path(raw_path);
54 static const llvm::StringRef kResourceDirSuffixes[] = {
55 // LLVM.org's build of LLDB uses the clang resource directory placed
56 // in $install_dir/lib{,64}/clang/$clang_version.
57 "lib" CLANG_LIBDIR_SUFFIX "/clang/" CLANG_VERSION_STRING,
58 // swift-lldb uses the clang resource directory copied from swift, which
59 // by default is placed in $install_dir/lib{,64}/lldb/clang. LLDB places
60 // it there, so we use LLDB_LIBDIR_SUFFIX.
61 "lib" LLDB_LIBDIR_SUFFIX "/lldb/clang",
64 for (const auto &Suffix : kResourceDirSuffixes) {
65 llvm::SmallString<256> clang_dir(parent_dir);
66 llvm::SmallString<32> relative_path(Suffix);
67 llvm::sys::path::native(relative_path);
68 llvm::sys::path::append(clang_dir, relative_path);
69 if (!verify || VerifyClangPath(clang_dir)) {
71 log->Printf("DefaultComputeClangResourceDir: Setting ClangResourceDir "
72 "to \"%s\", verify = %s",
73 clang_dir.str().str().c_str(), verify ? "true" : "false");
74 file_spec.GetDirectory().SetString(clang_dir);
75 FileSystem::Instance().Resolve(file_spec);
83 bool lldb_private::ComputeClangResourceDirectory(FileSpec &lldb_shlib_spec,
84 FileSpec &file_spec, bool verify) {
85 #if !defined(__APPLE__)
86 return DefaultComputeClangResourceDirectory(lldb_shlib_spec, file_spec,
89 std::string raw_path = lldb_shlib_spec.GetPath();
91 auto rev_it = llvm::sys::path::rbegin(raw_path);
92 auto r_end = llvm::sys::path::rend(raw_path);
94 // Check for a Posix-style build of LLDB.
95 while (rev_it != r_end) {
96 if (*rev_it == "LLDB.framework")
101 // We found a non-framework build of LLDB
103 return DefaultComputeClangResourceDirectory(lldb_shlib_spec, file_spec,
106 // Inside Xcode and in Xcode toolchains LLDB is always in lockstep
107 // with the Swift compiler, so it can reuse its Clang resource
108 // directory. This allows LLDB and the Swift compiler to share the
109 // same Clang module cache.
110 llvm::SmallString<256> clang_path;
111 const char *swift_clang_resource_dir = "usr/lib/swift/clang";
112 auto parent = std::next(rev_it);
113 if (parent != r_end && *parent == "SharedFrameworks") {
114 // This is the top-level LLDB in the Xcode.app bundle.
115 // E.g., "Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A"
116 raw_path.resize(parent - r_end);
117 llvm::sys::path::append(clang_path, raw_path,
118 "Developer/Toolchains/XcodeDefault.xctoolchain",
119 swift_clang_resource_dir);
120 if (!verify || VerifyClangPath(clang_path)) {
121 file_spec.GetDirectory().SetString(clang_path.c_str());
122 FileSystem::Instance().Resolve(file_spec);
125 } else if (parent != r_end && *parent == "PrivateFrameworks" &&
126 std::distance(parent, r_end) > 2) {
129 if (*parent == "System") {
130 // This is LLDB inside an Xcode toolchain.
131 // E.g., "Xcode.app/Contents/Developer/Toolchains/" \
132 // "My.xctoolchain/System/Library/PrivateFrameworks/LLDB.framework"
133 raw_path.resize(parent - r_end);
134 llvm::sys::path::append(clang_path, raw_path, swift_clang_resource_dir);
135 if (!verify || VerifyClangPath(clang_path)) {
136 file_spec.GetDirectory().SetString(clang_path.c_str());
137 FileSystem::Instance().Resolve(file_spec);
140 raw_path = lldb_shlib_spec.GetPath();
142 raw_path.resize(rev_it - r_end);
144 raw_path.resize(rev_it - r_end);
147 // Fall back to the Clang resource directory inside the framework.
148 raw_path.append("LLDB.framework/Resources/Clang");
149 file_spec.GetDirectory().SetString(raw_path.c_str());
150 FileSystem::Instance().Resolve(file_spec);
155 FileSpec lldb_private::GetClangResourceDir() {
156 static FileSpec g_cached_resource_dir;
157 static llvm::once_flag g_once_flag;
158 llvm::call_once(g_once_flag, []() {
159 if (FileSpec lldb_file_spec = HostInfo::GetShlibDir())
160 ComputeClangResourceDirectory(lldb_file_spec, g_cached_resource_dir,
162 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
164 log->Printf("GetClangResourceDir() => '%s'",
165 g_cached_resource_dir.GetPath().c_str());
167 return g_cached_resource_dir;