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