Skip to content

Instantly share code, notes, and snippets.

@madhavajay
Last active February 6, 2022 15:36
Show Gist options
  • Save madhavajay/39ff8bfee92e7cd97ea4270b8c99b3bc to your computer and use it in GitHub Desktop.
Save madhavajay/39ff8bfee92e7cd97ea4270b8c99b3bc to your computer and use it in GitHub Desktop.
# VERSION 1.0.4
# Author: @madhavajay
# This currently works for iOS and watchOS in the Simulator and Devices
# Changes
# Using ${TOOLCHAIN} in two places now
# Added double quotes " around paths
# Fixed watchOS Issues
# Instructions iOS
# 1. Copy this into a new Build Phase Script for your App Target
# 2. Add all the Frameworks you need as Input Files, internal and external
# 3. Reset your iOS and watchOS simulators (seriously)
# Screenshot: http://i.imgur.com/3dTjVcW.png
# Instructions watchOS
# 1. Same as above only you need to add the Build Phase Script and its
# Framework Input Files to both the Watch App and the Watch Extension.
# You can only add the Extension one in Xcode. So Add the Extension one,
# then open the .xcodeproj in a Text Editor and find the Entry you just added.
# Its a lot easier with Git. There will be two, changes, one like this:
#
# 28F65CA91D3FDC7D00043613 /* Fix Xcode 8 Beta 3 */
#
# Under the WatchKit Extension buildPhases, and a second with the actual
# script and inputs.
# 2. Cut and Paste the Line like above, linking the Script from the Extension
# Build Phase to the WatchKit App buildPhases.
# 3. Make a new one for the WatchKit Extension Again, either in Xcode or
# Text Editor.
# 4. Reset your iOS and watchOS simulators (seriously)
# 5. Clean deriveddata, spin around in your chair, build and shazam!
# Whats happening here is the same Copy Swift standard libraries step is performed
# on both the App and the Extension Targets. I was able to get the app running
# with only one of these but i'm skeptical its correct as you can see Xcode
# attempts the Copy Swift standard libraries on both Targets by default so there
# must be a need for it.
# Note, you should use the Beta 3 Toolchain, so make sure $TOOLCHAIN_DIR is resolving to:
# /Applications/{YOUR BETA}.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
# Trouble Shooting
# Check the Build Logs and you should see the New Step you add for each target.
# If its working you can see the files like libswiftCore.dylib appear in the
# Framework folders of your .app and .appex files.
# If its not working, try putting `set` on a new line of your Build Phase Script
# This will echo out all the Environment Variables used below and you can compare them
# with the expected output that Xcode uses in the normal Build Step.
# Be careful the Path Spaces are escaped by using double " around the paths below
# or the build paths for watchOS wont work, because they have spaces in their
# folder names like: APPNAME WatchKit Extension and APPNAME WatchKit App
# Include Frameworks
for i in `seq 1 ${SCRIPT_INPUT_FILE_COUNT}`; do
NUM=`expr $i - 1`
NEXT_FOLDER_ARG=SCRIPT_INPUT_FILE_$NUM
INCLUDE_FRAMEWORK_FOLDER_ARGS="${INCLUDE_FRAMEWORK_FOLDER_ARGS} --scan-folder ${!NEXT_FOLDER_ARG}"
done
# Run the correct tool 🤗
${TOOLCHAIN_DIR}/usr/bin/swift-stdlib-tool \
--copy --verbose --sign ${EXPANDED_CODE_SIGN_IDENTITY} \
--scan-executable "${TARGET_BUILD_DIR}/${EXECUTABLE_PATH}" \
--scan-folder "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}" \
--scan-folder "${TARGET_BUILD_DIR}/${PLUGINS_FOLDER_PATH}" \
--platform ${PLATFORM_NAME} \
--toolchain ${TOOLCHAIN_DIR} \
--destination "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}" \
--strip-bitcode --resource-destination "${TARGET_BUILD_DIR}/${FULL_PRODUCT_NAME}" \
--resource-library libswiftRemoteMirror.dylib \
${INCLUDE_FRAMEWORK_FOLDER_ARGS}
@madhavajay
Copy link
Author

Yup just tested with Xcode 8 Beta 4, still seeing this problem, and still working with the script. My Radar rdar://27564402 has been marked dupe with rdar://27434450 so I guess they are aware of the problem. Maybe its an incomplete implementation of final Swift 3.0 std libs to be shipped with watchOS 3 final to lower app size / memory footprint and speed up install?

@benjohnde
Copy link

Yeap, same thoughts here! :)

@antoinepalazzolo
Copy link

antoinepalazzolo commented Aug 2, 2016

Do you have any issues (Xcode crash) when exporting archive or submit for testflight with this script ?

@farice
Copy link

farice commented Aug 3, 2016

Greatly appreciated, this solved the problem which had been bugging me for hours.

@svenbacia
Copy link

@antoinepalazzolo Yes, I can't export my app currently. Xcode 8 always crashes when I try to upload it to testflight and when I upload it with the Application Loader I get the following error:

ERROR ITMS-90171: "Invalid Bundle Structure - The binary file '<app_name>.app/libswiftRemoteMirror.dylib' is not permitted. Your app can’t contain standalone executables or libraries, other than the CFBundleExecutable of supported bundles. Refer to the Bundle Programming Guide at https://developer.apple.com/go/?id=bundle-structure for information on the iOS app bundle structure."

@drekka
Copy link

drekka commented Aug 5, 2016

Been trying this script and others on XCode 8b4 with no luck. So far the best I have is this error on start up:

dyld: Symbol not found: __TMps11CVarArgType
 Referenced from: /Users/derekclarkson/Library/Developer/CoreSimulator/Devices/277C19C2-718E-4CC2-9873-BB6E4C3A46E3/data/Containers/Bundle/Application/0AA690B4-815B-4C27-A8FA-4C144D857E0A/crux-iOS.app/Frameworks/SocketIOClientSwift.framework/SocketIOClientSwift
  Expected in: /Users/derekclarkson/Library/Developer/CoreSimulator/Devices/277C19C2-718E-4CC2-9873-BB6E4C3A46E3/data/Containers/Bundle/Application/0AA690B4-815B-4C27-A8FA-4C144D857E0A/crux-iOS.app/Frameworks/libswiftCore.dylib
in /Users/derekclarkson/Library/Developer/CoreSimulator/Devices/277C19C2-718E-4CC2-9873-BB6E4C3A46E3/data/Containers/Bundle/Application/0AA690B4-815B-4C27-A8FA-4C144D857E0A/crux-iOS.app/Frameworks/SocketIOClientSwift.framework/SocketIOClientSwift

Which indicates to me that the libs are present, but are somehow incompatible.

@JackieQi
Copy link

JackieQi commented Aug 5, 2016

Thanks very much for the script. It cost me two days to figure out the issue. I just tested it with Xcode 8 beta 4. And it works for me perfectly.

@nesterenkodm
Copy link

There are changes in xcode 8b5 related to frameworks being linked correctly. Can anyone confirm the issue is still exists in b5?

Xcode correctly embeds or links frameworks across projects without needing to set up a direct reference between the project that produces the framework and the project that wants to embed or link it. (27631386)

@nesterenkodm
Copy link

Also in known issues:

If your app uses pre-built frameworks that combine device and simulator architectures into a single framework binary, Xcode won't know how to embed the right Swift standard libraries for your app.
Workaround: Make sure that your build only includes binaries with architectures for the current platform (whether device or simulator). (27434450)

@benjohnde
Copy link

Still in need for this script :/ Guess it is going to be fixed in the next release. At least the bug found its way to the known issues list.

@capnslipp
Copy link

capnslipp commented Aug 16, 2016

@cherbur The issue still exists in Xcode 8b5.  I actually didn't start seeing it until 8b5 (I guess I hadn't done a Clean Build Folder in a good while, so the correctly-copied-over libswift….dylib frameworks were still in my App's build dir from 8b2).  Thinking it was just a bug in 8b5, I went back to Xcode 8b4 but the issue persisted, leading me to the Apple Forums, then to a Carthage issue, then here.

Your script works great for me both on Xcode 8b4 & 8b5 building for an iOS 10 beta 5 target.  Cheers!

@capnslipp
Copy link

Xcode's builtin-swiftStdLibTool bug appears to be fixed as of Xcode 8 beta 6.

@madhavajay
Copy link
Author

Woohoo! Glad it helped and even gladder its fixed in Xcode. Thanks to everyone who helped with this.

@kukat
Copy link

kukat commented Dec 22, 2016

@madhavajay do we still need this for Xcode 8.1 and above?

@emrahgunduz
Copy link

Xcode 9.3, and I'm still using this script to solve swift problems on objc projects.
🥇

@trvd1707
Copy link

trvd1707 commented Sep 8, 2018

I'm using Cocoapods. Can I use this script to sole my problem too? In my case, if I build a fat framework it doesn't build my app. It gives me a invalid bitcode signature when linking with my swift pods. If I build just for active architecture, it gives me a runtime error dyld: Library not loaded: @rpath/...reason; Image not found when loading the app. I looked inside the app container and all swift libraries and all my pods frameworks are there inside Frameworks and my @Runpath does have Frameworks dir in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment