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