Skip to content

Commit 6ad268e

Browse files
authored
[lldb] refactor Target::Install function (#108996)
refactor Target::Install function
1 parent df6855b commit 6ad268e

File tree

1 file changed

+94
-40
lines changed

1 file changed

+94
-40
lines changed

lldb/source/Target/Target.cpp

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,80 @@
7777
using namespace lldb;
7878
using namespace lldb_private;
7979

80+
namespace {
81+
82+
struct ExecutableInstaller {
83+
84+
ExecutableInstaller(PlatformSP platform, ModuleSP module)
85+
: m_platform{platform}, m_module{module},
86+
m_local_file{m_module->GetFileSpec()},
87+
m_remote_file{m_module->GetRemoteInstallFileSpec()} {}
88+
89+
void setupRemoteFile() const { m_module->SetPlatformFileSpec(m_remote_file); }
90+
91+
PlatformSP m_platform;
92+
ModuleSP m_module;
93+
const FileSpec m_local_file;
94+
const FileSpec m_remote_file;
95+
};
96+
97+
struct MainExecutableInstaller {
98+
99+
MainExecutableInstaller(PlatformSP platform, ModuleSP module, TargetSP target,
100+
ProcessLaunchInfo &launch_info)
101+
: m_platform{platform}, m_module{module},
102+
m_local_file{m_module->GetFileSpec()},
103+
m_remote_file{
104+
getRemoteFileSpec(m_platform, target, m_module, m_local_file)},
105+
m_launch_info{launch_info} {}
106+
107+
void setupRemoteFile() const {
108+
m_module->SetPlatformFileSpec(m_remote_file);
109+
m_launch_info.SetExecutableFile(m_remote_file,
110+
/*add_exe_file_as_first_arg=*/false);
111+
m_platform->SetFilePermissions(m_remote_file, 0700 /*-rwx------*/);
112+
}
113+
114+
PlatformSP m_platform;
115+
ModuleSP m_module;
116+
const FileSpec m_local_file;
117+
const FileSpec m_remote_file;
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+
ProcessLaunchInfo &m_launch_info;
137+
};
138+
} // namespace
139+
140+
template <typename Installer>
141+
static Status installExecutable(const Installer &installer) {
142+
if (!installer.m_local_file || !installer.m_remote_file)
143+
return Status();
144+
145+
Status error = installer.m_platform->Install(installer.m_local_file,
146+
installer.m_remote_file);
147+
if (error.Fail())
148+
return error;
149+
150+
installer.setupRemoteFile();
151+
return Status();
152+
}
153+
80154
constexpr std::chrono::milliseconds EvaluateExpressionOptions::default_timeout;
81155

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

0 commit comments

Comments
 (0)