/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangHost.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- ClangHost.cpp -----------------------------------------------------===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "ClangHost.h" |
10 | | |
11 | | #include "clang/Basic/Version.h" |
12 | | #include "clang/Config/config.h" |
13 | | #include "clang/Driver/Driver.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/Config.h" |
21 | | #include "lldb/Host/FileSystem.h" |
22 | | #include "lldb/Host/HostInfo.h" |
23 | | #include "lldb/Utility/FileSpec.h" |
24 | | #include "lldb/Utility/LLDBLog.h" |
25 | | #include "lldb/Utility/Log.h" |
26 | | |
27 | | #include <string> |
28 | | |
29 | | using namespace lldb_private; |
30 | | |
31 | 476 | static bool VerifyClangPath(const llvm::Twine &clang_path) { |
32 | 476 | if (FileSystem::Instance().IsDirectory(clang_path)) |
33 | 455 | return true; |
34 | 21 | Log *log = GetLog(LLDBLog::Host); |
35 | 21 | LLDB_LOGF(log, |
36 | 21 | "VerifyClangPath(): " |
37 | 21 | "failed to stat clang resource directory at \"%s\"", |
38 | 21 | clang_path.str().c_str()); |
39 | 21 | return false; |
40 | 476 | } |
41 | | |
42 | | /// |
43 | | /// This will compute the clang resource directory assuming that clang was |
44 | | /// installed with the same prefix as lldb. |
45 | | /// |
46 | | /// If verify is true, the first candidate resource directory will be returned. |
47 | | /// This mode is only used for testing. |
48 | | /// |
49 | | static bool DefaultComputeClangResourceDirectory(FileSpec &lldb_shlib_spec, |
50 | | FileSpec &file_spec, |
51 | 467 | bool verify) { |
52 | 467 | Log *log = GetLog(LLDBLog::Host); |
53 | 467 | std::string raw_path = lldb_shlib_spec.GetPath(); |
54 | 467 | llvm::StringRef parent_dir = llvm::sys::path::parent_path(raw_path); |
55 | 467 | static const std::string clang_resource_path = |
56 | 467 | clang::driver::Driver::GetResourcesPath("bin/lldb", CLANG_RESOURCE_DIR); |
57 | | |
58 | 467 | static const llvm::StringRef kResourceDirSuffixes[] = { |
59 | | // LLVM.org's build of LLDB uses the clang resource directory placed |
60 | | // in $install_dir/lib{,64}/clang/$clang_version or |
61 | | // $install_dir/bin/$CLANG_RESOURCE_DIR |
62 | 467 | clang_resource_path, |
63 | | // swift-lldb uses the clang resource directory copied from swift, which |
64 | | // by default is placed in $install_dir/lib{,64}/lldb/clang. LLDB places |
65 | | // it there, so we use LLDB_INSTALL_LIBDIR_BASENAME. |
66 | 467 | LLDB_INSTALL_LIBDIR_BASENAME "/lldb/clang", |
67 | 467 | }; |
68 | | |
69 | 477 | for (const auto &Suffix : kResourceDirSuffixes) { |
70 | 477 | llvm::SmallString<256> clang_dir(parent_dir); |
71 | 477 | llvm::SmallString<32> relative_path(Suffix); |
72 | 477 | llvm::sys::path::native(relative_path); |
73 | 477 | llvm::sys::path::append(clang_dir, relative_path); |
74 | 477 | if (!verify || VerifyClangPath(clang_dir)475 ) { |
75 | 457 | LLDB_LOG(log, |
76 | 457 | "DefaultComputeClangResourceDir: Setting ClangResourceDir " |
77 | 457 | "to \"{0}\", verify = {1}", |
78 | 457 | clang_dir.str(), verify ? "true" : "false"); |
79 | 457 | file_spec.SetDirectory(clang_dir); |
80 | 457 | FileSystem::Instance().Resolve(file_spec); |
81 | 457 | return true; |
82 | 457 | } |
83 | 477 | } |
84 | | |
85 | 10 | return false; |
86 | 467 | } |
87 | | |
88 | | bool lldb_private::ComputeClangResourceDirectory(FileSpec &lldb_shlib_spec, |
89 | | FileSpec &file_spec, |
90 | 473 | bool verify) { |
91 | | #if !defined(__APPLE__) |
92 | | return DefaultComputeClangResourceDirectory(lldb_shlib_spec, file_spec, |
93 | | verify); |
94 | | #else |
95 | 473 | std::string raw_path = lldb_shlib_spec.GetPath(); |
96 | | |
97 | 473 | auto rev_it = llvm::sys::path::rbegin(raw_path); |
98 | 473 | auto r_end = llvm::sys::path::rend(raw_path); |
99 | | |
100 | | // Check for a Posix-style build of LLDB. |
101 | 4.23k | while (rev_it != r_end) { |
102 | 3.76k | if (*rev_it == "LLDB.framework") |
103 | 6 | break; |
104 | 3.75k | ++rev_it; |
105 | 3.75k | } |
106 | | |
107 | | // We found a non-framework build of LLDB |
108 | 473 | if (rev_it == r_end) |
109 | 467 | return DefaultComputeClangResourceDirectory(lldb_shlib_spec, file_spec, |
110 | 467 | verify); |
111 | | |
112 | | // Inside Xcode and in Xcode toolchains LLDB is always in lockstep |
113 | | // with the Swift compiler, so it can reuse its Clang resource |
114 | | // directory. This allows LLDB and the Swift compiler to share the |
115 | | // same Clang module cache. |
116 | 6 | llvm::SmallString<256> clang_path; |
117 | 6 | const char *swift_clang_resource_dir = "usr/lib/swift/clang"; |
118 | 6 | auto parent = std::next(rev_it); |
119 | 6 | if (parent != r_end && *parent == "SharedFrameworks") { |
120 | | // This is the top-level LLDB in the Xcode.app bundle. |
121 | | // E.g., "Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A" |
122 | 3 | raw_path.resize(parent - r_end); |
123 | 3 | llvm::sys::path::append(clang_path, raw_path, |
124 | 3 | "Developer/Toolchains/XcodeDefault.xctoolchain", |
125 | 3 | swift_clang_resource_dir); |
126 | 3 | if (!verify || VerifyClangPath(clang_path)1 ) { |
127 | 2 | file_spec.SetDirectory(clang_path); |
128 | 2 | FileSystem::Instance().Resolve(file_spec); |
129 | 2 | return true; |
130 | 2 | } |
131 | 3 | } else if (parent != r_end && *parent == "PrivateFrameworks" && |
132 | 3 | std::distance(parent, r_end) > 22 ) { |
133 | 2 | ++parent; |
134 | 2 | ++parent; |
135 | 2 | if (*parent == "System") { |
136 | | // This is LLDB inside an Xcode toolchain. |
137 | | // E.g., "Xcode.app/Contents/Developer/Toolchains/" \ |
138 | | // "My.xctoolchain/System/Library/PrivateFrameworks/LLDB.framework" |
139 | 1 | raw_path.resize(parent - r_end); |
140 | 1 | llvm::sys::path::append(clang_path, raw_path, swift_clang_resource_dir); |
141 | 1 | if (!verify || VerifyClangPath(clang_path)0 ) { |
142 | 1 | file_spec.SetDirectory(clang_path); |
143 | 1 | FileSystem::Instance().Resolve(file_spec); |
144 | 1 | return true; |
145 | 1 | } |
146 | 1 | } |
147 | 2 | } |
148 | | |
149 | | // Fall back to the Clang resource directory inside the framework. |
150 | 3 | raw_path = lldb_shlib_spec.GetPath(); |
151 | 3 | raw_path.resize(rev_it - r_end); |
152 | 3 | raw_path.append("LLDB.framework/Resources/Clang"); |
153 | 3 | file_spec.SetDirectory(raw_path); |
154 | 3 | FileSystem::Instance().Resolve(file_spec); |
155 | 3 | return true; |
156 | 6 | #endif // __APPLE__ |
157 | 6 | } |
158 | | |
159 | 1.77k | FileSpec lldb_private::GetClangResourceDir() { |
160 | 1.77k | static FileSpec g_cached_resource_dir; |
161 | 1.77k | static llvm::once_flag g_once_flag; |
162 | 1.77k | llvm::call_once(g_once_flag, []() { |
163 | 464 | if (FileSpec lldb_file_spec = HostInfo::GetShlibDir()) |
164 | 464 | ComputeClangResourceDirectory(lldb_file_spec, g_cached_resource_dir, |
165 | 464 | true); |
166 | 464 | Log *log = GetLog(LLDBLog::Host); |
167 | 464 | LLDB_LOGF(log, "GetClangResourceDir() => '%s'", |
168 | 464 | g_cached_resource_dir.GetPath().c_str()); |
169 | 464 | }); |
170 | 1.77k | return g_cached_resource_dir; |
171 | 1.77k | } |