Skip to content

Commit 5161bba

Browse files
committed
[lldb] refactor Target::Install function
1 parent 0f97b48 commit 5161bba

File tree

1 file changed

+93
-40
lines changed

1 file changed

+93
-40
lines changed

lldb/source/Target/Target.cpp

Lines changed: 93 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,79 @@
7676
using namespace lldb;
7777
using namespace lldb_private;
7878

79+
namespace {
80+
81+
struct ExecutableInstaller {
82+
83+
ExecutableInstaller(PlatformSP platform, ModuleSP module)
84+
: m_platform{platform}, m_module{module},
85+
m_local_file{m_module->GetFileSpec()},
86+
m_remote_file{m_module->GetRemoteInstallFileSpec()} {}
87+
88+
void setRemoteFile() const { m_module->SetPlatformFileSpec(m_remote_file); }
89+
90+
PlatformSP m_platform;
91+
ModuleSP m_module;
92+
const FileSpec m_local_file;
93+
const FileSpec m_remote_file;
94+
};
95+
96+
struct MainExecutableInstaller {
97+
98+
MainExecutableInstaller(PlatformSP platform, TargetSP target, ModuleSP module,
99+
ProcessLaunchInfo *launch_info)
100+
: m_platform{platform}, m_module{module},
101+
m_local_file{m_module->GetFileSpec()},
102+
m_remote_file{
103+
getRemoteFileSpec(m_platform, target, m_module, m_local_file)},
104+
m_launch_info{launch_info} {}
105+
106+
void setRemoteFile() const {
107+
m_module->SetPlatformFileSpec(m_remote_file);
108+
m_launch_info->SetExecutableFile(m_remote_file,
109+
false /*add_exe_file_as_first_arg*/);
110+
m_platform->SetFilePermissions(m_remote_file, 0700 /*-rwx------*/);
111+
}
112+
113+
PlatformSP m_platform;
114+
ModuleSP m_module;
115+
const FileSpec m_local_file;
116+
const FileSpec m_remote_file;
117+
ProcessLaunchInfo *m_launch_info;
118+
119+
private:
120+
static FileSpec getRemoteFileSpec(PlatformSP platform, TargetSP target,
121+
ModuleSP module,
122+
const FileSpec &local_file) {
123+
FileSpec remote_file = module->GetRemoteInstallFileSpec();
124+
if (remote_file || !target->GetAutoInstallMainExecutable())
125+
return remote_file;
126+
127+
if (!local_file)
128+
return {};
129+
130+
remote_file = platform->GetRemoteWorkingDirectory();
131+
remote_file.AppendPathComponent(local_file.GetFilename().GetCString());
132+
133+
return remote_file;
134+
}
135+
};
136+
} // namespace
137+
138+
template <typename Installer>
139+
static Status installExecutable(const Installer &installer) {
140+
if (!installer.m_local_file || !installer.m_remote_file)
141+
return Status();
142+
143+
Status error = installer.m_platform->Install(installer.m_local_file,
144+
installer.m_remote_file);
145+
if (error.Fail())
146+
return error;
147+
148+
installer.setRemoteFile();
149+
return Status();
150+
}
151+
79152
constexpr std::chrono::milliseconds EvaluateExpressionOptions::default_timeout;
80153

81154
Target::Arch::Arch(const ArchSpec &spec)
@@ -3076,48 +3149,28 @@ TargetProperties &Target::GetGlobalProperties() {
30763149
Status Target::Install(ProcessLaunchInfo *launch_info) {
30773150
Status error;
30783151
PlatformSP platform_sp(GetPlatform());
3079-
if (platform_sp) {
3080-
if (platform_sp->IsRemote()) {
3081-
if (platform_sp->IsConnected()) {
3082-
// Install all files that have an install path when connected to a
3083-
// remote platform. If target.auto-install-main-executable is set then
3084-
// also install the main executable even if it does not have an explicit
3085-
// install path specified.
3086-
const ModuleList &modules = GetImages();
3087-
const size_t num_images = modules.GetSize();
3088-
for (size_t idx = 0; idx < num_images; ++idx) {
3089-
ModuleSP module_sp(modules.GetModuleAtIndex(idx));
3090-
if (module_sp) {
3091-
const bool is_main_executable = module_sp == GetExecutableModule();
3092-
FileSpec local_file(module_sp->GetFileSpec());
3093-
if (local_file) {
3094-
FileSpec remote_file(module_sp->GetRemoteInstallFileSpec());
3095-
if (!remote_file) {
3096-
if (is_main_executable && GetAutoInstallMainExecutable()) {
3097-
// Automatically install the main executable.
3098-
remote_file = platform_sp->GetRemoteWorkingDirectory();
3099-
remote_file.AppendPathComponent(
3100-
module_sp->GetFileSpec().GetFilename().GetCString());
3101-
}
3102-
}
3103-
if (remote_file) {
3104-
error = platform_sp->Install(local_file, remote_file);
3105-
if (error.Success()) {
3106-
module_sp->SetPlatformFileSpec(remote_file);
3107-
if (is_main_executable) {
3108-
platform_sp->SetFilePermissions(remote_file, 0700);
3109-
if (launch_info)
3110-
launch_info->SetExecutableFile(remote_file, false);
3111-
}
3112-
} else
3113-
break;
3114-
}
3115-
}
3116-
}
3117-
}
3118-
}
3152+
if (!platform_sp || !platform_sp->IsRemote() || !platform_sp->IsConnected())
3153+
return error;
3154+
3155+
// Install all files that have an install path when connected to a
3156+
// remote platform. If target.auto-install-main-executable is set then
3157+
// also install the main executable even if it does not have an explicit
3158+
// install path specified.
3159+
3160+
for (auto module_sp : GetImages().Modules()) {
3161+
if (module_sp == GetExecutableModule()) {
3162+
MainExecutableInstaller installer{platform_sp, shared_from_this(),
3163+
module_sp, launch_info};
3164+
error = installExecutable(installer);
3165+
} else {
3166+
ExecutableInstaller installer{platform_sp, module_sp};
3167+
error = installExecutable(installer);
31193168
}
3169+
3170+
if (error.Fail())
3171+
return error;
31203172
}
3173+
31213174
return error;
31223175
}
31233176

0 commit comments

Comments
 (0)