New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-6004] Static libraries don't automatically load extensions #48561
Comments
Heh, we have |
You can use
|
Not all Swift extensions have corresponding categories, and the protocol conformance info probably isn't attached. I suppose we could start vending dummy categories… |
If I recall correctly there is some symbol attribute that the compiler can add which tells the linker to pull in that definition unconditionally. Let me look around. |
There definitely is, at some point I was actually trying to debug an issue with something like that, you can see when they're marked with that attribute using `nm -m`. Example:
|
I talked to the linker guys and there is not. Symbols can be marked no-dead-strip, but the lazy-loading semantics of static archives do not honor that. We may need to get linker enhancements if we need a fix for Swift there. The usual solution is to avoid static archives. Instead, use |
@swift-ci create |
Comment by Michael Bachand (JIRA) We are experiencing this issue at Airbnb with Buck-generated Xcode projects that use Swift static libraries. We plan to use |
Comment by Michael Bachand (JIRA) Here is a repro in a Buck-generated project: airbnb/BuckSample#38 |
@belkadan @gparker42 any idea on how we can move forward to a fix for this one? I'd be willing to take a stab at some implementation for this if possible |
soffes (JIRA User), Could you verify if the problem is fixed in the latest Xcode betas and if so move the JIRA to "Closed"? Thanks! |
Did something change in Swift that should have fixed this? |
In Swift 5.1, per the Xcode release notes, this is now fixed for us. We use |
There was a linker side change to address this. |
Ah interesting, I see this in the release notes "Static libraries are now always force-loaded in their entirety during linking, fixing runtime errors related to demangling failures. (47598583)" On the surface this sounds pretty scary! Since we're intentionally not force loading all of our libraries, only the ones that experience this problem. But I can try to see if it fixed our case as well |
The sample project attached to this issue actually still has the original problem with Xcode 11 beta 5 |
Setting GENERATE_MASTER_OBJECT_FILE on the Xcode target (as suggested in order to do the `ld -r` combination above) does fix this case. |
The -ObjC in the original project is on the static libraries rather than the client of the static libraries (the app). If I fix that, and tell ModelsUI not to link with Models, it works in Xcode 11 (beta 6, but should be earlier ones too). I do notice that there's not a dedicated build setting for -ObjC and people have to add it to "Other Linker Flags", so I'll go talk to some linker and Xcode folks about that. That'll be Apple-internal conversations only, though, sorry. |
@belkadan to clarify, this new swift linking behavior is enabled by the -ObjC flag? |
Ok after a bit more testing with this project it looks like -ObjC does enable this new behavior? I updated the sample project locally to make sure DogView wasn't a NSObject subclass since adding -ObjC actually fixed this example even in Xcode 10.3, but it looks like that does enable this new Swift loading behavior. |
Attachment: Download
Environment
Xcode 9.0 (9A235)
Additional Detail from JIRA
md5: a88822ddd333e2719ad59655283f788c
is duplicated by:
Issue Description:
I'm not sure if this is the intended behavior, but a static library doesn't load extensions unless something else in the same file is explicitly referenced. I realize adding `all_load` to the target it is linked into solves this. I'd love to avoid that if possible.
This is more of a question of is this the indeed behavior and if so is doing `all_load` the intended use?
I've attached a sample project reproducing this. Here's an explanation:
Create a static library A containing a protocol and some types that conform to it
Create another static library B that creates some another protocol and adds extensions to types in static library A
In another target that links both A & B, you can observe types from A that conform to a protocol from B not doing so at runtime.
If you explicitly reference something in a file that adds conformation, it will load the extension with the conformance and work as expected.
The attached sample project may make this more clear. Start in ViewController.swift and check out the comments.
The text was updated successfully, but these errors were encountered: