1254721Semaste//===-- OptionGroupPlatform.cpp ---------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/lldb-python.h"
11254721Semaste
12254721Semaste#include "lldb/Interpreter/OptionGroupPlatform.h"
13254721Semaste
14254721Semaste// C Includes
15254721Semaste// C++ Includes
16254721Semaste// Other libraries and framework includes
17254721Semaste// Project includes
18254721Semaste#include "lldb/Interpreter/CommandInterpreter.h"
19254721Semaste#include "lldb/Target/Platform.h"
20254721Semaste#include "lldb/Utility/Utils.h"
21254721Semaste
22254721Semasteusing namespace lldb;
23254721Semasteusing namespace lldb_private;
24254721Semaste
25254721SemastePlatformSP
26254721SemasteOptionGroupPlatform::CreatePlatformWithOptions (CommandInterpreter &interpreter,
27254721Semaste                                                const ArchSpec &arch,
28254721Semaste                                                bool make_selected,
29254721Semaste                                                Error& error,
30254721Semaste                                                ArchSpec &platform_arch) const
31254721Semaste{
32254721Semaste    PlatformSP platform_sp;
33254721Semaste
34254721Semaste    if (!m_platform_name.empty())
35254721Semaste    {
36254721Semaste        platform_sp = Platform::Create (m_platform_name.c_str(), error);
37254721Semaste        if (platform_sp)
38254721Semaste        {
39254721Semaste            if (platform_arch.IsValid() && !platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch))
40254721Semaste            {
41254721Semaste                error.SetErrorStringWithFormat ("platform '%s' doesn't support '%s'",
42254721Semaste                                                platform_sp->GetName().GetCString(),
43254721Semaste                                                arch.GetTriple().getTriple().c_str());
44254721Semaste                platform_sp.reset();
45254721Semaste                return platform_sp;
46254721Semaste            }
47254721Semaste        }
48254721Semaste    }
49254721Semaste    else if (arch.IsValid())
50254721Semaste    {
51254721Semaste        platform_sp = Platform::Create (arch, &platform_arch, error);
52254721Semaste    }
53254721Semaste
54254721Semaste    if (platform_sp)
55254721Semaste    {
56254721Semaste        interpreter.GetDebugger().GetPlatformList().Append (platform_sp, make_selected);
57254721Semaste        if (m_os_version_major != UINT32_MAX)
58254721Semaste        {
59254721Semaste            platform_sp->SetOSVersion (m_os_version_major,
60254721Semaste                                       m_os_version_minor,
61254721Semaste                                       m_os_version_update);
62254721Semaste        }
63254721Semaste
64254721Semaste        if (m_sdk_sysroot)
65254721Semaste            platform_sp->SetSDKRootDirectory (m_sdk_sysroot);
66254721Semaste
67254721Semaste        if (m_sdk_build)
68254721Semaste            platform_sp->SetSDKBuild (m_sdk_build);
69254721Semaste    }
70254721Semaste
71254721Semaste    return platform_sp;
72254721Semaste}
73254721Semaste
74254721Semastevoid
75254721SemasteOptionGroupPlatform::OptionParsingStarting (CommandInterpreter &interpreter)
76254721Semaste{
77254721Semaste    m_platform_name.clear();
78254721Semaste    m_sdk_sysroot.Clear();
79254721Semaste    m_sdk_build.Clear();
80254721Semaste    m_os_version_major = UINT32_MAX;
81254721Semaste    m_os_version_minor = UINT32_MAX;
82254721Semaste    m_os_version_update = UINT32_MAX;
83254721Semaste}
84254721Semaste
85254721Semastestatic OptionDefinition
86254721Semasteg_option_table[] =
87254721Semaste{
88263363Semaste    { LLDB_OPT_SET_ALL, false, "platform", 'p', OptionParser::eRequiredArgument, NULL, 0, eArgTypePlatform, "Specify name of the platform to use for this target, creating the platform if necessary."},
89263363Semaste    { LLDB_OPT_SET_ALL, false, "version" , 'v', OptionParser::eRequiredArgument, NULL, 0, eArgTypeNone, "Specify the initial SDK version to use prior to connecting." },
90263363Semaste    { LLDB_OPT_SET_ALL, false, "build"   , 'b', OptionParser::eRequiredArgument, NULL, 0, eArgTypeNone, "Specify the initial SDK build number." },
91263363Semaste    { LLDB_OPT_SET_ALL, false, "sysroot" , 'S', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFilename, "Specify the SDK root directory that contains a root of all remote system files." }
92254721Semaste};
93254721Semaste
94254721Semasteconst OptionDefinition*
95254721SemasteOptionGroupPlatform::GetDefinitions ()
96254721Semaste{
97254721Semaste    if (m_include_platform_option)
98254721Semaste        return g_option_table;
99254721Semaste    return g_option_table + 1;
100254721Semaste}
101254721Semaste
102254721Semasteuint32_t
103254721SemasteOptionGroupPlatform::GetNumDefinitions ()
104254721Semaste{
105254721Semaste    if (m_include_platform_option)
106254721Semaste        return llvm::array_lengthof(g_option_table);
107254721Semaste    return llvm::array_lengthof(g_option_table) - 1;
108254721Semaste}
109254721Semaste
110254721Semaste
111254721SemasteError
112254721SemasteOptionGroupPlatform::SetOptionValue (CommandInterpreter &interpreter,
113254721Semaste                                     uint32_t option_idx,
114254721Semaste                                     const char *option_arg)
115254721Semaste{
116254721Semaste    Error error;
117254721Semaste    if (!m_include_platform_option)
118254721Semaste        ++option_idx;
119254721Semaste
120254721Semaste    const int short_option = g_option_table[option_idx].short_option;
121254721Semaste
122254721Semaste    switch (short_option)
123254721Semaste    {
124254721Semaste        case 'p':
125254721Semaste            m_platform_name.assign (option_arg);
126254721Semaste            break;
127254721Semaste
128254721Semaste        case 'v':
129254721Semaste            if (Args::StringToVersion (option_arg,
130254721Semaste                                       m_os_version_major,
131254721Semaste                                       m_os_version_minor,
132254721Semaste                                       m_os_version_update) == option_arg)
133254721Semaste                error.SetErrorStringWithFormat ("invalid version string '%s'", option_arg);
134254721Semaste            break;
135254721Semaste
136254721Semaste        case 'b':
137254721Semaste            m_sdk_build.SetCString (option_arg);
138254721Semaste            break;
139254721Semaste
140254721Semaste        case 'S':
141254721Semaste            m_sdk_sysroot.SetCString (option_arg);
142254721Semaste            break;
143254721Semaste
144254721Semaste        default:
145254721Semaste            error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
146254721Semaste            break;
147254721Semaste    }
148254721Semaste    return error;
149254721Semaste}
150