socketio / socket.io-client-swift

Other
5.2k stars 840 forks source link

Fix SwiftUI Previews (support arm64 on simulators) #1430

Closed dan-ryan closed 1 year ago

dan-ryan commented 1 year ago

This library is breaking SwiftUI Previews, and it looks like I'm not the only one with the issue.

Error: x.swift:10:8: Could not find module 'SocketIO' for target 'arm64-apple-ios-simulator'; found: x86_64-apple-ios-simulator, at: /Users/x/Library/Developer/Xcode/DerivedData/CricHQ-dmjqadgxdcinrrcgwugqdedztquy/Build/Products/Debug-iphonesimulator/SocketIO.swiftmodule

yi-ge commented 1 year ago

Trying to upgrade Xcode Version 14.1 beta 3 (14B5033e). I have addressed this issue in this version.

dan-ryan commented 1 year ago

@yi-ge Are you saying this problem is an Xcode issue and has been fixed in the latest beta?

yi-ge commented 1 year ago

@dan-ryan yes.

dan-ryan commented 1 year ago

I'm using Xcode 14.1, still have the same issue.

yi-ge commented 1 year ago

@dan-ryan beta3?

dan-ryan commented 1 year ago

Stable. It's now on the App Store.

yi-ge commented 1 year ago

@dan-ryan try Version 14.1 beta 3 ? ^_^

dan-ryan commented 1 year ago

It seems I've fixed it. Unsure if it's an Xcode problem or this lib, but turning off some diagnostic options like Address Sanitizer appeared to do the trick.

fhfuih commented 2 months ago

Hi all, I have also experienced this issue at XCode Version 15.4 (15F31d), and I have already disabled Address Sanitizer.

Build failed because SocketIO.swiftmodule is not built for arm64. Please try a run destination with a different architecture.

@dan-ryan may I know how have you configered your previous project: Development device and target device? (For me it's M1 macbook pro and M2 iPad Air) Cocoapod or Swift Packages? (For me it's Swift Packages) The built target? (For me it's the latest iPad Air 13-inch M2, iOS 17.4). I have also set Excluded Architectures to be Any iOS simulator: arm64 (In build settings, for both debug and release) or otherwise the program won't compile before adding this package. Thanks!

fhfuih commented 2 months ago

Nvm I finally solved my issue. It is documented here for anyone's reference:

The cause

I also use a legacy SDK from a random periphery device vendor that only supports x86_64 arch, not arm. That's why I previously set Excluded Architectures to be Any iOS simulator: arm64. This basically tells the simulator to NOT compile arm64 version (which is, to only compile x86_64 version).

This socketIO package, on the other hand, presumably only supports arm64 for newer devices (not sure about this part: for newer host devices or newer target devices? How does @yi-ge approach this original issue.) But anyway, this library does complain when I force the simulator to compile an x86_64 version.

The solution

Regarding my previous approach to getting the legacy SDK running (setting Excluded Architectures to be Any iOS simulator: arm64), it is not an ideal solution. When I searched for that error online, which is

building for 'iOS-simulator', but linking in object file <the legacy SDK library> is built for 'iOS'

that solution is frequently mentioned. But it was at the time when M1 was newly released and people try to develop new programs at old x86_64 host MacBooks.

For the modern cases -- importing old x86_64 libraries in new arm64 host & target devices -- the issue is elaborated in this StackOverflow post: https://stackoverflow.com/questions/65978359/xcode-error-building-for-ios-simulator-but-linking-in-dylib-built-for-ios-f

The solution in the accepted post (force running the entire XCode in Rosetta) is still not ideal. Michael Long's comment under that post links to a better solution with explanation: https://blog.sudeium.com/2021/06/18/build-for-x86-simulator-on-apple-silicon-macs/

So TL;DR: I revert my previous settings of Excluded Architectures, and then set the entire project's Architecture (under Build Settings) to Debug (+) Any iOS Simulator SDK = x86_64

圖片