apple / swift-atomics

Low-level atomic operations for Swift
Apache License 2.0
1.06k stars 50 forks source link

Cannot use this package in a static library - `Missing required module '_AtomicsShims'` error. #54

Open serges147 opened 2 years ago

serges147 commented 2 years ago

In static library swift file, on attempt to import Atomics I'm getting Missing required module '_AtomicsShims' error.

This is structure of project in a nutshell:

  1. StaticLibA // has Atomics static SPM library as dependency (but NOT as "Link Binary with Libraries" phase)
    • foo.swift // needs atomic stuff, so does import Atomics which fails with the above error
  2. FrameworkB // links with StaticLibA
    • has some swift files which depend on StaticLibA types
  3. ExecutableC // links with StaticLibA, FrameworkB & Atomics
    • uses both the static lib and dynamic framework

The very same import Atomics works well and without errors when it's in a final executable swift files, so problem is specific for static library target only.

Note also that StaticLibA deliberately is NOT linked with Atomics - if I did so then step # 1 succeeds but then step # 3 fails with "ld: 465 duplicate symbols for architecture arm64". I believe it's b/c the very same symbols of Atomic lib comes to final executable from both StaticLibA and FrameworkB.

Could you please advice what could be wrong or missing? OR, is it just not supported yet configuration? Thanks!

lorentey commented 2 years ago

Thanks for the report! Unfortunately the reproduction steps don't contain enough information to debug this problem -- could you please provide a sample project that exhibits this issue?

serges147 commented 2 years ago

Yup, I'll prepare the sample soon. Thanks!

gaetanzanella commented 1 year ago

👋 Any news regarding this issue? I encountered the same problem when adding some tests to my iOS app.

To reproduce the issue:

  1. Create an iOS app with unit tests in Xcode
  2. Add the swift-atomics package to your main target
  3. Try to execute the tests on your M1 Pro Macbook Pro (Xcode 14.0.1 / macOS 12.6 in my case)

test.zip

vykut commented 1 year ago

Hello,

I have the same issue as @gaetanzanella mentioned above. The difference is that my project has the grpc-swift package dependency, which in turn uses swift-atomics.

Xcode 14.0.1 macOS 12.6.1 M1 Pro Macbook Pro

SimplyKyra commented 1 year ago

I'm also getting this error. Running a Multiplatform app and when I go to test it I'm getting this error "_atomicsshims" on every imported module in the test. The swift_atomics package is included on my target because of another package I'm using.

MarcusSmith commented 1 year ago

I'm also getting this error when trying to archive a Swift package (from the command line using xcodebuild archive) to convert my package into a framework.

The binary just looks to be absent after the first build step and it fails when trying to ld Swift Atomics. Sorry this is all pretty new to me - don't know what I don't know - happy to add any information that might be helpful here.

keisukeYamagishi commented 1 year ago

Hello,

I have the same issue too, but different Xcode version

Xcode 13.4.1 macOS 12.6.1 M1 Pro Macbook Pro

keisukeYamagishi commented 1 year ago

Hi🎉 I fix it㊗️ I imported it into the UITest bundle and was able to build it. Added Atomic to Link Binary With Libraries. Please see the attached photo.

Screenshot 2023-01-24 at 12 56 05
imthath-m commented 1 year ago

Thanks for sharing the fix @keisukeYamagishi . Unfortuately for me, the error kept repeating for different packages because of AsyncHttpClient (which depends on a handful of such packages including swift-atomics and was used in one of my dependencies). I was able to run the tests only after removing it.

imthath-m commented 1 year ago

in case if anyone faces this issue recursively like me for multiple packages, then you can try the approach by duckduckgo/ios.

create a static framework in your project and add all Swift Packages to it. Then use that framework in all your targets. For tests, add the framework in your Build Phases like mentioned above by @keisukeYamagishi

lorentey commented 1 year ago

Quick update: @gaetanzanella's sample project has been very helpful in reproducing this. (Thank you!)

Unfortunately it's a tooling issue, so the package won't be able to fix it on its own.

A proper fix is in the pipeline, but it will need time. @keisukeYamagishi's or @imthath-m's suggestions are currently the best workarounds that I am aware of.

lorentey commented 1 year ago

Getting rid of the C module with something along the lines of https://github.com/apple/swift-atomics/pull/74 would likely also get rid of these issues. However, this will require some Swift compiler & stdlib work, so it will take time.

joshgalvan commented 1 year ago

In my case, async-http-client was a dependency to a package I'm using, OpenAIKit, which is dependent upon swift-atomics (as @imthath-m mentioned) and adding that to my UI and unit test build phases as @gaetanzanella did allowed my tests to build.