Skip to content

Instantly share code, notes, and snippets.

@eladnava
Last active November 26, 2021 14:43
Show Gist options
  • Save eladnava/0824d08da8f99419ef2c7b7fb6d4cc78 to your computer and use it in GitHub Desktop.
Save eladnava/0824d08da8f99419ef2c7b7fb6d4cc78 to your computer and use it in GitHub Desktop.
exec > /tmp/${PROJECT_NAME}_archive.log 2>&1
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
if [ "true" == ${ALREADYINVOKED:-false} ]
then
echo "RECURSION: Detected, stopping"
else
export ALREADYINVOKED="true"
# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
echo "Building for iPhoneSimulator"
xcodebuild -workspace "${WORKSPACE_PATH}" -scheme "${TARGET_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6' ONLY_ACTIVE_ARCH=NO ARCHS='i386 x86_64' BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" ENABLE_BITCODE=YES OTHER_CFLAGS="-fembed-bitcode" BITCODE_GENERATION_MODE=bitcode clean build
# Step 1. Copy the framework structure (from iphoneos build) to the universal folder
echo "Copying to output folder"
cp -R "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${FULL_PRODUCT_NAME}" "${UNIVERSAL_OUTPUTFOLDER}/"
# Step 2. Copy Swift modules from iphonesimulator build (if it exists) to the copied framework directory
SIMULATOR_SWIFT_MODULES_DIR="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${TARGET_NAME}.framework/Modules/${TARGET_NAME}.swiftmodule/."
if [ -d "${SIMULATOR_SWIFT_MODULES_DIR}" ]; then
cp -R "${SIMULATOR_SWIFT_MODULES_DIR}" "${UNIVERSAL_OUTPUTFOLDER}/${TARGET_NAME}.framework/Modules/${TARGET_NAME}.swiftmodule"
fi
# Step 3. Create universal binary file using lipo and place the combined executable in the copied framework directory
echo "Combining executables"
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${EXECUTABLE_PATH}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${EXECUTABLE_PATH}" "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${EXECUTABLE_PATH}"
# Step 4. Create universal binaries for embedded frameworks
#for SUB_FRAMEWORK in $( ls "${UNIVERSAL_OUTPUTFOLDER}/${TARGET_NAME}.framework/Frameworks" ); do
#BINARY_NAME="${SUB_FRAMEWORK%.*}"
#lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${TARGET_NAME}.framework/Frameworks/${SUB_FRAMEWORK}/${BINARY_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${SUB_FRAMEWORK}/${BINARY_NAME}" "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${TARGET_NAME}.framework/Frameworks/${SUB_FRAMEWORK}/${BINARY_NAME}"
#done
# Step 5. Convenience step to copy the framework to the project's directory
echo "Copying to project dir"
yes | cp -Rf "${UNIVERSAL_OUTPUTFOLDER}/${FULL_PRODUCT_NAME}" "${PROJECT_DIR}"
open "${PROJECT_DIR}"
fi
@IvanovDeveloper
Copy link

@eladnava @fer662 @BalaKarunakaran

It seems that in XCode 10, the folder under Release-universal path does not contain the {YOUR_FRAMEWORK}.framework file but its contents. I solved it by replacing this line under echo "Copying to output folder":

cp -R "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${FULL_PRODUCT_NAME}" "${UNIVERSAL_OUTPUTFOLDER}/"

with this one:

cp -R "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}" "${UNIVERSAL_OUTPUTFOLDER}/"

Please, confirm that it works for you too.

It's work for xcode 10

@ignaval
Copy link

ignaval commented Oct 22, 2018

@eladnava @fer662 @BalaKarunakaran

It seems that in XCode 10, the folder under Release-universal path does not contain the {YOUR_FRAMEWORK}.framework file but its contents. I solved it by replacing this line under echo "Copying to output folder":

cp -R "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${FULL_PRODUCT_NAME}" "${UNIVERSAL_OUTPUTFOLDER}/"

with this one:

cp -R "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}" "${UNIVERSAL_OUTPUTFOLDER}/"

Please, confirm that it works for you too.

I actually had to change a different line for this to work on Xcode 10 (Version 10.0 (10A255)). I added a missing target name. But kept the original line you mentioned changing. I just changed:

lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${EXECUTABLE_PATH}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${EXECUTABLE_PATH}" "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${EXECUTABLE_PATH}"

for:

lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${EXECUTABLE_PATH}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${TARGET_NAME}/${EXECUTABLE_PATH}" "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${EXECUTABLE_PATH}"

and it worked correctly.

@canisterexister
Copy link

canisterexister commented Oct 26, 2018

If still not working check AlexM answer https://stackoverflow.com/a/52873099/4311935

@antonc27
Copy link

Just want to notice that script will not work as is if you have different target and product names (e.g., target MyLib_iOS and product MyLib.framework). This can be fixed by using $PRODUCT_NAME instead of $TARGET_NAME (except of -scheme parameter)

@GriffinStudios
Copy link

GriffinStudios commented Apr 29, 2019

@eladnava thanks so much for creating this! This and your article at https://eladnava.com/publish-a-universal-binary-ios-framework-in-swift-using-cocoapods/ was amazing.

--
Here is the change I made to get it to working:

Make ${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${FULL_PRODUCT_NAME} to ${ARCHIVE_PRODUCTS_PATH}/${INSTALL_PATH}/

Why?

  • products_path and install_path needed a file slash otherwise it doesn’t reference the correct file.
  • Including the full_product_name causes ONLY the framework contents to be copied.

——
Also, as you’ll probably have to modify this Xcode script to fit your particular framework, here's three debugging tips:

  1. The file at /tmp/${PROJECT_NAME}_archive.log is a lifesaver for trouble-shooting efforts. Every archive, you should be referencing this file.

  2. I highly suggest adding this echo statement to the file as this path is were 99% of your problems will occur: echo "Framework location: ${ARCHIVE_PRODUCTS_PATH}/${INSTALL_PATH}/“

  3. If the error log tries a cp command and "file can't be found" then it is most likely a path error.

@aevapps
Copy link

aevapps commented Sep 24, 2019

For Xcode 11 change 'platform=iOS Simulator,name=iPhone 6' to 'platform=iOS Simulator,name=iPhone 8'

@caiovncius
Copy link

I had some issues in copy files. I resolved with this code:

exec > /tmp/${PROJECT_NAME}_archive.log 2>&1

UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal

if [ "true" == ${ALREADYINVOKED:-false} ]
then
echo "RECURSION: Detected, stopping"
else
export ALREADYINVOKED="true"


echo "Building for iPhoneSimulator"
xcodebuild -workspace "${WORKSPACE_PATH}" -scheme "${TARGET_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6' ONLY_ACTIVE_ARCH=NO ARCHS='i386 x86_64' BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" ENABLE_BITCODE=YES OTHER_CFLAGS="-fembed-bitcode" BITCODE_GENERATION_MODE=bitcode clean build

# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}/${TARGET_NAME}.framework"

# Step 1. Copy the framework structure (from iphoneos build) to the universal folder
echo "Copying to output folder"
cp -R "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${FULL_PRODUCT_NAME}" "${UNIVERSAL_OUTPUTFOLDER}/"

# Step 2. Copy Swift modules from iphonesimulator build (if it exists) to the copied framework directory
SIMULATOR_SWIFT_MODULES_DIR="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${TARGET_NAME}.framework/Modules/${TARGET_NAME}.swiftmodule/."
if [ -d "${SIMULATOR_SWIFT_MODULES_DIR}" ]; then
cp -R "${SIMULATOR_SWIFT_MODULES_DIR}" "${UNIVERSAL_OUTPUTFOLDER}/${TARGET_NAME}.framework/Modules/${TARGET_NAME}.swiftmodule"
fi

# Step 3. Create universal binary file using lipo and place the combined executable in the copied framework directory
echo "Combining executables"
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${EXECUTABLE_PATH}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${EXECUTABLE_PATH}" "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${EXECUTABLE_PATH}"

# Step 4. Create universal binaries for embedded frameworks
#for SUB_FRAMEWORK in $( ls "${UNIVERSAL_OUTPUTFOLDER}/${TARGET_NAME}.framework/Frameworks" ); do
#BINARY_NAME="${SUB_FRAMEWORK%.*}"
#lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${TARGET_NAME}.framework/Frameworks/${SUB_FRAMEWORK}/${BINARY_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${SUB_FRAMEWORK}/${BINARY_NAME}" "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${TARGET_NAME}.framework/Frameworks/${SUB_FRAMEWORK}/${BINARY_NAME}"
#done

# Step 5. Convenience step to copy the framework to the project's directory
echo "Copying to project dir"
echo ${FULL_PRODUCT_NAME}
cp -Rf "${UNIVERSAL_OUTPUTFOLDER}/${FULL_PRODUCT_NAME}" "${PROJECT_DIR}"

open "${PROJECT_DIR}"

fi

@nick3389
Copy link

nick3389 commented May 15, 2020

@caiovncius environment variables are not working since XCode 11.4. I just try to echo "${TARGET_NAME}" but the result is blank. Any thought about this? It only works if I embed it as a post archive script.

@zhoulijun12315
Copy link

in xCode12 , the result is blank

@GeorgeVashakidze
Copy link

in xCode12 , the result is blank - SAME HERE : )) what the hell : ))

@avjiang
Copy link

avjiang commented Jan 12, 2021

Hello i am currently learning about framework, and faced the issue with blank result.

I am using Xcode 12

@shawnkc
Copy link

shawnkc commented Jun 29, 2021

Comment out the first line if your results are blank. It redirects the output to a file.

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