OptionGroupPlatform.cpp revision 353358
1//===-- OptionGroupPlatform.cpp ---------------------------------*- C++ -*-===//
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
15using namespace lldb;
16using namespace lldb_private;
17
18PlatformSP 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
55void 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
63static 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
79llvm::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
86Status
87OptionGroupPlatform::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(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    error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
117    break;
118  }
119  return error;
120}
121
122bool OptionGroupPlatform::PlatformMatches(
123    const lldb::PlatformSP &platform_sp) const {
124  if (platform_sp) {
125    if (!m_platform_name.empty()) {
126      if (platform_sp->GetName() != ConstString(m_platform_name.c_str()))
127        return false;
128    }
129
130    if (m_sdk_build && m_sdk_build != platform_sp->GetSDKBuild())
131      return false;
132
133    if (m_sdk_sysroot && m_sdk_sysroot != platform_sp->GetSDKRootDirectory())
134      return false;
135
136    if (!m_os_version.empty() && m_os_version != platform_sp->GetOSVersion())
137      return false;
138    return true;
139  }
140  return false;
141}
142