Skip to content

Instantly share code, notes, and snippets.

@acalism
Last active May 26, 2022 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acalism/76430d4e3fba7a1a7494636b67f54a69 to your computer and use it in GitHub Desktop.
Save acalism/76430d4e3fba7a1a7494636b67f54a69 to your computer and use it in GitHub Desktop.
best zshell script to create xcframework
#!/bin/zsh
# 必须用 zsh,否则后面的数组操作可能失败
# 需要编译的 scheme,可以改为你自己的,或者给脚本传递 -s 参数
scheme="T4"
configuration=Release
usesFramework=1 # 是否以 framework 为容器。framework 装的也可能是静态库
buildDir=build
derivedDataPath=${buildDir}/DerivedData
archiveDir=archives
execName=$(basename $0)
function displayHelp()
{
echo "Usage: ${execName} [-h] [-s <scheme>] [-u <usesFramework>]"
echo "-h display this help message"
echo "-s <scheme> specify scheme"
echo "-u <usesFramework> 0 or 1, whether uses framework as container"
}
while getopts ":hs:u:" opt; do
case $opt in
h)
displayHelp
exit 0
;;
s)
scheme=${OPTARG}
;;
u)
usesFramework=${OPTARG}
;;
*)
displayHelp
exit 0
;;
esac
done
function displayError()
{
if [[ "$2" != 0 ]]; then
echo "\n=============== Failed to ${1} with result: ${2}\n"
exit ${2}
fi
}
# clean
rm -rf ${buildDir} ${archiveDir}
# 以framework 为容器
productFolder=Products/Library/Frameworks
productName=${scheme}.framework
container=framework
if [[ $usesFramework != 1 ]]; then
# 不以 framework 为容器
productFolder=Products/usr/local/lib
productName=lib${scheme}.a
container=library
fi
SDKs=(
iphoneos
iphonesimulator
macosx
)
archiveNames=($SDKs[@]) # copy array
archiveNames[3]=maccatalyst # append or replace
echo "\n=============== \$archiveNames is ${archiveNames[@]}\n"
output=${archiveDir}/${scheme}.xcframework
for i in {1..${#SDKs}}; do
archivePath=${archiveDir}/${configuration}-${archiveNames[i]}.xcarchive
echo "\n=============== $archivePath is ${archivePath}\n"
xcodebuild \
archive \
-scheme ${scheme} \
-configuration ${configuration} \
-sdk ${SDKs[i]} \
-derivedDataPath ${derivedDataPath} \
-archivePath ${archivePath} \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
SKIP_INSTALL=NO SUPPORTS_MACCATALYST=YES
displayError "${SDKs[i]}" $?
libArgs=(
-${container}
${archivePath}/${productFolder}/${productName}
)
# find the first folder named "include" and quit
includeDir=$(find ${derivedDataPath} -name include -print -quit)
if [[ $usesFramework == 0 ]] && [[ -n "$includeDir" ]]; then
libArgs+=(-headers ${includeDir})
echo "\n=============== \$includeDir is ${includeDir}\n"
fi
echo "\n=============== \$libArgs is ${libArgs[@]}\n"
# 使用数组灵活拼接命令参数,因为可能没有 include 目录,比如纯 swift 代码写的库
command=(
xcodebuild
-create-xcframework
-output ${output}
)
command+=($libArgs)
# command+=(-output ${output})
echo "\n=============== ${command}\n"
#$(echo ${libArgs[@]})
$(echo ${command})
displayError "create-xcframework" $?
done
command=( zip -r ${output}.zip ${output} )
echo "\n=============== $command\n"
$(echo ${command})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment