1a114ec0cSJonas Devlieghere //===-- PlatformTest.cpp --------------------------------------------------===//
2a114ec0cSJonas Devlieghere //
3a114ec0cSJonas Devlieghere // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a114ec0cSJonas Devlieghere // See https://llvm.org/LICENSE.txt for license information.
5a114ec0cSJonas Devlieghere // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a114ec0cSJonas Devlieghere //
7a114ec0cSJonas Devlieghere //===----------------------------------------------------------------------===//
8a114ec0cSJonas Devlieghere
9a114ec0cSJonas Devlieghere #include "gtest/gtest.h"
10a114ec0cSJonas Devlieghere
11a114ec0cSJonas Devlieghere #include "Plugins/Platform/POSIX/PlatformPOSIX.h"
12a114ec0cSJonas Devlieghere #include "TestingSupport/SubsystemRAII.h"
13a114ec0cSJonas Devlieghere #include "lldb/Core/PluginManager.h"
14a114ec0cSJonas Devlieghere #include "lldb/Host/FileSystem.h"
15a114ec0cSJonas Devlieghere #include "lldb/Host/HostInfo.h"
16a114ec0cSJonas Devlieghere #include "lldb/Target/Platform.h"
17a114ec0cSJonas Devlieghere
18a114ec0cSJonas Devlieghere using namespace lldb;
19a114ec0cSJonas Devlieghere using namespace lldb_private;
20a114ec0cSJonas Devlieghere
21a114ec0cSJonas Devlieghere class TestPlatform : public PlatformPOSIX {
22a114ec0cSJonas Devlieghere public:
TestPlatform()23a114ec0cSJonas Devlieghere TestPlatform() : PlatformPOSIX(false) {}
24a114ec0cSJonas Devlieghere };
25a114ec0cSJonas Devlieghere
26a114ec0cSJonas Devlieghere class PlatformArm : public TestPlatform {
27a114ec0cSJonas Devlieghere public:
28a114ec0cSJonas Devlieghere PlatformArm() = default;
29a114ec0cSJonas Devlieghere
30a114ec0cSJonas Devlieghere std::vector<ArchSpec>
GetSupportedArchitectures(const ArchSpec & process_host_arch)31a114ec0cSJonas Devlieghere GetSupportedArchitectures(const ArchSpec &process_host_arch) override {
32a114ec0cSJonas Devlieghere return {ArchSpec("arm64-apple-ps4")};
33a114ec0cSJonas Devlieghere }
34a114ec0cSJonas Devlieghere
GetPluginName()35a114ec0cSJonas Devlieghere llvm::StringRef GetPluginName() override { return "arm"; }
GetDescription()36a114ec0cSJonas Devlieghere llvm::StringRef GetDescription() override { return "arm"; }
37a114ec0cSJonas Devlieghere };
38a114ec0cSJonas Devlieghere
39a114ec0cSJonas Devlieghere class PlatformIntel : public TestPlatform {
40a114ec0cSJonas Devlieghere public:
41a114ec0cSJonas Devlieghere PlatformIntel() = default;
42a114ec0cSJonas Devlieghere
43a114ec0cSJonas Devlieghere std::vector<ArchSpec>
GetSupportedArchitectures(const ArchSpec & process_host_arch)44a114ec0cSJonas Devlieghere GetSupportedArchitectures(const ArchSpec &process_host_arch) override {
45a114ec0cSJonas Devlieghere return {ArchSpec("x86_64-apple-ps4")};
46a114ec0cSJonas Devlieghere }
47a114ec0cSJonas Devlieghere
GetPluginName()48a114ec0cSJonas Devlieghere llvm::StringRef GetPluginName() override { return "intel"; }
GetDescription()49a114ec0cSJonas Devlieghere llvm::StringRef GetDescription() override { return "intel"; }
50a114ec0cSJonas Devlieghere };
51a114ec0cSJonas Devlieghere
52a114ec0cSJonas Devlieghere class PlatformThumb : public TestPlatform {
53a114ec0cSJonas Devlieghere public:
Initialize()54a114ec0cSJonas Devlieghere static void Initialize() {
55a114ec0cSJonas Devlieghere PluginManager::RegisterPlugin("thumb", "thumb",
56a114ec0cSJonas Devlieghere PlatformThumb::CreateInstance);
57a114ec0cSJonas Devlieghere }
Terminate()58a114ec0cSJonas Devlieghere static void Terminate() {
59a114ec0cSJonas Devlieghere PluginManager::UnregisterPlugin(PlatformThumb::CreateInstance);
60a114ec0cSJonas Devlieghere }
61a114ec0cSJonas Devlieghere
CreateInstance(bool force,const ArchSpec * arch)62*d6678404SMed Ismail Bennani static PlatformSP CreateInstance(bool force, const ArchSpec *arch) {
63a114ec0cSJonas Devlieghere return std::make_shared<PlatformThumb>();
64a114ec0cSJonas Devlieghere }
65a114ec0cSJonas Devlieghere
66a114ec0cSJonas Devlieghere std::vector<ArchSpec>
GetSupportedArchitectures(const ArchSpec & process_host_arch)67a114ec0cSJonas Devlieghere GetSupportedArchitectures(const ArchSpec &process_host_arch) override {
68a114ec0cSJonas Devlieghere return {ArchSpec("thumbv7-apple-ps4"), ArchSpec("thumbv7f-apple-ps4")};
69a114ec0cSJonas Devlieghere }
70a114ec0cSJonas Devlieghere
GetPluginName()71a114ec0cSJonas Devlieghere llvm::StringRef GetPluginName() override { return "thumb"; }
GetDescription()72a114ec0cSJonas Devlieghere llvm::StringRef GetDescription() override { return "thumb"; }
73a114ec0cSJonas Devlieghere };
74a114ec0cSJonas Devlieghere
75a114ec0cSJonas Devlieghere class PlatformTest : public ::testing::Test {
76a114ec0cSJonas Devlieghere SubsystemRAII<FileSystem, HostInfo> subsystems;
77af921006SPavel Labath
78af921006SPavel Labath protected:
79*d6678404SMed Ismail Bennani PlatformList list;
80af921006SPavel Labath
SetHostPlatform(const PlatformSP & platform_sp)81af921006SPavel Labath void SetHostPlatform(const PlatformSP &platform_sp) {
82af921006SPavel Labath Platform::SetHostPlatform(platform_sp);
83af921006SPavel Labath ASSERT_EQ(Platform::GetHostPlatform(), platform_sp);
84*d6678404SMed Ismail Bennani list.Append(platform_sp, /*set_selected=*/true);
85af921006SPavel Labath }
86a114ec0cSJonas Devlieghere };
87a114ec0cSJonas Devlieghere
TEST_F(PlatformTest,GetPlatformForArchitecturesHost)88a114ec0cSJonas Devlieghere TEST_F(PlatformTest, GetPlatformForArchitecturesHost) {
89af921006SPavel Labath SetHostPlatform(std::make_shared<PlatformArm>());
90a114ec0cSJonas Devlieghere
91a114ec0cSJonas Devlieghere const std::vector<ArchSpec> archs = {ArchSpec("arm64-apple-ps4"),
92a114ec0cSJonas Devlieghere ArchSpec("arm64e-apple-ps4")};
93a114ec0cSJonas Devlieghere std::vector<PlatformSP> candidates;
94a114ec0cSJonas Devlieghere
95a114ec0cSJonas Devlieghere // The host platform matches all architectures.
96*d6678404SMed Ismail Bennani PlatformSP platform_sp = list.GetOrCreate(archs, {}, candidates);
97a114ec0cSJonas Devlieghere ASSERT_TRUE(platform_sp);
98af921006SPavel Labath EXPECT_EQ(platform_sp, Platform::GetHostPlatform());
99a114ec0cSJonas Devlieghere }
100a114ec0cSJonas Devlieghere
TEST_F(PlatformTest,GetPlatformForArchitecturesSelected)101a114ec0cSJonas Devlieghere TEST_F(PlatformTest, GetPlatformForArchitecturesSelected) {
102af921006SPavel Labath SetHostPlatform(std::make_shared<PlatformIntel>());
103a114ec0cSJonas Devlieghere
104a114ec0cSJonas Devlieghere const std::vector<ArchSpec> archs = {ArchSpec("arm64-apple-ps4"),
105a114ec0cSJonas Devlieghere ArchSpec("arm64e-apple-ps4")};
106a114ec0cSJonas Devlieghere std::vector<PlatformSP> candidates;
107a114ec0cSJonas Devlieghere
108af921006SPavel Labath // The host platform matches no architectures.
109*d6678404SMed Ismail Bennani PlatformSP platform_sp = list.GetOrCreate(archs, {}, candidates);
110a114ec0cSJonas Devlieghere ASSERT_FALSE(platform_sp);
111a114ec0cSJonas Devlieghere
112a114ec0cSJonas Devlieghere // The selected platform matches all architectures.
113a114ec0cSJonas Devlieghere const PlatformSP selected_platform_sp = std::make_shared<PlatformArm>();
114*d6678404SMed Ismail Bennani list.Append(selected_platform_sp, /*set_selected=*/true);
115*d6678404SMed Ismail Bennani platform_sp = list.GetOrCreate(archs, {}, candidates);
116a114ec0cSJonas Devlieghere ASSERT_TRUE(platform_sp);
117a114ec0cSJonas Devlieghere EXPECT_EQ(platform_sp, selected_platform_sp);
118a114ec0cSJonas Devlieghere }
119a114ec0cSJonas Devlieghere
TEST_F(PlatformTest,GetPlatformForArchitecturesSelectedOverHost)120a114ec0cSJonas Devlieghere TEST_F(PlatformTest, GetPlatformForArchitecturesSelectedOverHost) {
121af921006SPavel Labath SetHostPlatform(std::make_shared<PlatformIntel>());
122a114ec0cSJonas Devlieghere
123a114ec0cSJonas Devlieghere const std::vector<ArchSpec> archs = {ArchSpec("arm64-apple-ps4"),
124a114ec0cSJonas Devlieghere ArchSpec("x86_64-apple-ps4")};
125a114ec0cSJonas Devlieghere std::vector<PlatformSP> candidates;
126a114ec0cSJonas Devlieghere
127af921006SPavel Labath // The host platform matches one architecture.
128*d6678404SMed Ismail Bennani PlatformSP platform_sp = list.GetOrCreate(archs, {}, candidates);
129a114ec0cSJonas Devlieghere ASSERT_TRUE(platform_sp);
130af921006SPavel Labath EXPECT_EQ(platform_sp, Platform::GetHostPlatform());
131a114ec0cSJonas Devlieghere
132a114ec0cSJonas Devlieghere // The selected and host platform each match one architecture.
133a114ec0cSJonas Devlieghere // The selected platform is preferred.
134a114ec0cSJonas Devlieghere const PlatformSP selected_platform_sp = std::make_shared<PlatformArm>();
135*d6678404SMed Ismail Bennani list.Append(selected_platform_sp, /*set_selected=*/true);
136*d6678404SMed Ismail Bennani platform_sp = list.GetOrCreate(archs, {}, candidates);
137a114ec0cSJonas Devlieghere ASSERT_TRUE(platform_sp);
138a114ec0cSJonas Devlieghere EXPECT_EQ(platform_sp, selected_platform_sp);
139a114ec0cSJonas Devlieghere }
140a114ec0cSJonas Devlieghere
TEST_F(PlatformTest,GetPlatformForArchitecturesCandidates)141a114ec0cSJonas Devlieghere TEST_F(PlatformTest, GetPlatformForArchitecturesCandidates) {
142a114ec0cSJonas Devlieghere PlatformThumb::Initialize();
143a114ec0cSJonas Devlieghere
144af921006SPavel Labath SetHostPlatform(std::make_shared<PlatformIntel>());
145af921006SPavel Labath
146a114ec0cSJonas Devlieghere const PlatformSP selected_platform_sp = std::make_shared<PlatformArm>();
147*d6678404SMed Ismail Bennani list.Append(selected_platform_sp, /*set_selected=*/true);
148a114ec0cSJonas Devlieghere
149a114ec0cSJonas Devlieghere const std::vector<ArchSpec> archs = {ArchSpec("thumbv7-apple-ps4"),
150a114ec0cSJonas Devlieghere ArchSpec("thumbv7f-apple-ps4")};
151a114ec0cSJonas Devlieghere std::vector<PlatformSP> candidates;
152a114ec0cSJonas Devlieghere
153af921006SPavel Labath // The host platform matches one architecture.
154*d6678404SMed Ismail Bennani PlatformSP platform_sp = list.GetOrCreate(archs, {}, candidates);
155a114ec0cSJonas Devlieghere ASSERT_TRUE(platform_sp);
156a114ec0cSJonas Devlieghere EXPECT_EQ(platform_sp->GetName(), "thumb");
157a114ec0cSJonas Devlieghere
158a114ec0cSJonas Devlieghere PlatformThumb::Terminate();
159a114ec0cSJonas Devlieghere }
160