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 #include "lldb/Utility/Utils.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 PlatformSP
24 OptionGroupPlatform::CreatePlatformWithOptions (CommandInterpreter &interpreter, const ArchSpec &arch, bool make_selected, Error& error) const
25 {
26     PlatformSP platform_sp;
27 
28     if (!m_platform_name.empty())
29     {
30         platform_sp = Platform::Create (m_platform_name.c_str(), error);
31     }
32     else if (arch.IsValid())
33     {
34         platform_sp = Platform::Create (arch, error);
35     }
36 
37     if (platform_sp)
38     {
39         interpreter.GetDebugger().GetPlatformList().Append (platform_sp, make_selected);
40         if (m_os_version_major != UINT32_MAX)
41         {
42             platform_sp->SetOSVersion (m_os_version_major,
43                                        m_os_version_minor,
44                                        m_os_version_update);
45         }
46 
47         if (m_sdk_sysroot)
48             platform_sp->SetSDKRootDirectory (m_sdk_sysroot);
49 
50         if (m_sdk_build)
51             platform_sp->SetSDKBuild (m_sdk_build);
52     }
53 
54     return platform_sp;
55 }
56 
57 void
58 OptionGroupPlatform::OptionParsingStarting (CommandInterpreter &interpreter)
59 {
60     m_platform_name.clear();
61     m_sdk_sysroot.Clear();
62     m_sdk_build.Clear();
63     m_os_version_major = UINT32_MAX;
64     m_os_version_minor = UINT32_MAX;
65     m_os_version_update = UINT32_MAX;
66 }
67 
68 static OptionDefinition
69 g_option_table[] =
70 {
71     { LLDB_OPT_SET_ALL, false, "platform", 'p', required_argument, NULL, 0, eArgTypePlatform, "Specify name of the platform to use for this target, creating the platform if necessary."},
72     { LLDB_OPT_SET_ALL, false, "version" , 'v', required_argument, NULL, 0, eArgTypeNone, "Specify the initial SDK version to use prior to connecting." },
73     { LLDB_OPT_SET_ALL, false, "build"   , 'b', required_argument, NULL, 0, eArgTypeNone, "Specify the initial SDK build number." },
74     { LLDB_OPT_SET_ALL, false, "sysroot" , 's', required_argument, NULL, 0, eArgTypeFilename, "Specify the SDK root directory that contains a root of all remote system files." }
75 };
76 
77 const OptionDefinition*
78 OptionGroupPlatform::GetDefinitions ()
79 {
80     if (m_include_platform_option)
81         return g_option_table;
82     return g_option_table + 1;
83 }
84 
85 uint32_t
86 OptionGroupPlatform::GetNumDefinitions ()
87 {
88     if (m_include_platform_option)
89         return arraysize(g_option_table);
90     return arraysize(g_option_table) - 1;
91 }
92 
93 
94 Error
95 OptionGroupPlatform::SetOptionValue (CommandInterpreter &interpreter,
96                                      uint32_t option_idx,
97                                      const char *option_arg)
98 {
99     Error error;
100     if (!m_include_platform_option)
101         ++option_idx;
102 
103     char short_option = (char) g_option_table[option_idx].short_option;
104 
105     switch (short_option)
106     {
107         case 'p':
108             m_platform_name.assign (option_arg);
109             break;
110 
111         case 'v':
112             if (Args::StringToVersion (option_arg,
113                                        m_os_version_major,
114                                        m_os_version_minor,
115                                        m_os_version_update) == option_arg)
116                 error.SetErrorStringWithFormat ("invalid version string '%s'", option_arg);
117             break;
118 
119         case 'b':
120             m_sdk_build.SetCString (option_arg);
121             break;
122 
123         case 's':
124             m_sdk_sysroot.SetCString (option_arg);
125             break;
126 
127         default:
128             error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
129             break;
130     }
131     return error;
132 }
133