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 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Interpreter/CommandInterpreter.h"
17 #include "lldb/Target/Platform.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 PlatformSP OptionGroupPlatform::CreatePlatformWithOptions(
23     CommandInterpreter &interpreter, const ArchSpec &arch, bool make_selected,
24     Error &error, ArchSpec &platform_arch) const {
25   PlatformSP platform_sp;
26 
27   if (!m_platform_name.empty()) {
28     platform_sp = Platform::Create(ConstString(m_platform_name.c_str()), error);
29     if (platform_sp) {
30       if (platform_arch.IsValid() &&
31           !platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch)) {
32         error.SetErrorStringWithFormat("platform '%s' doesn't support '%s'",
33                                        platform_sp->GetName().GetCString(),
34                                        arch.GetTriple().getTriple().c_str());
35         platform_sp.reset();
36         return platform_sp;
37       }
38     }
39   } else if (arch.IsValid()) {
40     platform_sp = Platform::Create(arch, &platform_arch, error);
41   }
42 
43   if (platform_sp) {
44     interpreter.GetDebugger().GetPlatformList().Append(platform_sp,
45                                                        make_selected);
46     if (m_os_version_major != UINT32_MAX) {
47       platform_sp->SetOSVersion(m_os_version_major, m_os_version_minor,
48                                 m_os_version_update);
49     }
50 
51     if (m_sdk_sysroot)
52       platform_sp->SetSDKRootDirectory(m_sdk_sysroot);
53 
54     if (m_sdk_build)
55       platform_sp->SetSDKBuild(m_sdk_build);
56   }
57 
58   return platform_sp;
59 }
60 
61 void OptionGroupPlatform::OptionParsingStarting(
62     ExecutionContext *execution_context) {
63   m_platform_name.clear();
64   m_sdk_sysroot.Clear();
65   m_sdk_build.Clear();
66   m_os_version_major = UINT32_MAX;
67   m_os_version_minor = UINT32_MAX;
68   m_os_version_update = UINT32_MAX;
69 }
70 
71 static OptionDefinition g_option_table[] = {
72     {LLDB_OPT_SET_ALL, false, "platform", 'p', OptionParser::eRequiredArgument,
73      nullptr, nullptr, 0, eArgTypePlatform, "Specify name of the platform to "
74                                             "use for this target, creating the "
75                                             "platform if necessary."},
76     {LLDB_OPT_SET_ALL, false, "version", 'v', OptionParser::eRequiredArgument,
77      nullptr, nullptr, 0, eArgTypeNone,
78      "Specify the initial SDK version to use prior to connecting."},
79     {LLDB_OPT_SET_ALL, false, "build", 'b', OptionParser::eRequiredArgument,
80      nullptr, nullptr, 0, eArgTypeNone,
81      "Specify the initial SDK build number."},
82     {LLDB_OPT_SET_ALL, false, "sysroot", 'S', OptionParser::eRequiredArgument,
83      nullptr, nullptr, 0, eArgTypeFilename, "Specify the SDK root directory "
84                                             "that contains a root of all "
85                                             "remote system files."}};
86 
87 llvm::ArrayRef<OptionDefinition> OptionGroupPlatform::GetDefinitions() {
88   llvm::ArrayRef<OptionDefinition> result(g_option_table);
89   if (m_include_platform_option)
90     return result;
91   return result.drop_front();
92 }
93 
94 Error OptionGroupPlatform::SetOptionValue(uint32_t option_idx,
95                                           llvm::StringRef option_arg,
96                                           ExecutionContext *execution_context) {
97   Error 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(option_arg);
106     break;
107 
108   case 'v':
109     if (!Args::StringToVersion(option_arg, m_os_version_major,
110                                m_os_version_minor, m_os_version_update))
111       error.SetErrorStringWithFormat("invalid version string '%s'",
112                                      option_arg.str().c_str());
113     break;
114 
115   case 'b':
116     m_sdk_build.SetString(option_arg);
117     break;
118 
119   case 'S':
120     m_sdk_sysroot.SetString(option_arg);
121     break;
122 
123   default:
124     error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
125     break;
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() != ConstString(m_platform_name.c_str()))
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_major != UINT32_MAX) {
145       uint32_t major, minor, update;
146       if (platform_sp->GetOSVersion(major, minor, update)) {
147         if (m_os_version_major != major)
148           return false;
149         if (m_os_version_minor != minor)
150           return false;
151         if (m_os_version_update != update)
152           return false;
153       }
154     }
155     return true;
156   }
157   return false;
158 }
159