blob: ffc5120dc4100ba1ec9908786b2f9893db74aaf0 [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
20// Project includes
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) {
45 if (llvm::sys::fs::is_directory(clang_path))
46 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)) {
87 file_spec.SetFile(clang_path.c_str(), true);
88 return true;
89 }
90 } else if (parent != r_end && *parent == "PrivateFrameworks" &&
91 std::distance(parent, r_end) > 2) {
92 ++parent;
93 ++parent;
94 if (*parent == "System") {
95 // This is LLDB inside an Xcode toolchain.
96 // E.g., "Xcode.app/Contents/Developer/Toolchains/" \
97 // "My.xctoolchain/System/Library/PrivateFrameworks/LLDB.framework"
98 raw_path.resize(parent - r_end);
99 llvm::sys::path::append(clang_path, raw_path, swift_clang_resource_dir);
100 if (!verify || VerifyClangPath(clang_path)) {
101 file_spec.SetFile(clang_path.c_str(), true);
102 return true;
103 }
104 raw_path = lldb_shlib_spec.GetPath();
105 }
106 raw_path.resize(rev_it - r_end);
107 } else {
108 raw_path.resize(rev_it - r_end);
109 }
110
111 // Fall back to the Clang resource directory inside the framework.
112 raw_path.append("LLDB.framework/Resources/Clang");
113 file_spec.SetFile(raw_path.c_str(), true);
114 return true;
115}
116
117static bool ComputeClangDirectory(FileSpec &file_spec) {
118 FileSpec lldb_file_spec;
119 if (!HostInfo::GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
120 return false;
121 return ComputeClangDirectory(lldb_file_spec, file_spec, true);
122}
123#else // __APPLE__
124
125// All non-Apple posix systems.
126static bool ComputeClangDirectory(FileSpec &file_spec) {
127 return DefaultComputeClangDirectory(file_spec);
128}
129#endif // __APPLE__
130#endif // _WIN32
131
132FileSpec lldb_private::GetClangResourceDir() {
133 static FileSpec g_cached_resource_dir;
134 static llvm::once_flag g_once_flag;
135 llvm::call_once(g_once_flag, []() {
136 ::ComputeClangDirectory(g_cached_resource_dir);
137 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
138 if (log)
139 log->Printf("GetClangResourceDir() => '%s'",
140 g_cached_resource_dir.GetPath().c_str());
141 });
142 return g_cached_resource_dir;
143}