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