hmlongco / Factory

A new approach to Container-Based Dependency Injection for Swift and SwiftUI.
MIT License
1.7k stars 107 forks source link

Building xcframework on Xcode 15.0 #173

Closed CEDENOGLOBANT closed 3 months ago

CEDENOGLOBANT commented 6 months ago

Hello I have an issue while building xcframework when BUILD_LIBRARY_FOR_DISTRIBUTION=YES when is set to NO, builds perfectly

Factory is added to the project by SPM

let' property 'type' may not be initialized directly; use "self.init(...)" or "self = ..." instead

    self.type = ObjectIdentifier(type)

'let' property 'key' may not be initialized directly; use "self.init(...)" or "self = ..." instead self.key = key

hmlongco commented 6 months ago

Can you go into the Factory code and make the FactoryKey initializer public and see if that helps matters?

CEDENOGLOBANT commented 6 months ago

Hello, that didn't work, after many intends , the only thing that work was remove or inline, usableFromInline, also was necessary to add this settings to the package

let settings: [SwiftSetting] settings = [.unsafeFlags(["-no-verify-emitted-module-interface"], .when(configuration: .release))]

How can I push a new branch?

CEDENOGLOBANT commented 6 months ago

I figure it out a way to keep the inline and has a successful build, just change it to a class and create a convenience init

public class FactoryKey: Hashable {

@usableFromInline let type: ObjectIdentifier
@usableFromInline let key: StaticString

@inlinable
@inline(__always)
public convenience init(type: Any.Type, key: StaticString = #function) {
    self.init(type: ObjectIdentifier(type),key: key)
}

public init(type: ObjectIdentifier, key: StaticString) {
    self.type = type
    self.key = key
}

@inlinable
@inline(__always)
public func hash(into hasher: inout Hasher) {
    hasher.combine(self.type)
    if key.hasPointerRepresentation {
        hasher.combine(bytes: UnsafeRawBufferPointer(start: key.utf8Start, count: key.utf8CodeUnitCount))
    } else {
        hasher.combine(key.unicodeScalar.value)
    }
}

@inlinable
public static func == (lhs: FactoryKey, rhs: FactoryKey) -> Bool {
    guard lhs.type == rhs.type && lhs.key.hasPointerRepresentation == rhs.key.hasPointerRepresentation else {
        return false
    }
    if lhs.key.hasPointerRepresentation {
        return lhs.key.utf8Start == rhs.key.utf8Start || strcmp(lhs.key.utf8Start, rhs.key.utf8Start) == 0
    } else {
        return lhs.key.unicodeScalar.value == rhs.key.unicodeScalar.value
    }
}

}

hmlongco commented 6 months ago

It may be that the inlines need to be removed. FactoryKey is created and recreated quite a bit and it most certainly doesn't want to become a class with allocation overhead.

CEDENOGLOBANT commented 6 months ago

Still work as struct without the convenience , as long we have a init is not inline

// // Key.swift // // // Created by Michael Long on 8/17/23. //

import Foundation

public struct FactoryKey: Hashable {

@usableFromInline let type: ObjectIdentifier
@usableFromInline let key: StaticString

@inlinable
@inline(__always)
public init(type: Any.Type, key: StaticString = #function) {
    self.init(type: ObjectIdentifier(type),key: key)
}

public init(type: ObjectIdentifier, key: StaticString) {
    self.type = type
    self.key = key
}

@inlinable
@inline(__always)
public func hash(into hasher: inout Hasher) {
    hasher.combine(self.type)
    if key.hasPointerRepresentation {
        hasher.combine(bytes: UnsafeRawBufferPointer(start: key.utf8Start, count: key.utf8CodeUnitCount))
    } else {
        hasher.combine(key.unicodeScalar.value)
    }
}

@inlinable
public static func == (lhs: Self, rhs: Self) -> Bool {
    guard lhs.type == rhs.type && lhs.key.hasPointerRepresentation == rhs.key.hasPointerRepresentation else {
        return false
    }
    if lhs.key.hasPointerRepresentation {
        return lhs.key.utf8Start == rhs.key.utf8Start || strcmp(lhs.key.utf8Start, rhs.key.utf8Start) == 0
    } else {
        return lhs.key.unicodeScalar.value == rhs.key.unicodeScalar.value
    }
}

}

hmlongco commented 6 months ago

Probably need to just do a plain initializer. Makes no sense to make it inlineable only to call a second initializer.

hmlongco commented 5 months ago

There's a new branch, "key", which has a change which may fix this. Can you pull it and check?

CEDENOGLOBANT commented 4 months ago

Still not working with key branch same error

hmlongco commented 4 months ago

Updated converting key to an internal type. Please try branch again.

aaskrv commented 4 months ago

Still not working with key branch same error

@hmlongco I am facing the same issue trying to create a .xcframework

Tried to do so using key branch you mentioned, but it didn't work out. However, @CEDENOGLOBANT 's solution in xcode_15_1_build_xcframework branch works fine

aaskrv commented 4 months ago

@CEDENOGLOBANT @hmlongco hi guys, any news regarding this issue?

hmlongco commented 4 months ago

Key branch changes makes key internal and not inlinable. If you pull the branch make sure you clean the pod cache.

hmlongco commented 3 months ago

Key no longer inlined and internal in 2.3.2