2
0

build_utm.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/bin/sh
  2. set -e
  3. command -v realpath >/dev/null 2>&1 || realpath() {
  4. [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
  5. }
  6. BASEDIR="$(dirname "$(realpath $0)")"
  7. usage () {
  8. echo "Usage: $(basename $0) [-t teamid] [-k SDK] [-s scheme] [-a architecture] [-o output]"
  9. echo ""
  10. echo " -t teamid Team Identifier for app groups. Optional for iOS. Required for macOS."
  11. echo " -k sdk Target SDK. Default iphoneos. [iphoneos|iphonesimulator|xros|xrsimulator|macosx]"
  12. echo " -s scheme Target scheme. Default iOS/macOS depending on platform. [iOS|iOS-TCI|iOS-Remote|macOS]"
  13. echo " -a architecture Target architecture. Default arm64. [arm64|x86_64]"
  14. echo " -o output Output archive path. Default is current directory."
  15. echo ""
  16. exit 1
  17. }
  18. PRODUCT_BUNDLE_PREFIX="com.utmapp"
  19. TEAM_IDENTIFIER=
  20. ARCH=arm64
  21. OUTPUT=$PWD
  22. SDK=iphoneos
  23. SCHEME=
  24. while [ "x$1" != "x" ]; do
  25. case $1 in
  26. -t )
  27. TEAM_IDENTIFIER=$2
  28. shift
  29. ;;
  30. -a )
  31. ARCH=$2
  32. shift
  33. ;;
  34. -k )
  35. SDK=$2
  36. shift
  37. ;;
  38. -s )
  39. SCHEME=$2
  40. shift
  41. ;;
  42. -o )
  43. OUTPUT=$2
  44. shift
  45. ;;
  46. * )
  47. usage
  48. ;;
  49. esac
  50. shift
  51. done
  52. case $SDK in
  53. macos )
  54. SCHEME="macOS"
  55. ;;
  56. * )
  57. if [ -z "$SCHEME" ]; then
  58. SCHEME="iOS"
  59. fi
  60. ;;
  61. esac
  62. ARCH_ARGS=$(echo $ARCH | xargs printf -- "-arch %s ")
  63. if [ ! -z "$TEAM_IDENTIFIER" ]; then
  64. TEAM_IDENTIFIER_PREFIX="TeamIdentifierPrefix=${TEAM_IDENTIFIER}."
  65. fi
  66. xcodebuild archive -archivePath "$OUTPUT" -scheme "$SCHEME" -sdk "$SDK" $ARCH_ARGS -configuration Release CODE_SIGNING_ALLOWED=NO $TEAM_IDENTIFIER_PREFIX
  67. BUILT_PATH=$(find $OUTPUT.xcarchive -name '*.app' -type d | head -1)
  68. # Only retain the target architecture to address < iOS 15 crash & save disk space
  69. if [ "$SDK" == "iphoneos" ]; then
  70. find "$BUILT_PATH" -type f -path '*/Frameworks/*.dylib' | while read FILE; do
  71. if [[ $(lipo -info "$FILE") =~ "Architectures in the fat file" ]]; then
  72. lipo -thin $ARCH "$FILE" -output "$FILE"
  73. fi
  74. done
  75. find "$BUILT_PATH" -type d -path '*/Frameworks/*.framework' | while read FRAMEWORK; do
  76. FILE="${FRAMEWORK}"/$(basename "${FRAMEWORK%.*}")
  77. if [[ $(lipo -info "$FILE") =~ "Architectures in the fat file" ]]; then
  78. lipo -thin $ARCH "$FILE" -output "$FILE"
  79. fi
  80. done
  81. fi
  82. find "$BUILT_PATH" -type d -path '*/Frameworks/*.framework' -exec codesign --force --sign - --timestamp=none \{\} \;
  83. if [ "$SDK" == "macosx" ]; then
  84. # always build with vm entitlements, package_mac.sh can strip it later
  85. # this way we can import into Xcode and re-sign from there
  86. UTM_ENTITLEMENTS="/tmp/utm.$$.entitlements"
  87. LAUNCHER_ENTITLEMENTS="/tmp/launcher.$$.entitlements"
  88. HELPER_ENTITLEMENTS="/tmp/helper.$$.entitlements"
  89. CLI_ENTITLEMENTS="/tmp/cli.$$.entitlements"
  90. cp "$BASEDIR/../Platform/macOS/macOS.entitlements" "$UTM_ENTITLEMENTS"
  91. cp "$BASEDIR/../QEMULauncher/QEMULauncher.entitlements" "$LAUNCHER_ENTITLEMENTS"
  92. cp "$BASEDIR/../QEMUHelper/QEMUHelper.entitlements" "$HELPER_ENTITLEMENTS"
  93. cp "$BASEDIR/../utmctl/utmctl.entitlements" "$CLI_ENTITLEMENTS"
  94. if [ ! -z "$TEAM_IDENTIFIER" ]; then
  95. TEAM_ID_PREFIX="${TEAM_IDENTIFIER}."
  96. fi
  97. /usr/libexec/PlistBuddy -c "Set :com.apple.security.application-groups:0 ${TEAM_ID_PREFIX}${PRODUCT_BUNDLE_PREFIX}.UTM" "$UTM_ENTITLEMENTS"
  98. /usr/libexec/PlistBuddy -c "Set :com.apple.security.application-groups:0 ${TEAM_ID_PREFIX}${PRODUCT_BUNDLE_PREFIX}.UTM" "$HELPER_ENTITLEMENTS"
  99. /usr/libexec/PlistBuddy -c "Set :com.apple.security.application-groups:0 ${TEAM_ID_PREFIX}${PRODUCT_BUNDLE_PREFIX}.UTM" "$CLI_ENTITLEMENTS"
  100. codesign --force --sign - --entitlements "$LAUNCHER_ENTITLEMENTS" --timestamp=none --options runtime "$BUILT_PATH/Contents/XPCServices/QEMUHelper.xpc/Contents/MacOS/QEMULauncher.app/Contents/MacOS/QEMULauncher"
  101. codesign --force --sign - --entitlements "$HELPER_ENTITLEMENTS" --timestamp=none --options runtime "$BUILT_PATH/Contents/XPCServices/QEMUHelper.xpc/Contents/MacOS/QEMUHelper"
  102. codesign --force --sign - --entitlements "$CLI_ENTITLEMENTS" --timestamp=none --options runtime "$BUILT_PATH/Contents/MacOS/utmctl"
  103. codesign --force --sign - --entitlements "$UTM_ENTITLEMENTS" --timestamp=none --options runtime "$BUILT_PATH/Contents/MacOS/UTM"
  104. rm "$UTM_ENTITLEMENTS"
  105. rm "$LAUNCHER_ENTITLEMENTS"
  106. rm "$HELPER_ENTITLEMENTS"
  107. rm "$CLI_ENTITLEMENTS"
  108. fi