Skip to content

Sink SwiftHost.cpp into HostInfo (NFC) #5165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lldb/include/lldb/Host/HostInfoBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ class HostInfoBase {

static FileSpec GetXcodeContentsDirectory() { return {}; }
static FileSpec GetXcodeDeveloperDirectory() { return {}; }
#ifdef LLDB_ENABLE_SWIFT
static FileSpec GetSwiftResourceDir() { return {}; }
static bool ComputeSwiftResourceDirectory(
FileSpec &lldb_shlib_spec, FileSpec &file_spec, bool verify) {
return false;
}
#endif

/// Return the directory containing a specific Xcode SDK.
static llvm::StringRef GetXcodeSDKPath(XcodeSDK sdk) { return {}; }
Expand Down
21 changes: 21 additions & 0 deletions lldb/include/lldb/Host/common/HostInfoSwift.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- HostInfoSwift.h -----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_HOST_COMMON_HOSTINFOSWIFT_H
#define LLDB_HOST_COMMON_HOSTINFOSWIFT_H

#include "lldb/lldb-forward.h"
#include "llvm/ADT/Twine.h"

namespace lldb_private {
bool VerifySwiftPath(const llvm::Twine &swift_path);

bool DefaultComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
FileSpec &file_spec, bool verify);
} // namespace lldb_private
#endif
6 changes: 6 additions & 0 deletions lldb/include/lldb/Host/macosx/HostInfoMacOSX.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class HostInfoMacOSX : public HostInfoPosix {
static FileSpec GetXcodeContentsDirectory();
static FileSpec GetXcodeDeveloperDirectory();

#ifdef LLDB_ENABLE_SWIFT
static FileSpec GetSwiftResourceDir();
static bool ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
FileSpec &file_spec, bool verify);
#endif

/// Query xcrun to find an Xcode SDK directory.
static llvm::StringRef GetXcodeSDKPath(XcodeSDK sdk);

Expand Down
6 changes: 6 additions & 0 deletions lldb/include/lldb/Host/posix/HostInfoPosix.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class HostInfoPosix : public HostInfoBase {

static UserIDResolver &GetUserIDResolver();

#ifdef LLDB_ENABLE_SWIFT
static FileSpec GetSwiftResourceDir();
static bool ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
FileSpec &file_spec, bool verify);
#endif

protected:
static bool ComputeSupportExeDirectory(FileSpec &file_spec);
static bool ComputeHeaderDirectory(FileSpec &file_spec);
Expand Down
5 changes: 5 additions & 0 deletions lldb/include/lldb/Host/windows/HostInfoWindows.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class HostInfoWindows : public HostInfoBase {
static bool GetHostname(std::string &s);
static FileSpec GetProgramFileSpec();
static FileSpec GetDefaultShell();
#ifdef LLDB_ENABLE_SWIFT
static FileSpec GetSwiftResourceDir();
static bool ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
FileSpec &file_spec, bool verify);
#endif

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

Expand Down
18 changes: 18 additions & 0 deletions lldb/source/Host/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ add_host_subdirectory(common
common/XML.cpp
)

if(LLDB_ENABLE_SWIFT_SUPPORT)
add_host_subdirectory(posix
common/HostInfoSwift.cpp
)
endif()

if (LLDB_ENABLE_LIBEDIT)
add_host_subdirectory(common
common/Editline.cpp
Expand All @@ -67,6 +73,12 @@ if (CMAKE_SYSTEM_NAME MATCHES "Windows")
windows/ProcessLauncherWindows.cpp
windows/ProcessRunLock.cpp
)
if(LLDB_ENABLE_SWIFT_SUPPORT)
add_host_subdirectory(posix
windows/HostInfoWindowsSwift.cpp
)
endif()

else()
add_host_subdirectory(posix
posix/DomainSocket.cpp
Expand All @@ -79,6 +91,12 @@ else()
posix/ProcessLauncherPosixFork.cpp
)

if(LLDB_ENABLE_SWIFT_SUPPORT)
add_host_subdirectory(posix
posix/HostInfoPosixSwift.cpp
)
endif()

if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
add_subdirectory(macosx/objcxx)
set(LLDBObjCLibs lldbHostMacOSXObjCXX)
Expand Down
59 changes: 59 additions & 0 deletions lldb/source/Host/common/HostInfoSwift.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===-- HostInfoSwift.cpp -------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/Host/common/HostInfoSwift.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfoBase.h"
#include "lldb/Host/Config.h"
#include "lldb/Utility/Log.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "lldb/Utility/LLDBLog.h"

bool lldb_private::VerifySwiftPath(const llvm::Twine &swift_path) {
if (FileSystem::Instance().IsDirectory(swift_path))
return true;
Log *log = GetLog(LLDBLog::Host);
if (log)
log->Printf("VerifySwiftPath(): "
"failed to stat swift resource directory at \"%s\"",
swift_path.str().c_str());
return false;
}

bool lldb_private::DefaultComputeSwiftResourceDirectory(
FileSpec &lldb_shlib_spec, FileSpec &file_spec, bool verify) {
if (!lldb_shlib_spec)
return false;
Log *log = GetLog(LLDBLog::Host);
std::string raw_path = lldb_shlib_spec.GetPath();
// Drop bin (windows) or lib
llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path);

static const llvm::StringRef kResourceDirSuffixes[] = {
"lib/swift",
"lib" LLDB_LIBDIR_SUFFIX "/lldb/swift",
};
for (const auto &Suffix : kResourceDirSuffixes) {
llvm::SmallString<256> swift_path(parent_path);
llvm::SmallString<32> relative_path(Suffix);
llvm::sys::path::append(swift_path, relative_path);
if (!verify || VerifySwiftPath(swift_path)) {
if (log)
log->Printf("DefaultComputeSwiftResourceDir: Setting SwiftResourceDir "
"to \"%s\", verify = %s",
swift_path.str().str().c_str(), verify ? "true" : "false");
file_spec.GetDirectory().SetString(swift_path);
FileSystem::Instance().Resolve(file_spec);
return true;
}
}
return false;
}
8 changes: 8 additions & 0 deletions lldb/source/Host/macosx/objcxx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
set(SWIFT_SOURCES HostInfoMacOSXSwift.cpp)
set(LLVM_OPTIONAL_SOURCES ${SWIFT_SOURCES})
if (NOT LLDB_ENABLE_SWIFT_SUPPORT)
unset(SWIFT_SOURCES)
endif()

remove_module_flags()
include_directories(..)
Expand All @@ -6,6 +11,9 @@ add_lldb_library(lldbHostMacOSXObjCXX
Host.mm
HostInfoMacOSX.mm
HostThreadMacOSX.mm
HostInfoMacOSXSwift.cpp

${SWIFT_SOURCES}

LINK_LIBS
lldbUtility
Expand Down
64 changes: 64 additions & 0 deletions lldb/source/Host/macosx/objcxx/HostInfoMacOSXSwift.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//===-- HostInfoMacOSSwift.cpp --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Host/Config.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Host/posix/HostInfoPosix.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"

#include <string>

using namespace lldb_private;

static bool VerifySwiftPath(const llvm::Twine &swift_path) {
if (FileSystem::Instance().IsDirectory(swift_path))
return true;
Log *log = GetLog(LLDBLog::Host);
if (log)
log->Printf("VerifySwiftPath(): "
"failed to stat swift resource directory at \"%s\"",
swift_path.str().c_str());
return false;
}

bool HostInfoMacOSX::ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
FileSpec &file_spec,
bool verify) {
if (!lldb_shlib_spec)
return false;

std::string raw_path = lldb_shlib_spec.GetPath();
size_t framework_pos = raw_path.find("LLDB.framework");
if (framework_pos == std::string::npos)
return HostInfoPosix::ComputeSwiftResourceDirectory(lldb_shlib_spec,
file_spec, verify);

framework_pos += strlen("LLDB.framework");
raw_path.resize(framework_pos);
raw_path.append("/Resources/Swift");
if (!verify || VerifySwiftPath(raw_path)) {
file_spec.GetDirectory().SetString(raw_path);
FileSystem::Instance().Resolve(file_spec);
return true;
}
return true;
}

FileSpec HostInfoMacOSX::GetSwiftResourceDir() {
static std::once_flag g_once_flag;
static FileSpec g_swift_resource_dir;
std::call_once(g_once_flag, []() {
FileSpec lldb_file_spec = HostInfo::GetShlibDir();
ComputeSwiftResourceDirectory(lldb_file_spec, g_swift_resource_dir, true);
Log *log = GetLog(LLDBLog::Host);
LLDB_LOG(log, "swift dir -> '{0}'", g_swift_resource_dir);
});
return g_swift_resource_dir;
}
2 changes: 1 addition & 1 deletion lldb/source/Host/posix/HostInfoPosix.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===-- HostInfoPosix.cpp -------------------------------------------------===//
//===-- HostInfoPosixSwift.cpp --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand Down
39 changes: 39 additions & 0 deletions lldb/source/Host/posix/HostInfoPosixSwift.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===-- SwiftHost.cpp -----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/Host/Config.h"
#include "lldb/Host/common/HostInfoSwift.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"

#include <string>

using namespace lldb_private;

bool HostInfoPosix::ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
FileSpec &file_spec,
bool verify) {
return DefaultComputeSwiftResourceDirectory(lldb_shlib_spec, file_spec,
verify);
}

FileSpec HostInfoPosix::GetSwiftResourceDir() {
static std::once_flag g_once_flag;
static FileSpec g_swift_resource_dir;
std::call_once(g_once_flag, []() {
FileSpec lldb_file_spec = HostInfoPosix::GetShlibDir();
HostInfoPosix::ComputeSwiftResourceDirectory(lldb_file_spec,
g_swift_resource_dir, true);
Log *log = GetLog(LLDBLog::Host);
LLDB_LOG(log, "swift dir -> '{0}'", g_swift_resource_dir);
});
return g_swift_resource_dir;
}
38 changes: 38 additions & 0 deletions lldb/source/Host/windows/HostInfoWindowsSwift.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===-- HostInfoWindowsSwift.cpp ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/Host/Config.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"

#include <string>

using namespace lldb_private;

bool HostInfoPosix::ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
FileSpec &file_spec,
bool verify) {
return DefaultComputeSwiftResourceDirectory(lldb_shlib_spec, file_spec,
verify);
}

FileSpec HostInfoPosix::GetSwiftResourceDir() {
static std::once_flag g_once_flag;
static FileSpec g_swift_resource_dir;
std::call_once(g_once_flag, []() {
FileSpec lldb_file_spec = HostInfoPosix::GetShlibDir();
HostInfoPosix::ComputeSwiftResourceDirectory(lldb_file_spec,
g_swift_resource_dir, true);
Log *log = GetLog(LLDBLog::Host);
LLDB_LOG(log, "swift dir -> '{0}'", g_swift_resource_dir);
});
return g_swift_resource_dir;
}
1 change: 0 additions & 1 deletion lldb/source/Plugins/ExpressionParser/Swift/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ add_lldb_library(lldbPluginExpressionParserSwift PLUGIN
SwiftExpressionParser.cpp
SwiftExpressionSourceCode.cpp
SwiftExpressionVariable.cpp
SwiftHost.cpp
SwiftPersistentExpressionState.cpp
SwiftREPL.cpp
SwiftREPLMaterializer.cpp
Expand Down
Loading