-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindows-build-sbagenx.sh
More file actions
executable file
·1126 lines (989 loc) · 36.3 KB
/
Copy pathwindows-build-sbagenx.sh
File metadata and controls
executable file
·1126 lines (989 loc) · 36.3 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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# SBaGenX Windows build script
# Builds 32-bit and 64-bit Windows binaries with FLAC, MP3 and OGG support using MinGW
# Source common library
. ./lib.sh
section_header "Building SBaGenX for Windows (32-bit and 64-bit) with FLAC, MP3 and OGG support..."
AUTO_INSTALL_DEPS="${SBAGENX_AUTO_INSTALL_DEPS:-1}"
MSYS_REPO=""
prepend_path_dir() {
local dir="$1"
case ":$PATH:" in
*":$dir:"*) ;;
*) PATH="$dir:$PATH" ;;
esac
}
augment_msys_toolchain_path() {
[ -d "/usr/bin" ] && prepend_path_dir "/usr/bin"
case "$MSYS_REPO" in
ucrt64)
[ -d "/ucrt64/bin" ] && prepend_path_dir "/ucrt64/bin"
;;
mingw64)
[ -d "/mingw64/bin" ] && prepend_path_dir "/mingw64/bin"
;;
mingw32)
[ -d "/mingw32/bin" ] && prepend_path_dir "/mingw32/bin"
;;
clang64)
[ -d "/clang64/bin" ] && prepend_path_dir "/clang64/bin"
;;
clang32)
[ -d "/clang32/bin" ] && prepend_path_dir "/clang32/bin"
;;
clangarm64)
[ -d "/clangarm64/bin" ] && prepend_path_dir "/clangarm64/bin"
;;
esac
[ -d "/mingw32/bin" ] && prepend_path_dir "/mingw32/bin"
[ -d "/mingw64/bin" ] && prepend_path_dir "/mingw64/bin"
[ -d "/ucrt64/bin" ] && prepend_path_dir "/ucrt64/bin"
hash -r
}
resolve_msys_repo() {
case "${MSYSTEM:-}" in
UCRT64) echo "ucrt64" ;;
MINGW64) echo "mingw64" ;;
MINGW32) echo "mingw32" ;;
CLANG64) echo "clang64" ;;
CLANG32) echo "clang32" ;;
CLANGARM64) echo "clangarm64" ;;
*)
echo ""
;;
esac
}
pacman_install() {
local pkg="$1"
if ! command -v pacman >/dev/null 2>&1; then
return 1
fi
info "Installing ${pkg} with pacman..."
pacman -Sy --noconfirm --needed "$pkg" >/dev/null 2>&1 || return 1
hash -r
return 0
}
resolve_msys_package_for_command() {
local cmd="$1"
case "$cmd" in
x86_64-w64-mingw32-gcc|x86_64-w64-mingw32-ar|x86_64-w64-mingw32-gcc-ar|x86_64-w64-mingw32-windres)
case "$MSYS_REPO" in
ucrt64) echo "mingw-w64-ucrt-x86_64-gcc" ;;
mingw64) echo "mingw-w64-x86_64-gcc" ;;
*)
echo ""
;;
esac
;;
i686-w64-mingw32-gcc|i686-w64-mingw32-ar|i686-w64-mingw32-gcc-ar|i686-w64-mingw32-windres)
echo "mingw-w64-i686-gcc"
;;
*)
echo ""
;;
esac
}
ensure_command_available() {
local cmd="$1"
local pacman_pkg=""
if command -v "$cmd" >/dev/null 2>&1; then
return 0
fi
if [ "$AUTO_INSTALL_DEPS" = "0" ]; then
return 1
fi
pacman_pkg="$(resolve_msys_package_for_command "$cmd")"
if [ -n "$pacman_pkg" ] && pacman_install "$pacman_pkg"; then
if command -v "$cmd" >/dev/null 2>&1; then
return 0
fi
fi
return 1
}
MSYS_REPO="$(resolve_msys_repo)"
augment_msys_toolchain_path
REQUESTED_WINDOWS_ARCHES="${SBAGENX_WINDOWS_ARCHES:-win32,win64}"
BUILD_WIN32=0
BUILD_WIN64=0
case ",${REQUESTED_WINDOWS_ARCHES}," in
*,win32,*) BUILD_WIN32=1 ;;
esac
case ",${REQUESTED_WINDOWS_ARCHES}," in
*,win64,*) BUILD_WIN64=1 ;;
esac
if [ "$BUILD_WIN32" -eq 0 ] && [ "$BUILD_WIN64" -eq 0 ]; then
error "No valid Windows architectures requested via SBAGENX_WINDOWS_ARCHES=${REQUESTED_WINDOWS_ARCHES}"
info "Supported values include: win32, win64, or win32,win64"
exit 1
fi
# Check for MinGW cross-compilers
if [ "$BUILD_WIN32" -eq 1 ] && ! ensure_command_available i686-w64-mingw32-gcc; then
error "32-bit MinGW cross-compiler not found. Please install it."
info "On Debian/Ubuntu: sudo apt-get install mingw-w64"
info "On Fedora: sudo dnf install mingw32-gcc"
info "On Arch/MSYS2: sudo pacman -S mingw-w64-i686-gcc"
exit 1
fi
if [ "$BUILD_WIN64" -eq 1 ] && ! ensure_command_available x86_64-w64-mingw32-gcc; then
error "64-bit MinGW cross-compiler not found. Please install it."
info "On Debian/Ubuntu: sudo apt-get install mingw-w64"
info "On Fedora: sudo dnf install mingw64-gcc"
info "On Arch/MSYS2: sudo pacman -S mingw-w64-x86_64-gcc"
exit 1
fi
resolve_mingw_ar() {
local target="$1"
local ar_path=""
ar_path=$(command -v "${target}-ar" 2>/dev/null || true)
if [ -n "$ar_path" ]; then
echo "$ar_path"
return 0
fi
ar_path=$(command -v "${target}-gcc-ar" 2>/dev/null || true)
if [ -n "$ar_path" ]; then
echo "$ar_path"
return 0
fi
return 1
}
AR_32=""
AR_64=""
if [ "$BUILD_WIN32" -eq 1 ]; then
AR_32=$(resolve_mingw_ar "i686-w64-mingw32" || true)
if [ -z "$AR_32" ]; then
error "32-bit MinGW archiver tools not found."
warning "Expected one of: i686-w64-mingw32-ar or i686-w64-mingw32-gcc-ar"
exit 1
fi
if [ "$(basename "$AR_32")" != "i686-w64-mingw32-ar" ]; then
warning "Using fallback archiver for win32: $(basename "$AR_32")"
fi
fi
if [ "$BUILD_WIN64" -eq 1 ]; then
AR_64=$(resolve_mingw_ar "x86_64-w64-mingw32" || true)
if [ -z "$AR_64" ]; then
error "64-bit MinGW archiver tools not found."
warning "Expected one of: x86_64-w64-mingw32-ar or x86_64-w64-mingw32-gcc-ar"
exit 1
fi
if [ "$(basename "$AR_64")" != "x86_64-w64-mingw32-ar" ]; then
warning "Using fallback archiver for win64: $(basename "$AR_64")"
fi
fi
# Create libs directory if it doesn't exist
create_dir_if_not_exists "libs"
# Check distribution directory
create_dir_if_not_exists "dist"
# Prefer original icon if present; keep fallback for older checkouts.
ICON_PATH_WINDOWS_RESOURCE=""
if [ -f "assets/sbagen.ico" ]; then
ICON_PATH_WINDOWS_RESOURCE="assets/sbagen.ico"
elif [ -f "assets/sbagenx.ico" ]; then
ICON_PATH_WINDOWS_RESOURCE="assets/sbagenx.ico"
else
error "No Windows icon found (expected assets/sbagen.ico or assets/sbagenx.ico)"
fi
info "Embedding executable icon: ${ICON_PATH_WINDOWS_RESOURCE}"
# Get version from VERSION file
VERSION=$(cat VERSION)
# Extract numeric version and build number for RC file
NUMERIC_VERSION=$(echo $VERSION | sed 's/-.*$//')
BUILD_DATE=$(echo $VERSION | sed -n 's/.*-dev\.\([0-9]\{8\}\)\..*$/\1/p')
BUILD_NUMBER="0"
if [ ! -z "$BUILD_DATE" ]; then
# Use last 4 digits of date as build number (avoid overflow)
BUILD_NUMBER=$(echo $BUILD_DATE | tail -c 5) # Gets "0606"
fi
# Create version for RC file (format: major,minor,patch,build)
VERSION_RC=$(echo $NUMERIC_VERSION | sed 's/\./,/g'),$BUILD_NUMBER
# Create resource file with version information
cat > /tmp/sbagen.rc << EOF
#include <windows.h>
// Include icon
1 ICON "${ICON_PATH_WINDOWS_RESOURCE}"
VS_VERSION_INFO VERSIONINFO
FILEVERSION $VERSION_RC
PRODUCTVERSION $VERSION_RC
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
FILEFLAGS 0
FILEOS VOS__WINDOWS32
FILETYPE VFT_APP
FILESUBTYPE 0
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904E4"
BEGIN
VALUE "CompanyName", "SBaGenX"
VALUE "FileDescription", "SBaGenX Brainwave Generator"
VALUE "FileVersion", "$VERSION"
VALUE "InternalName", "sbagenx"
VALUE "LegalCopyright", "GPLv2"
VALUE "OriginalFilename", "sbagenx.exe"
VALUE "ProductName", "SBaGenX"
VALUE "ProductVersion", "$VERSION"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1252
END
END
EOF
# Compile resource file for requested architectures
if [ "$BUILD_WIN32" -eq 1 ]; then
if ! ensure_command_available i686-w64-mingw32-windres; then
error "32-bit MinGW resource compiler not found."
exit 1
fi
i686-w64-mingw32-windres /tmp/sbagen.rc -O coff -o /tmp/sbagen32.res
fi
if [ "$BUILD_WIN64" -eq 1 ]; then
if ! ensure_command_available x86_64-w64-mingw32-windres; then
error "64-bit MinGW resource compiler not found."
exit 1
fi
x86_64-w64-mingw32-windres /tmp/sbagen.rc -O coff -o /tmp/sbagen64.res
fi
# Define paths for libraries
LIB_PATH_32="libs/windows-win32-libmad.a"
LIB_PATH_64="libs/windows-win64-libmad.a"
OGG_LIB_PATH_32="libs/windows-win32-libogg.a"
OGG_LIB_PATH_64="libs/windows-win64-libogg.a"
TREMOR_LIB_PATH_32="libs/windows-win32-libvorbisidec.a"
TREMOR_LIB_PATH_64="libs/windows-win64-libvorbisidec.a"
SNDFILE_STATIC_LIB_PATH_32="libs/windows-win32-libsndfile.a"
SNDFILE_STATIC_LIB_PATH_64="libs/windows-win64-libsndfile.a"
LAME_STATIC_LIB_PATH_32="libs/windows-win32-libmp3lame.a"
LAME_STATIC_LIB_PATH_64="libs/windows-win64-libmp3lame.a"
RUNTIME_DLL_DIR_32="libs/windows-win32-runtime"
RUNTIME_DLL_DIR_64="libs/windows-win64-runtime"
PY_RUNTIME_DIR_32="libs/windows-win32-python-runtime"
PY_RUNTIME_DIR_64="libs/windows-win64-python-runtime"
PY_RUNTIME_ARCHIVE_32_DEFAULT="libs/windows-win32-python-runtime.zip"
PY_RUNTIME_ARCHIVE_64_DEFAULT="libs/windows-win64-python-runtime.zip"
PLOT_SCRIPT_SRC="scripts/sbagenx_plot.py"
PY_RUNTIME_CACHE_DIR="build/python-runtime-cache"
PY_RUNTIME_REQUIRED="${SBAGENX_REQUIRE_PY_RUNTIME:-1}"
PY_RUNTIME_AUTO_PREPARE="${SBAGENX_AUTO_PREPARE_PY_RUNTIME:-1}"
PY_EMBED_VERSION="${SBAGENX_PY_EMBED_VERSION:-3.13.12}"
PYCAIRO_VERSION="${SBAGENX_PYCAIRO_VERSION:-1.29.0}"
STATIC_ENCODERS_32=0
STATIC_ENCODERS_64=0
find_mingw_dll() {
local target="$1"
shift
local gcc_bin="${target}-gcc"
local sysroot=""
local gcc_path=""
local gcc_dir=""
local mingw_prefix=""
local dll
local candidate
case "$target" in
i686-w64-mingw32) mingw_prefix="mingw32" ;;
x86_64-w64-mingw32) mingw_prefix="mingw64" ;;
*) mingw_prefix="" ;;
esac
if command -v "$gcc_bin" >/dev/null 2>&1; then
gcc_path=$(command -v "$gcc_bin" 2>/dev/null)
gcc_dir=$(dirname "$gcc_path")
sysroot=$($gcc_bin -print-sysroot 2>/dev/null)
fi
for dll in "$@"; do
for candidate in \
"${gcc_dir}/${dll}" \
"${gcc_dir}/../bin/${dll}" \
"/${mingw_prefix}/bin/${dll}" \
"/usr/${target}/bin/${dll}" \
"/usr/${target}/lib/${dll}" \
"${sysroot}/bin/${dll}" \
"${sysroot}/usr/bin/${dll}" \
"${sysroot}/${target}/bin/${dll}"; do
if [ -n "$candidate" ] && [ -f "$candidate" ]; then
echo "$candidate"
return 0
fi
done
if [ -n "$mingw_prefix" ]; then
candidate=$(find "/${mingw_prefix}" -maxdepth 7 -type f -name "$dll" 2>/dev/null | head -n 1)
if [ -n "$candidate" ] && [ -f "$candidate" ]; then
echo "$candidate"
return 0
fi
fi
candidate=$(find /usr -maxdepth 7 -type f -name "$dll" -path "*${target}*" 2>/dev/null | head -n 1)
if [ -n "$candidate" ] && [ -f "$candidate" ]; then
echo "$candidate"
return 0
fi
done
return 1
}
copy_runtime_dll() {
local arch_tag="$1"
local target="$2"
local output_name="$3"
shift 3
local src
local dll
local manual_dir=""
local project_dir=""
if [ "$arch_tag" = "win32" ]; then
manual_dir="${SBAGENX_WIN32_DLL_DIR}"
project_dir="${RUNTIME_DLL_DIR_32}"
elif [ "$arch_tag" = "win64" ]; then
manual_dir="${SBAGENX_WIN64_DLL_DIR}"
project_dir="${RUNTIME_DLL_DIR_64}"
fi
if [ -n "$manual_dir" ]; then
for dll in "$@"; do
if [ -f "${manual_dir}/${dll}" ]; then
src="${manual_dir}/${dll}"
break
fi
done
fi
if [ -z "$src" ] && [ -n "$project_dir" ]; then
for dll in "$@"; do
if [ -f "${project_dir}/${dll}" ]; then
src="${project_dir}/${dll}"
break
fi
done
fi
if [ -z "$src" ]; then
src=$(find_mingw_dll "$target" "$@")
fi
if [ -n "$src" ] && [ -f "$src" ]; then
cp "$src" "dist/${output_name}-${arch_tag}.dll"
success "Bundled runtime DLL: dist/${output_name}-${arch_tag}.dll"
else
warning "Could not find ${output_name} runtime DLL for ${target}; installer will rely on system-provided runtime"
warning "Hint: add DLLs under ${project_dir}/, or set SBAGENX_${arch_tag^^}_DLL_DIR to a folder containing one of: $*"
fi
}
download_file_if_missing() {
local url="$1"
local out_path="$2"
local tmp_path="${out_path}.tmp.$$"
if [ -f "$out_path" ]; then
return 0
fi
mkdir -p "$(dirname "$out_path")"
info "Downloading: ${url}"
if command -v curl >/dev/null 2>&1; then
if ! curl -fL --retry 3 --retry-delay 2 -o "$tmp_path" "$url"; then
rm -f "$tmp_path"
return 1
fi
elif command -v wget >/dev/null 2>&1; then
if ! wget -O "$tmp_path" "$url"; then
rm -f "$tmp_path"
return 1
fi
else
warning "Cannot download ${url}: neither curl nor wget is available"
return 1
fi
mv "$tmp_path" "$out_path"
return 0
}
extract_zip_file() {
local archive="$1"
local dest_dir="$2"
mkdir -p "$dest_dir"
if command -v unzip >/dev/null 2>&1; then
unzip -q -o "$archive" -d "$dest_dir"
return $?
fi
if command -v bsdtar >/dev/null 2>&1; then
bsdtar -xf "$archive" -C "$dest_dir"
return $?
fi
# Fallback for minimal CI environments where unzip/bsdtar are unavailable.
if command -v python3 >/dev/null 2>&1; then
python3 - "$archive" "$dest_dir" <<'PY'
import sys
import zipfile
archive = sys.argv[1]
dest = sys.argv[2]
with zipfile.ZipFile(archive, "r") as zf:
zf.extractall(dest)
PY
return $?
fi
if command -v python >/dev/null 2>&1; then
python - "$archive" "$dest_dir" <<'PY'
import sys
import zipfile
archive = sys.argv[1]
dest = sys.argv[2]
with zipfile.ZipFile(archive, "r") as zf:
zf.extractall(dest)
PY
return $?
fi
warning "Cannot extract ${archive}: need unzip, bsdtar, or python with zipfile support"
return 1
}
find_pycairo_wheel_url() {
local wheel_name="$1"
local simple_html=""
local wheel_url=""
if command -v curl >/dev/null 2>&1; then
if ! simple_html="$(curl -fsSL https://pypi.org/simple/pycairo/)"; then
return 1
fi
elif command -v wget >/dev/null 2>&1; then
if ! simple_html="$(wget -q -O - https://pypi.org/simple/pycairo/)"; then
return 1
fi
else
return 1
fi
wheel_url="$(printf '%s' "$simple_html" \
| tr '"' '\n' \
| grep -F "$wheel_name" \
| grep -E '^https?://' \
| head -n 1 \
| sed 's/#.*$//')"
if [ -z "$wheel_url" ]; then
return 1
fi
printf '%s\n' "$wheel_url"
return 0
}
configure_embedded_python_pth() {
local runtime_dir="$1"
local py_mm="$2"
local pth_file="${runtime_dir}/python${py_mm}._pth"
local py_zip=""
if [ ! -f "$pth_file" ]; then
pth_file="$(find "$runtime_dir" -maxdepth 1 -type f -name 'python*._pth' | head -n 1)"
fi
if [ -z "$pth_file" ] || [ ! -f "$pth_file" ]; then
warning "Could not find embedded Python ._pth file in ${runtime_dir}"
return 1
fi
py_zip="$(find "$runtime_dir" -maxdepth 1 -type f -name "python${py_mm}.zip" | head -n 1)"
if [ -z "$py_zip" ]; then
py_zip="$(find "$runtime_dir" -maxdepth 1 -type f -name 'python*.zip' | head -n 1)"
fi
if [ -z "$py_zip" ]; then
warning "Could not find embedded stdlib zip in ${runtime_dir}"
return 1
fi
py_zip="$(basename "$py_zip")"
cat > "$pth_file" << EOF
${py_zip}
.
Lib
Lib/site-packages
import site
EOF
return 0
}
python_runtime_tree_valid() {
local dir="$1"
if [ ! -f "${dir}/python.exe" ]; then
return 1
fi
if [ ! -d "${dir}/Lib/site-packages/cairo" ]; then
return 1
fi
if ! find "${dir}/Lib/site-packages/cairo" -maxdepth 1 -type f -name '_cairo*.pyd' | grep -q .; then
return 1
fi
return 0
}
auto_prepare_python_runtime_tree() {
local arch_tag="$1"
local target_dir="$2"
local embed_arch=""
local wheel_arch=""
local py_mm=""
local py_tag=""
local embed_name=""
local embed_url=""
local embed_path=""
local wheel_name=""
local wheel_path=""
local wheel_url=""
case "$arch_tag" in
win32)
embed_arch="win32"
wheel_arch="win32"
;;
win64)
embed_arch="amd64"
wheel_arch="win_amd64"
;;
*)
warning "Unknown Python runtime arch tag: ${arch_tag}"
return 1
;;
esac
py_mm="$(printf '%s' "$PY_EMBED_VERSION" | awk -F. '{ printf "%d%d", $1, $2 }')"
if [ -z "$py_mm" ]; then
warning "Could not derive Python ABI tag from SBAGENX_PY_EMBED_VERSION=${PY_EMBED_VERSION}"
return 1
fi
py_tag="cp${py_mm}"
embed_name="python-${PY_EMBED_VERSION}-embed-${embed_arch}.zip"
embed_url="https://www.python.org/ftp/python/${PY_EMBED_VERSION}/${embed_name}"
wheel_name="pycairo-${PYCAIRO_VERSION}-${py_tag}-${py_tag}-${wheel_arch}.whl"
embed_path="${PY_RUNTIME_CACHE_DIR}/${embed_name}"
wheel_path="${PY_RUNTIME_CACHE_DIR}/${wheel_name}"
if ! download_file_if_missing "$embed_url" "$embed_path"; then
warning "Failed to download embedded Python runtime: ${embed_url}"
return 1
fi
if [ ! -f "$wheel_path" ]; then
wheel_url="$(find_pycairo_wheel_url "$wheel_name")"
if [ -z "$wheel_url" ]; then
warning "Could not resolve pycairo wheel URL for ${wheel_name}"
return 1
fi
if ! download_file_if_missing "$wheel_url" "$wheel_path"; then
warning "Failed to download pycairo wheel: ${wheel_url}"
return 1
fi
fi
rm -rf "$target_dir"
mkdir -p "$target_dir"
if ! extract_zip_file "$embed_path" "$target_dir"; then
warning "Failed to extract embedded Python archive: ${embed_path}"
return 1
fi
mkdir -p "${target_dir}/Lib/site-packages"
if ! extract_zip_file "$wheel_path" "${target_dir}/Lib/site-packages"; then
warning "Failed to extract pycairo wheel: ${wheel_path}"
return 1
fi
if ! configure_embedded_python_pth "$target_dir" "$py_mm"; then
warning "Failed to configure embedded Python search path in ${target_dir}"
return 1
fi
cat > "${target_dir}/SBAGENX_PY_RUNTIME.txt" << EOF
SBaGenX auto-prepared Python+Cairo runtime
Python embed version: ${PY_EMBED_VERSION}
pycairo version: ${PYCAIRO_VERSION}
Architecture: ${arch_tag}
EOF
if ! python_runtime_tree_valid "$target_dir"; then
warning "Prepared runtime in ${target_dir} did not pass validation"
return 1
fi
success "Prepared Python+Cairo runtime for ${arch_tag} in ${target_dir}"
return 0
}
copy_python_runtime_tree() {
local arch_tag="$1"
local src_dir=""
local manual_dir=""
local project_dir=""
local out_dir=""
local archive_path=""
local archive_url=""
extract_archive_into_runtime_dir() {
local archive="$1"
local target_dir="$2"
local temp_root=""
local extracted_root=""
local py_candidate=""
temp_root="$(mktemp -d)"
rm -rf "$target_dir"
mkdir -p "$target_dir"
if [[ "$archive" == *.zip ]]; then
if ! extract_zip_file "$archive" "$temp_root"; then
warning "Failed to extract python runtime archive: $archive"
rm -rf "$temp_root"
return 1
fi
else
if command -v bsdtar >/dev/null 2>&1; then
if ! bsdtar -xf "$archive" -C "$temp_root"; then
warning "Failed to extract python runtime archive: $archive"
rm -rf "$temp_root"
return 1
fi
elif command -v tar >/dev/null 2>&1; then
if ! tar -xf "$archive" -C "$temp_root"; then
warning "Failed to extract python runtime archive: $archive"
rm -rf "$temp_root"
return 1
fi
else
warning "Cannot extract $archive: need bsdtar or tar"
rm -rf "$temp_root"
return 1
fi
fi
if [ -f "${temp_root}/python.exe" ]; then
extracted_root="$temp_root"
else
py_candidate="$(find "$temp_root" -type f -name 'python.exe' | head -n 1)"
if [ -n "$py_candidate" ]; then
extracted_root="$(dirname "$py_candidate")"
fi
fi
if [ -z "$extracted_root" ] || [ ! -f "${extracted_root}/python.exe" ]; then
warning "Archive $archive does not appear to contain a runtime root with python.exe"
rm -rf "$temp_root"
return 1
fi
cp -R "${extracted_root}/." "$target_dir/"
rm -rf "$temp_root"
return 0
}
if [ "$arch_tag" = "win32" ]; then
manual_dir="${SBAGENX_WIN32_PY_RUNTIME_DIR}"
project_dir="${PY_RUNTIME_DIR_32}"
archive_path="${SBAGENX_WIN32_PY_RUNTIME_ARCHIVE:-$PY_RUNTIME_ARCHIVE_32_DEFAULT}"
archive_url="${SBAGENX_WIN32_PY_RUNTIME_URL:-}"
elif [ "$arch_tag" = "win64" ]; then
manual_dir="${SBAGENX_WIN64_PY_RUNTIME_DIR}"
project_dir="${PY_RUNTIME_DIR_64}"
archive_path="${SBAGENX_WIN64_PY_RUNTIME_ARCHIVE:-$PY_RUNTIME_ARCHIVE_64_DEFAULT}"
archive_url="${SBAGENX_WIN64_PY_RUNTIME_URL:-}"
else
warning "Unknown python runtime arch tag: $arch_tag"
return 1
fi
if [ -n "$manual_dir" ] && python_runtime_tree_valid "$manual_dir"; then
src_dir="$manual_dir"
elif [ -d "$project_dir" ] && python_runtime_tree_valid "$project_dir"; then
src_dir="$project_dir"
fi
if [ -z "$src_dir" ]; then
if [ ! -f "$archive_path" ] && [ -n "$archive_url" ]; then
if ! download_file_if_missing "$archive_url" "$archive_path"; then
warning "Failed to download Python runtime archive for ${arch_tag} from ${archive_url}"
fi
fi
if [ -f "$archive_path" ]; then
info "Extracting ${arch_tag} Python+Cairo runtime archive: ${archive_path}"
if extract_archive_into_runtime_dir "$archive_path" "$project_dir" && python_runtime_tree_valid "$project_dir"; then
src_dir="$project_dir"
else
warning "Failed to prepare a valid ${arch_tag} Python runtime from archive ${archive_path}"
fi
fi
fi
if [ -z "$src_dir" ] && [ "$PY_RUNTIME_AUTO_PREPARE" = "1" ]; then
section_header "Auto-preparing ${arch_tag} Python+Cairo runtime..."
if auto_prepare_python_runtime_tree "$arch_tag" "$project_dir"; then
src_dir="$project_dir"
fi
fi
if [ -z "$src_dir" ]; then
warning "Python+Cairo runtime tree is missing for ${arch_tag}"
warning "Tried runtime dir (${project_dir}), archive path (${archive_path}), and auto-preparation"
if [ "$PY_RUNTIME_REQUIRED" = "1" ]; then
return 1
fi
return 0
fi
out_dir="dist/python-${arch_tag}"
rm -rf "$out_dir"
mkdir -p "$out_dir"
cp -R "${src_dir}/." "$out_dir/"
if ! python_runtime_tree_valid "$out_dir"; then
warning "Bundled runtime failed validation after copy: ${out_dir}"
if [ "$PY_RUNTIME_REQUIRED" = "1" ]; then
return 1
fi
fi
success "Bundled Python+Cairo runtime tree: ${out_dir}"
return 0
}
# Replace VERSION with the actual version number once for any requested arch.
sed "s/__VERSION__/\"$VERSION\"/" sbagenx.c > sbagenx.tmp.c
if [ $? -ne 0 ] || [ ! -f sbagenx.tmp.c ]; then
error "Failed to prepare temporary Windows source file: sbagenx.tmp.c"
rm -f /tmp/sbagen.rc /tmp/sbagen32.res /tmp/sbagen64.res sbagenx.tmp.c
exit 1
fi
# Remove stale outputs for the requested architectures so failures cannot be
# masked by old artifacts from earlier builds.
if [ "$BUILD_WIN32" -eq 1 ]; then
rm -f dist/sbagenx-win32.exe dist/libsbagenx-win32.a dist/libsbagenx-win32.dll.a dist/sbagenxlib-win32.dll
fi
if [ "$BUILD_WIN64" -eq 1 ]; then
rm -f dist/sbagenx-win64.exe dist/libsbagenx-win64.a dist/libsbagenx-win64.dll.a dist/sbagenxlib-win64.dll
fi
if [ "$BUILD_WIN32" -eq 1 ]; then
# Build 32-bit version
section_header "Building 32-bit version..."
# Set up compilation flags for 32-bit
CFLAGS_32="-DT_MINGW -DFLAC_DECODE -Wall -O3 -I. -Ilibs"
LIBS_32="-lwinmm"
# Check for MP3 support (32-bit)
if [ -f "$LIB_PATH_32" ]; then
info "Including MP3 support for 32-bit using: $LIB_PATH_32"
CFLAGS_32="$CFLAGS_32 -DMP3_DECODE"
LIBS_32="$LIBS_32 $LIB_PATH_32"
else
warning "MP3 library not found at $LIB_PATH_32"
warning "MP3 support will not be included in 32-bit build"
warning "Run ./windows-build-libs.sh to build the required libraries"
fi
# Check for OGG support (32-bit)
if [ -f "$OGG_LIB_PATH_32" ] && [ -f "$TREMOR_LIB_PATH_32" ]; then
info "Including OGG support for 32-bit using: $OGG_LIB_PATH_32 and $TREMOR_LIB_PATH_32"
CFLAGS_32="$CFLAGS_32 -DOGG_DECODE"
# Order is important: first tremor, then ogg
LIBS_32="$LIBS_32 $TREMOR_LIB_PATH_32 $OGG_LIB_PATH_32"
else
warning "OGG libraries not found at $OGG_LIB_PATH_32 or $TREMOR_LIB_PATH_32"
warning "OGG support will not be included in 32-bit build"
warning "Run ./windows-build-libs.sh to build the required libraries"
fi
# Optional static output encoder path (single-binary friendly)
if [ -f "$SNDFILE_STATIC_LIB_PATH_32" ] && [ -f "$LAME_STATIC_LIB_PATH_32" ]; then
info "Including static output encoders for 32-bit using: $SNDFILE_STATIC_LIB_PATH_32 and $LAME_STATIC_LIB_PATH_32"
CFLAGS_32="$CFLAGS_32 -DSTATIC_OUTPUT_ENCODERS"
LIBS_32="$LIBS_32 $SNDFILE_STATIC_LIB_PATH_32 $LAME_STATIC_LIB_PATH_32 ${SBAGENX_STATIC_ENCODER_DEPS_WIN32}"
STATIC_ENCODERS_32=1
else
warning "Static encoder libs not found for 32-bit at $SNDFILE_STATIC_LIB_PATH_32 or $LAME_STATIC_LIB_PATH_32"
warning "Falling back to runtime-loaded encoder DLLs for 32-bit output encoding"
fi
# Compile 32-bit version
info "Compiling 32-bit version with flags: $CFLAGS_32"
info "Libraries: $LIBS_32"
SBX_LIB_CFLAGS_32="$CFLAGS_32 -DSBAGENXLIB_VERSION=\"\\\"$VERSION\\\"\""
i686-w64-mingw32-gcc $SBX_LIB_CFLAGS_32 -shared sbagenxlib.c \
-Wl,--out-implib,dist/libsbagenx-win32.dll.a \
-Wl,--export-all-symbols -Wl,--enable-auto-import \
-o dist/sbagenxlib-win32.dll $LIBS_32
if [ $? -ne 0 ]; then
error "Failed to link dist/sbagenxlib-win32.dll needed by the 32-bit CLI"
fi
i686-w64-mingw32-gcc $CFLAGS_32 sbagenx.tmp.c /tmp/sbagen32.res -o dist/sbagenx-win32.exe \
dist/libsbagenx-win32.dll.a $LIBS_32
if [ $? -eq 0 ]; then
success "32-bit compilation successful! Created 32-bit binary: dist/sbagenx-win32.exe"
else
error "32-bit compilation failed!"
fi
fi
if [ "$BUILD_WIN64" -eq 1 ]; then
# Build 64-bit version
section_header "Building 64-bit version..."
# Set up compilation flags for 64-bit
CFLAGS_64="-DT_MINGW -DFLAC_DECODE -Wall -O3 -I. -Ilibs"
LIBS_64="-lwinmm"
# Check for MP3 support (64-bit)
if [ -f "$LIB_PATH_64" ]; then
info "Including MP3 support for 64-bit using: $LIB_PATH_64"
CFLAGS_64="$CFLAGS_64 -DMP3_DECODE"
LIBS_64="$LIBS_64 $LIB_PATH_64"
else
warning "MP3 library not found at $LIB_PATH_64"
warning "MP3 support will not be included in 64-bit build"
warning "Run ./windows-build-libs.sh to build the required libraries"
fi
# Check for OGG support (64-bit)
if [ -f "$OGG_LIB_PATH_64" ] && [ -f "$TREMOR_LIB_PATH_64" ]; then
info "Including OGG support for 64-bit using: $OGG_LIB_PATH_64 and $TREMOR_LIB_PATH_64"
CFLAGS_64="$CFLAGS_64 -DOGG_DECODE"
# Order is important: first tremor, then ogg
LIBS_64="$LIBS_64 $TREMOR_LIB_PATH_64 $OGG_LIB_PATH_64"
else
warning "OGG libraries not found at $OGG_LIB_PATH_64 or $TREMOR_LIB_PATH_64"
warning "OGG support will not be included in 64-bit build"
warning "Run ./windows-build-libs.sh to build the required libraries"
fi
# Optional static output encoder path (single-binary friendly)
if [ -f "$SNDFILE_STATIC_LIB_PATH_64" ] && [ -f "$LAME_STATIC_LIB_PATH_64" ]; then
info "Including static output encoders for 64-bit using: $SNDFILE_STATIC_LIB_PATH_64 and $LAME_STATIC_LIB_PATH_64"
CFLAGS_64="$CFLAGS_64 -DSTATIC_OUTPUT_ENCODERS"
LIBS_64="$LIBS_64 $SNDFILE_STATIC_LIB_PATH_64 $LAME_STATIC_LIB_PATH_64 ${SBAGENX_STATIC_ENCODER_DEPS_WIN64}"
STATIC_ENCODERS_64=1
else
warning "Static encoder libs not found for 64-bit at $SNDFILE_STATIC_LIB_PATH_64 or $LAME_STATIC_LIB_PATH_64"
warning "Falling back to runtime-loaded encoder DLLs for 64-bit output encoding"
fi
# Compile 64-bit version
info "Compiling 64-bit version with flags: $CFLAGS_64"
info "Libraries: $LIBS_64"
SBX_LIB_CFLAGS_64="$CFLAGS_64 -DSBAGENXLIB_VERSION=\"\\\"$VERSION\\\"\""
x86_64-w64-mingw32-gcc $SBX_LIB_CFLAGS_64 -shared sbagenxlib.c \
-Wl,--out-implib,dist/libsbagenx-win64.dll.a \
-Wl,--export-all-symbols -Wl,--enable-auto-import \
-o dist/sbagenxlib-win64.dll $LIBS_64
if [ $? -ne 0 ]; then
error "Failed to link dist/sbagenxlib-win64.dll needed by the 64-bit CLI"
fi
x86_64-w64-mingw32-gcc $CFLAGS_64 sbagenx.tmp.c /tmp/sbagen64.res -o dist/sbagenx-win64.exe \
dist/libsbagenx-win64.dll.a $LIBS_64
if [ $? -eq 0 ]; then
success "64-bit compilation successful! Created 64-bit binary: dist/sbagenx-win64.exe"
else
error "64-bit compilation failed!"
fi
fi
MISSING_WINDOWS_EXES=()
if [ "$BUILD_WIN32" -eq 1 ] && [ ! -f dist/sbagenx-win32.exe ]; then
MISSING_WINDOWS_EXES+=("dist/sbagenx-win32.exe")
fi
if [ "$BUILD_WIN64" -eq 1 ] && [ ! -f dist/sbagenx-win64.exe ]; then
MISSING_WINDOWS_EXES+=("dist/sbagenx-win64.exe")
fi
if [ "${#MISSING_WINDOWS_EXES[@]}" -ne 0 ]; then
error "Required Windows executables were not produced:"
for exe in "${MISSING_WINDOWS_EXES[@]}"; do
error " $exe"
done
error "Aborting remaining Windows packaging steps."
rm -f /tmp/sbagen.rc /tmp/sbagen32.res /tmp/sbagen64.res sbagenx.tmp.c
exit 1
fi
# Build sbagenxlib static archives (Phase 1 extraction artifacts)
section_header "Building sbagenxlib static libraries..."
create_dir_if_not_exists "build/sbagenxlib"
create_dir_if_not_exists "dist/include"
create_dir_if_not_exists "dist/pkgconfig"
if [ "$BUILD_WIN32" -eq 1 ]; then
i686-w64-mingw32-gcc $SBX_LIB_CFLAGS_32 -c sbagenxlib.c -o build/sbagenxlib/sbagenxlib-win32.o
if [ $? -eq 0 ]; then
"$AR_32" rcs dist/libsbagenx-win32.a build/sbagenxlib/sbagenxlib-win32.o
if [ $? -eq 0 ]; then
success "Created sbagenxlib archive: dist/libsbagenx-win32.a"
else
warning "Failed to archive dist/libsbagenx-win32.a"
fi
else
warning "Failed to compile sbagenxlib for win32"
fi
fi
if [ "$BUILD_WIN64" -eq 1 ]; then
x86_64-w64-mingw32-gcc $SBX_LIB_CFLAGS_64 -c sbagenxlib.c -o build/sbagenxlib/sbagenxlib-win64.o
if [ $? -eq 0 ]; then
"$AR_64" rcs dist/libsbagenx-win64.a build/sbagenxlib/sbagenxlib-win64.o
if [ $? -eq 0 ]; then
success "Created sbagenxlib archive: dist/libsbagenx-win64.a"
else
warning "Failed to archive dist/libsbagenx-win64.a"
fi
else
warning "Failed to compile sbagenxlib for win64"
fi
fi