180814287SRaphael Isemann //===-- OptionGroupPlatform.cpp -------------------------------------------===//
27260f620SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67260f620SGreg Clayton //
77260f620SGreg Clayton //===----------------------------------------------------------------------===//
87260f620SGreg Clayton 
97260f620SGreg Clayton #include "lldb/Interpreter/OptionGroupPlatform.h"
107260f620SGreg Clayton 
113eb2b44dSZachary Turner #include "lldb/Host/OptionParser.h"
127260f620SGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h"
137260f620SGreg Clayton #include "lldb/Target/Platform.h"
147260f620SGreg Clayton 
157260f620SGreg Clayton using namespace lldb;
167260f620SGreg Clayton using namespace lldb_private;
177260f620SGreg Clayton 
CreatePlatformWithOptions(CommandInterpreter & interpreter,const ArchSpec & arch,bool make_selected,Status & error,ArchSpec & platform_arch) const18b9c1b51eSKate Stone PlatformSP OptionGroupPlatform::CreatePlatformWithOptions(
19b9c1b51eSKate Stone     CommandInterpreter &interpreter, const ArchSpec &arch, bool make_selected,
2097206d57SZachary Turner     Status &error, ArchSpec &platform_arch) const {
21*af921006SPavel Labath   PlatformList &platforms = interpreter.GetDebugger().GetPlatformList();
22*af921006SPavel Labath 
237260f620SGreg Clayton   PlatformSP platform_sp;
24b3a40ba8SGreg Clayton 
25b9c1b51eSKate Stone   if (!m_platform_name.empty()) {
26*af921006SPavel Labath     platform_sp = platforms.Create(m_platform_name);
27*af921006SPavel Labath     if (!platform_sp) {
28*af921006SPavel Labath       error.SetErrorStringWithFormatv(
29*af921006SPavel Labath           "unable to find a plug-in for the platform named \"{0}\"",
30*af921006SPavel Labath           m_platform_name);
31*af921006SPavel Labath     }
32b9c1b51eSKate Stone     if (platform_sp) {
33dde487e5SJonas Devlieghere       if (platform_arch.IsValid() && !platform_sp->IsCompatibleArchitecture(
34dde487e5SJonas Devlieghere                                          arch, {}, false, &platform_arch)) {
35d2edca62SPavel Labath         error.SetErrorStringWithFormatv("platform '{0}' doesn't support '{1}'",
36d2edca62SPavel Labath                                         platform_sp->GetPluginName(),
37d2edca62SPavel Labath                                         arch.GetTriple().getTriple());
3870512317SGreg Clayton         platform_sp.reset();
3970512317SGreg Clayton         return platform_sp;
4070512317SGreg Clayton       }
4170512317SGreg Clayton     }
42b9c1b51eSKate Stone   } else if (arch.IsValid()) {
43*af921006SPavel Labath     platform_sp = platforms.GetOrCreate(arch, {}, &platform_arch, error);
44b3a40ba8SGreg Clayton   }
457260f620SGreg Clayton 
46b9c1b51eSKate Stone   if (platform_sp) {
47*af921006SPavel Labath     if (make_selected)
48*af921006SPavel Labath       platforms.SetSelectedPlatform(platform_sp);
492272c481SPavel Labath     if (!m_os_version.empty())
502272c481SPavel Labath       platform_sp->SetOSVersion(m_os_version);
51f3dd93c8SGreg Clayton 
52f3dd93c8SGreg Clayton     if (m_sdk_sysroot)
53f3dd93c8SGreg Clayton       platform_sp->SetSDKRootDirectory(m_sdk_sysroot);
54f3dd93c8SGreg Clayton 
55f3dd93c8SGreg Clayton     if (m_sdk_build)
56f3dd93c8SGreg Clayton       platform_sp->SetSDKBuild(m_sdk_build);
577260f620SGreg Clayton   }
58b3a40ba8SGreg Clayton 
597260f620SGreg Clayton   return platform_sp;
607260f620SGreg Clayton }
617260f620SGreg Clayton 
OptionParsingStarting(ExecutionContext * execution_context)62b9c1b51eSKate Stone void OptionGroupPlatform::OptionParsingStarting(
63b9c1b51eSKate Stone     ExecutionContext *execution_context) {
647260f620SGreg Clayton   m_platform_name.clear();
65f3dd93c8SGreg Clayton   m_sdk_sysroot.Clear();
66f3dd93c8SGreg Clayton   m_sdk_build.Clear();
672272c481SPavel Labath   m_os_version = llvm::VersionTuple();
687260f620SGreg Clayton }
697260f620SGreg Clayton 
708fe53c49STatyana Krasnukha static constexpr OptionDefinition g_option_table[] = {
71b9c1b51eSKate Stone     {LLDB_OPT_SET_ALL, false, "platform", 'p', OptionParser::eRequiredArgument,
728fe53c49STatyana Krasnukha      nullptr, {}, 0, eArgTypePlatform, "Specify name of the platform to "
73b9c1b51eSKate Stone                                        "use for this target, creating the "
74b9c1b51eSKate Stone                                        "platform if necessary."},
75b9c1b51eSKate Stone     {LLDB_OPT_SET_ALL, false, "version", 'v', OptionParser::eRequiredArgument,
768fe53c49STatyana Krasnukha      nullptr, {}, 0, eArgTypeNone,
77b9c1b51eSKate Stone      "Specify the initial SDK version to use prior to connecting."},
78b9c1b51eSKate Stone     {LLDB_OPT_SET_ALL, false, "build", 'b', OptionParser::eRequiredArgument,
798fe53c49STatyana Krasnukha      nullptr, {}, 0, eArgTypeNone,
80b9c1b51eSKate Stone      "Specify the initial SDK build number."},
81b9c1b51eSKate Stone     {LLDB_OPT_SET_ALL, false, "sysroot", 'S', OptionParser::eRequiredArgument,
828fe53c49STatyana Krasnukha      nullptr, {}, 0, eArgTypeFilename, "Specify the SDK root directory "
83b9c1b51eSKate Stone                                        "that contains a root of all "
84b9c1b51eSKate Stone                                        "remote system files."}};
857260f620SGreg Clayton 
GetDefinitions()861f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> OptionGroupPlatform::GetDefinitions() {
871f0f5b5bSZachary Turner   llvm::ArrayRef<OptionDefinition> result(g_option_table);
887260f620SGreg Clayton   if (m_include_platform_option)
891f0f5b5bSZachary Turner     return result;
901f0f5b5bSZachary Turner   return result.drop_front();
917260f620SGreg Clayton }
927260f620SGreg Clayton 
9397206d57SZachary Turner Status
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)9497206d57SZachary Turner OptionGroupPlatform::SetOptionValue(uint32_t option_idx,
958cef4b0bSZachary Turner                                     llvm::StringRef option_arg,
96b9c1b51eSKate Stone                                     ExecutionContext *execution_context) {
9797206d57SZachary Turner   Status error;
987260f620SGreg Clayton   if (!m_include_platform_option)
99effe5c95SGreg Clayton     ++option_idx;
1007260f620SGreg Clayton 
1013bcdfc0eSGreg Clayton   const int short_option = g_option_table[option_idx].short_option;
1027260f620SGreg Clayton 
103b9c1b51eSKate Stone   switch (short_option) {
1047260f620SGreg Clayton   case 'p':
105adcd0268SBenjamin Kramer     m_platform_name.assign(std::string(option_arg));
1067260f620SGreg Clayton     break;
1077260f620SGreg Clayton 
1087260f620SGreg Clayton   case 'v':
1092272c481SPavel Labath     if (m_os_version.tryParse(option_arg))
1102272c481SPavel Labath       error.SetErrorStringWithFormatv("invalid version string '{0}'",
1112272c481SPavel Labath                                       option_arg);
1127260f620SGreg Clayton     break;
1137260f620SGreg Clayton 
114f3dd93c8SGreg Clayton   case 'b':
1158cef4b0bSZachary Turner     m_sdk_build.SetString(option_arg);
116f3dd93c8SGreg Clayton     break;
117f3dd93c8SGreg Clayton 
118a7d8c21bSGreg Clayton   case 'S':
1198cef4b0bSZachary Turner     m_sdk_sysroot.SetString(option_arg);
120f3dd93c8SGreg Clayton     break;
121f3dd93c8SGreg Clayton 
1227260f620SGreg Clayton   default:
12336162014SRaphael Isemann     llvm_unreachable("Unimplemented option");
1247260f620SGreg Clayton   }
1257260f620SGreg Clayton   return error;
1267260f620SGreg Clayton }
127ccd2a6d9SGreg Clayton 
PlatformMatches(const lldb::PlatformSP & platform_sp) const128b9c1b51eSKate Stone bool OptionGroupPlatform::PlatformMatches(
129b9c1b51eSKate Stone     const lldb::PlatformSP &platform_sp) const {
130b9c1b51eSKate Stone   if (platform_sp) {
131b9c1b51eSKate Stone     if (!m_platform_name.empty()) {
132d2edca62SPavel Labath       if (platform_sp->GetName() != m_platform_name)
133ccd2a6d9SGreg Clayton         return false;
134ccd2a6d9SGreg Clayton     }
135ccd2a6d9SGreg Clayton 
136ccd2a6d9SGreg Clayton     if (m_sdk_build && m_sdk_build != platform_sp->GetSDKBuild())
137ccd2a6d9SGreg Clayton       return false;
138ccd2a6d9SGreg Clayton 
139ccd2a6d9SGreg Clayton     if (m_sdk_sysroot && m_sdk_sysroot != platform_sp->GetSDKRootDirectory())
140ccd2a6d9SGreg Clayton       return false;
141ccd2a6d9SGreg Clayton 
1422272c481SPavel Labath     if (!m_os_version.empty() && m_os_version != platform_sp->GetOSVersion())
143ccd2a6d9SGreg Clayton       return false;
144ccd2a6d9SGreg Clayton     return true;
145ccd2a6d9SGreg Clayton   }
146ccd2a6d9SGreg Clayton   return false;
147ccd2a6d9SGreg Clayton }
148