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