Skip to content

Commit

Permalink
Update Metadata implementation (#95)
Browse files Browse the repository at this point in the history
* Add OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED support

* Update OGTypeID

* Update metadata.cpp

* Add OGTypeNominalDescriptorName

* Merge OGSwiftMetadata into OGTypeID file

* Fix Linux build issue

* Update DarwinPrivateFrameworks version

* Fix OGTypeKind issue
  • Loading branch information
Kyle-Ye authored Dec 29, 2024
1 parent 6040b4d commit 86fb886
Show file tree
Hide file tree
Showing 22 changed files with 344 additions and 218 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"originHash" : "2553be1b4dad3da670fb2da41763983faffa9e83c086d8616c6d4205bcfc0e61",
"originHash" : "e34af969d8f4a303070ee7bea78ad5d898a415892447c83e19cdc16c141e6722",
"pins" : [
{
"identity" : "darwinprivateframeworks",
"kind" : "remoteSourceControl",
"location" : "https://github.com/OpenSwiftUIProject/DarwinPrivateFrameworks.git",
"state" : {
"branch" : "main",
"revision" : "539681dd9bd257b311ae09f8e7266bb8fa710aca"
"revision" : "e7a96d8a5bc76c3e7e9713e084a1ffd5271e933b"
}
},
{
Expand Down
73 changes: 45 additions & 28 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,36 @@ 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)),
]

// 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:
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
//
// cd $OPENGRAPH_SWIFT_TOOLCHAIN_PATH
// mkdir -p build/Default
// ln -s build/<Ninja-Build>/llvm-<os+arch> build/Default/llvm
// ln -s build/<Ninja-Build>/swift-<os+arch> build/Default/swift
// Create a couple of symlinks to an existing Ninja build:
//
// where <project-root> is the parent directory of the swift repository.
// ```shell
// cd $OPENGRAPH_SWIFT_TOOLCHAIN_PATH
// mkdir -p build/Default
// ln -s build/<Ninja-Build>/llvm-<os+arch> build/Default/llvm
// ln -s build/<Ninja-Build>/swift-<os+arch> build/Default/swift
// ```
//
// FIXME: We may want to consider generating Package.swift as a part of the
// build.

let swiftToolchainVersion = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_VERSION"] ?? ""
let swiftToolchainPath = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_PATH"] ?? ""

var sharedCSettings: [CSetting] = [
.unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)),
.define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)),
]
// where <$OPENGRAPH_SWIFT_TOOLCHAIN_PATH> is the parent directory of the swift repository.

let swiftToolchainPath = Context.environment["OPENGRAPH_SWIFT_TOOLCHAIN_PATH"] ?? (development ? "/Volumes/BuildMachine/swift-project" : "")
if !swiftToolchainPath.isEmpty {
sharedCSettings.append(
.unsafeFlags(
Expand All @@ -68,31 +67,42 @@ 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
]
)
)
}

// 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)
)
}

// MARK: - Swift Settings
// MARK: - [env] OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED

var sharedSwiftSettings: [SwiftSetting] = [
.enableUpcomingFeature("InternalImportsByDefault"),
.define("OPENGRAPH_RELEASE_\(releaseVersion)"),
.swiftLanguageMode(.v5),
]
let swiftToolchainSupported = envEnable("OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED", default: !swiftToolchainVersion.isEmpty)
if swiftToolchainSupported {
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"]))
Expand All @@ -102,6 +112,7 @@ if warningsAsErrorsCondition {

let openGraphShimsTarget = Target.target(
name: "OpenGraphShims",
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)

Expand All @@ -111,6 +122,7 @@ let openGraphShimsTestTarget = Target.testTarget(
"OpenGraphShims",
],
exclude: ["README.md"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)

Expand All @@ -120,6 +132,7 @@ let openGraphTestTarget = Target.testTarget(
"OpenGraph",
],
exclude: ["README.md"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)
let openGraphCompatibilityTestTarget = Target.testTarget(
Expand All @@ -128,6 +141,7 @@ let openGraphCompatibilityTestTarget = Target.testTarget(
.product(name: "RealModule", package: "swift-numerics"),
],
exclude: ["README.md"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)

Expand All @@ -149,11 +163,14 @@ 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",
dependencies: ["OpenGraph_SPI"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
),
openGraphShimsTarget,
Expand Down
8 changes: 7 additions & 1 deletion Sources/OpenGraphShims/GraphShims.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// GraphShims.swift
// OpenGraphShims

#if OPENGRAPH_ATTRIBUTEGRAPH
@_exported public import AttributeGraph
Expand All @@ -21,13 +22,18 @@ 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
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
1 change: 1 addition & 0 deletions Sources/OpenGraphShims/Metadata+Debug.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Metadata+Debug.swift
// OpenGraphShims

import Foundation

Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenGraph_SPI/Graph/OGSubgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<OG::swift::metadata const*>(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<OG::swift::metadata const*>(type), key, flags);
}
}

Expand Down
11 changes: 0 additions & 11 deletions Sources/OpenGraph_SPI/Runtime/OGSwiftMetadata.h

This file was deleted.

1 change: 0 additions & 1 deletion Sources/OpenGraph_SPI/Runtime/OGTupleType.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#define OGTupleType_h

#include "OGBase.h"
#include "OGSwiftMetadata.h"
#include "OGTypeID.h"

typedef const OGSwiftMetadata *OGTupleType OG_SWIFT_STRUCT;
Expand Down
17 changes: 0 additions & 17 deletions Sources/OpenGraph_SPI/Runtime/OGTypeDescription.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions Sources/OpenGraph_SPI/Runtime/OGTypeDescription.h

This file was deleted.

83 changes: 67 additions & 16 deletions Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
//
// OGTypeID.cpp
// OpenGraph_SPI
//
//
// Created by Kyle on 2024/1/7.
//
// Audited for iOS 18.0
// Status: WIP

#include "OGTypeID.h"
#include "metadata.hpp"

#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_VERSION
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
#include <swift/Runtime/Metadata.h>
#endif

OGTypeKind OGTypeGetKind(OGTypeID typeID) {
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_VERSION
const swift::Metadata *type = reinterpret_cast<const swift::Metadata *>(typeID);
switch (type->getKind()) {
case swift::MetadataKind::Class:
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
auto metadata = reinterpret_cast<OG::swift::metadata const*>(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;
Expand All @@ -39,6 +40,56 @@ OGTypeKind OGTypeGetKind(OGTypeID typeID) {
#endif
}

const void * OGTypeNominalDescriptor(OGTypeID typeID) {
#if OPENGRAPH_RELEASE >= 2024

void const* OGTypeGetSignature(OGTypeID typeID) {
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
// TODO
return nullptr;
#else
return nullptr;
#endif
}
void const* OGTypeGetDescriptor(OGTypeID typeID) {
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
auto metadata = reinterpret_cast<OG::swift::metadata const*>(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
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
metadata->append_description(ref);
#endif
return ref;
}

void const* OGTypeNominalDescriptor(OGTypeID typeID) {
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);

return metadata->nominal_descriptor();
#else
return nullptr;
#endif
}

char const* OGTypeNominalDescriptorName(OGTypeID typeID) {
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
auto nominal_descriptor = metadata->nominal_descriptor();
if (nominal_descriptor == nullptr) {
return nullptr;
}
return nominal_descriptor->Name.get();
#else
return nullptr;
#endif
}
Loading

0 comments on commit 86fb886

Please sign in to comment.