ClangHost.cpp revision 360784
1//===-- ClangHost.cpp -------------------------------------------*- C++ -*-===//
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
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/Support/FileSystem.h"
17#include "llvm/Support/Threading.h"
18
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"
24
25#include <string>
26
27using namespace lldb_private;
28
29static bool VerifyClangPath(const llvm::Twine &clang_path) {
30  if (FileSystem::Instance().IsDirectory(clang_path))
31    return true;
32  Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
33  LLDB_LOGF(log,
34            "VerifyClangPath(): "
35            "failed to stat clang resource directory at \"%s\"",
36            clang_path.str().c_str());
37  return false;
38}
39
40///
41/// This will compute the clang resource directory assuming that clang was
42/// installed with the same prefix as lldb.
43///
44/// If verify is true, the first candidate resource directory will be returned.
45/// This mode is only used for testing.
46///
47static bool DefaultComputeClangResourceDirectory(FileSpec &lldb_shlib_spec,
48                                                 FileSpec &file_spec,
49                                                 bool verify) {
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);
53
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",
62  };
63
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)) {
70      LLDB_LOGF(log,
71                "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);
76      return true;
77    }
78  }
79
80  return false;
81}
82
83bool 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,
87                                              verify);
88#else
89  std::string raw_path = lldb_shlib_spec.GetPath();
90
91  auto rev_it = llvm::sys::path::rbegin(raw_path);
92  auto r_end = llvm::sys::path::rend(raw_path);
93
94  // Check for a Posix-style build of LLDB.
95  while (rev_it != r_end) {
96    if (*rev_it == "LLDB.framework")
97      break;
98    ++rev_it;
99  }
100
101  // We found a non-framework build of LLDB
102  if (rev_it == r_end)
103    return DefaultComputeClangResourceDirectory(lldb_shlib_spec, file_spec,
104                                                verify);
105
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);
123      return true;
124    }
125  } else if (parent != r_end && *parent == "PrivateFrameworks" &&
126             std::distance(parent, r_end) > 2) {
127    ++parent;
128    ++parent;
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);
138        return true;
139      }
140      raw_path = lldb_shlib_spec.GetPath();
141    }
142    raw_path.resize(rev_it - r_end);
143  } else {
144    raw_path.resize(rev_it - r_end);
145  }
146
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);
151  return true;
152#endif // __APPLE__
153}
154
155FileSpec 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,
161                                    true);
162    Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
163    LLDB_LOGF(log, "GetClangResourceDir() => '%s'",
164              g_cached_resource_dir.GetPath().c_str());
165  });
166  return g_cached_resource_dir;
167}
168