IOS-DFU-Library icon indicating copy to clipboard operation
IOS-DFU-Library copied to clipboard

NordicDFU dynamic linking?

Open mateusforgi opened this issue 2 years ago • 9 comments

Can we change the linking type to .dynamic? .library(name: "NordicDFU", type: .dynamic, targets: ["NordicDFU"])

I have a XCFramework, that depends on NordicDFU but it cannot link to NordicDFU at runtime because NordicDFU is not linked with dynamic type.

On apple documentation it says this: Leave this parameter so Swift Package Manager can choose between static or dynamic linking (recommended).

But on the other part it says this: A library’s product can be either statically or dynamically linked. It’s recommended that you don’t explicity declare the type of library, so Swift Package Manager can choose between static or dynamic linking based on the preference of the package’s consumer.

It is kind of confusing as it recommends the dynamic linking but It also says to let the type as nil. https://developer.apple.com/documentation/packagedescription/product/library(name:type:targets:)

Note: ZIPFoundation would need to have dynamic linking too.

mateusforgi avatar Feb 08 '23 22:02 mateusforgi

@philips77 can you take a look on that?

mateusforgi avatar Feb 08 '23 22:02 mateusforgi

So, if you change the library to .dynamic, does this work for you? It fixes the issue?

dinesharjani avatar Feb 23 '23 11:02 dinesharjani

Are you referring to this line: https://github.com/NordicSemiconductor/IOS-DFU-Library/blob/22952128c6499740f0d04b3dba9b51813cc19a4c/Package.swift#L17

But we don't specify that parameter, so SPM should be able to link it any way you want, right?

philips77 avatar Feb 23 '23 11:02 philips77

So, if you change the library to .dynamic, does this work for you? It fixes the issue?

Correct, but we also need to make ZIPFoundation dynamic. I already opened a issue there.

Are you referring to this line:

https://github.com/NordicSemiconductor/IOS-DFU-Library/blob/22952128c6499740f0d04b3dba9b51813cc19a4c/Package.swift#L17

But we don't specify that parameter, so SPM should be able to link it any way you want, right?

Yes, it is this line. We need to change to.

.library(name: "NordicDFU", type: .dynamic, targets: ["NordicDFU"]),

But it will not work unless ZIPFoundation is also dynamic.

Regarding SPM should be able to link it anyway. This is the real issue. It looks like it does not. According to this comment on swift forum there is no way of influence that beyond package manifest.

See also this comment about possible solutions. For me the only think that worked was making dynamic IOS-DFU-Library and ZIPFoundation.

mateusforgi avatar Feb 23 '23 13:02 mateusforgi

@mateusforgi as soon as ZIPFoundation makes their change, either make a PR here or let us know and we'll change it as well.

dinesharjani avatar Feb 23 '23 13:02 dinesharjani

@dinesharjani thanks, I will open a pr as soon as they make their change.

mateusforgi avatar Feb 23 '23 14:02 mateusforgi

No, no.. that change on our side would force the lib to be dynamically linked. Now it can be linked dynamically or staticly, depending on settings in your project's SPM config. We would only change if our lib had to be dynamically linked, but I guess that's not the case

philips77 avatar Feb 23 '23 19:02 philips77

I guess you're referring to https://stackoverflow.com/a/70812112 which says that xcode always prefers static linking, right? Unless we specify otherwise.

philips77 avatar Feb 23 '23 19:02 philips77

@philips77 correct. But as NordicDFU is a dependency from my SDK, this answer from stack overflow does not solve the issue because it assumes I am adding a package to an App. The final user would not add my SDK to their app, NordicDFU would be downloaded by swift package because it is a dependency from my SDK. So the solution of adding to "Frameworks, Libraries, and Embedded Content" does not work.

The real issue is that swift package manager does not allow to import a dependency and say how it will be linked, the only way to do that is by saying it on the package manifest.

Things that I did and worked.

  1. Forked dependencies and changed NordicDFU and ZIPFoundation to .dynamic. Or
  2. Made NordicDFU and ZIPFoundation a XCFramework too.

mateusforgi avatar Feb 24 '23 17:02 mateusforgi