1 //===-- OptionGroupPlatform.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Interpreter/OptionGroupPlatform.h"
10
11 #include "lldb/Host/OptionParser.h"
12 #include "lldb/Interpreter/CommandInterpreter.h"
13 #include "lldb/Target/Platform.h"
14
15 using namespace lldb;
16 using namespace lldb_private;
17
CreatePlatformWithOptions(CommandInterpreter & interpreter,const ArchSpec & arch,bool make_selected,Status & error,ArchSpec & platform_arch) const18 PlatformSP OptionGroupPlatform::CreatePlatformWithOptions(
19 CommandInterpreter &interpreter, const ArchSpec &arch, bool make_selected,
20 Status &error, ArchSpec &platform_arch) const {
21 PlatformList &platforms = interpreter.GetDebugger().GetPlatformList();
22
23 PlatformSP platform_sp;
24
25 if (!m_platform_name.empty()) {
26 platform_sp = platforms.Create(m_platform_name);
27 if (!platform_sp) {
28 error.SetErrorStringWithFormatv(
29 "unable to find a plug-in for the platform named \"{0}\"",
30 m_platform_name);
31 }
32 if (platform_sp) {
33 if (platform_arch.IsValid() && !platform_sp->IsCompatibleArchitecture(
34 arch, {}, false, &platform_arch)) {
35 error.SetErrorStringWithFormatv("platform '{0}' doesn't support '{1}'",
36 platform_sp->GetPluginName(),
37 arch.GetTriple().getTriple());
38 platform_sp.reset();
39 return platform_sp;
40 }
41 }
42 } else if (arch.IsValid()) {
43 platform_sp = platforms.GetOrCreate(arch, {}, &platform_arch, error);
44 }
45
46 if (platform_sp) {
47 if (make_selected)
48 platforms.SetSelectedPlatform(platform_sp);
49 if (!m_os_version.empty())
50 platform_sp->SetOSVersion(m_os_version);
51
52 if (m_sdk_sysroot)
53 platform_sp->SetSDKRootDirectory(m_sdk_sysroot);
54
55 if (m_sdk_build)
56 platform_sp->SetSDKBuild(m_sdk_build);
57 }
58
59 return platform_sp;
60 }
61
OptionParsingStarting(ExecutionContext * execution_context)62 void OptionGroupPlatform::OptionParsingStarting(
63 ExecutionContext *execution_context) {
64 m_platform_name.clear();
65 m_sdk_sysroot.Clear();
66 m_sdk_build.Clear();
67 m_os_version = llvm::VersionTuple();
68 }
69
70 static constexpr OptionDefinition g_option_table[] = {
71 {LLDB_OPT_SET_ALL, false, "platform", 'p', OptionParser::eRequiredArgument,
72 nullptr, {}, 0, eArgTypePlatform, "Specify name of the platform to "
73 "use for this target, creating the "
74 "platform if necessary."},
75 {LLDB_OPT_SET_ALL, false, "version", 'v', OptionParser::eRequiredArgument,
76 nullptr, {}, 0, eArgTypeNone,
77 "Specify the initial SDK version to use prior to connecting."},
78 {LLDB_OPT_SET_ALL, false, "build", 'b', OptionParser::eRequiredArgument,
79 nullptr, {}, 0, eArgTypeNone,
80 "Specify the initial SDK build number."},
81 {LLDB_OPT_SET_ALL, false, "sysroot", 'S', OptionParser::eRequiredArgument,
82 nullptr, {}, 0, eArgTypeFilename, "Specify the SDK root directory "
83 "that contains a root of all "
84 "remote system files."}};
85
GetDefinitions()86 llvm::ArrayRef<OptionDefinition> OptionGroupPlatform::GetDefinitions() {
87 llvm::ArrayRef<OptionDefinition> result(g_option_table);
88 if (m_include_platform_option)
89 return result;
90 return result.drop_front();
91 }
92
93 Status
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)94 OptionGroupPlatform::SetOptionValue(uint32_t option_idx,
95 llvm::StringRef option_arg,
96 ExecutionContext *execution_context) {
97 Status error;
98 if (!m_include_platform_option)
99 ++option_idx;
100
101 const int short_option = g_option_table[option_idx].short_option;
102
103 switch (short_option) {
104 case 'p':
105 m_platform_name.assign(std::string(option_arg));
106 break;
107
108 case 'v':
109 if (m_os_version.tryParse(option_arg))
110 error.SetErrorStringWithFormatv("invalid version string '{0}'",
111 option_arg);
112 break;
113
114 case 'b':
115 m_sdk_build.SetString(option_arg);
116 break;
117
118 case 'S':
119 m_sdk_sysroot.SetString(option_arg);
120 break;
121
122 default:
123 llvm_unreachable("Unimplemented option");
124 }
125 return error;
126 }
127
PlatformMatches(const lldb::PlatformSP & platform_sp) const128 bool OptionGroupPlatform::PlatformMatches(
129 const lldb::PlatformSP &platform_sp) const {
130 if (platform_sp) {
131 if (!m_platform_name.empty()) {
132 if (platform_sp->GetName() != m_platform_name)
133 return false;
134 }
135
136 if (m_sdk_build && m_sdk_build != platform_sp->GetSDKBuild())
137 return false;
138
139 if (m_sdk_sysroot && m_sdk_sysroot != platform_sp->GetSDKRootDirectory())
140 return false;
141
142 if (!m_os_version.empty() && m_os_version != platform_sp->GetOSVersion())
143 return false;
144 return true;
145 }
146 return false;
147 }
148