xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/ExpressionParser/Clang/ClangHost.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- ClangHost.cpp -----------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "ClangHost.h"
10061da546Spatrick 
11061da546Spatrick #include "clang/Basic/Version.h"
12061da546Spatrick #include "clang/Config/config.h"
13061da546Spatrick 
14061da546Spatrick #include "llvm/ADT/StringRef.h"
15061da546Spatrick #include "llvm/ADT/Twine.h"
16061da546Spatrick #include "llvm/Support/FileSystem.h"
17061da546Spatrick #include "llvm/Support/Threading.h"
18061da546Spatrick 
19061da546Spatrick #include "lldb/Host/Config.h"
20061da546Spatrick #include "lldb/Host/FileSystem.h"
21061da546Spatrick #include "lldb/Host/HostInfo.h"
22061da546Spatrick #include "lldb/Utility/FileSpec.h"
23*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
24061da546Spatrick #include "lldb/Utility/Log.h"
25061da546Spatrick 
26061da546Spatrick #include <string>
27061da546Spatrick 
28061da546Spatrick using namespace lldb_private;
29061da546Spatrick 
VerifyClangPath(const llvm::Twine & clang_path)30061da546Spatrick static bool VerifyClangPath(const llvm::Twine &clang_path) {
31061da546Spatrick   if (FileSystem::Instance().IsDirectory(clang_path))
32061da546Spatrick     return true;
33*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Host);
34061da546Spatrick   LLDB_LOGF(log,
35061da546Spatrick             "VerifyClangPath(): "
36061da546Spatrick             "failed to stat clang resource directory at \"%s\"",
37061da546Spatrick             clang_path.str().c_str());
38061da546Spatrick   return false;
39061da546Spatrick }
40061da546Spatrick 
41061da546Spatrick ///
42061da546Spatrick /// This will compute the clang resource directory assuming that clang was
43061da546Spatrick /// installed with the same prefix as lldb.
44061da546Spatrick ///
45061da546Spatrick /// If verify is true, the first candidate resource directory will be returned.
46061da546Spatrick /// This mode is only used for testing.
47061da546Spatrick ///
DefaultComputeClangResourceDirectory(FileSpec & lldb_shlib_spec,FileSpec & file_spec,bool verify)48061da546Spatrick static bool DefaultComputeClangResourceDirectory(FileSpec &lldb_shlib_spec,
49061da546Spatrick                                                  FileSpec &file_spec,
50061da546Spatrick                                                  bool verify) {
51*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Host);
52061da546Spatrick   std::string raw_path = lldb_shlib_spec.GetPath();
53061da546Spatrick   llvm::StringRef parent_dir = llvm::sys::path::parent_path(raw_path);
54061da546Spatrick 
55061da546Spatrick   static const llvm::StringRef kResourceDirSuffixes[] = {
56061da546Spatrick       // LLVM.org's build of LLDB uses the clang resource directory placed
57061da546Spatrick       // in $install_dir/lib{,64}/clang/$clang_version.
58*f6aab3d8Srobert       CLANG_INSTALL_LIBDIR_BASENAME "/clang/" CLANG_VERSION_MAJOR_STRING,
59061da546Spatrick       // swift-lldb uses the clang resource directory copied from swift, which
60061da546Spatrick       // by default is placed in $install_dir/lib{,64}/lldb/clang. LLDB places
61*f6aab3d8Srobert       // it there, so we use LLDB_INSTALL_LIBDIR_BASENAME.
62*f6aab3d8Srobert       LLDB_INSTALL_LIBDIR_BASENAME "/lldb/clang",
63061da546Spatrick   };
64061da546Spatrick 
65061da546Spatrick   for (const auto &Suffix : kResourceDirSuffixes) {
66061da546Spatrick     llvm::SmallString<256> clang_dir(parent_dir);
67061da546Spatrick     llvm::SmallString<32> relative_path(Suffix);
68061da546Spatrick     llvm::sys::path::native(relative_path);
69061da546Spatrick     llvm::sys::path::append(clang_dir, relative_path);
70061da546Spatrick     if (!verify || VerifyClangPath(clang_dir)) {
71dda28197Spatrick       LLDB_LOG(log,
72061da546Spatrick                "DefaultComputeClangResourceDir: Setting ClangResourceDir "
73dda28197Spatrick                "to \"{0}\", verify = {1}",
74dda28197Spatrick                clang_dir.str(), verify ? "true" : "false");
75*f6aab3d8Srobert       file_spec.SetDirectory(clang_dir);
76061da546Spatrick       FileSystem::Instance().Resolve(file_spec);
77061da546Spatrick       return true;
78061da546Spatrick     }
79061da546Spatrick   }
80061da546Spatrick 
81061da546Spatrick   return false;
82061da546Spatrick }
83061da546Spatrick 
ComputeClangResourceDirectory(FileSpec & lldb_shlib_spec,FileSpec & file_spec,bool verify)84061da546Spatrick bool lldb_private::ComputeClangResourceDirectory(FileSpec &lldb_shlib_spec,
85061da546Spatrick                                          FileSpec &file_spec, bool verify) {
86061da546Spatrick #if !defined(__APPLE__)
87061da546Spatrick   return DefaultComputeClangResourceDirectory(lldb_shlib_spec, file_spec,
88061da546Spatrick                                               verify);
89061da546Spatrick #else
90061da546Spatrick   std::string raw_path = lldb_shlib_spec.GetPath();
91061da546Spatrick 
92061da546Spatrick   auto rev_it = llvm::sys::path::rbegin(raw_path);
93061da546Spatrick   auto r_end = llvm::sys::path::rend(raw_path);
94061da546Spatrick 
95061da546Spatrick   // Check for a Posix-style build of LLDB.
96061da546Spatrick   while (rev_it != r_end) {
97061da546Spatrick     if (*rev_it == "LLDB.framework")
98061da546Spatrick       break;
99061da546Spatrick     ++rev_it;
100061da546Spatrick   }
101061da546Spatrick 
102061da546Spatrick   // We found a non-framework build of LLDB
103061da546Spatrick   if (rev_it == r_end)
104061da546Spatrick     return DefaultComputeClangResourceDirectory(lldb_shlib_spec, file_spec,
105061da546Spatrick                                                 verify);
106061da546Spatrick 
107061da546Spatrick   // Inside Xcode and in Xcode toolchains LLDB is always in lockstep
108061da546Spatrick   // with the Swift compiler, so it can reuse its Clang resource
109061da546Spatrick   // directory. This allows LLDB and the Swift compiler to share the
110061da546Spatrick   // same Clang module cache.
111061da546Spatrick   llvm::SmallString<256> clang_path;
112061da546Spatrick   const char *swift_clang_resource_dir = "usr/lib/swift/clang";
113061da546Spatrick   auto parent = std::next(rev_it);
114061da546Spatrick   if (parent != r_end && *parent == "SharedFrameworks") {
115061da546Spatrick     // This is the top-level LLDB in the Xcode.app bundle.
116061da546Spatrick     // E.g., "Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A"
117061da546Spatrick     raw_path.resize(parent - r_end);
118061da546Spatrick     llvm::sys::path::append(clang_path, raw_path,
119061da546Spatrick                             "Developer/Toolchains/XcodeDefault.xctoolchain",
120061da546Spatrick                             swift_clang_resource_dir);
121061da546Spatrick     if (!verify || VerifyClangPath(clang_path)) {
122*f6aab3d8Srobert       file_spec.SetDirectory(clang_path);
123061da546Spatrick       FileSystem::Instance().Resolve(file_spec);
124061da546Spatrick       return true;
125061da546Spatrick     }
126061da546Spatrick   } else if (parent != r_end && *parent == "PrivateFrameworks" &&
127061da546Spatrick              std::distance(parent, r_end) > 2) {
128061da546Spatrick     ++parent;
129061da546Spatrick     ++parent;
130061da546Spatrick     if (*parent == "System") {
131061da546Spatrick       // This is LLDB inside an Xcode toolchain.
132061da546Spatrick       // E.g., "Xcode.app/Contents/Developer/Toolchains/"               \
133061da546Spatrick       //       "My.xctoolchain/System/Library/PrivateFrameworks/LLDB.framework"
134061da546Spatrick       raw_path.resize(parent - r_end);
135061da546Spatrick       llvm::sys::path::append(clang_path, raw_path, swift_clang_resource_dir);
136061da546Spatrick       if (!verify || VerifyClangPath(clang_path)) {
137*f6aab3d8Srobert         file_spec.SetDirectory(clang_path);
138061da546Spatrick         FileSystem::Instance().Resolve(file_spec);
139061da546Spatrick         return true;
140061da546Spatrick       }
141061da546Spatrick     }
142061da546Spatrick   }
143061da546Spatrick 
144061da546Spatrick   // Fall back to the Clang resource directory inside the framework.
145be691f3bSpatrick   raw_path = lldb_shlib_spec.GetPath();
146be691f3bSpatrick   raw_path.resize(rev_it - r_end);
147061da546Spatrick   raw_path.append("LLDB.framework/Resources/Clang");
148*f6aab3d8Srobert   file_spec.SetDirectory(raw_path);
149061da546Spatrick   FileSystem::Instance().Resolve(file_spec);
150061da546Spatrick   return true;
151061da546Spatrick #endif // __APPLE__
152061da546Spatrick }
153061da546Spatrick 
GetClangResourceDir()154061da546Spatrick FileSpec lldb_private::GetClangResourceDir() {
155061da546Spatrick   static FileSpec g_cached_resource_dir;
156061da546Spatrick   static llvm::once_flag g_once_flag;
157061da546Spatrick   llvm::call_once(g_once_flag, []() {
158061da546Spatrick     if (FileSpec lldb_file_spec = HostInfo::GetShlibDir())
159061da546Spatrick       ComputeClangResourceDirectory(lldb_file_spec, g_cached_resource_dir,
160061da546Spatrick                                     true);
161*f6aab3d8Srobert     Log *log = GetLog(LLDBLog::Host);
162061da546Spatrick     LLDB_LOGF(log, "GetClangResourceDir() => '%s'",
163061da546Spatrick               g_cached_resource_dir.GetPath().c_str());
164061da546Spatrick   });
165061da546Spatrick   return g_cached_resource_dir;
166061da546Spatrick }
167