-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathxcframework.sh
More file actions
executable file
·359 lines (286 loc) · 10.5 KB
/
xcframework.sh
File metadata and controls
executable file
·359 lines (286 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env bash -e -o pipefail
#
# xcframework.sh
# Usage example: ./xcframework.sh --output <some_path>/<name>.xcframework
# Set Script Variables
SCRIPT="$("$(dirname "$0")/resolvepath.sh" "$0")"
SCRIPTS_DIR="$(dirname "$SCRIPT")"
ROOT_DIR="$(dirname "$SCRIPTS_DIR")"
CURRENT_DIR="$(pwd -P)"
IS_PARSING_BUILD_ARGS=0
BUILD_ARGS=()
EXIT_CODE=0
# Help
function printhelp() {
local HELP="Builds an XCFramework for the given project.\n"
HELP+="\n"
HELP+="xcframework.sh [--help | -h] [--output (<output_framework> | <output_folder>)]\n"
HELP+=" [--configuration <configuration>] [--project-name <project_name>]\n"
HELP+=" [--exclude-dsyms] [--verbose] [--no-clean | --no-clean-on-fail]\n"
HELP+="xcframework.sh [options] -- <build_flags> ...\n"
HELP+="\n"
HELP+="OPTIONS:\n"
HELP+="\n"
HELP+="--help, -h) Print this help message and exit.\n"
HELP+="\n"
HELP+="--verbose) Enable verbose logging. If enabled, no logs are created and\n"
HELP+=" hence the '--no-clean' and '--no-clean-on-fail' flags are\n"
HELP+=" moot.\n"
HELP+="\n"
HELP+="--output) The directory or fully qualified path to export the\n"
HELP+=" generated XCFramework to. If not specified, this defaults to\n"
HELP+=" \"<scripts_dir>/build/<project_name>.xcframework\"\n"
HELP+="\n"
HELP+="--configuration) The configuration to use when building each scheme. This is\n"
HELP+=" passed directly to 'xcodebuild' without modification. If not\n"
HELP+=" specified, this defaults to \"Release\".\n"
HELP+="\n"
HELP+="--project-name) The name of the project to run tests against. If not\n"
HELP+=" provided it will attempt to be resolved by searching the\n"
HELP+=" working directory for an Xcode project and using its name.\n"
HELP+="\n"
HELP+="--exclude-dsyms) Do not include the generated dSYMs nor BCSymbolMaps in the\n"
HELP+=" final XCFramework.\n"
HELP+="\n"
HELP+="--no-clean) Do not clean up the logs created to run these tests upon\n"
HELP+=" completion.\n"
HELP+="\n"
HELP+="--no-clean-on-fail) Same as --no-clean with the exception that if the succeed\n"
HELP+=" clean up will continue as normal. This is mutually exclusive\n"
HELP+=" with --no-clean, with --no-clean taking precedence.\n"
HELP+="\n"
HELP+="--build-dir) The directory in which to store temporary build artifacts\n"
HELP+=" and logs. The directory will be created if needed. If\n"
HELP+=" specified this directory will not be deleted when the\n"
HELP+=" script finishes running.\n"
HELP+="\n"
HELP+="ARGUMENTS:\n"
HELP+="\n"
HELP+="<build_flags> Any arguments that appear after a '--' argument are treated\n"
HELP+=" as Xcode build arguments and are passed as is to 'xcodebuild'\n"
HELP+=" when building the architectures for the XCFramework.\n"
IFS='%'
echo -e "$HELP" 1>&2
unset IFS
exit $EXIT_CODE
}
# Parse Arguments
while [[ $# -gt 0 ]]; do
if [ "$IS_PARSING_BUILD_ARGS" == "1" ]; then
BUILD_ARGS=("${BUILD_ARGS[@]}" "$1")
shift
else
case "$1" in
--output)
OUTPUT="$2"
shift # --output
shift # <project_name>
;;
--configuration)
CONFIGURATION="$2"
shift # --configuration
shift # <configuration>
;;
--project-name)
PROJECT_NAME="$2"
shift # --project-name
shift # <project_name>
;;
--exclude-dsyms)
EXCLUDE_DSYMS=1
shift # --exclude-dsyms
;;
--no-clean)
NO_CLEAN=1
shift # --no-clean
;;
--no-clean-on-fail)
NO_CLEAN_ON_FAIL=1
shift # --no-clean-on-fail
;;
--build-dir)
BUILD_DIR="$2"
shift # --build-dir
shift # <build_dir>
;;
--verbose)
VERBOSE=1
shift # --verbose
;;
--help | -h)
printhelp
;;
--)
IS_PARSING_BUILD_ARGS=1
shift # --
;;
*)
"$SCRIPTS_DIR/printformat.sh" "foreground:red" "Unknown argument: $1\n" 1>&2
EXIT_CODE=1
printhelp
;;
esac
fi
done
#
ARGUMENTS=()
if [ "${#PROJECT_NAME}" != 0 ]; then
ARGUMENTS=(--project-name "$PROJECT_NAME")
fi
PROJECT_NAME="$("$SCRIPTS_DIR/findproject.sh" "${ARGUMENTS[@]}")"
EXIT_CODE=$?
if [ "$EXIT_CODE" != "0" ]; then
printhelp
fi
#
if [ -z ${OUTPUT+x} ]; then
OUTPUT="$SCRIPTS_DIR/build/$PROJECT_NAME.xcframework"
elif [ "${OUTPUT##*.}" != "xcframework" ]; then
if [ "${OUTPUT: -1}" == "/" ]; then
OUTPUT="${OUTPUT}${PROJECT_NAME}.xcframework"
else
OUTPUT="${OUTPUT}/${PROJECT_NAME}.xcframework"
fi
fi
if [ -z ${CONFIGURATION+x} ]; then
CONFIGURATION="Release"
fi
if [ -z ${BUILD_DIR+x} ]; then
BUILD_DIR="$(mktemp -d -t ".$(echo "$PROJECT_NAME" | tr '[:upper:]' '[:lower:]').xcframework.build")"
BUILD_DIR_IS_TEMP=1
else
mkdir -p "$BUILD_DIR"
EXIT_CODE=$?
if [ "$EXIT_CODE" != "0" ]; then
"$SCRIPTS_DIR/printformat.sh" "foreground:red" "Unable to create build directory: $BUILD_DIR"
exit $EXIT_CODE
fi
fi
# Function Declarations
function cleanup() {
cd "$CURRENT_DIR"
if [[ "$VERBOSE" != "1" && "$BUILD_DIR_IS_TEMP" == "1" && ("$NO_CLEAN" == "1" || ("$NO_CLEAN_ON_FAIL" == "1" && "$EXIT_CODE" != "0")) ]]; then
if [ "$EXIT_CODE" == "0" ]; then
"$SCRIPTS_DIR/printformat.sh" "foreground:yellow" "Build Directory: $BUILD_DIR"
fi
elif [ "$BUILD_DIR_IS_TEMP" == "1" ]; then
rm -rf "$BUILD_DIR"
fi
#
if [ "${#EXIT_MESSAGE}" != 0 ]; then
echo -e "$EXIT_MESSAGE" 1>&2
fi
exit $EXIT_CODE
}
function checkresult() {
if [ "$1" != "0" ]; then
if [ "${#2}" != "0" ]; then
EXIT_MESSAGE="$("$SCRIPTS_DIR/printformat.sh" "foreground:red" "$2")"
fi
EXIT_CODE=$1
cleanup
fi
}
function createlogfile() {
if [ ! -d "$BUILD_DIR/Logs" ]; then
mkdir -p "$BUILD_DIR/Logs"
fi
local LOG="$BUILD_DIR/Logs/$1.log"
touch "$LOG"
echo "$LOG"
}
function errormessage() {
local ERROR_MESSAGE=""
if [[ "$NO_CLEAN" == "1" ]] || [[ "$NO_CLEAN_ON_FAIL" == "1" ]]; then
ERROR_MESSAGE="$("$SCRIPTS_DIR/printformat.sh" "foreground:default" "Build Failed. See xcodebuild log for more details: $("$SCRIPTS_DIR/printformat.sh" "foreground:yellow" "$1")")"
elif [ "$VERBOSE" != "1" ]; then
ERROR_MESSAGE="$("$SCRIPTS_DIR/printformat.sh" "foreground:default" "Build Failed. Use the '--no-clean' or '--no-clean-on-fail' flag to inspect the logs.")"
fi
echo "$ERROR_MESSAGE"
}
# Build Platforms
cd "$ROOT_DIR"
#
for PLATFORM in "iOS" "iOS Simulator" "Mac Catalyst" "macOS" "tvOS" "tvOS Simulator" "watchOS" "watchOS Simulator"; do
echo -e "$("$SCRIPTS_DIR/printformat.sh" "foreground:blue" "***") Building $("$SCRIPTS_DIR/printformat.sh" "foreground:green" "$PLATFORM") architecture(s) of $("$SCRIPTS_DIR/printformat.sh" "bold" "${PROJECT_NAME}.xcodeproj")"
SCHEME="${PROJECT_NAME}"
ARCHIVE=""
ARCHS=""
case "$PLATFORM" in
"iOS")
ARCHS="armv7 armv7s arm64 arm64e"
ARCHIVE="iphoneos"
;;
"iOS Simulator")
ARCHS="i386 x86_64 arm64"
ARCHIVE="iphonesimulator"
;;
"Mac Catalyst")
PLATFORM="macOS,variant=Mac Catalyst"
ARCHS="x86_64 arm64 arm64e"
ARCHIVE="maccatalyst"
;;
"macOS")
SCHEME="${PROJECT_NAME} macOS"
ARCHS="x86_64 arm64 arm64e"
ARCHIVE="macos"
;;
"tvOS")
SCHEME="${PROJECT_NAME} tvOS"
ARCHS="arm64 arm64e"
ARCHIVE="appletvos"
;;
"tvOS Simulator")
SCHEME="${PROJECT_NAME} tvOS"
ARCHS="x86_64 arm64"
ARCHIVE="appletvsimulator"
;;
"watchOS")
SCHEME="${PROJECT_NAME} watchOS"
ARCHS="arm64_32 armv7k"
ARCHIVE="watchos"
;;
"watchOS Simulator")
SCHEME="${PROJECT_NAME} watchOS"
ARCHS="i386 x86_64 arm64"
ARCHIVE="watchsimulator"
;;
esac
ERROR_MESSAGE=""
#
if [ "$VERBOSE" == "1" ]; then
xcodebuild -project "${PROJECT_NAME}.xcodeproj" -scheme "$SCHEME" -destination "generic/platform=$PLATFORM" -archivePath "${BUILD_DIR}/$ARCHIVE.xcarchive" -configuration ${CONFIGURATION} SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES ONLY_ACTIVE_ARCH=NO ARCHS="$ARCHS" "${BUILD_ARGS[@]}" archive
else
LOG="$(createlogfile "$ARCHIVE-build")"
ERROR_MESSAGE="$(errormessage "$LOG")"
#
xcodebuild -project "${PROJECT_NAME}.xcodeproj" -scheme "$SCHEME" -destination "generic/platform=$PLATFORM" -archivePath "${BUILD_DIR}/$ARCHIVE.xcarchive" -configuration ${CONFIGURATION} SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES ONLY_ACTIVE_ARCH=NO ARCHS="$ARCHS" "${BUILD_ARGS[@]}" archive > "$LOG" 2>&1
fi
checkresult $? "$ERROR_MESSAGE"
done
# Make XCFramework
if [[ -d "${OUTPUT}" ]]; then
rm -rf "${OUTPUT}"
fi
ARGUMENTS=(-create-xcframework -output "${OUTPUT}")
for ARCHIVE in ${BUILD_DIR}/*.xcarchive; do
ARGUMENTS=(${ARGUMENTS[@]} -framework "${ARCHIVE}/Products/Library/Frameworks/${PROJECT_NAME}.framework")
if [ "$EXCLUDE_DSYMS" != "1" ]; then
if [[ -d "${ARCHIVE}/dSYMs/${PROJECT_NAME}.framework.dSYM" ]]; then
ARGUMENTS=(${ARGUMENTS[@]} -debug-symbols "${ARCHIVE}/dSYMs/${PROJECT_NAME}.framework.dSYM")
fi
if [[ -d "${ARCHIVE}/BCSymbolMaps" ]]; then
for SYMBOLMAP in ${ARCHIVE}/BCSymbolMaps/*.bcsymbolmap; do
ARGUMENTS=(${ARGUMENTS[@]} -debug-symbols "${SYMBOLMAP}")
done
fi
fi
done
#
echo -e "$("$SCRIPTS_DIR/printformat.sh" "foreground:blue" "***") Generating final XCFramework"
LOG="$(createlogfile "create-xcframework")"
ERROR_MESSAGE="$(errormessage "$LOG")"
xcodebuild "${ARGUMENTS[@]}"
checkresult $? "$ERROR_MESSAGE"
# Cleanup
cleanup