Skip to content

Commit e40bc8e

Browse files
committed
[ORC][MachO] Make BuildVersionOpts::fromTriple result optional, add test.
Only platform specific darwin OS values (e.g. macosx, ios, watchos, ...) can be mapped to an LC_BUILD_VERSION platform. For all other values return an empty optional to indicate that the load command can't be constructed. Also fixes the simulator conditions to return the correct platform, and adds a testcase.
1 parent d192b64 commit e40bc8e

File tree

4 files changed

+69
-13
lines changed

4 files changed

+69
-13
lines changed

llvm/include/llvm/ExecutionEngine/Orc/MachOPlatform.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ class MachOPlatform : public Platform {
6060

6161
struct BuildVersionOpts {
6262

63-
// Derive platform from triple.
64-
static BuildVersionOpts fromTriple(const Triple &TT, uint32_t MinOS,
65-
uint32_t SDK);
63+
// Derive platform from triple if possible.
64+
static std::optional<BuildVersionOpts>
65+
fromTriple(const Triple &TT, uint32_t MinOS, uint32_t SDK);
6666

6767
uint32_t Platform; // Platform.
6868
uint32_t MinOS; // X.Y.Z is encoded in nibbles xxxx.yy.zz

llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,34 +255,33 @@ struct ObjCImageInfoFlags {
255255
namespace llvm {
256256
namespace orc {
257257

258-
MachOPlatform::HeaderOptions::BuildVersionOpts
258+
std::optional<MachOPlatform::HeaderOptions::BuildVersionOpts>
259259
MachOPlatform::HeaderOptions::BuildVersionOpts::fromTriple(const Triple &TT,
260260
uint32_t MinOS,
261261
uint32_t SDK) {
262262

263263
uint32_t Platform;
264264
switch (TT.getOS()) {
265265
case Triple::IOS:
266-
Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_IOS
267-
: MachO::PLATFORM_IOSSIMULATOR;
266+
Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_IOSSIMULATOR
267+
: MachO::PLATFORM_IOS;
268268
break;
269269
case Triple::MacOSX:
270270
Platform = MachO::PLATFORM_MACOS;
271271
break;
272272
case Triple::TvOS:
273-
Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_TVOS
274-
: MachO::PLATFORM_TVOSSIMULATOR;
273+
Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_TVOSSIMULATOR
274+
: MachO::PLATFORM_TVOS;
275275
break;
276276
case Triple::WatchOS:
277-
Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_WATCHOS
278-
: MachO::PLATFORM_WATCHOSSIMULATOR;
277+
Platform = TT.isSimulatorEnvironment() ? MachO::PLATFORM_WATCHOSSIMULATOR
278+
: MachO::PLATFORM_WATCHOS;
279279
break;
280280
default:
281-
Platform = MachO::PLATFORM_UNKNOWN;
282-
break;
281+
return std::nullopt;
283282
}
284283

285-
return {Platform, MinOS, SDK};
284+
return MachOPlatform::HeaderOptions::BuildVersionOpts{Platform, MinOS, SDK};
286285
}
287286

288287
Expected<std::unique_ptr<MachOPlatform>> MachOPlatform::Create(

llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_llvm_unittest(OrcJITTests
2626
JITTargetMachineBuilderTest.cpp
2727
LazyCallThroughAndReexportsTest.cpp
2828
LookupAndRecordAddrsTest.cpp
29+
MachOPlatformTest.cpp
2930
MapperJITLinkMemoryManagerTest.cpp
3031
MemoryMapperTest.cpp
3132
ObjectFormatsTest.cpp
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===---------- MachOPlatformTest.cpp - MachPlatform API Tests ------------===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/ExecutionEngine/Orc/MachOPlatform.h"
10+
#include "llvm/BinaryFormat/MachO.h"
11+
#include "gtest/gtest.h"
12+
13+
using namespace llvm;
14+
using namespace llvm::orc;
15+
16+
TEST(MachOPlatformTests, BuildVersionOptsFromTriple) {
17+
18+
auto darwinOS = MachOPlatform::HeaderOptions::BuildVersionOpts::fromTriple(
19+
Triple("arm64-apple-darwin"), 0, 0);
20+
EXPECT_FALSE(darwinOS);
21+
22+
auto macOS = MachOPlatform::HeaderOptions::BuildVersionOpts::fromTriple(
23+
Triple("arm64-apple-macosx"), 0, 0);
24+
EXPECT_TRUE(macOS);
25+
EXPECT_EQ(macOS->Platform, MachO::PLATFORM_MACOS);
26+
27+
auto iOS = MachOPlatform::HeaderOptions::BuildVersionOpts::fromTriple(
28+
Triple("arm64-apple-ios"), 0, 0);
29+
EXPECT_TRUE(iOS);
30+
EXPECT_EQ(iOS->Platform, MachO::PLATFORM_IOS);
31+
32+
auto iOSSim = MachOPlatform::HeaderOptions::BuildVersionOpts::fromTriple(
33+
Triple("arm64-apple-ios-simulator"), 0, 0);
34+
EXPECT_TRUE(iOSSim);
35+
EXPECT_EQ(iOSSim->Platform, MachO::PLATFORM_IOSSIMULATOR);
36+
37+
auto tvOS = MachOPlatform::HeaderOptions::BuildVersionOpts::fromTriple(
38+
Triple("arm64-apple-tvos"), 0, 0);
39+
EXPECT_TRUE(tvOS);
40+
EXPECT_EQ(tvOS->Platform, MachO::PLATFORM_TVOS);
41+
42+
auto tvOSSim = MachOPlatform::HeaderOptions::BuildVersionOpts::fromTriple(
43+
Triple("arm64-apple-tvos-simulator"), 0, 0);
44+
EXPECT_TRUE(tvOSSim);
45+
EXPECT_EQ(tvOSSim->Platform, MachO::PLATFORM_TVOSSIMULATOR);
46+
47+
auto watchOS = MachOPlatform::HeaderOptions::BuildVersionOpts::fromTriple(
48+
Triple("arm64-apple-watchos"), 0, 0);
49+
EXPECT_TRUE(watchOS);
50+
EXPECT_EQ(watchOS->Platform, MachO::PLATFORM_WATCHOS);
51+
52+
auto watchOSSim = MachOPlatform::HeaderOptions::BuildVersionOpts::fromTriple(
53+
Triple("arm64-apple-watchos-simulator"), 0, 0);
54+
EXPECT_TRUE(watchOSSim);
55+
EXPECT_EQ(watchOSSim->Platform, MachO::PLATFORM_WATCHOSSIMULATOR);
56+
}

0 commit comments

Comments
 (0)