diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp index 262a7dc731713..e156c7e53a4ed 100644 --- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp +++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp @@ -1030,6 +1030,40 @@ PlatformDarwin::ExtractAppSpecificInfo(Process &process) { return dict_sp; } +static llvm::Expected +ResolveSDKPathFromDebugInfo(lldb_private::Target *target) { + + ModuleSP exe_module_sp = target->GetExecutableModule(); + if (!exe_module_sp) + return llvm::createStringError("Failed to get module from target"); + + SymbolFile *sym_file = exe_module_sp->GetSymbolFile(); + if (!sym_file) + return llvm::createStringError("Failed to get symbol file from module"); + + XcodeSDK merged_sdk; + for (unsigned i = 0; i < sym_file->GetNumCompileUnits(); ++i) { + if (auto cu_sp = sym_file->GetCompileUnitAtIndex(i)) { + auto cu_sdk = sym_file->ParseXcodeSDK(*cu_sp); + merged_sdk.Merge(cu_sdk); + } + } + + // TODO: The result of this loop is almost equivalent to deriving the SDK + // from the target triple, which would be a lot cheaper. + FileSpec sdk_path = merged_sdk.GetSysroot(); + if (FileSystem::Instance().Exists(sdk_path)) { + return sdk_path; + } + auto path_or_err = HostInfo::GetSDKRoot(HostInfo::SDKOptions{merged_sdk}); + if (!path_or_err) + return llvm::createStringError( + llvm::formatv("Failed to resolve SDK path: {0}", + llvm::toString(path_or_err.takeError()))); + + return FileSpec(*path_or_err); +} + void PlatformDarwin::AddClangModuleCompilationOptionsForSDKType( Target *target, std::vector &options, XcodeSDK::Type sdk_type) { const std::vector apple_arguments = { @@ -1129,15 +1163,13 @@ void PlatformDarwin::AddClangModuleCompilationOptionsForSDKType( FileSpec sysroot_spec; if (target) { - if (ModuleSP exe_module_sp = target->GetExecutableModule()) { - auto path_or_err = ResolveSDKPathFromDebugInfo(*exe_module_sp); - if (path_or_err) { - sysroot_spec = FileSpec(*path_or_err); - } else { - LLDB_LOG_ERROR(GetLog(LLDBLog::Types | LLDBLog::Host), - path_or_err.takeError(), - "Failed to resolve SDK path: {0}"); - } + auto sysroot_spec_or_err = ResolveSDKPathFromDebugInfo(target); + if (!sysroot_spec_or_err) { + LLDB_LOG_ERROR(GetLog(LLDBLog::Types | LLDBLog::Host), + sysroot_spec_or_err.takeError(), + "Failed to resolve sysroot: {0}"); + } else { + sysroot_spec = *sysroot_spec_or_err; } }