Skip to content

Commit 75cfd24

Browse files
Merge pull request #5165 from adrian-prantl/92640609
Sink SwiftHost.cpp into HostInfo (NFC)
2 parents ac00b90 + 81be9ba commit 75cfd24

File tree

19 files changed

+283
-140
lines changed

19 files changed

+283
-140
lines changed

lldb/include/lldb/Host/HostInfoBase.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ class HostInfoBase {
106106

107107
static FileSpec GetXcodeContentsDirectory() { return {}; }
108108
static FileSpec GetXcodeDeveloperDirectory() { return {}; }
109+
#ifdef LLDB_ENABLE_SWIFT
110+
static FileSpec GetSwiftResourceDir() { return {}; }
111+
static bool ComputeSwiftResourceDirectory(
112+
FileSpec &lldb_shlib_spec, FileSpec &file_spec, bool verify) {
113+
return false;
114+
}
115+
#endif
109116

110117
/// Return the directory containing a specific Xcode SDK.
111118
static llvm::StringRef GetXcodeSDKPath(XcodeSDK sdk) { return {}; }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- HostInfoSwift.h -----------------------------------------*- 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+
#ifndef LLDB_HOST_COMMON_HOSTINFOSWIFT_H
10+
#define LLDB_HOST_COMMON_HOSTINFOSWIFT_H
11+
12+
#include "lldb/lldb-forward.h"
13+
#include "llvm/ADT/Twine.h"
14+
15+
namespace lldb_private {
16+
bool VerifySwiftPath(const llvm::Twine &swift_path);
17+
18+
bool DefaultComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
19+
FileSpec &file_spec, bool verify);
20+
} // namespace lldb_private
21+
#endif

lldb/include/lldb/Host/macosx/HostInfoMacOSX.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ class HostInfoMacOSX : public HostInfoPosix {
2929
static FileSpec GetXcodeContentsDirectory();
3030
static FileSpec GetXcodeDeveloperDirectory();
3131

32+
#ifdef LLDB_ENABLE_SWIFT
33+
static FileSpec GetSwiftResourceDir();
34+
static bool ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
35+
FileSpec &file_spec, bool verify);
36+
#endif
37+
3238
/// Query xcrun to find an Xcode SDK directory.
3339
static llvm::StringRef GetXcodeSDKPath(XcodeSDK sdk);
3440

lldb/include/lldb/Host/posix/HostInfoPosix.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class HostInfoPosix : public HostInfoBase {
3535

3636
static UserIDResolver &GetUserIDResolver();
3737

38+
#ifdef LLDB_ENABLE_SWIFT
39+
static FileSpec GetSwiftResourceDir();
40+
static bool ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
41+
FileSpec &file_spec, bool verify);
42+
#endif
43+
3844
protected:
3945
static bool ComputeSupportExeDirectory(FileSpec &file_spec);
4046
static bool ComputeHeaderDirectory(FileSpec &file_spec);

lldb/include/lldb/Host/windows/HostInfoWindows.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class HostInfoWindows : public HostInfoBase {
3232
static bool GetHostname(std::string &s);
3333
static FileSpec GetProgramFileSpec();
3434
static FileSpec GetDefaultShell();
35+
#ifdef LLDB_ENABLE_SWIFT
36+
static FileSpec GetSwiftResourceDir();
37+
static bool ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
38+
FileSpec &file_spec, bool verify);
39+
#endif
3540

3641
static bool GetEnvironmentVar(const std::string &var_name, std::string &var);
3742

lldb/source/Host/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ add_host_subdirectory(common
4444
common/XML.cpp
4545
)
4646

47+
if(LLDB_ENABLE_SWIFT_SUPPORT)
48+
add_host_subdirectory(posix
49+
common/HostInfoSwift.cpp
50+
)
51+
endif()
52+
4753
if (LLDB_ENABLE_LIBEDIT)
4854
add_host_subdirectory(common
4955
common/Editline.cpp
@@ -67,6 +73,12 @@ if (CMAKE_SYSTEM_NAME MATCHES "Windows")
6773
windows/ProcessLauncherWindows.cpp
6874
windows/ProcessRunLock.cpp
6975
)
76+
if(LLDB_ENABLE_SWIFT_SUPPORT)
77+
add_host_subdirectory(posix
78+
windows/HostInfoWindowsSwift.cpp
79+
)
80+
endif()
81+
7082
else()
7183
add_host_subdirectory(posix
7284
posix/DomainSocket.cpp
@@ -79,6 +91,12 @@ else()
7991
posix/ProcessLauncherPosixFork.cpp
8092
)
8193

94+
if(LLDB_ENABLE_SWIFT_SUPPORT)
95+
add_host_subdirectory(posix
96+
posix/HostInfoPosixSwift.cpp
97+
)
98+
endif()
99+
82100
if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
83101
add_subdirectory(macosx/objcxx)
84102
set(LLDBObjCLibs lldbHostMacOSXObjCXX)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===-- HostInfoSwift.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 "lldb/Host/common/HostInfoSwift.h"
10+
#include "lldb/Host/FileSystem.h"
11+
#include "lldb/Host/HostInfoBase.h"
12+
#include "lldb/Host/Config.h"
13+
#include "lldb/Utility/Log.h"
14+
#include "llvm/ADT/SmallString.h"
15+
#include "llvm/ADT/Twine.h"
16+
#include "llvm/Support/Path.h"
17+
#include "llvm/Support/raw_ostream.h"
18+
#include "lldb/Utility/LLDBLog.h"
19+
20+
bool lldb_private::VerifySwiftPath(const llvm::Twine &swift_path) {
21+
if (FileSystem::Instance().IsDirectory(swift_path))
22+
return true;
23+
Log *log = GetLog(LLDBLog::Host);
24+
if (log)
25+
log->Printf("VerifySwiftPath(): "
26+
"failed to stat swift resource directory at \"%s\"",
27+
swift_path.str().c_str());
28+
return false;
29+
}
30+
31+
bool lldb_private::DefaultComputeSwiftResourceDirectory(
32+
FileSpec &lldb_shlib_spec, FileSpec &file_spec, bool verify) {
33+
if (!lldb_shlib_spec)
34+
return false;
35+
Log *log = GetLog(LLDBLog::Host);
36+
std::string raw_path = lldb_shlib_spec.GetPath();
37+
// Drop bin (windows) or lib
38+
llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path);
39+
40+
static const llvm::StringRef kResourceDirSuffixes[] = {
41+
"lib/swift",
42+
"lib" LLDB_LIBDIR_SUFFIX "/lldb/swift",
43+
};
44+
for (const auto &Suffix : kResourceDirSuffixes) {
45+
llvm::SmallString<256> swift_path(parent_path);
46+
llvm::SmallString<32> relative_path(Suffix);
47+
llvm::sys::path::append(swift_path, relative_path);
48+
if (!verify || VerifySwiftPath(swift_path)) {
49+
if (log)
50+
log->Printf("DefaultComputeSwiftResourceDir: Setting SwiftResourceDir "
51+
"to \"%s\", verify = %s",
52+
swift_path.str().str().c_str(), verify ? "true" : "false");
53+
file_spec.GetDirectory().SetString(swift_path);
54+
FileSystem::Instance().Resolve(file_spec);
55+
return true;
56+
}
57+
}
58+
return false;
59+
}

lldb/source/Host/macosx/objcxx/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
set(SWIFT_SOURCES HostInfoMacOSXSwift.cpp)
2+
set(LLVM_OPTIONAL_SOURCES ${SWIFT_SOURCES})
3+
if (NOT LLDB_ENABLE_SWIFT_SUPPORT)
4+
unset(SWIFT_SOURCES)
5+
endif()
16

27
remove_module_flags()
38
include_directories(..)
@@ -6,6 +11,9 @@ add_lldb_library(lldbHostMacOSXObjCXX
611
Host.mm
712
HostInfoMacOSX.mm
813
HostThreadMacOSX.mm
14+
HostInfoMacOSXSwift.cpp
15+
16+
${SWIFT_SOURCES}
917

1018
LINK_LIBS
1119
lldbUtility
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//===-- HostInfoMacOSSwift.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+
#include "lldb/Host/Config.h"
9+
#include "lldb/Host/FileSystem.h"
10+
#include "lldb/Host/HostInfo.h"
11+
#include "lldb/Host/posix/HostInfoPosix.h"
12+
#include "lldb/Utility/FileSpec.h"
13+
#include "lldb/Utility/LLDBLog.h"
14+
#include "lldb/Utility/Log.h"
15+
16+
#include <string>
17+
18+
using namespace lldb_private;
19+
20+
static bool VerifySwiftPath(const llvm::Twine &swift_path) {
21+
if (FileSystem::Instance().IsDirectory(swift_path))
22+
return true;
23+
Log *log = GetLog(LLDBLog::Host);
24+
if (log)
25+
log->Printf("VerifySwiftPath(): "
26+
"failed to stat swift resource directory at \"%s\"",
27+
swift_path.str().c_str());
28+
return false;
29+
}
30+
31+
bool HostInfoMacOSX::ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
32+
FileSpec &file_spec,
33+
bool verify) {
34+
if (!lldb_shlib_spec)
35+
return false;
36+
37+
std::string raw_path = lldb_shlib_spec.GetPath();
38+
size_t framework_pos = raw_path.find("LLDB.framework");
39+
if (framework_pos == std::string::npos)
40+
return HostInfoPosix::ComputeSwiftResourceDirectory(lldb_shlib_spec,
41+
file_spec, verify);
42+
43+
framework_pos += strlen("LLDB.framework");
44+
raw_path.resize(framework_pos);
45+
raw_path.append("/Resources/Swift");
46+
if (!verify || VerifySwiftPath(raw_path)) {
47+
file_spec.GetDirectory().SetString(raw_path);
48+
FileSystem::Instance().Resolve(file_spec);
49+
return true;
50+
}
51+
return true;
52+
}
53+
54+
FileSpec HostInfoMacOSX::GetSwiftResourceDir() {
55+
static std::once_flag g_once_flag;
56+
static FileSpec g_swift_resource_dir;
57+
std::call_once(g_once_flag, []() {
58+
FileSpec lldb_file_spec = HostInfo::GetShlibDir();
59+
ComputeSwiftResourceDirectory(lldb_file_spec, g_swift_resource_dir, true);
60+
Log *log = GetLog(LLDBLog::Host);
61+
LLDB_LOG(log, "swift dir -> '{0}'", g_swift_resource_dir);
62+
});
63+
return g_swift_resource_dir;
64+
}

lldb/source/Host/posix/HostInfoPosix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- HostInfoPosix.cpp -------------------------------------------------===//
1+
//===-- HostInfoPosixSwift.cpp --------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- SwiftHost.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 "lldb/Host/Config.h"
10+
#include "lldb/Host/common/HostInfoSwift.h"
11+
#include "lldb/Host/FileSystem.h"
12+
#include "lldb/Host/HostInfo.h"
13+
#include "lldb/Utility/FileSpec.h"
14+
#include "lldb/Utility/LLDBLog.h"
15+
#include "lldb/Utility/Log.h"
16+
17+
#include <string>
18+
19+
using namespace lldb_private;
20+
21+
bool HostInfoPosix::ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
22+
FileSpec &file_spec,
23+
bool verify) {
24+
return DefaultComputeSwiftResourceDirectory(lldb_shlib_spec, file_spec,
25+
verify);
26+
}
27+
28+
FileSpec HostInfoPosix::GetSwiftResourceDir() {
29+
static std::once_flag g_once_flag;
30+
static FileSpec g_swift_resource_dir;
31+
std::call_once(g_once_flag, []() {
32+
FileSpec lldb_file_spec = HostInfoPosix::GetShlibDir();
33+
HostInfoPosix::ComputeSwiftResourceDirectory(lldb_file_spec,
34+
g_swift_resource_dir, true);
35+
Log *log = GetLog(LLDBLog::Host);
36+
LLDB_LOG(log, "swift dir -> '{0}'", g_swift_resource_dir);
37+
});
38+
return g_swift_resource_dir;
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- HostInfoWindowsSwift.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 "lldb/Host/Config.h"
10+
#include "lldb/Host/FileSystem.h"
11+
#include "lldb/Host/HostInfo.h"
12+
#include "lldb/Utility/FileSpec.h"
13+
#include "lldb/Utility/LLDBLog.h"
14+
#include "lldb/Utility/Log.h"
15+
16+
#include <string>
17+
18+
using namespace lldb_private;
19+
20+
bool HostInfoPosix::ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
21+
FileSpec &file_spec,
22+
bool verify) {
23+
return DefaultComputeSwiftResourceDirectory(lldb_shlib_spec, file_spec,
24+
verify);
25+
}
26+
27+
FileSpec HostInfoPosix::GetSwiftResourceDir() {
28+
static std::once_flag g_once_flag;
29+
static FileSpec g_swift_resource_dir;
30+
std::call_once(g_once_flag, []() {
31+
FileSpec lldb_file_spec = HostInfoPosix::GetShlibDir();
32+
HostInfoPosix::ComputeSwiftResourceDirectory(lldb_file_spec,
33+
g_swift_resource_dir, true);
34+
Log *log = GetLog(LLDBLog::Host);
35+
LLDB_LOG(log, "swift dir -> '{0}'", g_swift_resource_dir);
36+
});
37+
return g_swift_resource_dir;
38+
}

lldb/source/Plugins/ExpressionParser/Swift/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ add_lldb_library(lldbPluginExpressionParserSwift PLUGIN
77
SwiftExpressionParser.cpp
88
SwiftExpressionSourceCode.cpp
99
SwiftExpressionVariable.cpp
10-
SwiftHost.cpp
1110
SwiftPersistentExpressionState.cpp
1211
SwiftREPL.cpp
1312
SwiftREPLMaterializer.cpp

0 commit comments

Comments
 (0)