Skip to content

Commit d65ec4a

Browse files
committed
Add OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED support
1 parent 6040b4d commit d65ec4a

File tree

6 files changed

+73
-49
lines changed

6 files changed

+73
-49
lines changed

Package.swift

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ let includePath = SDKPath.appending("/usr/lib/swift")
3131

3232
// MARK: - C/CXX Settings
3333

34-
// Source: https://github.com/swiftlang/swift/blob/main/SwiftCompilerSources/Package.swift
35-
// To successfully build, you'll need to create a couple of symlinks to an
36-
// existing Ninja build:
34+
// Modified from: https://github.com/swiftlang/swift/blob/main/SwiftCompilerSources/Package.swift
3735
//
38-
// cd $OPENGRAPH_SWIFT_TOOLCHAIN_PATH
39-
// mkdir -p build/Default
40-
// ln -s build/<Ninja-Build>/llvm-<os+arch> build/Default/llvm
41-
// ln -s build/<Ninja-Build>/swift-<os+arch> build/Default/swift
36+
// Create a couple of symlinks to an existing Ninja build:
4237
//
43-
// where <project-root> is the parent directory of the swift repository.
38+
// ```shell
39+
// cd $OPENGRAPH_SWIFT_TOOLCHAIN_PATH
40+
// mkdir -p build/Default
41+
// ln -s build/<Ninja-Build>/llvm-<os+arch> build/Default/llvm
42+
// ln -s build/<Ninja-Build>/swift-<os+arch> build/Default/swift
43+
// ```
4444
//
45-
// FIXME: We may want to consider generating Package.swift as a part of the
46-
// build.
45+
// where <$OPENGRAPH_SWIFT_TOOLCHAIN_PATH> is the parent directory of the swift repository.
4746

48-
let swiftToolchainVersion = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_VERSION"] ?? ""
49-
let swiftToolchainPath = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_PATH"] ?? ""
47+
let swiftToolchainVersion = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_VERSION"] ?? (development ? "6.0.2" : "")
48+
let swiftToolchainPath = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_PATH"] ?? (development ? "/Volumes/BuildMachine/swift-project" : "")
49+
let swiftToolchainSupported = envEnable("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED", default: !swiftToolchainVersion.isEmpty)
5050

5151
var sharedCSettings: [CSetting] = [
5252
.unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)),
@@ -68,6 +68,7 @@ if !swiftToolchainPath.isEmpty {
6868
"-I\(swiftToolchainPath)/build/Default/swift/include",
6969
"-I\(swiftToolchainPath)/build/Default/llvm/include",
7070
"-I\(swiftToolchainPath)/build/Default/llvm/tools/clang/include",
71+
"-DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING", // Required to fix LLVM link issue
7172
]
7273
)
7374
)
@@ -79,6 +80,12 @@ if !swiftToolchainVersion.isEmpty {
7980
)
8081
}
8182

83+
if swiftToolchainSupported {
84+
sharedCSettings.append(
85+
.define("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED")
86+
)
87+
}
88+
8289
// MARK: - Swift Settings
8390

8491
var sharedSwiftSettings: [SwiftSetting] = [
@@ -87,6 +94,12 @@ var sharedSwiftSettings: [SwiftSetting] = [
8794
.swiftLanguageMode(.v5),
8895
]
8996

97+
if swiftToolchainSupported {
98+
sharedSwiftSettings.append(
99+
.define("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED")
100+
)
101+
}
102+
90103
if releaseVersion >= 2021 {
91104
for year in 2021 ... releaseVersion {
92105
sharedSwiftSettings.append(.define("OPENGRAPH_SUPPORT_\(year)_API"))

Sources/OpenGraphShims/GraphShims.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ public typealias OGValue = AGValue
2727
public typealias OGValueOptions = AGValueOptions
2828
public typealias OGValueState = AGValueState
2929
public let attributeGraphEnabled = true
30+
public let swiftToolchainSupported = true
3031
#else
3132
@_exported import OpenGraph
3233
public let attributeGraphEnabled = false
34+
#if OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
35+
public let swiftToolchainSupported = true
36+
#else
37+
public let swiftToolchainSupported = false
38+
#endif
3339
#endif

Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
#include "OGTypeID.h"
99

10-
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_VERSION
10+
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
1111
#include <swift/Runtime/Metadata.h>
1212
#endif
1313

1414
OGTypeKind OGTypeGetKind(OGTypeID typeID) {
15-
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_VERSION
15+
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
1616
const swift::Metadata *type = reinterpret_cast<const swift::Metadata *>(typeID);
1717
switch (type->getKind()) {
1818
case swift::MetadataKind::Class:

Tests/OpenGraphCompatibilityTests/GraphShims.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//
22
// GraphShims.swift
3-
//
4-
//
5-
//
3+
// OpenGraphCompatibilityTests
64

75
#if OPENGRAPH_COMPATIBILITY_TEST
86
@_exported public import AttributeGraph
@@ -29,8 +27,14 @@ public typealias OGUniqueID = AGUniqueID
2927
public typealias OGValue = AGValue
3028
public typealias OGValueOptions = AGValueOptions
3129
public typealias OGValueState = AGValueState
32-
let compatibilityTestEnabled = true
30+
public let compatibilityTestEnabled = true
31+
public let swiftToolchainSupported = true
3332
#else
3433
@_exported import OpenGraph
3534
let compatibilityTestEnabled = false
35+
#if OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
36+
public let swiftToolchainSupported = true
37+
#else
38+
public let swiftToolchainSupported = false
39+
#endif
3640
#endif

Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,37 @@
44

55
import Testing
66

7-
@Suite(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
87
struct MetadataTests {
9-
@Test
8+
@Test(.enabled(if: swiftToolchainSupported))
9+
func kindCases() throws {
10+
class T1 {}
11+
struct T2 {}
12+
enum T3 {}
13+
protocol P {}
14+
15+
#expect(Metadata(T1.self).kind == .class)
16+
#expect(Metadata(T2.self).kind == .struct)
17+
#expect(Metadata(T3.self).kind == .enum)
18+
19+
#expect(Metadata(Void?.self).kind == .optional)
20+
#expect(Metadata(Int?.self).kind == .optional)
21+
#expect(Metadata(T1?.self).kind == .optional)
22+
#expect(Metadata((T1, T2)?.self).kind == .optional)
23+
24+
#expect(Metadata(Void.self).kind == .tuple)
25+
#expect(Metadata((Int, Double?).self).kind == .tuple)
26+
#expect(Metadata((T1, T2, T3).self).kind == .tuple)
27+
28+
#expect(Metadata((() -> Void).self).kind == .function)
29+
30+
#expect(Metadata(P.self).kind == .existential)
31+
#expect(Metadata((any P).self).kind == .existential)
32+
33+
#expect(Metadata(P.Protocol.self).kind == .metatype)
34+
#expect(Metadata(type(of: Int.self)).kind == .metatype)
35+
}
36+
37+
@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
1038
func descriptor() throws {
1139
let n1 = try #require(Metadata(Int.self).nominalDescriptor)
1240
let n2 = try #require(Metadata(String.self).nominalDescriptor)
@@ -30,14 +58,14 @@ struct MetadataTests {
3058
case a, b
3159
}
3260

33-
@Test
61+
@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
3462
func description() {
3563
#expect(Metadata(T1.self).description == "MetadataTests.T1")
3664
#expect(Metadata(T2.self).description == "MetadataTests.T2")
3765
#expect(Metadata(T3.self).description == "MetadataTests.T3")
3866
}
3967

40-
@Test
68+
@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
4169
func forEachField() throws {
4270
for options in [OGTypeApplyOptions._1] {
4371
let result = Metadata(T1.self).forEachField(options: options) { name, offset, type in

Tests/OpenGraphCompatibilityTests/Runtime/TypeKindTests.swift

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,8 @@
66

77
import Testing
88

9-
private protocol P {}
109

1110
@Suite(.disabled(if: !compatibilityTestEnabled, "Metadata.kind is not implemented"))
1211
struct TypeKindTests {
13-
class T1 {}
14-
struct T2 {}
15-
enum T3 {}
16-
17-
@Test
18-
func kindCases() throws {
19-
#expect(Metadata(T1.self).kind == .class)
20-
#expect(Metadata(T2.self).kind == .struct)
21-
#expect(Metadata(T3.self).kind == .enum)
22-
23-
#expect(Metadata(Void?.self).kind == .optional)
24-
#expect(Metadata(Int?.self).kind == .optional)
25-
#expect(Metadata(T1?.self).kind == .optional)
26-
#expect(Metadata((T1, T2)?.self).kind == .optional)
2712

28-
#expect(Metadata(Void.self).kind == .tuple)
29-
#expect(Metadata((Int, Double?).self).kind == .tuple)
30-
#expect(Metadata((T1, T2, T3).self).kind == .tuple)
31-
32-
#expect(Metadata((() -> Void).self).kind == .function)
33-
34-
#expect(Metadata(P.self).kind == .existential)
35-
#expect(Metadata((any P).self).kind == .existential)
36-
37-
#expect(Metadata(P.Protocol.self).kind == .metatype)
38-
#expect(Metadata(type(of: Int.self)).kind == .metatype)
39-
}
4013
}

0 commit comments

Comments
 (0)