blob: 44a13353818a67e221c1b5d7cbde3aab64995639 [file] [log] [blame]
Zachary Turnerbdf08922018-06-04 17:41:00 +00001//===-- ClangHost.cpp -------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ClangHost.h"
11
12#include "clang/Basic/Version.h"
13#include "clang/Config/config.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
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000020#include "lldb/Host/FileSystem.h"
Jim Ingham34b67982018-06-05 00:19:03 +000021#include "lldb/Host/HostInfo.h"
Zachary Turnerbdf08922018-06-04 17:41:00 +000022#if !defined(_WIN32)
23#include "lldb/Host/posix/HostInfoPosix.h"
24#endif
25#include "lldb/Utility/FileSpec.h"
26#include "lldb/Utility/Log.h"
27
28#include <string>
29
30using namespace lldb_private;
31
32#if defined(_WIN32)
33static bool ComputeClangDirectory(FileSpec &file_spec) { return false; }
34#else
35static bool DefaultComputeClangDirectory(FileSpec &file_spec) {
36 return HostInfoPosix::ComputePathRelativeToLibrary(
37 file_spec, (llvm::Twine("/lib") + CLANG_LIBDIR_SUFFIX + "/clang/" +
38 CLANG_VERSION_STRING)
39 .str());
40}
41
42#if defined(__APPLE__)
43
44static bool VerifyClangPath(const llvm::Twine &clang_path) {
Jonas Devlieghere3a58d892018-11-08 00:14:50 +000045 if (FileSystem::Instance().IsDirectory(clang_path))
Zachary Turnerbdf08922018-06-04 17:41:00 +000046 return true;
47 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
48 if (log)
49 log->Printf("VerifyClangPath(): "
50 "failed to stat clang resource directory at \"%s\"",
51 clang_path.str().c_str());
52 return false;
53}
54
55bool lldb_private::ComputeClangDirectory(FileSpec &lldb_shlib_spec,
56 FileSpec &file_spec, bool verify) {
57 std::string raw_path = lldb_shlib_spec.GetPath();
58
59 auto rev_it = llvm::sys::path::rbegin(raw_path);
60 auto r_end = llvm::sys::path::rend(raw_path);
61
62 // Check for a Posix-style build of LLDB.
63 while (rev_it != r_end) {
64 if (*rev_it == "LLDB.framework")
65 break;
66 ++rev_it;
67 }
68
69 if (rev_it == r_end)
70 return DefaultComputeClangDirectory(file_spec);
71
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)) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000087 file_spec.SetFile(clang_path.c_str(), FileSpec::Style::native);
88 FileSystem::Instance().Resolve(file_spec);
Zachary Turnerbdf08922018-06-04 17:41:00 +000089 return true;
90 }
91 } else if (parent != r_end && *parent == "PrivateFrameworks" &&
92 std::distance(parent, r_end) > 2) {
93 ++parent;
94 ++parent;
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)) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000102 file_spec.SetFile(clang_path.c_str(), FileSpec::Style::native);
103 FileSystem::Instance().Resolve(file_spec);
Zachary Turnerbdf08922018-06-04 17:41:00 +0000104 return true;
105 }
106 raw_path = lldb_shlib_spec.GetPath();
107 }
108 raw_path.resize(rev_it - r_end);
109 } else {
110 raw_path.resize(rev_it - r_end);
111 }
112
113 // Fall back to the Clang resource directory inside the framework.
114 raw_path.append("LLDB.framework/Resources/Clang");
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000115 file_spec.SetFile(raw_path.c_str(), FileSpec::Style::native);
116 FileSystem::Instance().Resolve(file_spec);
Zachary Turnerbdf08922018-06-04 17:41:00 +0000117 return true;
118}
119
120static bool ComputeClangDirectory(FileSpec &file_spec) {
Pavel Labath60f028f2018-06-19 15:09:07 +0000121 if (FileSpec lldb_file_spec = HostInfo::GetShlibDir())
122 return ComputeClangDirectory(lldb_file_spec, file_spec, true);
123 return false;
Zachary Turnerbdf08922018-06-04 17:41:00 +0000124}
125#else // __APPLE__
126
127// All non-Apple posix systems.
128static bool ComputeClangDirectory(FileSpec &file_spec) {
129 return DefaultComputeClangDirectory(file_spec);
130}
131#endif // __APPLE__
132#endif // _WIN32
133
134FileSpec 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);
140 if (log)
141 log->Printf("GetClangResourceDir() => '%s'",
142 g_cached_resource_dir.GetPath().c_str());
143 });
144 return g_cached_resource_dir;
145}