1 //===-- PlatformTest.cpp --------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "gtest/gtest.h"
11 #include "Plugins/Platform/POSIX/PlatformPOSIX.h"
12 #include "TestingSupport/SubsystemRAII.h"
13 #include "lldb/Core/PluginManager.h"
14 #include "lldb/Host/FileSystem.h"
15 #include "lldb/Host/HostInfo.h"
16 #include "lldb/Target/Platform.h"
19 using namespace lldb_private
;
21 class TestPlatform
: public PlatformPOSIX
{
23 TestPlatform() : PlatformPOSIX(false) {}
26 class PlatformArm
: public TestPlatform
{
28 PlatformArm() = default;
31 GetSupportedArchitectures(const ArchSpec
&process_host_arch
) override
{
32 return {ArchSpec("arm64-apple-ps4")};
35 llvm::StringRef
GetPluginName() override
{ return "arm"; }
36 llvm::StringRef
GetDescription() override
{ return "arm"; }
39 class PlatformIntel
: public TestPlatform
{
41 PlatformIntel() = default;
44 GetSupportedArchitectures(const ArchSpec
&process_host_arch
) override
{
45 return {ArchSpec("x86_64-apple-ps4")};
48 llvm::StringRef
GetPluginName() override
{ return "intel"; }
49 llvm::StringRef
GetDescription() override
{ return "intel"; }
52 class PlatformThumb
: public TestPlatform
{
54 static void Initialize() {
55 PluginManager::RegisterPlugin("thumb", "thumb",
56 PlatformThumb::CreateInstance
);
58 static void Terminate() {
59 PluginManager::UnregisterPlugin(PlatformThumb::CreateInstance
);
62 static PlatformSP
CreateInstance(bool force
, const ArchSpec
*arch
) {
63 return std::make_shared
<PlatformThumb
>();
67 GetSupportedArchitectures(const ArchSpec
&process_host_arch
) override
{
68 return {ArchSpec("thumbv7-apple-ps4"), ArchSpec("thumbv7f-apple-ps4")};
71 llvm::StringRef
GetPluginName() override
{ return "thumb"; }
72 llvm::StringRef
GetDescription() override
{ return "thumb"; }
75 class PlatformTest
: public ::testing::Test
{
76 SubsystemRAII
<FileSystem
, HostInfo
> subsystems
;
81 void SetHostPlatform(const PlatformSP
&platform_sp
) {
82 Platform::SetHostPlatform(platform_sp
);
83 ASSERT_EQ(Platform::GetHostPlatform(), platform_sp
);
84 list
.Append(platform_sp
, /*set_selected=*/true);
88 TEST_F(PlatformTest
, GetPlatformForArchitecturesHost
) {
89 SetHostPlatform(std::make_shared
<PlatformArm
>());
91 const std::vector
<ArchSpec
> archs
= {ArchSpec("arm64-apple-ps4"),
92 ArchSpec("arm64e-apple-ps4")};
93 std::vector
<PlatformSP
> candidates
;
95 // The host platform matches all architectures.
96 PlatformSP platform_sp
= list
.GetOrCreate(archs
, {}, candidates
);
97 ASSERT_TRUE(platform_sp
);
98 EXPECT_EQ(platform_sp
, Platform::GetHostPlatform());
101 TEST_F(PlatformTest
, GetPlatformForArchitecturesSelected
) {
102 SetHostPlatform(std::make_shared
<PlatformIntel
>());
104 const std::vector
<ArchSpec
> archs
= {ArchSpec("arm64-apple-ps4"),
105 ArchSpec("arm64e-apple-ps4")};
106 std::vector
<PlatformSP
> candidates
;
108 // The host platform matches no architectures.
109 PlatformSP platform_sp
= list
.GetOrCreate(archs
, {}, candidates
);
110 ASSERT_FALSE(platform_sp
);
112 // The selected platform matches all architectures.
113 const PlatformSP selected_platform_sp
= std::make_shared
<PlatformArm
>();
114 list
.Append(selected_platform_sp
, /*set_selected=*/true);
115 platform_sp
= list
.GetOrCreate(archs
, {}, candidates
);
116 ASSERT_TRUE(platform_sp
);
117 EXPECT_EQ(platform_sp
, selected_platform_sp
);
120 TEST_F(PlatformTest
, GetPlatformForArchitecturesSelectedOverHost
) {
121 SetHostPlatform(std::make_shared
<PlatformIntel
>());
123 const std::vector
<ArchSpec
> archs
= {ArchSpec("arm64-apple-ps4"),
124 ArchSpec("x86_64-apple-ps4")};
125 std::vector
<PlatformSP
> candidates
;
127 // The host platform matches one architecture.
128 PlatformSP platform_sp
= list
.GetOrCreate(archs
, {}, candidates
);
129 ASSERT_TRUE(platform_sp
);
130 EXPECT_EQ(platform_sp
, Platform::GetHostPlatform());
132 // The selected and host platform each match one architecture.
133 // The selected platform is preferred.
134 const PlatformSP selected_platform_sp
= std::make_shared
<PlatformArm
>();
135 list
.Append(selected_platform_sp
, /*set_selected=*/true);
136 platform_sp
= list
.GetOrCreate(archs
, {}, candidates
);
137 ASSERT_TRUE(platform_sp
);
138 EXPECT_EQ(platform_sp
, selected_platform_sp
);
141 TEST_F(PlatformTest
, GetPlatformForArchitecturesCandidates
) {
142 PlatformThumb::Initialize();
144 SetHostPlatform(std::make_shared
<PlatformIntel
>());
146 const PlatformSP selected_platform_sp
= std::make_shared
<PlatformArm
>();
147 list
.Append(selected_platform_sp
, /*set_selected=*/true);
149 const std::vector
<ArchSpec
> archs
= {ArchSpec("thumbv7-apple-ps4"),
150 ArchSpec("thumbv7f-apple-ps4")};
151 std::vector
<PlatformSP
> candidates
;
153 // The host platform matches one architecture.
154 PlatformSP platform_sp
= list
.GetOrCreate(archs
, {}, candidates
);
155 ASSERT_TRUE(platform_sp
);
156 EXPECT_EQ(platform_sp
->GetName(), "thumb");
158 PlatformThumb::Terminate();