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   PlatformSP platform_sp;
22 
23   if (!m_platform_name.empty()) {
24     platform_sp = Platform::Create(ConstString(m_platform_name.c_str()), error);
25     if (platform_sp) {
26       if (platform_arch.IsValid() &&
27           !platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch)) {
28         error.SetErrorStringWithFormat("platform '%s' doesn't support '%s'",
29                                        platform_sp->GetName().GetCString(),
30                                        arch.GetTriple().getTriple().c_str());
31         platform_sp.reset();
32         return platform_sp;
33       }
34     }
35   } else if (arch.IsValid()) {
36     platform_sp = Platform::Create(arch, &platform_arch, error);
37   }
38 
39   if (platform_sp) {
40     interpreter.GetDebugger().GetPlatformList().Append(platform_sp,
41                                                        make_selected);
42     if (!m_os_version.empty())
43       platform_sp->SetOSVersion(m_os_version);
44 
45     if (m_sdk_sysroot)
46       platform_sp->SetSDKRootDirectory(m_sdk_sysroot);
47 
48     if (m_sdk_build)
49       platform_sp->SetSDKBuild(m_sdk_build);
50   }
51 
52   return platform_sp;
53 }
54 
OptionParsingStarting(ExecutionContext * execution_context)55 void OptionGroupPlatform::OptionParsingStarting(
56     ExecutionContext *execution_context) {
57   m_platform_name.clear();
58   m_sdk_sysroot.Clear();
59   m_sdk_build.Clear();
60   m_os_version = llvm::VersionTuple();
61 }
62 
63 static constexpr OptionDefinition g_option_table[] = {
64     {LLDB_OPT_SET_ALL, false, "platform", 'p', OptionParser::eRequiredArgument,
65      nullptr, {}, 0, eArgTypePlatform, "Specify name of the platform to "
66                                        "use for this target, creating the "
67                                        "platform if necessary."},
68     {LLDB_OPT_SET_ALL, false, "version", 'v', OptionParser::eRequiredArgument,
69      nullptr, {}, 0, eArgTypeNone,
70      "Specify the initial SDK version to use prior to connecting."},
71     {LLDB_OPT_SET_ALL, false, "build", 'b', OptionParser::eRequiredArgument,
72      nullptr, {}, 0, eArgTypeNone,
73      "Specify the initial SDK build number."},
74     {LLDB_OPT_SET_ALL, false, "sysroot", 'S', OptionParser::eRequiredArgument,
75      nullptr, {}, 0, eArgTypeFilename, "Specify the SDK root directory "
76                                        "that contains a root of all "
77                                        "remote system files."}};
78 
GetDefinitions()79 llvm::ArrayRef<OptionDefinition> OptionGroupPlatform::GetDefinitions() {
80   llvm::ArrayRef<OptionDefinition> result(g_option_table);
81   if (m_include_platform_option)
82     return result;
83   return result.drop_front();
84 }
85 
86 Status
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)87 OptionGroupPlatform::SetOptionValue(uint32_t option_idx,
88                                     llvm::StringRef option_arg,
89                                     ExecutionContext *execution_context) {
90   Status error;
91   if (!m_include_platform_option)
92     ++option_idx;
93 
94   const int short_option = g_option_table[option_idx].short_option;
95 
96   switch (short_option) {
97   case 'p':
98     m_platform_name.assign(std::string(option_arg));
99     break;
100 
101   case 'v':
102     if (m_os_version.tryParse(option_arg))
103       error.SetErrorStringWithFormatv("invalid version string '{0}'",
104                                       option_arg);
105     break;
106 
107   case 'b':
108     m_sdk_build.SetString(option_arg);
109     break;
110 
111   case 'S':
112     m_sdk_sysroot.SetString(option_arg);
113     break;
114 
115   default:
116     llvm_unreachable("Unimplemented option");
117   }
118   return error;
119 }
120 
PlatformMatches(const lldb::PlatformSP & platform_sp) const121 bool OptionGroupPlatform::PlatformMatches(
122     const lldb::PlatformSP &platform_sp) const {
123   if (platform_sp) {
124     if (!m_platform_name.empty()) {
125       if (platform_sp->GetName() != ConstString(m_platform_name.c_str()))
126         return false;
127     }
128 
129     if (m_sdk_build && m_sdk_build != platform_sp->GetSDKBuild())
130       return false;
131 
132     if (m_sdk_sysroot && m_sdk_sysroot != platform_sp->GetSDKRootDirectory())
133       return false;
134 
135     if (!m_os_version.empty() && m_os_version != platform_sp->GetOSVersion())
136       return false;
137     return true;
138   }
139   return false;
140 }
141