-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlame_xc.sh
executable file
·451 lines (355 loc) · 10.4 KB
/
lame_xc.sh
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/bin/bash
minVersionIos="12.0"
minVersionTvOS="12.0"
minVersionMacOS="10.14"
bundleVersion="1.2.6"
# Function for logging info with a timestamp
log() {
local message="$1" # Message to be logged
# Formatting current date and time
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
# Green color
local green='\033[1;32m'
# Reset color
local reset='\033[0m'
# Output the step log in the format: [Date and time] Step: Step name
echo -e "${green}[$timestamp]${reset}: $message"
}
# Function for logging details without a timestamp
info() {
local message="$1" # Message to be logged
# Formatting current date and time
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
# Green color
local green='\033[1;32m'
# Yellow color
local yellow='\033[1;33m'
# Reset color
local reset='\033[0m'
# Output the log message in the format: [Date and time] Message
echo -e "${green}[$timestamp]${reset}: ${yellow}$message${reset}"
}
# Initialize global variables
initialize_global_variables() {
log "Initializing global variables"
# Set the version of LAME to be used
local lame_version="3.100"
LAME_FILE="lame-$lame_version.tar.gz"
LAME_DIR="lame-$lame_version"
SOURCE=$LAME_DIR
FAT=".fat-lame"
SCRATCH=".scratch-lame"
THIN="$(pwd)/.thin-lame"
CONFIGURE_FLAGS="--disable-shared --disable-frontend --disable-debug --disable-dependency-tracking"
}
# Remove previous LAME build
remove_lame() {
log "Removing previous LAME"
rm -rf $LAME_DIR
}
# Download LAME if not present
download_lame() {
log "Downloading LAME"
if ! [ -f $LAME_FILE ]; then
echo "downloading $LAME_FILE ..."
curl -o $LAME_FILE https://altushost-swe.dl.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz?viasf=1
if [ $? != 0 ]; then
echo "downloading $LAME_FILE error ..."
exit -1
else
echo "downloading $LAME_FILE done ..."
fi
fi
# Verify file integrity
file_type=$(file -b --mime-type $LAME_FILE)
if [ "$file_type" != "application/gzip" ]; then
echo "Error: $LAME_FILE is not a valid gzip file."
rm -f $LAME_FILE
exit 1
fi
}
# Extract LAME tarball
extract_lame_tarball() {
log "Extracting tarball"
if ! tar xf "$LAME_FILE"; then
echo "Error extracting $LAME_FILE"
rm -rf "$LAME_FILE" "$LAME_DIR"
exit 1
fi
}
# Compile function for Mac Catalyst
compile_mac_catalyst() {
local cwd=$(pwd) # Current working directory
local arch=$1 # Architecture
local host=$2 # Host
local platform=$3 # Platform
local target="-target ${arch}-apple-ios14.0-macabi" # Target for Mac Catalyst
info "Compiling for $arch"
# Setup environment variables for compilation
local sdk=$(echo $platform | tr '[:upper:]' '[:lower:]')
local cc="xcrun -sdk $sdk clang -arch $arch"
local cflag="$target"
local cxxflags="$cflag"
local ldflags="$cflag"
# Create scratch directory and move to it
mkdir -p "$SCRATCH/$arch"
cd "$SCRATCH/$arch"
# Configure and compile
CC=$cc $cwd/$SOURCE/configure \
$CONFIGURE_FLAGS \
--host=$host \
--prefix="$THIN/$arch" \
CC="$cc" CFLAGS="$cflag" LDFLAGS="$ldflags"
# Install the compiled files
make -j3 -s install
# Return to the previous directory
cd $cwd
}
# Generic compile function
# $1-arch $2-host $3- platform
compile() {
local cwd=$(pwd) # Current working directory
local arch=$1 # Architecture
local host=$2 # Host
local platform=$3 # Platform
info "Compiling for $arch"
# Determine deployment target based on platform
min_os_version=""
deployment_target=""
if [ $platform == "iPhoneOS" ]; then
deployment_target=$minVersionIos
elif [ $platform == "iPhoneSimulator" ]; then
deployment_target=$minVersionIos
elif [ $platform == "AppleTVOS" ]; then
deployment_target=$minVersionTvOS
elif [ $platform == "AppleTVSimulator" ]; then
deployment_target=$minVersionTvOS
elif [ $platform == "MacOSX" ]; then
deployment_target=$minVersionMacOS
else
min_os_version=""
fi
# Set minimum OS version flag
if [ $platform == "iPhoneOS" ]; then
min_os_version="-mios-version-min=$deployment_target"
elif [ $platform == "iPhoneSimulator" ]; then
min_os_version="-mios-simulator-version-min=$deployment_target"
elif [ $platform == "AppleTVOS" ]; then
min_os_version="-mtvos-version-min=$deployment_target"
elif [ $platform == "AppleTVSimulator" ]; then
min_os_version="-mtvos-simulator-version-min=$deployment_target"
elif [ $platform == "MacOSX" ]; then
min_os_version="-mmacos-version-min=$deployment_target"
else
min_os_version=""
fi
# Setup environment variables for compilation
local sdk=$(echo $platform | tr '[:upper:]' '[:lower:]')
local cc="xcrun -sdk $sdk clang -arch $arch $min_os_version"
local cflag="-arch $arch"
local cxxflags="$cflag"
local ldflags="$cflag"
# Create scratch directory and move to it
mkdir -p "$SCRATCH/$arch"
cd "$SCRATCH/$arch"
# Configure and compile
CC=$cc $cwd/$SOURCE/configure \
$CONFIGURE_FLAGS \
--host=$host \
--prefix="$THIN/$arch" \
CC="$cc" CFLAGS="$cflag" LDFLAGS="$ldflags"
# Install the compiled files
make -j3 -s install
# Return to the previous directory
cd $cwd
}
# Make universal libraries for the specified architectures
make_universal_libs_for_archs() {
local cwd=$(pwd) # Current working directory
local archs=$1 # Architectures
info "Building universal library for architectures: $archs"
# Create FAT directory
mkdir -p $FAT/lib
# Split architectures into an array
set - $archs
cd $THIN/$1/lib
# Create universal libraries
for lib in *.a; do
cd $cwd
lipo -create $(find $THIN -name $lib) -output $FAT/lib/$lib
info "Universal library: $lib created"
done
cd $cwd
# Copy include files
cp -rf $THIN/$1/include $FAT
}
# Make framework function
make_framework() {
local name=$1 # Framework name
local min_os_version=$2 # Minimum OS version
local framework="$name/lame.framework" # Framework directory
# Remove existing framework
rm -rf $framework
# Create framework directories
mkdir -p $framework/Headers/
mkdir -p $framework/Modules/
# Create module map
echo "framework module lame { header \"lame.h\" export * }" >$framework/Modules/module.modulemap
# Copy headers and library
cp -rf $FAT/include/lame/* $framework/Headers/
cp -f $FAT/lib/libmp3lame.a $framework/lame
# Create Info.plist
cat << EOF > $framework/Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>com.pro100andrey.lame</string>
<key>CFBundleName</key>
<string>lame</string>
<key>CFBundleVersion</key>
<string>$bundleVersion</string>
<key>CFBundleShortVersionString</key>
<string>$bundleVersion</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleExecutable</key>
<string>lame</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>MinimumOSVersion</key>
<string>100.0</string>
</dict>
</plist>
EOF
# Clean up temporary directories
rm -rf $FAT $THIN $SCRATCH
}
# Make xcframework function
make_xcframework() {
info "Building xcframework"
# Remove existing xcframework
if [ -d "lame.xcframework" ]; then
info "Removing previous xcframework"
rm -rf lame.xcframework
fi
# Initialize xcodebuild arguments
local xcodebuild_args=("-verbose" "-create-xcframework")
# Add each framework to the arguments
for arg in "$@"; do
xcodebuild_args+=("-framework" "${arg}/lame.framework")
done
# Set the output path
xcodebuild_args+=("-output" "lame.xcframework")
# Create the xcframework
xcodebuild "${xcodebuild_args[@]}"
# Clean up frameworks
for arg in "$@"; do
rm -rf "${arg}"
done
}
# Clean function
clean() {
rm -rf $SOURCE $FAT $SCRATCH $THIN $LAME_FILE
}
# Build iOS Simulator framework
make_ios_simulator_framework() {
local name=$1
# Compile for arm64 and x86_64 architectures
compile arm64 arm-apple-darwin "iPhoneSimulator"
compile x86_64 x86_64-apple-darwin "iPhoneSimulator"
# Create universal libraries
make_universal_libs_for_archs "arm64 x86_64"
# Create the framework
make_framework $name $minVersionIos
}
# Build tvOS Simulator framework
make_tvos_simulator_framework() {
local name=$1
# Compile for arm64 and x86_64 architectures
compile arm64 arm-apple-darwin "AppleTVSimulator"
compile x86_64 x86_64-apple-darwin "AppleTVSimulator"
# Create universal libraries
make_universal_libs_for_archs "arm64 x86_64"
# Create the framework
make_framework $name $minVersionTvOS
}
# Build tvOS framework
make_tvos_framework() {
local name=$1
# Compile for arm64 architecture
compile arm64 arm-apple-darwin "AppleTVOS"
# Create universal libraries
make_universal_libs_for_archs arm64
# Create the framework
make_framework $name $minVersionTvOS
}
# Build iOS framework
make_ios_framework() {
local name=$1
# Compile for arm64 architecture
compile arm64 arm-apple-darwin "iPhoneOS"
# Create universal libraries
make_universal_libs_for_archs arm64
# Create the framework
make_framework $name $minVersionIos
}
# Build macOS framework
make_macos_framework() {
local name=$1
# Compile for arm64 and x86_64 architectures
compile arm64 arm-apple-darwin "MacOSX"
compile x86_64 x86_64-apple-darwin "MacOSX"
# Create universal libraries
make_universal_libs_for_archs "arm64 x86_64"
# Create the framework
make_framework $name $minVersionMacOS
}
# Build Mac Catalyst framework
make_maccatalist_framework() {
local name=$1
# Compile for arm64 and x86_64 architectures
compile_mac_catalyst arm64 arm-apple-darwin "MacOSX"
compile_mac_catalyst x86_64 x86_64-apple-darwin "MacOSX"
# Create universal libraries
make_universal_libs_for_archs "arm64 x86_64"
# Create the framework
make_framework $name $minVersionMacOS
}
# Build all frameworks
make_framewokrs() {
local ios="ios-arm64"
local ios_simulator="ios-arm64_x86_64-simulator"
local tvos="tvos-arm64"
local tvos_simulator="tvos-arm64_x86_64-simulator"
local macos="macos-arm64_x86_64"
local maccatalyst="maccatalyst-arm64_x86_64"
# Build individual frameworks
make_ios_framework $ios
make_ios_simulator_framework $ios_simulator
make_tvos_framework $tvos
make_tvos_simulator_framework $tvos_simulator
make_macos_framework $macos
make_maccatalist_framework $maccatalyst
# Create xcframework
make_xcframework $ios $ios_simulator \
$tvos $tvos_simulator \
$macos $maccatalyst
}
# Main execution
main() {
# Initialize global variables
initialize_global_variables
# Remove previous build
remove_lame
# Download and extract LAME
download_lame
extract_lame_tarball
# Build frameworks
make_framewokrs
# Clean up
clean
}
# Execute the main function
main