Debian Codegraph @

Apple/testbed/Python.xcframework/build/utils.sh

raw
1 # Utility methods for use in an Xcode project.
2 #
3 # An iOS XCframework cannot include any content other than the library binary
4 # and relevant metadata. However, Python requires a standard library at runtime.
5 # Therefore, it is necessary to add a build step to an Xcode app target that
6 # processes the standard library and puts the content into the final app.
7 #
8 # In general, these tools will be invoked after bundle resources have been
9 # copied into the app, but before framework embedding (and signing).
10 #
11 # The following is an example script, assuming that:
12 # * Python.xcframework is in the root of the project
13 # * There is an `app` folder that contains the app code
14 # * There is an `app_packages` folder that contains installed Python packages.
15 # -----
16 # set -e
17 # source $PROJECT_DIR/Python.xcframework/build/build_utils.sh
18 # install_python Python.xcframework app app_packages
19 # -----
20
21 # Copy the standard library from the XCframework into the app bundle.
22 #
23 # Accepts one argument:
24 # 1. The path, relative to the root of the Xcode project, where the Python
25 # XCframework can be found.
26 install_stdlib() {
27 PYTHON_XCFRAMEWORK_PATH=$1
28
29 mkdir -p "$CODESIGNING_FOLDER_PATH/python/lib"
30 if [ "$EFFECTIVE_PLATFORM_NAME" = "-iphonesimulator" ]; then
31 echo "Installing Python modules for iOS Simulator"
32 if [ -d "$PROJECT_DIR/$PYTHON_XCFRAMEWORK_PATH/ios-arm64-simulator" ]; then
33 SLICE_FOLDER="ios-arm64-simulator"
34 else
35 SLICE_FOLDER="ios-arm64_x86_64-simulator"
36 fi
37 else
38 echo "Installing Python modules for iOS Device"
39 SLICE_FOLDER="ios-arm64"
40 fi
41
42 # If the XCframework has a shared lib folder, then it's a full framework.
43 # Copy both the common and slice-specific part of the lib directory.
44 # Otherwise, it's a single-arch framework; use the "full" lib folder.
45 # Don't include any libpython symlink; that can't be included at runtime
46 if [ -d "$PROJECT_DIR/$PYTHON_XCFRAMEWORK_PATH/lib" ]; then
47 rsync -au --delete "$PROJECT_DIR/$PYTHON_XCFRAMEWORK_PATH/lib/" "$CODESIGNING_FOLDER_PATH/python/lib/" --exclude 'libpython*.dylib'
48 rsync -au "$PROJECT_DIR/$PYTHON_XCFRAMEWORK_PATH/$SLICE_FOLDER/lib-$ARCHS/" "$CODESIGNING_FOLDER_PATH/python/lib/" --exclude 'libpython*.dylib'
49 else
50 rsync -au --delete "$PROJECT_DIR/$PYTHON_XCFRAMEWORK_PATH/$SLICE_FOLDER/lib/" "$CODESIGNING_FOLDER_PATH/python/lib/" --exclude 'libpython*.dylib'
51 fi
52 }
53
54 # Convert a single .so library into a framework that iOS can load.
55 #
56 # Accepts three arguments:
57 # 1. The path, relative to the root of the Xcode project, where the Python
58 # XCframework can be found.
59 # 2. The base path, relative to the installed location in the app bundle, that
60 # needs to be processed. Any .so file found in this path (or a subdirectory
61 # of it) will be processed.
62 # 2. The full path to a single .so file to process. This path should include
63 # the base path.
64 install_dylib () {
65 PYTHON_XCFRAMEWORK_PATH=$1
66 INSTALL_BASE=$2
67 FULL_EXT=$3
68
69 # The name of the extension file
70 EXT=$(basename "$FULL_EXT")
71 # The name and location of the module
72 MODULE_PATH=$(dirname "$FULL_EXT")
73 MODULE_NAME=$(echo $EXT | cut -d "." -f 1)
74 # The location of the extension file, relative to the bundle
75 RELATIVE_EXT=${FULL_EXT#$CODESIGNING_FOLDER_PATH/}
76 # The path to the extension file, relative to the install base
77 PYTHON_EXT=${RELATIVE_EXT/$INSTALL_BASE/}
78 # The full dotted name of the extension module, constructed from the file path.
79 FULL_MODULE_NAME=$(echo $PYTHON_EXT | cut -d "." -f 1 | tr "/" ".");
80 # A bundle identifier; not actually used, but required by Xcode framework packaging
81 FRAMEWORK_BUNDLE_ID=$(echo $PRODUCT_BUNDLE_IDENTIFIER.$FULL_MODULE_NAME | tr "_" "-")
82 # The name of the framework folder.
83 FRAMEWORK_FOLDER="Frameworks/$FULL_MODULE_NAME.framework"
84
85 # If the framework folder doesn't exist, create it.
86 if [ ! -d "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER" ]; then
87 echo "Creating framework for $RELATIVE_EXT"
88 mkdir -p "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER"
89 cp "$PROJECT_DIR/$PYTHON_XCFRAMEWORK_PATH/build/$PLATFORM_FAMILY_NAME-dylib-Info-template.plist" "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER/Info.plist"
90 plutil -replace CFBundleExecutable -string "$FULL_MODULE_NAME" "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER/Info.plist"
91 plutil -replace CFBundleIdentifier -string "$FRAMEWORK_BUNDLE_ID" "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER/Info.plist"
92 fi
93
94 echo "Installing binary for $FRAMEWORK_FOLDER/$FULL_MODULE_NAME"
95 mv "$FULL_EXT" "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER/$FULL_MODULE_NAME"
96 # Create a placeholder .fwork file where the .so was
97 echo "$FRAMEWORK_FOLDER/$FULL_MODULE_NAME" > ${FULL_EXT%.so}.fwork
98 # Create a back reference to the .so file location in the framework
99 echo "${RELATIVE_EXT%.so}.fwork" > "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER/$FULL_MODULE_NAME.origin"
100
101 # If the framework provides an xcprivacy file, install it.
102 if [ -e "$MODULE_PATH/$MODULE_NAME.xcprivacy" ]; then
103 echo "Installing XCPrivacy file for $FRAMEWORK_FOLDER/$FULL_MODULE_NAME"
104 XCPRIVACY_FILE="$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER/PrivacyInfo.xcprivacy"
105 if [ -e "$XCPRIVACY_FILE" ]; then
106 rm -rf "$XCPRIVACY_FILE"
107 fi
108 mv "$MODULE_PATH/$MODULE_NAME.xcprivacy" "$XCPRIVACY_FILE"
109 fi
110
111 echo "Signing framework as $EXPANDED_CODE_SIGN_IDENTITY_NAME ($EXPANDED_CODE_SIGN_IDENTITY)..."
112 /usr/bin/codesign --force --sign "$EXPANDED_CODE_SIGN_IDENTITY" ${OTHER_CODE_SIGN_FLAGS:-} -o runtime --timestamp=none --preserve-metadata=identifier,entitlements,flags --generate-entitlement-der "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER"
113 }
114
115 # Process all the dynamic libraries in a path into Framework format.
116 #
117 # Accepts two arguments:
118 # 1. The path, relative to the root of the Xcode project, where the Python
119 # XCframework can be found.
120 # 2. The base path, relative to the installed location in the app bundle, that
121 # needs to be processed. Any .so file found in this path (or a subdirectory
122 # of it) will be processed.
123 process_dylibs () {
124 PYTHON_XCFRAMEWORK_PATH=$1
125 LIB_PATH=$2
126 find "$CODESIGNING_FOLDER_PATH/$LIB_PATH" -name "*.so" | while read FULL_EXT; do
127 install_dylib $PYTHON_XCFRAMEWORK_PATH "$LIB_PATH/" "$FULL_EXT"
128 done
129 }
130
131 # The entry point for post-processing a Python XCframework.
132 #
133 # Accepts 1 or more arguments:
134 # 1. The path, relative to the root of the Xcode project, where the Python
135 # XCframework can be found. If the XCframework is in the root of the project,
136 # 2+. The path of a package, relative to the root of the packaged app, that contains
137 # library content that should be processed for binary libraries.
138 install_python() {
139 PYTHON_XCFRAMEWORK_PATH=$1
140 shift
141
142 install_stdlib $PYTHON_XCFRAMEWORK_PATH
143 PYTHON_VER=$(ls -1 "$CODESIGNING_FOLDER_PATH/python/lib" | grep -E "^python3\.\d+$")
144 echo "Install Python $PYTHON_VER standard library extension modules..."
145 process_dylibs $PYTHON_XCFRAMEWORK_PATH python/lib/$PYTHON_VER/lib-dynload
146
147 for package_path in $@; do
148 echo "Installing $package_path extension modules ..."
149 process_dylibs $PYTHON_XCFRAMEWORK_PATH $package_path
150 done
151 }