Skip to content

[lldb] Fix layering violation between Host & Platform on the Swift fork #10302

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 2 commits into from
Mar 20, 2025
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
2 changes: 2 additions & 0 deletions lldb/include/lldb/Host/HostInfoBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class HostInfoBase {

static FileSpec GetXcodeContentsDirectory() { return {}; }
static FileSpec GetXcodeDeveloperDirectory() { return {}; }
static FileSpec GetCurrentXcodeToolchainDirectory() { return {}; }
static FileSpec GetCurrentCommandLineToolsDirectory() { return {}; }
#ifdef LLDB_ENABLE_SWIFT
static FileSpec GetSwiftResourceDir() { return {}; }
static FileSpec GetSwiftResourceDir(llvm::Triple triple) { return {}; }
Expand Down
5 changes: 5 additions & 0 deletions lldb/include/lldb/Host/macosx/HostInfoMacOSX.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class HostInfoMacOSX : public HostInfoPosix {
static FileSpec GetProgramFileSpec();
static FileSpec GetXcodeContentsDirectory();
static FileSpec GetXcodeDeveloperDirectory();
static FileSpec GetCurrentXcodeToolchainDirectory();
static FileSpec GetCurrentCommandLineToolsDirectory();

#ifdef LLDB_ENABLE_SWIFT
static FileSpec GetSwiftResourceDir();
Expand Down Expand Up @@ -72,6 +74,9 @@ class HostInfoMacOSX : public HostInfoPosix {
static bool ComputeHeaderDirectory(FileSpec &file_spec);
static bool ComputeSystemPluginsDirectory(FileSpec &file_spec);
static bool ComputeUserPluginsDirectory(FileSpec &file_spec);

static std::string FindComponentInPath(llvm::StringRef path,
llvm::StringRef component);
};
}

Expand Down
11 changes: 1 addition & 10 deletions lldb/source/Host/macosx/objcxx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
set(SWIFT_SOURCES HostInfoMacOSXSwift.cpp)
set(LLVM_OPTIONAL_SOURCES ${SWIFT_SOURCES})
if (NOT LLDB_ENABLE_SWIFT_SUPPORT)
unset(SWIFT_SOURCES)
set(PLUGIN_DEPENDENCY_ARG NO_PLUGIN_DEPENDENCIES)
set(LLDB_PLUGIN_DEPENDENCIES)
else()
set(PLUGIN_DEPENDENCY_ARG)
set(LLDB_PLUGIN_DEPENDENCIES lldbPluginPlatformMacOSX)
endif()

remove_module_flags()
include_directories(..)

add_lldb_library(lldbHostMacOSXObjCXX ${PLUGIN_DEPENDENCY_ARG}
add_lldb_library(lldbHostMacOSXObjCXX NO_PLUGIN_DEPENDENCIES
Host.mm
HostInfoMacOSX.mm
HostThreadMacOSX.mm
Expand All @@ -21,7 +13,6 @@ add_lldb_library(lldbHostMacOSXObjCXX ${PLUGIN_DEPENDENCY_ARG}

LINK_LIBS
lldbUtility
${LLDB_PLUGIN_DEPENDENCIES}
${EXTRA_LIBS}

LINK_COMPONENTS
Expand Down
27 changes: 27 additions & 0 deletions lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,33 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
return g_developer_directory;
}

std::string HostInfoMacOSX::FindComponentInPath(llvm::StringRef path,
llvm::StringRef component) {
auto begin = llvm::sys::path::begin(path);
auto end = llvm::sys::path::end(path);
for (auto it = begin; it != end; ++it) {
if (it->contains(component)) {
llvm::SmallString<128> buffer;
llvm::sys::path::append(buffer, begin, ++it,
llvm::sys::path::Style::posix);
return buffer.str().str();
}
}
return {};
}

FileSpec HostInfoMacOSX::GetCurrentXcodeToolchainDirectory() {
if (FileSpec fspec = HostInfo::GetShlibDir())
return FileSpec(FindComponentInPath(fspec.GetPath(), ".xctoolchain"));
return {};
}

FileSpec HostInfoMacOSX::GetCurrentCommandLineToolsDirectory() {
if (FileSpec fspec = HostInfo::GetShlibDir())
return FileSpec(FindComponentInPath(fspec.GetPath(), "CommandLineTools"));
return {};
}

static llvm::Expected<std::string>
xcrun(const std::string &sdk, llvm::ArrayRef<llvm::StringRef> arguments,
llvm::StringRef developer_dir = "") {
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Host/macosx/objcxx/HostInfoMacOSXSwift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ HostInfoMacOSX::GetSwiftResourceDir(llvm::Triple triple,
platform_sdk_path, swift_stdlib_os_dir,
HostInfo::GetSwiftResourceDir().GetPath(),
HostInfo::GetXcodeContentsDirectory().GetPath(),
PlatformDarwin::GetCurrentToolchainDirectory().GetPath(),
PlatformDarwin::GetCurrentCommandLineToolsDirectory().GetPath());
HostInfoMacOSX::GetCurrentXcodeToolchainDirectory().GetPath(),
HostInfoMacOSX::GetCurrentCommandLineToolsDirectory().GetPath());
g_resource_dir_cache.insert({key, value});
return g_resource_dir_cache[key];
}
Expand Down
27 changes: 0 additions & 27 deletions lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,33 +1332,6 @@ lldb_private::Status PlatformDarwin::FindBundleBinaryInExecSearchPaths(
return Status();
}

std::string PlatformDarwin::FindComponentInPath(llvm::StringRef path,
llvm::StringRef component) {
auto begin = llvm::sys::path::begin(path);
auto end = llvm::sys::path::end(path);
for (auto it = begin; it != end; ++it) {
if (it->contains(component)) {
llvm::SmallString<128> buffer;
llvm::sys::path::append(buffer, begin, ++it,
llvm::sys::path::Style::posix);
return buffer.str().str();
}
}
return {};
}

FileSpec PlatformDarwin::GetCurrentToolchainDirectory() {
if (FileSpec fspec = HostInfo::GetShlibDir())
return FileSpec(FindComponentInPath(fspec.GetPath(), ".xctoolchain"));
return {};
}

FileSpec PlatformDarwin::GetCurrentCommandLineToolsDirectory() {
if (FileSpec fspec = HostInfo::GetShlibDir())
return FileSpec(FindComponentInPath(fspec.GetPath(), "CommandLineTools"));
return {};
}

llvm::Triple::OSType PlatformDarwin::GetHostOSType() {
#if !defined(__APPLE__)
return llvm::Triple::MacOSX;
Expand Down
10 changes: 0 additions & 10 deletions lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,6 @@ class PlatformDarwin : public PlatformPOSIX {
llvm::Expected<StructuredData::DictionarySP>
FetchExtendedCrashInformation(Process &process) override;

/// Return the toolchain directory the current LLDB instance is located in.
static FileSpec GetCurrentToolchainDirectory();

/// Return the command line tools directory the current LLDB instance is
/// located in.
static FileSpec GetCurrentCommandLineToolsDirectory();

llvm::Expected<std::pair<XcodeSDK, bool>>
GetSDKPathFromDebugInfo(Module &module) override;

Expand Down Expand Up @@ -199,9 +192,6 @@ class PlatformDarwin : public PlatformPOSIX {
lldb::ModuleSP &module_sp, const FileSpecList *module_search_paths_ptr,
llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr);

static std::string FindComponentInPath(llvm::StringRef path,
llvm::StringRef component);

// The OSType where lldb is running.
static llvm::Triple::OSType GetHostOSType();

Expand Down
23 changes: 23 additions & 0 deletions lldb/unittests/Host/HostInfoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,26 @@ TEST(HostInfoTestInitialization, InitTwice) {
EXPECT_EQ(Version, HostInfo::GetOSVersion());
}
}

#ifdef __APPLE__
struct HostInfoTester : public HostInfoMacOSX {
public:
using HostInfoMacOSX::FindComponentInPath;
};

TEST_F(HostInfoTest, FindComponentInPath) {
EXPECT_EQ("/path/to/foo",
HostInfoTester::FindComponentInPath("/path/to/foo/", "foo"));

EXPECT_EQ("/path/to/foo",
HostInfoTester::FindComponentInPath("/path/to/foo", "foo"));

EXPECT_EQ("/path/to/foobar",
HostInfoTester::FindComponentInPath("/path/to/foobar", "foo"));

EXPECT_EQ("/path/to/foobar",
HostInfoTester::FindComponentInPath("/path/to/foobar", "bar"));

EXPECT_EQ("", HostInfoTester::FindComponentInPath("/path/to/foo", "bar"));
}
#endif
22 changes: 0 additions & 22 deletions lldb/unittests/Platform/PlatformDarwinTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
using namespace lldb;
using namespace lldb_private;

struct PlatformDarwinTester : public PlatformDarwin {
public:
using PlatformDarwin::FindComponentInPath;
};

TEST(PlatformDarwinTest, TestParseVersionBuildDir) {
llvm::VersionTuple V;
llvm::StringRef D;
Expand Down Expand Up @@ -49,20 +44,3 @@ TEST(PlatformDarwinTest, TestParseVersionBuildDir) {
std::tie(V, D) = PlatformDarwin::ParseVersionBuildDir("3.4.5");
EXPECT_EQ(llvm::VersionTuple(3, 4, 5), V);
}

TEST(PlatformDarwinTest, FindComponentInPath) {
EXPECT_EQ("/path/to/foo",
PlatformDarwinTester::FindComponentInPath("/path/to/foo/", "foo"));

EXPECT_EQ("/path/to/foo",
PlatformDarwinTester::FindComponentInPath("/path/to/foo", "foo"));

EXPECT_EQ("/path/to/foobar", PlatformDarwinTester::FindComponentInPath(
"/path/to/foobar", "foo"));

EXPECT_EQ("/path/to/foobar", PlatformDarwinTester::FindComponentInPath(
"/path/to/foobar", "bar"));

EXPECT_EQ("",
PlatformDarwinTester::FindComponentInPath("/path/to/foo", "bar"));
}