From d65ec4aca5ab82eca9da0c03f214b84117184d3f Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 29 Dec 2024 18:35:45 +0800 Subject: [PATCH 1/8] Add OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED support --- Package.swift | 37 +++++++++++++------ Sources/OpenGraphShims/GraphShims.swift | 6 +++ Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp | 4 +- .../GraphShims.swift | 12 ++++-- .../Runtime/MetadataTests.swift | 36 ++++++++++++++++-- .../Runtime/TypeKindTests.swift | 27 -------------- 6 files changed, 73 insertions(+), 49 deletions(-) diff --git a/Package.swift b/Package.swift index 57a1d90e..e1abf343 100644 --- a/Package.swift +++ b/Package.swift @@ -31,22 +31,22 @@ let includePath = SDKPath.appending("/usr/lib/swift") // MARK: - C/CXX Settings -// Source: https://github.com/swiftlang/swift/blob/main/SwiftCompilerSources/Package.swift -// To successfully build, you'll need to create a couple of symlinks to an -// existing Ninja build: +// Modified from: https://github.com/swiftlang/swift/blob/main/SwiftCompilerSources/Package.swift // -// cd $OPENGRAPH_SWIFT_TOOLCHAIN_PATH -// mkdir -p build/Default -// ln -s build//llvm- build/Default/llvm -// ln -s build//swift- build/Default/swift +// Create a couple of symlinks to an existing Ninja build: // -// where is the parent directory of the swift repository. +// ```shell +// cd $OPENGRAPH_SWIFT_TOOLCHAIN_PATH +// mkdir -p build/Default +// ln -s build//llvm- build/Default/llvm +// ln -s build//swift- build/Default/swift +// ``` // -// FIXME: We may want to consider generating Package.swift as a part of the -// build. +// where <$OPENGRAPH_SWIFT_TOOLCHAIN_PATH> is the parent directory of the swift repository. -let swiftToolchainVersion = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_VERSION"] ?? "" -let swiftToolchainPath = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_PATH"] ?? "" +let swiftToolchainVersion = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_VERSION"] ?? (development ? "6.0.2" : "") +let swiftToolchainPath = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_PATH"] ?? (development ? "/Volumes/BuildMachine/swift-project" : "") +let swiftToolchainSupported = envEnable("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED", default: !swiftToolchainVersion.isEmpty) var sharedCSettings: [CSetting] = [ .unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)), @@ -68,6 +68,7 @@ if !swiftToolchainPath.isEmpty { "-I\(swiftToolchainPath)/build/Default/swift/include", "-I\(swiftToolchainPath)/build/Default/llvm/include", "-I\(swiftToolchainPath)/build/Default/llvm/tools/clang/include", + "-DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING", // Required to fix LLVM link issue ] ) ) @@ -79,6 +80,12 @@ if !swiftToolchainVersion.isEmpty { ) } +if swiftToolchainSupported { + sharedCSettings.append( + .define("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED") + ) +} + // MARK: - Swift Settings var sharedSwiftSettings: [SwiftSetting] = [ @@ -87,6 +94,12 @@ var sharedSwiftSettings: [SwiftSetting] = [ .swiftLanguageMode(.v5), ] +if swiftToolchainSupported { + sharedSwiftSettings.append( + .define("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED") + ) +} + if releaseVersion >= 2021 { for year in 2021 ... releaseVersion { sharedSwiftSettings.append(.define("OPENGRAPH_SUPPORT_\(year)_API")) diff --git a/Sources/OpenGraphShims/GraphShims.swift b/Sources/OpenGraphShims/GraphShims.swift index 6605bb82..29e03f56 100644 --- a/Sources/OpenGraphShims/GraphShims.swift +++ b/Sources/OpenGraphShims/GraphShims.swift @@ -27,7 +27,13 @@ public typealias OGValue = AGValue public typealias OGValueOptions = AGValueOptions public typealias OGValueState = AGValueState public let attributeGraphEnabled = true +public let swiftToolchainSupported = true #else @_exported import OpenGraph public let attributeGraphEnabled = false +#if OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED +public let swiftToolchainSupported = true +#else +public let swiftToolchainSupported = false +#endif #endif diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp b/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp index 5c4bc1db..402df8af 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp +++ b/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp @@ -7,12 +7,12 @@ #include "OGTypeID.h" -#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_VERSION +#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED #include #endif OGTypeKind OGTypeGetKind(OGTypeID typeID) { - #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_VERSION + #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED const swift::Metadata *type = reinterpret_cast(typeID); switch (type->getKind()) { case swift::MetadataKind::Class: diff --git a/Tests/OpenGraphCompatibilityTests/GraphShims.swift b/Tests/OpenGraphCompatibilityTests/GraphShims.swift index 7336ad59..5cdf2a76 100644 --- a/Tests/OpenGraphCompatibilityTests/GraphShims.swift +++ b/Tests/OpenGraphCompatibilityTests/GraphShims.swift @@ -1,8 +1,6 @@ // // GraphShims.swift -// -// -// +// OpenGraphCompatibilityTests #if OPENGRAPH_COMPATIBILITY_TEST @_exported public import AttributeGraph @@ -29,8 +27,14 @@ public typealias OGUniqueID = AGUniqueID public typealias OGValue = AGValue public typealias OGValueOptions = AGValueOptions public typealias OGValueState = AGValueState -let compatibilityTestEnabled = true +public let compatibilityTestEnabled = true +public let swiftToolchainSupported = true #else @_exported import OpenGraph let compatibilityTestEnabled = false +#if OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED +public let swiftToolchainSupported = true +#else +public let swiftToolchainSupported = false +#endif #endif diff --git a/Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift b/Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift index be5b44f7..5b58677e 100644 --- a/Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift +++ b/Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift @@ -4,9 +4,37 @@ import Testing -@Suite(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented")) struct MetadataTests { - @Test + @Test(.enabled(if: swiftToolchainSupported)) + func kindCases() throws { + class T1 {} + struct T2 {} + enum T3 {} + protocol P {} + + #expect(Metadata(T1.self).kind == .class) + #expect(Metadata(T2.self).kind == .struct) + #expect(Metadata(T3.self).kind == .enum) + + #expect(Metadata(Void?.self).kind == .optional) + #expect(Metadata(Int?.self).kind == .optional) + #expect(Metadata(T1?.self).kind == .optional) + #expect(Metadata((T1, T2)?.self).kind == .optional) + + #expect(Metadata(Void.self).kind == .tuple) + #expect(Metadata((Int, Double?).self).kind == .tuple) + #expect(Metadata((T1, T2, T3).self).kind == .tuple) + + #expect(Metadata((() -> Void).self).kind == .function) + + #expect(Metadata(P.self).kind == .existential) + #expect(Metadata((any P).self).kind == .existential) + + #expect(Metadata(P.Protocol.self).kind == .metatype) + #expect(Metadata(type(of: Int.self)).kind == .metatype) + } + + @Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented")) func descriptor() throws { let n1 = try #require(Metadata(Int.self).nominalDescriptor) let n2 = try #require(Metadata(String.self).nominalDescriptor) @@ -30,14 +58,14 @@ struct MetadataTests { case a, b } - @Test + @Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented")) func description() { #expect(Metadata(T1.self).description == "MetadataTests.T1") #expect(Metadata(T2.self).description == "MetadataTests.T2") #expect(Metadata(T3.self).description == "MetadataTests.T3") } - @Test + @Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented")) func forEachField() throws { for options in [OGTypeApplyOptions._1] { let result = Metadata(T1.self).forEachField(options: options) { name, offset, type in diff --git a/Tests/OpenGraphCompatibilityTests/Runtime/TypeKindTests.swift b/Tests/OpenGraphCompatibilityTests/Runtime/TypeKindTests.swift index 38597217..5ee4e1b5 100644 --- a/Tests/OpenGraphCompatibilityTests/Runtime/TypeKindTests.swift +++ b/Tests/OpenGraphCompatibilityTests/Runtime/TypeKindTests.swift @@ -6,35 +6,8 @@ import Testing -private protocol P {} @Suite(.disabled(if: !compatibilityTestEnabled, "Metadata.kind is not implemented")) struct TypeKindTests { - class T1 {} - struct T2 {} - enum T3 {} - - @Test - func kindCases() throws { - #expect(Metadata(T1.self).kind == .class) - #expect(Metadata(T2.self).kind == .struct) - #expect(Metadata(T3.self).kind == .enum) - - #expect(Metadata(Void?.self).kind == .optional) - #expect(Metadata(Int?.self).kind == .optional) - #expect(Metadata(T1?.self).kind == .optional) - #expect(Metadata((T1, T2)?.self).kind == .optional) - #expect(Metadata(Void.self).kind == .tuple) - #expect(Metadata((Int, Double?).self).kind == .tuple) - #expect(Metadata((T1, T2, T3).self).kind == .tuple) - - #expect(Metadata((() -> Void).self).kind == .function) - - #expect(Metadata(P.self).kind == .existential) - #expect(Metadata((any P).self).kind == .existential) - - #expect(Metadata(P.Protocol.self).kind == .metatype) - #expect(Metadata(type(of: Int.self)).kind == .metatype) - } } From b1897c9802a335c8923f485f08bdc9b3a66b94c1 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 29 Dec 2024 19:14:15 +0800 Subject: [PATCH 2/8] Update OGTypeID --- Package.resolved | 2 +- Sources/OpenGraph_SPI/Graph/OGSubgraph.cpp | 4 +-- .../OpenGraph_SPI/Runtime/OGSwiftMetadata.h | 1 + .../Runtime/OGTypeDescription.cpp | 17 ----------- .../OpenGraph_SPI/Runtime/OGTypeDescription.h | 29 ------------------- Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp | 10 +++++++ Sources/OpenGraph_SPI/Runtime/OGTypeID.h | 29 +++++++++++++++---- Sources/OpenGraph_SPI/Runtime/OGTypeKind.h | 25 ---------------- Sources/OpenGraph_SPI/Runtime/metadata.cpp | 16 ++++++++-- Sources/OpenGraph_SPI/Runtime/metadata.hpp | 25 +++++++++++----- .../OpenGraph_SPI/include/OGTypeDescription.h | 1 - Sources/OpenGraph_SPI/include/OGTypeKind.h | 1 - .../include/OpenGraph-umbrella.h | 2 -- 13 files changed, 69 insertions(+), 93 deletions(-) delete mode 100644 Sources/OpenGraph_SPI/Runtime/OGTypeDescription.cpp delete mode 100644 Sources/OpenGraph_SPI/Runtime/OGTypeDescription.h delete mode 100644 Sources/OpenGraph_SPI/Runtime/OGTypeKind.h delete mode 120000 Sources/OpenGraph_SPI/include/OGTypeDescription.h delete mode 120000 Sources/OpenGraph_SPI/include/OGTypeKind.h diff --git a/Package.resolved b/Package.resolved index 47c37439..fa856e62 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "2553be1b4dad3da670fb2da41763983faffa9e83c086d8616c6d4205bcfc0e61", + "originHash" : "63fa61469a826aeb30fad884b1923ca3d94dc431595ef64806429df9d5711e10", "pins" : [ { "identity" : "darwinprivateframeworks", diff --git a/Sources/OpenGraph_SPI/Graph/OGSubgraph.cpp b/Sources/OpenGraph_SPI/Graph/OGSubgraph.cpp index 19cf5e78..36a99f7e 100644 --- a/Sources/OpenGraph_SPI/Graph/OGSubgraph.cpp +++ b/Sources/OpenGraph_SPI/Graph/OGSubgraph.cpp @@ -196,14 +196,14 @@ void OGSubgraphSetShouldRecordTree() { void OGSubgraphBeginTreeElement(OGAttribute attribute, OGTypeID type, uint32_t flags) { OG::Subgraph * subgraph = OG::Subgraph::get_current(); if (subgraph) { - subgraph->begin_tree(attribute, type, flags); + subgraph->begin_tree(attribute, reinterpret_cast(type), flags); } } void OGSubgraphAddTreeValue(OGAttribute attribute, OGTypeID type, const char * key, uint32_t flags) { OG::Subgraph * subgraph = OG::Subgraph::get_current(); if (subgraph) { - subgraph->add_tree_value(attribute, type, key, flags); + subgraph->add_tree_value(attribute, reinterpret_cast(type), key, flags); } } diff --git a/Sources/OpenGraph_SPI/Runtime/OGSwiftMetadata.h b/Sources/OpenGraph_SPI/Runtime/OGSwiftMetadata.h index 860995d9..1b05fd57 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGSwiftMetadata.h +++ b/Sources/OpenGraph_SPI/Runtime/OGSwiftMetadata.h @@ -1,6 +1,7 @@ // // OGSwiftMetadata.h // OpenGraph_SPI +// #ifndef OGSwiftMetadata_h #define OGSwiftMetadata_h diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeDescription.cpp b/Sources/OpenGraph_SPI/Runtime/OGTypeDescription.cpp deleted file mode 100644 index 52476e8b..00000000 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeDescription.cpp +++ /dev/null @@ -1,17 +0,0 @@ -// -// OGTypeDescription.cpp -// -// -// Created by Kyle on 2024/1/24. -// - -#include "OGTypeDescription.h" - -#if OG_TARGET_OS_DARWIN -CFStringRef OGTypeDescription(OGTypeID id) { - CFMutableStringRef ref = CFStringCreateMutable(CFAllocatorGetDefault(), 0); - // OG::swift::metadata::append_description(__CFString*) const - // cast id into metadata and call append_description - return ref; -} -#endif /* OG_TARGET_OS_DARWIN */ diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeDescription.h b/Sources/OpenGraph_SPI/Runtime/OGTypeDescription.h deleted file mode 100644 index 0c9b4d18..00000000 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeDescription.h +++ /dev/null @@ -1,29 +0,0 @@ -// -// OGTypeDescription.h -// -// -// Created by Kyle on 2024/1/24. -// - -#ifndef OGTypeDescription_h -#define OGTypeDescription_h - -#include "OGBase.h" -#include "OGTypeID.h" - -OG_ASSUME_NONNULL_BEGIN - -#if OG_TARGET_OS_DARWIN -OG_EXTERN_C_BEGIN - -OG_EXPORT -OG_REFINED_FOR_SWIFT -CFStringRef OGTypeDescription(OGTypeID type); - -OG_EXTERN_C_END - -#endif /* OG_TARGET_OS_DARWIN */ - -OG_ASSUME_NONNULL_END - -#endif /* OGTypeDescription_h */ diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp b/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp index 402df8af..1ac6cc95 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp +++ b/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp @@ -6,6 +6,7 @@ // #include "OGTypeID.h" +#include "metadata.hpp" #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED #include @@ -39,6 +40,15 @@ OGTypeKind OGTypeGetKind(OGTypeID typeID) { #endif } +CFStringRef OGTypeDescription(OGTypeID id) { + CFMutableStringRef ref = CFStringCreateMutable(CFAllocatorGetDefault(), 0); + #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED + OG::swift::metadata const *metadata = reinterpret_cast(id); + metadata->append_description(ref); + #endif + return ref; +} + const void * OGTypeNominalDescriptor(OGTypeID typeID) { return nullptr; } diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h index 702c604a..0da1f709 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h +++ b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h @@ -1,29 +1,48 @@ // // OGTypeID.h +// OpenGraph_SPI // -// -// Created by Kyle on 2024/1/8. -// +// Audited for iOS 18.0 +// Status: WIP #ifndef OGTypeID_h #define OGTypeID_h #include "OGBase.h" #include "OGSwiftMetadata.h" -#include "OGTypeKind.h" -typedef const OGSwiftMetadata *OGTypeID OG_SWIFT_STRUCT OG_SWIFT_NAME(Metadata); +OG_ASSUME_NONNULL_BEGIN OG_EXTERN_C_BEGIN +typedef const OGSwiftMetadata *OGTypeID OG_SWIFT_STRUCT OG_SWIFT_NAME(Metadata); + +typedef OG_ENUM(uint32_t, OGTypeKind) { + OGTypeKindNone, + OGTypeKindClass, + OGTypeKindStruct, + OGTypeKindEnum, + OGTypeKindOptional, + OGTypeKindTuple, + OGTypeKindFunction, + OGTypeKindExistential, + OGTypeKindMetatype, +}; + OG_EXPORT OG_REFINED_FOR_SWIFT OGTypeKind OGTypeGetKind(OGTypeID typeID) OG_SWIFT_NAME(getter:OGTypeID.kind(self:)); +OG_EXPORT +OG_REFINED_FOR_SWIFT +CFStringRef OGTypeDescription(OGTypeID type); + OG_EXPORT OG_REFINED_FOR_SWIFT const void * OGTypeNominalDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:OGTypeID.nominalDescriptor(self:)); OG_EXTERN_C_END +OG_ASSUME_NONNULL_END + #endif /* OGTypeID_h */ diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeKind.h b/Sources/OpenGraph_SPI/Runtime/OGTypeKind.h deleted file mode 100644 index 2bb4ead2..00000000 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeKind.h +++ /dev/null @@ -1,25 +0,0 @@ -// -// OGTypeKind.h -// -// -// Created by Kyle on 2024/1/7. -// - -#ifndef OGTypeGetKind_h -#define OGTypeGetKind_h - -#include "OGBase.h" - -typedef OG_ENUM(uint32_t, OGTypeKind) { - OGTypeKindNone, - OGTypeKindClass, - OGTypeKindStruct, - OGTypeKindEnum, - OGTypeKindOptional, - OGTypeKindTuple, - OGTypeKindFunction, - OGTypeKindExistential, - OGTypeKindMetatype, -}; - -#endif /* OGTypeKind_h */ diff --git a/Sources/OpenGraph_SPI/Runtime/metadata.cpp b/Sources/OpenGraph_SPI/Runtime/metadata.cpp index 833834e2..0c00d54c 100644 --- a/Sources/OpenGraph_SPI/Runtime/metadata.cpp +++ b/Sources/OpenGraph_SPI/Runtime/metadata.cpp @@ -1,8 +1,18 @@ // // metadata.cpp -// -// -// Created by Kyle on 2024/4/4. +// OpenGraph_SPI // +// Audited for iOS 18.0 +// Status: WIP #include "metadata.hpp" + +#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED + +using OG::swift::metadata; + +void metadata::append_description(CFMutableStringRef description) const OG_NOEXCEPT { + // TODO +} + +#endif /* OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED */ diff --git a/Sources/OpenGraph_SPI/Runtime/metadata.hpp b/Sources/OpenGraph_SPI/Runtime/metadata.hpp index 03637f85..d25bf1b3 100644 --- a/Sources/OpenGraph_SPI/Runtime/metadata.hpp +++ b/Sources/OpenGraph_SPI/Runtime/metadata.hpp @@ -1,19 +1,30 @@ // // metadata.hpp -// -// -// Created by Kyle on 2024/4/4. +// OpenGraph_SPI // +// Audited for iOS 18.0 +// Status: WIP #ifndef metadata_hpp #define metadata_hpp #include "OGBase.h" -#include "OGSwiftMetadata.h" + +#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED +#include +#endif namespace OG { namespace swift { -using metadata = OGSwiftMetadata; // FIXME: swift::Metadata -} -} +#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED +class metadata: public ::swift::Metadata { +public: + void append_description(CFMutableStringRef description) const OG_NOEXCEPT; +}; /* OG::swift::metadata */ +#else +class metadata {}; +#endif /* OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED */ +} /* OG::swift */ +} /* OG */ + #endif /* metadata_hpp */ diff --git a/Sources/OpenGraph_SPI/include/OGTypeDescription.h b/Sources/OpenGraph_SPI/include/OGTypeDescription.h deleted file mode 120000 index 1a830f51..00000000 --- a/Sources/OpenGraph_SPI/include/OGTypeDescription.h +++ /dev/null @@ -1 +0,0 @@ -../Runtime/OGTypeDescription.h \ No newline at end of file diff --git a/Sources/OpenGraph_SPI/include/OGTypeKind.h b/Sources/OpenGraph_SPI/include/OGTypeKind.h deleted file mode 120000 index b8643de6..00000000 --- a/Sources/OpenGraph_SPI/include/OGTypeKind.h +++ /dev/null @@ -1 +0,0 @@ -../Runtime/OGTypeKind.h \ No newline at end of file diff --git a/Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h b/Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h index 7a8341c1..46de9647 100644 --- a/Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h +++ b/Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h @@ -28,9 +28,7 @@ #include "OGTypeApplyEnumData.h" #include "OGTypeApplyField.h" #include "OGTypeApplyOptions.h" -#include "OGTypeDescription.h" #include "OGTypeID.h" -#include "OGTypeKind.h" #include "OGUniqueID.h" #include "OGValue.h" #include "OGValueOptions.h" From e2c7ebcfead1d0afe1074a3550bff09ac2f3df62 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 29 Dec 2024 20:10:07 +0800 Subject: [PATCH 3/8] Update metadata.cpp --- Package.swift | 52 +++++++++--------- Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp | 63 ++++++++++++++++------ Sources/OpenGraph_SPI/Runtime/OGTypeID.h | 24 +++++++-- Sources/OpenGraph_SPI/Runtime/metadata.cpp | 15 ++++++ Sources/OpenGraph_SPI/Runtime/metadata.hpp | 3 ++ 5 files changed, 110 insertions(+), 47 deletions(-) diff --git a/Package.swift b/Package.swift index e1abf343..701d7f76 100644 --- a/Package.swift +++ b/Package.swift @@ -22,14 +22,22 @@ func envEnable(_ key: String, default defaultValue: Bool = false) -> Bool { let isXcodeEnv = Context.environment["__CFBundleIdentifier"] == "com.apple.dt.Xcode" let development = envEnable("OPENGRAPH_DEVELOPMENT", default: false) -let releaseVersion = Context.environment["OPENGRAPH_TARGET_RELEASE"].flatMap { Int($0) } ?? 2024 - let swiftBinPath = Context.environment["_"] ?? "/usr/bin/swift" let swiftBinURL = URL(fileURLWithPath: swiftBinPath) let SDKPath = swiftBinURL.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().path let includePath = SDKPath.appending("/usr/lib/swift") -// MARK: - C/CXX Settings +var sharedCSettings: [CSetting] = [ + .unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)), + .define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)), +] + +var sharedSwiftSettings: [SwiftSetting] = [ + .enableUpcomingFeature("InternalImportsByDefault"), + .swiftLanguageMode(.v5), +] + +// MARK: [env] OPENGRAPH_SWIFT_TOOLCHAIN_PATH // Modified from: https://github.com/swiftlang/swift/blob/main/SwiftCompilerSources/Package.swift // @@ -44,15 +52,7 @@ let includePath = SDKPath.appending("/usr/lib/swift") // // where <$OPENGRAPH_SWIFT_TOOLCHAIN_PATH> is the parent directory of the swift repository. -let swiftToolchainVersion = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_VERSION"] ?? (development ? "6.0.2" : "") let swiftToolchainPath = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_PATH"] ?? (development ? "/Volumes/BuildMachine/swift-project" : "") -let swiftToolchainSupported = envEnable("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED", default: !swiftToolchainVersion.isEmpty) - -var sharedCSettings: [CSetting] = [ - .unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)), - .define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)), -] - if !swiftToolchainPath.isEmpty { sharedCSettings.append( .unsafeFlags( @@ -74,38 +74,36 @@ if !swiftToolchainPath.isEmpty { ) } +// MARK: [env] OPENGRAPH_SWIFT_TOOLCHAIN_VERSION + +let swiftToolchainVersion = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_VERSION"] ?? (development ? "6.0.2" : "") if !swiftToolchainVersion.isEmpty { sharedCSettings.append( .define("OPENGRAPH_SWIFT_TOOLCHAIN_VERSION", to: swiftToolchainVersion) ) } -if swiftToolchainSupported { - sharedCSettings.append( - .define("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED") - ) -} - -// MARK: - Swift Settings - -var sharedSwiftSettings: [SwiftSetting] = [ - .enableUpcomingFeature("InternalImportsByDefault"), - .define("OPENGRAPH_RELEASE_\(releaseVersion)"), - .swiftLanguageMode(.v5), -] +// MARK: - [env] OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED +let swiftToolchainSupported = envEnable("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED", default: !swiftToolchainVersion.isEmpty) if swiftToolchainSupported { - sharedSwiftSettings.append( - .define("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED") - ) + sharedCSettings.append(.define("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED")) + sharedSwiftSettings.append(.define("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED")) } +// MARK: - [env] OPENGRAPH_TARGET_RELEASE + +let releaseVersion = Context.environment["OPENGRAPH_TARGET_RELEASE"].flatMap { Int($0) } ?? 2024 +sharedCSettings.append(.define("OPENGRAPH_RELEASE", to: "\(releaseVersion)")) +sharedSwiftSettings.append(.define("OPENGRAPH_RELEASE_\(releaseVersion)")) if releaseVersion >= 2021 { for year in 2021 ... releaseVersion { sharedSwiftSettings.append(.define("OPENGRAPH_SUPPORT_\(year)_API")) } } +// MARK: - [env] OPENGRAPH_WERROR + let warningsAsErrorsCondition = envEnable("OPENGRAPH_WERROR", default: isXcodeEnv && development) if warningsAsErrorsCondition { sharedSwiftSettings.append(.unsafeFlags(["-warnings-as-errors"])) diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp b/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp index 1ac6cc95..906f7e79 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp +++ b/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp @@ -1,9 +1,9 @@ // // OGTypeID.cpp +// OpenGraph_SPI // -// -// Created by Kyle on 2024/1/7. -// +// Audited for iOS 18.0 +// Status: WIP #include "OGTypeID.h" #include "metadata.hpp" @@ -14,23 +14,23 @@ OGTypeKind OGTypeGetKind(OGTypeID typeID) { #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED - const swift::Metadata *type = reinterpret_cast(typeID); - switch (type->getKind()) { - case swift::MetadataKind::Class: + OG::swift::metadata const *metadata = reinterpret_cast(typeID); + switch (metadata->getKind()) { + case swift::MetadataKind::Class: // 0x0 return OGTypeKindClass; - case swift::MetadataKind::Struct: + case swift::MetadataKind::Struct: // 0x200 return OGTypeKindStruct; - case swift::MetadataKind::Enum: + case swift::MetadataKind::Enum: // 0x201 return OGTypeKindEnum; - case swift::MetadataKind::Optional: + case swift::MetadataKind::Optional: // 0x202 return OGTypeKindOptional; - case swift::MetadataKind::Tuple: + case swift::MetadataKind::Tuple: // 0x301 return OGTypeKindTuple; - case swift::MetadataKind::Function: + case swift::MetadataKind::Function: // 0x302 return OGTypeKindFunction; - case swift::MetadataKind::Existential: + case swift::MetadataKind::Existential: // 0x303 return OGTypeKindExistential; - case swift::MetadataKind::Metatype: + case swift::MetadataKind::Metatype: // 0x304 return OGTypeKindMetatype; default: return OGTypeKindNone; @@ -40,15 +40,46 @@ OGTypeKind OGTypeGetKind(OGTypeID typeID) { #endif } -CFStringRef OGTypeDescription(OGTypeID id) { +#if OPENGRAPH_RELEASE >= 2024 + +void const* OGTypeGetSignature(OGTypeID typeID) { + #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED + OG::swift::metadata const *metadata = reinterpret_cast(typeID); + // TODO + return nullptr; + #else + return nullptr; + #endif +} +void const* OGTypeGetDescriptor(OGTypeID typeID) { + #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED + OG::swift::metadata const *metadata = reinterpret_cast(typeID); + return metadata->descriptor(); + #else + return nullptr; + #endif +} + +#endif /* OPENGRAPH_RELEASE */ + +CFStringRef OGTypeDescription(OGTypeID typeID) { CFMutableStringRef ref = CFStringCreateMutable(CFAllocatorGetDefault(), 0); #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED - OG::swift::metadata const *metadata = reinterpret_cast(id); + OG::swift::metadata const *metadata = reinterpret_cast(typeID); metadata->append_description(ref); #endif return ref; } -const void * OGTypeNominalDescriptor(OGTypeID typeID) { +void const* OGTypeNominalDescriptor(OGTypeID typeID) { + #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED + OG::swift::metadata const *metadata = reinterpret_cast(typeID); + return metadata->nominal_descriptor(); + #else return nullptr; + #endif +} + +CFStringRef OGTypeNominalDescriptorName(OGTypeID typeID) { + // TODO } diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h index 0da1f709..14c49554 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h +++ b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h @@ -27,19 +27,35 @@ typedef OG_ENUM(uint32_t, OGTypeKind) { OGTypeKindFunction, OGTypeKindExistential, OGTypeKindMetatype, -}; +} OG_SWIFT_NAME(Metadata.Kind); OG_EXPORT OG_REFINED_FOR_SWIFT -OGTypeKind OGTypeGetKind(OGTypeID typeID) OG_SWIFT_NAME(getter:OGTypeID.kind(self:)); +OGTypeKind OGTypeGetKind(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.kind(self:)); + +#if OPENGRAPH_RELEASE >= 2024 + +OG_EXPORT +OG_REFINED_FOR_SWIFT +void const* OGTypeGetSignature(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.signature(self:)); + +OG_EXPORT +OG_REFINED_FOR_SWIFT +void const* OGTypeGetDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.descriptor(self:)); + +#endif /* OPENGRAPH_RELEASE */ + +OG_EXPORT +OG_REFINED_FOR_SWIFT +CFStringRef OGTypeDescription(OGTypeID typeID); OG_EXPORT OG_REFINED_FOR_SWIFT -CFStringRef OGTypeDescription(OGTypeID type); +void const* OGTypeNominalDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptor(self:)); OG_EXPORT OG_REFINED_FOR_SWIFT -const void * OGTypeNominalDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:OGTypeID.nominalDescriptor(self:)); +CFStringRef OGTypeNominalDescriptorName(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptorName(self:)); OG_EXTERN_C_END diff --git a/Sources/OpenGraph_SPI/Runtime/metadata.cpp b/Sources/OpenGraph_SPI/Runtime/metadata.cpp index 0c00d54c..f6854713 100644 --- a/Sources/OpenGraph_SPI/Runtime/metadata.cpp +++ b/Sources/OpenGraph_SPI/Runtime/metadata.cpp @@ -11,6 +11,21 @@ using OG::swift::metadata; +void const* metadata::descriptor() const OG_NOEXCEPT { + // TODO + return nullptr; +} + +void const* metadata::nominal_descriptor() const OG_NOEXCEPT { + void const* descriptor = this->descriptor(); + if (descriptor == nullptr) { + return nullptr; + } + // TODO + return nullptr; +} + + void metadata::append_description(CFMutableStringRef description) const OG_NOEXCEPT { // TODO } diff --git a/Sources/OpenGraph_SPI/Runtime/metadata.hpp b/Sources/OpenGraph_SPI/Runtime/metadata.hpp index d25bf1b3..fafd2336 100644 --- a/Sources/OpenGraph_SPI/Runtime/metadata.hpp +++ b/Sources/OpenGraph_SPI/Runtime/metadata.hpp @@ -19,6 +19,9 @@ namespace swift { #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED class metadata: public ::swift::Metadata { public: + void const* descriptor() const OG_NOEXCEPT; + void const* nominal_descriptor() const OG_NOEXCEPT; + void append_description(CFMutableStringRef description) const OG_NOEXCEPT; }; /* OG::swift::metadata */ #else From 1a994380e51dc9fb8817d6732262590b969404a0 Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 30 Dec 2024 00:29:59 +0800 Subject: [PATCH 4/8] Add OGTypeNominalDescriptorName --- Package.swift | 5 + Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp | 24 +++-- Sources/OpenGraph_SPI/Runtime/OGTypeID.h | 8 +- Sources/OpenGraph_SPI/Runtime/metadata.cpp | 15 --- Sources/OpenGraph_SPI/Runtime/metadata.hpp | 58 +++++++++++- .../Runtime/MetadataTests.swift | 92 +++++++++++++------ 6 files changed, 146 insertions(+), 56 deletions(-) diff --git a/Package.swift b/Package.swift index 701d7f76..45a1a255 100644 --- a/Package.swift +++ b/Package.swift @@ -113,6 +113,7 @@ if warningsAsErrorsCondition { let openGraphShimsTarget = Target.target( name: "OpenGraphShims", + cSettings: sharedCSettings, swiftSettings: sharedSwiftSettings ) @@ -122,6 +123,7 @@ let openGraphShimsTestTarget = Target.testTarget( "OpenGraphShims", ], exclude: ["README.md"], + cSettings: sharedCSettings, swiftSettings: sharedSwiftSettings ) @@ -131,6 +133,7 @@ let openGraphTestTarget = Target.testTarget( "OpenGraph", ], exclude: ["README.md"], + cSettings: sharedCSettings, swiftSettings: sharedSwiftSettings ) let openGraphCompatibilityTestTarget = Target.testTarget( @@ -139,6 +142,7 @@ let openGraphCompatibilityTestTarget = Target.testTarget( .product(name: "RealModule", package: "swift-numerics"), ], exclude: ["README.md"], + cSettings: sharedCSettings, swiftSettings: sharedSwiftSettings ) @@ -165,6 +169,7 @@ let package = Package( .target( name: "OpenGraph", dependencies: ["OpenGraph_SPI"], + cSettings: sharedCSettings, swiftSettings: sharedSwiftSettings ), openGraphShimsTarget, diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp b/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp index 906f7e79..0d7e7481 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp +++ b/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp @@ -14,7 +14,7 @@ OGTypeKind OGTypeGetKind(OGTypeID typeID) { #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED - OG::swift::metadata const *metadata = reinterpret_cast(typeID); + auto metadata = reinterpret_cast(typeID); switch (metadata->getKind()) { case swift::MetadataKind::Class: // 0x0 return OGTypeKindClass; @@ -44,7 +44,7 @@ OGTypeKind OGTypeGetKind(OGTypeID typeID) { void const* OGTypeGetSignature(OGTypeID typeID) { #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED - OG::swift::metadata const *metadata = reinterpret_cast(typeID); + auto metadata = reinterpret_cast(typeID); // TODO return nullptr; #else @@ -53,7 +53,7 @@ void const* OGTypeGetSignature(OGTypeID typeID) { } void const* OGTypeGetDescriptor(OGTypeID typeID) { #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED - OG::swift::metadata const *metadata = reinterpret_cast(typeID); + auto metadata = reinterpret_cast(typeID); return metadata->descriptor(); #else return nullptr; @@ -65,7 +65,7 @@ void const* OGTypeGetDescriptor(OGTypeID typeID) { CFStringRef OGTypeDescription(OGTypeID typeID) { CFMutableStringRef ref = CFStringCreateMutable(CFAllocatorGetDefault(), 0); #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED - OG::swift::metadata const *metadata = reinterpret_cast(typeID); + auto metadata = reinterpret_cast(typeID); metadata->append_description(ref); #endif return ref; @@ -73,13 +73,23 @@ CFStringRef OGTypeDescription(OGTypeID typeID) { void const* OGTypeNominalDescriptor(OGTypeID typeID) { #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED - OG::swift::metadata const *metadata = reinterpret_cast(typeID); + auto metadata = reinterpret_cast(typeID); + return metadata->nominal_descriptor(); #else return nullptr; #endif } -CFStringRef OGTypeNominalDescriptorName(OGTypeID typeID) { - // TODO +char const* OGTypeNominalDescriptorName(OGTypeID typeID) { + #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED + auto metadata = reinterpret_cast(typeID); + auto nominal_descriptor = metadata->nominal_descriptor(); + if (nominal_descriptor == nullptr) { + return nullptr; + } + return nominal_descriptor->Name.get(); + #else + return nullptr; + #endif } diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h index 14c49554..6fb0b361 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h +++ b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h @@ -37,11 +37,11 @@ OGTypeKind OGTypeGetKind(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.kind(sel OG_EXPORT OG_REFINED_FOR_SWIFT -void const* OGTypeGetSignature(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.signature(self:)); +void const* _Nullable OGTypeGetSignature(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.signature(self:)); OG_EXPORT OG_REFINED_FOR_SWIFT -void const* OGTypeGetDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.descriptor(self:)); +void const* _Nullable OGTypeGetDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.descriptor(self:)); #endif /* OPENGRAPH_RELEASE */ @@ -51,11 +51,11 @@ CFStringRef OGTypeDescription(OGTypeID typeID); OG_EXPORT OG_REFINED_FOR_SWIFT -void const* OGTypeNominalDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptor(self:)); +void const* _Nullable OGTypeNominalDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptor(self:)); OG_EXPORT OG_REFINED_FOR_SWIFT -CFStringRef OGTypeNominalDescriptorName(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptorName(self:)); +char const* _Nullable OGTypeNominalDescriptorName(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptorName(self:)); OG_EXTERN_C_END diff --git a/Sources/OpenGraph_SPI/Runtime/metadata.cpp b/Sources/OpenGraph_SPI/Runtime/metadata.cpp index f6854713..0c00d54c 100644 --- a/Sources/OpenGraph_SPI/Runtime/metadata.cpp +++ b/Sources/OpenGraph_SPI/Runtime/metadata.cpp @@ -11,21 +11,6 @@ using OG::swift::metadata; -void const* metadata::descriptor() const OG_NOEXCEPT { - // TODO - return nullptr; -} - -void const* metadata::nominal_descriptor() const OG_NOEXCEPT { - void const* descriptor = this->descriptor(); - if (descriptor == nullptr) { - return nullptr; - } - // TODO - return nullptr; -} - - void metadata::append_description(CFMutableStringRef description) const OG_NOEXCEPT { // TODO } diff --git a/Sources/OpenGraph_SPI/Runtime/metadata.hpp b/Sources/OpenGraph_SPI/Runtime/metadata.hpp index fafd2336..4bdc860f 100644 --- a/Sources/OpenGraph_SPI/Runtime/metadata.hpp +++ b/Sources/OpenGraph_SPI/Runtime/metadata.hpp @@ -12,15 +12,65 @@ #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED #include -#endif +#include +using namespace swift; +#endif /* OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED */ namespace OG { namespace swift { #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED -class metadata: public ::swift::Metadata { +class metadata: public Metadata { public: - void const* descriptor() const OG_NOEXCEPT; - void const* nominal_descriptor() const OG_NOEXCEPT; + OG_INLINE OG_CONSTEXPR + Metadata const* getType() const OG_NOEXCEPT { + return this; + } + + OG_INLINE OG_CONSTEXPR + TypeNamePair const name(bool qualified) const OG_NOEXCEPT { + return swift_getTypeName(getType(), qualified); + } + + OG_INLINE OG_CONSTEXPR + MetadataKind const getKind() const OG_NOEXCEPT { + return getType()->getKind(); + } + + OG_INLINE OG_CONSTEXPR + TypeContextDescriptor const* descriptor() const OG_NOEXCEPT { + switch (getKind()) { + case MetadataKind::Class: { + const auto cls = static_cast(getType()); + // We may build this with a newer OS SDK but run on old OS. + // So instead of using `isTypeMetadata` / `(Data & SWIFT_CLASS_IS_SWIFT_MASK)`, + // we manully use 3 here to check isTypeMetadata + if ((cls->Data & 3) == 0) return nullptr; + return cls->getDescription(); + } + case MetadataKind::Struct: + case MetadataKind::Enum: + case MetadataKind::Optional: { + return static_cast *>(getType())->Description; + } + default: + return nullptr; + } + } + + OG_INLINE OG_CONSTEXPR + TypeContextDescriptor const* nominal_descriptor() const OG_NOEXCEPT { + auto descriptor = this->descriptor(); + if (descriptor == nullptr) { + return nullptr; + } + switch(descriptor->getKind()) { + case ContextDescriptorKind::Struct: + case ContextDescriptorKind::Enum: + return descriptor; + default: + return nullptr; + } + } void append_description(CFMutableStringRef description) const OG_NOEXCEPT; }; /* OG::swift::metadata */ diff --git a/Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift b/Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift index 5b58677e..525b8b83 100644 --- a/Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift +++ b/Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift @@ -5,13 +5,27 @@ import Testing struct MetadataTests { + class T1 { + var a = 0 + var b: Double = 0 + } + + struct T2 { + var a: Int + var b: Double + } + + enum T3 { + case a, b + } + + protocol P { + var a: Int { get } + var b: Double { get } + } + @Test(.enabled(if: swiftToolchainSupported)) - func kindCases() throws { - class T1 {} - struct T2 {} - enum T3 {} - protocol P {} - + func kind() throws { #expect(Metadata(T1.self).kind == .class) #expect(Metadata(T2.self).kind == .struct) #expect(Metadata(T3.self).kind == .enum) @@ -34,29 +48,25 @@ struct MetadataTests { #expect(Metadata(type(of: Int.self)).kind == .metatype) } - @Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented")) - func descriptor() throws { - let n1 = try #require(Metadata(Int.self).nominalDescriptor) - let n2 = try #require(Metadata(String.self).nominalDescriptor) - let n3 = try #require(Metadata(Int.self).nominalDescriptor) - - #expect(n1 != n2) - #expect(n1 == n3) - } - - class T1 { - var a = 0 - var b: Double = 0 - } + #if OPENGRAPH_RELEASE_2024 + @Test(.enabled(if: swiftToolchainSupported)) + func descriptor() { + let t1 = Metadata(T1.self).descriptor + let t2 = Metadata(T2.self).descriptor + let t3 = Metadata(T3.self).descriptor + let p = Metadata(P.self).descriptor + let optionalP = Metadata(P?.self).descriptor - struct T2 { - var a: Int - var b: Double - } + #expect(t1 != nil) + #expect(t2 != nil) + #expect(t3 != nil) - enum T3 { - case a, b + #expect(p == nil) + #expect(optionalP != nil) + + #expect(t1 == Metadata(T1.self).descriptor) } + #endif @Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented")) func description() { @@ -65,6 +75,36 @@ struct MetadataTests { #expect(Metadata(T3.self).description == "MetadataTests.T3") } + @Test(.enabled(if: swiftToolchainSupported)) + func nominalDescriptor() { + let t1 = Metadata(T1.self).nominalDescriptor + let t2 = Metadata(T2.self).nominalDescriptor + let t3 = Metadata(T3.self).nominalDescriptor + let p = Metadata(P.self).nominalDescriptor + let optionalP = Metadata(P?.self).nominalDescriptor + + #expect(t1 == nil) + #expect(t2 != nil) + #expect(t3 != nil) + #expect(p == nil) + #expect(optionalP != nil) + } + + @Test(.enabled(if: swiftToolchainSupported)) + func nominalDescriptorName() throws { + let t1 = Metadata(T1.self).nominalDescriptorName + let t2 = Metadata(T2.self).nominalDescriptorName + let t3 = Metadata(T3.self).nominalDescriptorName + let p = Metadata(P.self).nominalDescriptorName + let optionalP = Metadata(P?.self).nominalDescriptorName + + #expect(t1 == nil) + try #expect(String(cString: #require(t2)) == "T2") + try #expect(String(cString: #require(t3)) == "T3") + #expect(p == nil) + try #expect(String(cString: #require(optionalP)) == "Optional") + } + @Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented")) func forEachField() throws { for options in [OGTypeApplyOptions._1] { From fc210e2af7c34283c333ba5b0122c3f8ef2ad652 Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 30 Dec 2024 00:51:09 +0800 Subject: [PATCH 5/8] Merge OGSwiftMetadata into OGTypeID file --- Sources/OpenGraph_SPI/Runtime/OGSwiftMetadata.h | 12 ------------ Sources/OpenGraph_SPI/Runtime/OGTypeID.h | 3 +++ Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h | 1 - 3 files changed, 3 insertions(+), 13 deletions(-) delete mode 100644 Sources/OpenGraph_SPI/Runtime/OGSwiftMetadata.h diff --git a/Sources/OpenGraph_SPI/Runtime/OGSwiftMetadata.h b/Sources/OpenGraph_SPI/Runtime/OGSwiftMetadata.h deleted file mode 100644 index 1b05fd57..00000000 --- a/Sources/OpenGraph_SPI/Runtime/OGSwiftMetadata.h +++ /dev/null @@ -1,12 +0,0 @@ -// -// OGSwiftMetadata.h -// OpenGraph_SPI -// - -#ifndef OGSwiftMetadata_h -#define OGSwiftMetadata_h - -struct OGSwiftMetadata {}; -typedef struct OGSwiftMetadata OGSwiftMetadata; - -#endif /* OGSwiftMetadata_h */ diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h index 6fb0b361..94189ee5 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h +++ b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h @@ -13,6 +13,9 @@ OG_ASSUME_NONNULL_BEGIN +struct OGSwiftMetadata {}; +typedef struct OGSwiftMetadata OGSwiftMetadata; + OG_EXTERN_C_BEGIN typedef const OGSwiftMetadata *OGTypeID OG_SWIFT_STRUCT OG_SWIFT_NAME(Metadata); diff --git a/Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h b/Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h index 46de9647..cfc452b1 100644 --- a/Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h +++ b/Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h @@ -23,7 +23,6 @@ #include "OGInputOptions.h" #include "OGSearchOptions.h" #include "OGSubgraph.h" -#include "OGSwiftMetadata.h" #include "OGTupleType.h" #include "OGTypeApplyEnumData.h" #include "OGTypeApplyField.h" From 6bba32ddc638f458d30c98d046f6a9f406657d89 Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 30 Dec 2024 01:05:48 +0800 Subject: [PATCH 6/8] Fix Linux build issue --- Package.resolved | 2 +- Package.swift | 5 +++-- Sources/OpenGraph_SPI/Runtime/OGTupleType.h | 1 - Sources/OpenGraph_SPI/Runtime/OGTypeID.h | 1 - Sources/OpenGraph_SPI/include/OGSwiftMetadata.h | 1 - .../Runtime/MetadataTests.swift | 2 +- .../Runtime/TypeKindTests.swift | 13 ------------- 7 files changed, 5 insertions(+), 20 deletions(-) delete mode 120000 Sources/OpenGraph_SPI/include/OGSwiftMetadata.h delete mode 100644 Tests/OpenGraphCompatibilityTests/Runtime/TypeKindTests.swift diff --git a/Package.resolved b/Package.resolved index fa856e62..96fe8068 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "63fa61469a826aeb30fad884b1923ca3d94dc431595ef64806429df9d5711e10", + "originHash" : "e34af969d8f4a303070ee7bea78ad5d898a415892447c83e19cdc16c141e6722", "pins" : [ { "identity" : "darwinprivateframeworks", diff --git a/Package.swift b/Package.swift index 45a1a255..e2e8ee24 100644 --- a/Package.swift +++ b/Package.swift @@ -29,7 +29,6 @@ let includePath = SDKPath.appending("/usr/lib/swift") var sharedCSettings: [CSetting] = [ .unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)), - .define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)), ] var sharedSwiftSettings: [SwiftSetting] = [ @@ -164,7 +163,9 @@ let package = Package( // The SwiftPM support for such usage is still in progress. .target( name: "OpenGraph_SPI", - cSettings: sharedCSettings + cSettings: sharedCSettings + [ + .define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)), + ] ), .target( name: "OpenGraph", diff --git a/Sources/OpenGraph_SPI/Runtime/OGTupleType.h b/Sources/OpenGraph_SPI/Runtime/OGTupleType.h index d348d9c2..3be7f040 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTupleType.h +++ b/Sources/OpenGraph_SPI/Runtime/OGTupleType.h @@ -9,7 +9,6 @@ #define OGTupleType_h #include "OGBase.h" -#include "OGSwiftMetadata.h" #include "OGTypeID.h" typedef const OGSwiftMetadata *OGTupleType OG_SWIFT_STRUCT; diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h index 94189ee5..5f8dd893 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h +++ b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h @@ -9,7 +9,6 @@ #define OGTypeID_h #include "OGBase.h" -#include "OGSwiftMetadata.h" OG_ASSUME_NONNULL_BEGIN diff --git a/Sources/OpenGraph_SPI/include/OGSwiftMetadata.h b/Sources/OpenGraph_SPI/include/OGSwiftMetadata.h deleted file mode 120000 index f4b35133..00000000 --- a/Sources/OpenGraph_SPI/include/OGSwiftMetadata.h +++ /dev/null @@ -1 +0,0 @@ -../Runtime/OGSwiftMetadata.h \ No newline at end of file diff --git a/Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift b/Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift index 525b8b83..59eec2a7 100644 --- a/Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift +++ b/Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift @@ -48,7 +48,7 @@ struct MetadataTests { #expect(Metadata(type(of: Int.self)).kind == .metatype) } - #if OPENGRAPH_RELEASE_2024 + #if OPENGRAPH_SUPPORT_2024_API @Test(.enabled(if: swiftToolchainSupported)) func descriptor() { let t1 = Metadata(T1.self).descriptor diff --git a/Tests/OpenGraphCompatibilityTests/Runtime/TypeKindTests.swift b/Tests/OpenGraphCompatibilityTests/Runtime/TypeKindTests.swift deleted file mode 100644 index 5ee4e1b5..00000000 --- a/Tests/OpenGraphCompatibilityTests/Runtime/TypeKindTests.swift +++ /dev/null @@ -1,13 +0,0 @@ -// -// TypeKindTests.swift -// -// -// - -import Testing - - -@Suite(.disabled(if: !compatibilityTestEnabled, "Metadata.kind is not implemented")) -struct TypeKindTests { - -} From 918f5a0cd68529c4f44b10f1072990560eee153e Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 30 Dec 2024 02:09:35 +0800 Subject: [PATCH 7/8] Update DarwinPrivateFrameworks version --- Package.resolved | 2 +- Sources/OpenGraph_SPI/Runtime/OGTypeID.h | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Package.resolved b/Package.resolved index 96fe8068..58a8ff3e 100644 --- a/Package.resolved +++ b/Package.resolved @@ -7,7 +7,7 @@ "location" : "https://github.com/OpenSwiftUIProject/DarwinPrivateFrameworks.git", "state" : { "branch" : "main", - "revision" : "539681dd9bd257b311ae09f8e7266bb8fa710aca" + "revision" : "e7a96d8a5bc76c3e7e9713e084a1ffd5271e933b" } }, { diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h index 5f8dd893..f3f47c7d 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h +++ b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h @@ -12,10 +12,8 @@ OG_ASSUME_NONNULL_BEGIN -struct OGSwiftMetadata {}; -typedef struct OGSwiftMetadata OGSwiftMetadata; - -OG_EXTERN_C_BEGIN +typedef struct OGSwiftMetadata { +} OGSwiftMetadata; typedef const OGSwiftMetadata *OGTypeID OG_SWIFT_STRUCT OG_SWIFT_NAME(Metadata); @@ -31,6 +29,8 @@ typedef OG_ENUM(uint32_t, OGTypeKind) { OGTypeKindMetatype, } OG_SWIFT_NAME(Metadata.Kind); +OG_EXTERN_C_BEGIN + OG_EXPORT OG_REFINED_FOR_SWIFT OGTypeKind OGTypeGetKind(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.kind(self:)); From b8339335650cd9b1c5123a60859c26c5fbe91142 Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 30 Dec 2024 02:18:22 +0800 Subject: [PATCH 8/8] Fix OGTypeKind issue --- Sources/OpenGraphShims/GraphShims.swift | 2 +- Sources/OpenGraphShims/Metadata+Debug.swift | 1 + Tests/OpenGraphCompatibilityTests/GraphShims.swift | 1 - .../Runtime/CompareValuesTests.swift | 4 +--- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Sources/OpenGraphShims/GraphShims.swift b/Sources/OpenGraphShims/GraphShims.swift index 29e03f56..1b1416cb 100644 --- a/Sources/OpenGraphShims/GraphShims.swift +++ b/Sources/OpenGraphShims/GraphShims.swift @@ -1,5 +1,6 @@ // // GraphShims.swift +// OpenGraphShims #if OPENGRAPH_ATTRIBUTEGRAPH @_exported public import AttributeGraph @@ -21,7 +22,6 @@ public typealias OGSubgraph = AGSubgraph public typealias OGSwiftMetadata = AGSwiftMetadata public typealias OGTupleType = AGTupleType public typealias OGTypeApplyOptions = AGTypeApplyOptions -public typealias OGTypeKind = AGTypeKind public typealias OGUniqueID = AGUniqueID public typealias OGValue = AGValue public typealias OGValueOptions = AGValueOptions diff --git a/Sources/OpenGraphShims/Metadata+Debug.swift b/Sources/OpenGraphShims/Metadata+Debug.swift index 7afdb45f..76bf2ba8 100644 --- a/Sources/OpenGraphShims/Metadata+Debug.swift +++ b/Sources/OpenGraphShims/Metadata+Debug.swift @@ -1,5 +1,6 @@ // // Metadata+Debug.swift +// OpenGraphShims import Foundation diff --git a/Tests/OpenGraphCompatibilityTests/GraphShims.swift b/Tests/OpenGraphCompatibilityTests/GraphShims.swift index 5cdf2a76..fe6d87ec 100644 --- a/Tests/OpenGraphCompatibilityTests/GraphShims.swift +++ b/Tests/OpenGraphCompatibilityTests/GraphShims.swift @@ -22,7 +22,6 @@ public typealias OGSubgraph = AGSubgraph public typealias OGSwiftMetadata = AGSwiftMetadata public typealias OGTupleType = AGTupleType public typealias OGTypeApplyOptions = AGTypeApplyOptions -public typealias OGTypeKind = AGTypeKind public typealias OGUniqueID = AGUniqueID public typealias OGValue = AGValue public typealias OGValueOptions = AGValueOptions diff --git a/Tests/OpenGraphCompatibilityTests/Runtime/CompareValuesTests.swift b/Tests/OpenGraphCompatibilityTests/Runtime/CompareValuesTests.swift index 48d8777d..56e64c4b 100644 --- a/Tests/OpenGraphCompatibilityTests/Runtime/CompareValuesTests.swift +++ b/Tests/OpenGraphCompatibilityTests/Runtime/CompareValuesTests.swift @@ -1,8 +1,6 @@ // // CompareValuesTests.swift -// -// -// +// OpenGraphCompatibilityTests import Testing