CocoaPods校驗的報錯
最近筋粗,我執(zhí)行pod lib lint --verbose --no-clean --verbose --allow-warnings
校驗CocoaPods逗鸣,結果卻是一個以前沒有見過的錯誤:
ld: building for iOS Simulator, but linking in dylib built for iOS, file '/var/folders/pn/4cgjvr1j7mzbnl55pfnzlgr00000gq/T/CocoaPods-Lint-20200929-48311-11st24n-DroneKit/Pods/DJI-SDK-iOS/iOS_Mobile_SDK/DJISDK.framework/DJISDK' for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
GitHub Issue:https://github.com/CocoaPods/CocoaPods/issues/10104
CocoaPods打包的報錯
在執(zhí)行pod package
時也出現(xiàn)了跟arm64架構有關的錯誤
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: Pods/build/package.a and Pods/build-sim/package.a have the same architectures (arm64) and can't be in the same fat output file
Github Issue:https://github.com/CocoaPods/cocoapods-packager/issues/259
出現(xiàn)問題的原因
根據(jù)這篇Medium的文章朗恳,由于我們的項目對于架構的設置是默認的:ARCHS_STANDARD
熬甚,隨著Xcode 12 的正式發(fā)布蜜自,蘋果在這個字段里面加入了arm64架構的模擬器诅病,??注意讽膏,是模擬器。
隨著蘋果的Apple Silicon計劃和M1芯片的推出棚瘟,Xcode為了適配arm64架構的Mac现斋,讓M1的用戶可以在模擬器上調試,蘋果加入了arm64架構模擬器的字段偎蘸,導致了CocoaPods校驗和打包的報錯庄蹋。
詳細的原因
至于詳細的原因,這篇StackOverFLow的回答給出了答案迷雪,下面粘貼部分內容并翻譯(大部分使用DeepL翻譯后校對):
The Build Settings editor no longer includes the Valid Architectures build setting "VALID ARCHS", and its use is discouraged. Instead, there is a new Excluded Architectures build setting (EXCLUDED ARCHS)
構建設置編輯器不再包含有效的架構構建設置 "VALID ARCHS"限书,并且不鼓勵使用它。取而代之的是一個新的排除架構構建設置(EXCLUDED ARCHS)章咧。
Xcode 12 is actually the stepping stone for Apple Silicon which unfortunately is not yet available. But with that platform, we are gonna get arm64 based macOS where simulators will also run on arm64 architecture unlike the present Intel-based x86_64 architecture.
Xcode 12其實是蘋果Silicon的墊腳石倦西,可惜目前還沒有上市。但有了這個平臺赁严,我們使用基于arm64的macOS扰柠,其中模擬器也將運行在arm64架構上,而不像現(xiàn)在基于英特爾的x86_64架構误澳。
Xcode usually depends on the "Run Destination" to build its libraries/apps. So when a simulator is chosen as the "Run Destination", it builds the app for available simulator architectures and when a device is chosen as the "Run Destination" it builds for the architecture that the device supports (arm*).
Xcode通常依賴于 "運行目的地 (Run Destination)"來構建它的庫/應用程序耻矮。因此,當模擬器被選擇為 "運行目的地(Run Destination) "時忆谓,它會為可用的模擬器架構構建應用程序裆装,而當設備被選擇為 "運行目的地 "時,它會為設備支持的架構(arm*)構建。
xcodebuild, in the Xcode 12+ build system, considers arm64 as a valid architecture for the simulator. So when a simulator is chosen as the run destination, it can potentially try to compile/link your libs/apps against arm64 based simulators as well (not available yet). So it sends clang(++) some -target flag like arm64-apple-ios13.0-simulator in --- format and clang tries to build/link against arm64 based simulator that eventually fails on Intel-based mac.
xcodebuild哨免,在Xcode 12+構建系統(tǒng)中茎活,認為arm64是模擬器的有效架構。所以當一個模擬器被選擇為運行目標時琢唾,它有可能會嘗試對基于arm64的模擬器也編譯/鏈接你的lib/apps(目前還沒有)(2021.3.16注:已經有了)载荔。因此,它向clang(++)發(fā)送一些-target標志采桃,如arm64-apple-ios13.0-simulator懒熙,格式為----,然后clang就會嘗試對基于arm64的模擬器進行編譯/鏈接普办,最終在基于Intel的Mac上失敗工扎。
But xcodebuild tries this only for Release builds. Why? Because, "Build Active Architecture Only (ONLY_ACTIVE_ARCH)" build settings is usually set to "No" for the "Release" configuration only. And that means xcodebuild will try to build all architectural variants of your libs/apps for the selected run destination for release builds. And for the Simulator run destination, it will includes both x86_64 and arm64 now on, since arm64 in Xcode 12+ is also a supported architecture for simulators to support Apple Silicon.
但xcodebuild只在Release構建時才會嘗試這樣做。為什么呢衔蹲?因為肢娘,"僅構建活動架構(ONLY_ACTIVE_ARCH) "構建設置通常只在 "Release "配置中設置為 "No"。這意味著xcodebuild將嘗試為所選的運行目標構建發(fā)行版構建的所有l(wèi)ib/apps的架構變體舆驶。對于模擬器的運行目標橱健,它將包括x86_64和arm64,因為在Xcode 12+中arm64也是模擬器支持的架構沙廉,以支持Apple Silicon拘荡。
Simply putting, Xcode will fail to build your app anytime it tries the command line, xcodebuild, (which defaults to release build, see the general tab of your project setting) or otherwise in release mode. So a simple workaround to this issue is to set "Build Active Architecture Only (ONLY_ACTIVE_ARCH)" to Yes in your libraries/apps, even for release mode.
簡單地說,Xcode在任何時候嘗試命令行蓝仲、xcodebuild(默認為release build俱病,見項目設置的general選項卡)或其他release模式下官疲,都會導致你的應用程序構建失敗袱结。所以這個問題的一個簡單的變通方法就是在你的庫/應用中把 "僅構建活動架構(ONLY_ACTIVE_ARCH)"設置為 "是",即使是發(fā)布模式途凫。
解決方案
目前似乎只有臨時的解決方案垢夹,就是在podspec文件里添加兩行繞過arm64架構模擬器的配置:
s.pod_target_xcconfig = {
'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64'
}
s.user_target_xcconfig = {
'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64'
}
根據(jù)大家的描述:
This will eventually be a problem when we are running on Apple Silicon Macs, but for now we'll be OK.
一個疑問
如果在podspec上設置繞開了arm64架構的模擬器,那開發(fā)者不就沒有辦法在M1的Mac上面使用模擬器調試了嗎维费?該怎么樣讓自己的庫適配arm64的模擬器呢果元?