-
Notifications
You must be signed in to change notification settings - Fork 266
/
media-suite_helper.sh
2662 lines (2397 loc) · 85.2 KB
/
media-suite_helper.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
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
# shellcheck disable=SC2154,SC2120,SC2119,SC2034,SC1090,SC1117,SC2030,SC2031
if [[ -z ${MSYS+x} ]]; then
export MSYS=winsymlinks:nativestrict
touch linktest
ln -s linktest symlinktest > /dev/null 2>&1
ln linktest hardlinktest > /dev/null 2>&1
test -h symlinktest || unset MSYS
[[ $(stat --printf '%h\n' hardlinktest) -eq 2 ]] || unset MSYS
rm -f symlinktest hardlinktest linktest
fi
case $cpuCount in
'' | *[!0-9]*) cpuCount=$(($(nproc) / 2)) ;;
esac
: "${bits:=64bit}"
curl_opts=(/usr/bin/curl --connect-timeout 15 --retry 3
--retry-delay 5 --silent --location --insecure --fail)
if test -n "$(tput colors)" && test "$(tput colors)" -ge 8; then
bold=$(tput bold)
blue=$(tput setaf 12)
orange=$(tput setaf 11)
purple=$(tput setaf 13)
green=$(tput setaf 2)
red=$(tput setaf 1)
reset=$(tput sgr0)
fi
ncols=72
[[ -f "$LOCALBUILDDIR"/grep.exe ]] &&
rm -f "$LOCALBUILDDIR"/{7za,wget,grep}.exe
do_simple_print() {
local plain=false formatString dateValue newline='\n' OPTION OPTIND
while getopts ':np' OPTION; do
case "$OPTION" in
n) newline='' ;;
p) plain=true ;;
*) break ;;
esac
done
shift "$((OPTIND - 1))"
if [[ $timeStamp == y ]]; then
formatString="${purple}"'%(%H:%M:%S)T'"${reset}"' '
dateValue='-1'
elif $plain; then
formatString='\t'
fi
if ! $plain; then
formatString+="${bold}├${reset} "
fi
printf "$formatString"'%b'"${reset}${newline}" $dateValue "$*"
}
do_print_status() {
local _prefix _prefixpad=0
if [[ $1 == prefix ]]; then
_prefix="$2" && shift 2
_prefixpad=2
fi
local name="$1 " color="$2" status="$3" pad
eval printf -v pad ".%.s" "{1..$ncols}"
if [[ $timeStamp == y ]]; then
printf "${purple}"'%(%H:%M:%S)T'"${reset}"' %s%s %s [%s]\n' -1 "$_prefix" "${bold}$name${reset}" \
"${pad:0:$((ncols - _prefixpad - ${#name} - ${#status} - 12))}" "${color}${status}${reset}"
else
printf '%s%s %s [%s]\n' "$_prefix" "${bold}$name${reset}" \
"${pad:0:$((ncols - _prefixpad - ${#name} - ${#status} - 2))}" "${color}${status}${reset}"
fi
}
do_print_progress() {
case $logging$timeStamp in
yy) printf "${purple}"'%(%H:%M:%S)T'"${reset}"' %s\n' -1 "$([[ $1 =~ ^[a-zA-Z] ]] && echo "${bold}├${reset} ")$*..." ;;
yn)
[[ $1 =~ ^[a-zA-Z] ]] &&
printf '%s' "${bold}├${reset} "
echo -e "$*..."
;;
*)
set_title "$* in $(get_first_subdir)"
if [[ $timeStamp == y ]]; then
printf "${purple}"'%(%H:%M:%S)T'"${reset}"' %s\n' -1 "${bold}$* in $(get_first_subdir)${reset}"
else
echo -e "${bold}$* in $(get_first_subdir)${reset}"
fi
;;
esac
}
set_title() {
printf '\033]0;media-autobuild_suite %s\a' "($bits)${1:+: $1}"
}
do_exit_prompt() {
if [[ -n $build32$build64 ]]; then # meaning "executing this in the suite's context"
create_diagnostic
zip_logs
fi
do_prompt "$*"
[[ -n $build32$build64 ]] && exit 1
}
cd_safe() {
cd "$1" || do_exit_prompt "Failed changing to directory $1."
}
test_newer() {
[[ $1 == installed ]] && local installed=y && shift
local file
local files=("$@")
local cmp="${files[-1]}"
[[ $installed ]] && cmp="$(file_installed "$cmp")"
[[ ${#files[@]} -gt 1 ]] && unset 'files[-1]'
[[ -f $cmp ]] || return 0
for file in "${files[@]}"; do
[[ $installed ]] && file="$(file_installed "$file")"
[[ -f $file ]] &&
[[ $file -nt $cmp ]] && return
done
return 1
}
# vcs_get_current_type /build/myrepo
vcs_get_current_type() {
git -C "${1:-$PWD}" rev-parse --is-inside-work-tree > /dev/null 2>&1 &&
echo "git" &&
return 0
echo "unknown"
return 1
}
# check_valid_vcs /build/ffmpeg-git
check_valid_vcs() {
[[ -d ${1:-$PWD}/.git ]] &&
git -C "${1:-$PWD}/.git" rev-parse HEAD > /dev/null 2>&1
}
# vcs_get_current_head /build/ffmpeg-git
vcs_get_current_head() {
git -C "${1:-$PWD}" rev-parse HEAD
}
# vcs_test_remote "https://github.com/m-ab-s/media-autobuild_suite.git"
vcs_test_remote() {
GIT_TERMINAL_PROMPT=0 git ls-remote -q --refs "$1" > /dev/null 2>&1
}
vcs_clean() {
GIT_TERMINAL_PROMPT=0 \
git -C "${1:-$PWD}" clean -dffxq \
-e{recently_{updated,checked},build_successful*,*.{patch,diff},custom_updated,**/ab-suite.*.log} "$@"
}
# vcs_get_latest_tag "libopenmpt-*"
vcs_get_latest_tag() {
if ! case $1 in
LATEST) git describe --abbrev=0 --tags "$(git rev-list --tags --max-count=1)" 2> /dev/null ;;
GREATEST) git describe --abbrev=0 --tags 2> /dev/null ;;
*\**) git describe --abbrev=0 --tags "$(git tag -l "$1" --sort=-version:refname | head -1)" 2> /dev/null ;;
*) false ;;
esac then
echo "$1"
fi
}
# vcs_set_url https://github.com/FFmpeg/FFmpeg.git
vcs_set_url() {
if vcs_test_remote "$1"; then
git remote set-url origin "$1"
fi
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
}
# vcs_clone https://gitlab.com/libtiff/libtiff.git tiff v4.1.0
vcs_clone() (
set -x
vcsURL=$1 vcsFolder=${2:-$(basename "$vcsURL" .git)}
[[ -z $vcsURL ]] && return 1
check_valid_vcs "$vcsFolder-git" && return 0
rm -rf "$vcsFolder-git"
case $- in
*i*) unset GIT_TERMINAL_PROMPT ;;
*) export GIT_TERMINAL_PROMPT=0 ;;
esac
git clone --filter=tree:0 "$vcsURL" "$vcsFolder-git"
git -C "$vcsFolder-git" reset --hard "${3:-origin/HEAD}"
check_valid_vcs "$vcsFolder-git"
)
vcs_get_merge_base() {
git merge-base HEAD "$(vcs_get_latest_tag "$1")"
}
vcs_reset() (
set -x
git checkout --no-track -fB ab-suite "$(vcs_get_latest_tag "$1")"
git log --oneline --no-merges --no-color -n 1 | tee /dev/null
)
vcs_fetch() (
set -x
[[ -f $(git rev-parse --git-dir)/shallow ]] && unshallow="--unshallow" || unshallow=''
git fetch --all -Ppft $unshallow
git remote set-head -a origin
)
# do_mabs_clone "$vcsURL" "$vcsFolder" "$ref"
# For internal use for fallback links
do_mabs_clone() {
vcs_test_remote "$1" &&
log -q git.clone vcs_clone "$1" "$2" "$3"
check_valid_vcs "$2-git"
}
vcs_ref_to_hash() (
vcsURL=$1 ref=$2 vcsFolder=${3:-$(basename "$vcsURL" .git)}
if _ref=$(git ls-remote --refs --exit-code -q -- "$vcsURL" "$ref"); then
cut -f1 <<< "$_ref"
return 0
fi
if git -C "$vcsFolder-git" rev-parse --verify -q --end-of-options "$ref" 2> /dev/null ||
git -C "$vcsFolder" rev-parse --verify -q --end-of-options "$ref" 2> /dev/null ||
git rev-parse --verify -q --end-of-options "$ref" 2> /dev/null; then
return 0
fi
return 1
)
# get source from VCS
# example:
# do_vcs "url#branch|revision|tag|commit=NAME[ folder]" "folder"
do_vcs() {
local vcsURL=${1#*::} vcsFolder=$2 vcsCheck=("${_check[@]}")
local vcsBranch=${vcsURL#*#} ref=origin/HEAD
local deps=("${_deps[@]}") && unset _deps
[[ $vcsBranch == "$vcsURL" ]] && unset vcsBranch
local vcsPotentialFolder=${vcsURL#* }
if [[ -z $vcsFolder ]] && [[ $vcsPotentialFolder != "$vcsURL" ]]; then
vcsFolder=$vcsPotentialFolder # if there was a space, use the folder name
fi
vcsURL=${vcsURL%#*}
: "${vcsFolder:=$(basename "$vcsURL" .git)}" # else just grab from the url like git normally does
if [[ -n $vcsBranch ]]; then
ref=${vcsBranch##*=}
unset vcsBranch
fi
cd_safe "$LOCALBUILDDIR"
rm -f "$vcsFolder-git/custom_updated"
check_custom_patches "$vcsFolder-git"
extra_script pre vcs
# try to see if we can "resolve" the currently provided ref to a commit,
# excluding special tags that we will resolve later. Ignore it if it's
# a specific head. glslang's HEAD != their main branch somehow.
case $ref in
LATEST | GREATEST | *\**) ;;
origin/HEAD | origin/* | HEAD) ;;
*) ref=$(vcs_ref_to_hash "$vcsURL" "$ref" "$vcsFolder") ;;
esac
if ! check_valid_vcs "$vcsFolder-git"; then
rm -rf "$vcsFolder-git"
do_print_progress " Running git clone for $vcsFolder"
if ! do_mabs_clone "$vcsURL" "$vcsFolder" "$ref"; then
echo "$vcsFolder git seems to be down"
echo "Try again later or <Enter> to continue"
do_prompt "if you're sure nothing depends on it."
unset_extra_script
return 1
fi
touch "$vcsFolder-git"/recently_{updated,checked}
fi
cd_safe "$vcsFolder-git"
if [[ $ffmpegUpdate == onlyFFmpeg && $vcsFolder != ffmpeg && $vcsFolder != mpv ]] &&
files_exist "${vcsCheck[@]:-$vcsFolder.pc}"; then
do_print_status "${vcsFolder} git" "$green" "Already built"
unset_extra_script
return 1
fi
vcs_set_url "$vcsURL"
log -q git.fetch vcs_fetch
oldHead=$(vcs_get_merge_base "$ref")
do_print_progress " Running git update for $vcsFolder"
log -q git.reset vcs_reset "$ref"
newHead=$(vcs_get_current_head "$PWD")
vcs_clean
if [[ $oldHead != "$newHead" || -f custom_updated ]]; then
touch recently_updated
rm -f ./build_successful{32,64}bit{,_*}
if [[ $build32$build64$bits == yesyes64bit ]]; then
new_updates=yes
new_updates_packages="$new_updates_packages [$vcsFolder]"
fi
{
echo "$vcsFolder"
git log --no-merges --pretty="%ci: %an - %h%n %s" "$oldHead..$newHead"
echo
} >> "$LOCALBUILDDIR/newchangelog"
do_print_status "┌ $vcsFolder git" "$orange" "Updates found"
elif [[ -f recently_updated && ! -f build_successful$bits${flavor:+_$flavor} ]]; then
do_print_status "┌ $vcsFolder git" "$orange" "Recently updated"
elif [[ -n ${vcsCheck[*]} ]] && ! files_exist "${vcsCheck[@]}"; then
do_print_status "┌ $vcsFolder git" "$orange" "Files missing"
elif [[ -n ${deps[*]} ]] && test_newer installed "${deps[@]}" "${vcsCheck[0]}"; then
do_print_status "┌ $vcsFolder git" "$orange" "Newer dependencies"
else
do_print_status "$vcsFolder git" "$green" "Up-to-date"
[[ ! -f recompile ]] && {
unset_extra_script
return 1
}
do_print_status "┌ $vcsFolder git" "$orange" "Forcing recompile"
do_print_status prefix "$bold├$reset " "Found recompile flag" "$orange" "Recompiling"
fi
extra_script post vcs
return 0
}
# get source from VCS to a local subfolder
# example:
# do_vcs_local "url#branch|revision|tag|commit=NAME" "subfolder"
do_vcs_local() {
local vcsURL=${1#*::} vcsFolder=$2 vcsCheck=("${_check[@]}")
local vcsBranch=${vcsURL#*#} ref=origin/HEAD
local deps=("${_deps[@]}") && unset _deps
[[ $vcsBranch == "$vcsURL" ]] && unset vcsBranch
vcsURL=${vcsURL%#*}
: "${vcsFolder:=$(basename "$vcsURL" .git)}"
if [[ -n $vcsBranch ]]; then
ref=${vcsBranch##*=}
[[ ${vcsBranch%%=*}/$ref == branch/${ref%/*} ]] && ref=origin/$ref
fi
rm -f "$vcsFolder/custom_updated"
# try to see if we can "resolve" the currently provided ref, minus the origin/ part,
# if so, set ref to the ref on the origin, this might make it harder for people who
# want use multiple remotes other than origin. Converts ref=develop to ref=origin/develop
# ignore those that use the special tags/branches
case $ref in
LATEST | GREATEST | *\**) ;;
*) git ls-remote --exit-code "$vcsURL" "${ref#origin/}" > /dev/null 2>&1 && ref=origin/${ref#origin/} ;;
esac
if ! check_valid_vcs "$vcsFolder"; then
rm -rf "$vcsFolder"
rm -rf "$vcsFolder-git"
do_print_progress " Running git clone for $vcsFolder"
if ! do_mabs_clone "$vcsURL" "$vcsFolder" "$ref"; then
echo "$vcsFolder git seems to be down"
echo "Try again later or <Enter> to continue"
do_prompt "if you're sure nothing depends on it."
# unset_extra_script
return 1
fi
mv "$vcsFolder-git" "$vcsFolder"
touch "$vcsFolder"/recently_{updated,checked}
fi
cd_safe "$vcsFolder"
vcs_set_url "$vcsURL"
log -q git.fetch vcs_fetch
oldHead=$(vcs_get_merge_base "$ref")
do_print_progress " Running git update for $vcsFolder"
log -q git.reset vcs_reset "$ref"
newHead=$(vcs_get_current_head "$PWD")
vcs_clean
cd ..
return 0
}
guess_dirname() {
expr "$1" : '\(.\+\)\.\(tar\(\.\(gz\|bz2\|xz\|lz\)\)\?\|7z\|zip\)$'
}
check_hash() {
local file="$1" check="$2" md5sum sha256sum
if [[ -z $file || ! -f $file ]]; then
return 1
elif [[ -z $check ]]; then
# if no hash to check, just check if the file exists
return 0
fi
sha256sum=$(sha256sum "$file" | cut -d' ' -f1)
if [[ $check == print ]]; then
echo "$sha256sum"
else
md5sum=$(md5sum "$file" | cut -d' ' -f1)
if [[ $sha256sum == "$check" || $md5sum == "$check" ]]; then
return 0
fi
do_simple_print "${orange}Hash mismatch, file may be broken: ${check} != ${sha256sum} || ${md5sum}"
return 1
fi
}
# get wget download
do_wget() {
local nocd=false norm=false quiet=false notmodified=false noextract=false hash
while true; do
case $1 in
-c) nocd=true && shift ;;
-r) norm=true && shift ;;
-q) quiet=true && shift ;;
-h) hash="$2" && shift 2 ;;
-z) notmodified=true && shift ;;
-n) noextract=true && shift ;;
--)
shift
break
;;
*) break ;;
esac
done
local url="$1" archive="$2" dirName="$3" response_code=000 curlcmds=("${curl_opts[@]}") tries=1 temp_file
if [[ -z $archive ]]; then
# remove arguments and filepath
archive=${url%%\?*}
archive=${archive##*/}
fi
if [[ -f $url ]]; then
return 1
fi
archive=${archive:-"$(/usr/bin/curl -sI "$url" | grep -Eo 'filename=.*$' | sed 's/filename=//')"}
[[ -z $dirName ]] && dirName=$(guess_dirname "$archive")
$nocd || cd_safe "$LOCALBUILDDIR"
$notmodified && [[ -f $archive ]] && curlcmds+=(-z "$archive" -R)
[[ $hash ]] && tries=3
if [[ -f $archive ]] && [[ $hash ]] && check_hash "$archive" "$hash"; then
$quiet || do_print_status prefix "${bold}├${reset} " "${dirName:-$archive}" "$green" "File up-to-date"
tries=0
fi
while [[ $tries -gt 0 ]]; do
temp_file=$(mktemp)
response_code=$("${curlcmds[@]}" -w "%{http_code}" -o "$temp_file" "$url")
if [[ -f $archive ]] && diff -q "$archive" "$temp_file" > /dev/null 2>&1; then
$quiet || do_print_status prefix "${bold}├${reset} " "${dirName:-$archive}" "$green" "File up-to-date"
rm -f "$temp_file"
break
fi
((tries -= 1))
case $response_code in
2**)
$quiet || do_print_status "┌ ${dirName:-$archive}" "$orange" "Downloaded"
check_hash "$temp_file" "$hash" && cp -f "$temp_file" "$archive"
rm -f "$temp_file"
break
;;
304)
$quiet || do_print_status "┌ ${dirName:-$archive}" "$orange" "File up-to-date"
rm -f "$temp_file"
break
;;
esac
if check_hash "$archive" "$hash"; then
printf '%b\n' "${orange}${archive}${reset}" \
'\tFile not found online. Using local copy.'
else
do_print_status "└ ${dirName:-$archive}" "$red" "Failed"
printf '%s\n' "Error $response_code while downloading $url" \
"<Ctrl+c> to cancel build or <Enter> to continue"
do_prompt "if you're sure nothing depends on it."
rm -f "$temp_file"
return 1
fi
done
$norm || add_to_remove "$(pwd)/$archive"
$noextract || do_extract "$archive" "$dirName"
! $norm && [[ -n $dirName ]] && ! $nocd && add_to_remove
[[ -z $response_code || $response_code != "304" ]] && return 0
}
real_extract() {
local archive="$1" dirName="$2" archive_type strip_comp=''
[[ -z $archive ]] && return 1
archive_type=$(expr "$archive" : '.\+\(tar\(\.\(gz\|bz2\|xz\|lz\)\)\?\|7z\|zip\)$')
[[ ! $dirName ]] && dirName=$(guess_dirname "$archive" || echo "${archive}")
case $archive_type in
zip | 7z)
7z x -aoa -o"$dirName" "$archive"
;;
tar*)
[[ -n $dirName && ! -d $dirName ]] && mkdir -p "$dirName"
case $archive_type in
tar\.lz)
do_pacman_install -m lzip
lzip -d "$archive"
;;
tar\.*) 7z x -aoa "$archive" ;;
esac
[[ $(tar -tf "${archive%.tar*}.tar" | cut -d'/' -f1 | sort -u | wc -l) == 1 ]] && strip_comp="--strip-components=1"
if ! tar $strip_comp -C "$dirName" -xf "${1%.tar*}.tar"; then
7z x -aoa "${archive%.tar*}.tar" -o"$dirName"
fi
rm -f "${archive%.tar*}.tar"
;;
esac
local temp_dir
temp_dir=$(find "$dirName/" -maxdepth 1 ! -wholename "$dirName/")
if [[ -n $temp_dir && $(wc -l <<< "$temp_dir") == 1 ]]; then
find "$temp_dir" -maxdepth 1 ! -wholename "$temp_dir" -exec mv -t "$dirName/" {} +
rmdir "$temp_dir" 2> /dev/null
fi
}
do_extract() {
local nocd="${nocd:-false}"
local archive="$1" dirName="$2"
# accepted: zip, 7z, tar, tar.gz, tar.bz2 and tar.xz
[[ -z $dirName ]] && dirName=$(guess_dirname "$archive")
if [[ $dirName != "." && -d $dirName ]]; then
if [[ $build32 == "yes" && ! -f \
"$dirName/build_successful32bit${flavor:+_$flavor}" ]]; then
rm -rf "$dirName"
elif [[ $build64 == "yes" && ! -f \
"$dirName/build_successful64bit${flavor:+_$flavor}" ]]; then
rm -rf "$dirName"
fi
elif [[ -d $dirName ]]; then
$nocd || cd_safe "$dirName"
return 0
elif ! expr "$archive" : '.\+\(tar\(\.\(gz\|bz2\|xz\|lz\)\)\?\|7z\|zip\)$' > /dev/null; then
return 0
fi
log "extract" real_extract "$archive" "$dirName"
$nocd || cd_safe "$dirName"
}
do_wget_sf() {
# do_wget_sf "faac/faac-src/faac-1.28/faac-$_ver.tar.bz2" "faac-$_ver"
local hash
[[ $1 == "-h" ]] && hash="$2" && shift 2
local url="https://download.sourceforge.net/$1"
shift 1
if [[ -n $hash ]]; then
do_wget -h "$hash" "$url" "$@"
else
do_wget "$url" "$@"
fi
local ret=$?
check_custom_patches
return $ret
}
do_strip() {
local cmd exts nostrip file
local cmd=(strip)
local nostrip="x265|x265-numa|ffmpeg|ffprobe|ffplay"
local exts="exe|dll|com|a"
[[ -f $LOCALDESTDIR/bin-video/mpv.exe.debug ]] && nostrip+="|mpv"
for file; do
if [[ $file =~ \.($exts)$ && ! $file =~ ($nostrip)\.exe$ ]]; then
do_print_progress Stripping
break
fi
done
for file; do
local orig_file="$file"
if ! file="$(file_installed "$orig_file")"; then
continue
fi
if [[ $file =~ \.(exe|com)$ ]] &&
[[ ! $file =~ ($nostrip)\.exe$ ]]; then
cmd+=(--strip-all)
elif [[ $file =~ \.dll$ ]] ||
[[ $file =~ (x265|x265-numa)\.exe$ ]]; then
cmd+=(--strip-unneeded)
elif ! disabled debug && [[ $file =~ \.a$ ]]; then
cmd+=(--strip-debug)
else
file=""
fi
[[ $file ]] &&
{ eval "${cmd[@]}" "$file" 2> /dev/null ||
eval "${cmd[@]}" "$file" -o "$file.stripped" 2> /dev/null; }
[[ -f ${file}.stripped ]] && mv -f "${file}"{.stripped,}
done
}
do_pack() {
local file
local cmd=(/opt/bin/upx -9 -qq)
local nopack=""
local exts="exe|dll"
[[ $bits == 64bit ]] && enabled_any libtls openssl && nopack="ffmpeg|mplayer|mpv"
for file; do
if [[ $file =~ \.($exts)$ && ! $file =~ ($nopack)\.exe$ ]]; then
do_print_progress Packing with UPX
break
fi
done
for file; do
local orig_file="$file"
if ! file="$(file_installed "$orig_file")"; then
continue
fi
if [[ $file =~ \.($exts)$ ]] &&
! [[ -n $nopack && $file =~ ($nopack)\.exe$ ]]; then
[[ $stripping == y ]] && cmd+=("--strip-relocs=0")
else
file=""
fi
[[ $file ]] && eval "${cmd[@]}" "$file"
done
}
do_zipman() {
local file files
local man_dirs=(/local{32,64}/share/man)
files=$(find "${man_dirs[@]}" -type f \! -name "*.gz" \! -name "*.db" \! -name "*.bz2" 2> /dev/null)
for file in $files; do
gzip -9 -n -f "$file"
rm -f "$file"
done
}
# check if compiled file exist
do_checkIfExist() {
local packetName
packetName="$(get_first_subdir)"
local packageDir="${LOCALBUILDDIR}/${packetName}"
local buildSuccessFile="${packageDir}/build_successful${bits}"
local dry="${dry:-n}"
local check=()
if [[ -n $1 ]]; then
check+=("$@")
else
check+=("${_check[@]}")
unset _check
fi
unset_extra_script
[[ -z ${check[*]} ]] && echo "No files to check" && return 1
if [[ $dry == y ]]; then
files_exist -v -s "${check[@]}"
return $?
fi
if files_exist -v "${check[@]}"; then
[[ $stripping == y ]] && do_strip "${check[@]}"
[[ $packing == y ]] && do_pack "${check[@]}"
do_print_status "└ $packetName" "$blue" "Updated"
[[ $build32 == yes || $build64 == yes ]] && [[ -d $packageDir ]] &&
touch "$buildSuccessFile"
else
[[ $build32 == yes || $build64 == yes ]] && [[ -d $packageDir ]] &&
rm -f "$buildSuccessFile"
do_print_status "└ $packetName" "$red" "Failed"
if ${_notrequired:-false}; then
printf '%s\n' \
"$orange"'Package failed to build, but is not required; proceeding with compilation.'"$reset"
else
printf '%s\n' \
'' "Try deleting '$packageDir' and start the script again." \
'If you are sure there are no dependencies, <Enter> to continue building.'
do_prompt "Close this window if you wish to stop building."
fi
fi
}
file_installed() {
local file silent
[[ $1 == "-s" ]] && silent=true && shift
case $1 in
/* | ./*)
file="$1"
;;
*.pc)
file="lib/pkgconfig/$1"
;;
*.a | *.la | *.lib)
file="lib/$1"
;;
*.h | *.hpp | *.c)
file="include/$1"
;;
*)
file="$1"
;;
esac
[[ ${file::1} != "/" ]] && file="$LOCALDESTDIR/$file"
${silent:-false} || echo "$file"
test -e "$file"
}
files_exist() {
local verbose list soft ignorebinaries term='\n' file
while true; do
case $1 in
-v) verbose=y && shift ;;
-l) list=y && shift ;;
-s) soft=y && shift ;;
-b) ignorebinaries=y && shift ;;
-l0) list=y && term='\0' && shift ;;
--)
shift
break
;;
*) break ;;
esac
done
[[ $list ]] && verbose= && soft=y
for opt; do
if file=$(file_installed "$opt"); then
[[ $verbose && $soft ]] && do_print_status "├ $file" "${green}" "Found"
if [[ $list ]]; then
if [[ $ignorebinaries && $file =~ .(exe|com)$ ]]; then
continue
fi
printf "%s%b" "$file" "$term"
fi
else
[[ $verbose ]] && do_print_status prefix "${bold}├${reset} " "$file" "${red}" "Not found"
[[ ! $soft ]] && return 1
fi
done
return 0
}
pc_exists() {
for opt; do
local _pkg=${opt%% *}
local _check=${opt#$_pkg}
[[ $_pkg == "$_check" ]] && _check=""
[[ $_pkg == *.pc ]] || _pkg="${LOCALDESTDIR}/lib/pkgconfig/${_pkg}.pc"
$PKG_CONFIG --exists --silence-errors "${_pkg}${_check}" || return
done
}
do_install() {
[[ $1 == dry ]] && local dryrun=y && shift
local files=("$@")
local dest="${files[-1]}"
[[ ${dest::1} != "/" ]] && dest="$(file_installed "$dest")"
[[ ${#files[@]} -gt 1 ]] && unset 'files[-1]'
[[ ${dest: -1:1} == "/" ]] && mkdir -p "$dest"
if [[ -n $dryrun ]]; then
echo install -D -p "${files[@]}" "$dest"
else
extra_script pre install
[[ -f "$(get_first_subdir -f)/do_not_install" ]] &&
return
install -D -p "${files[@]}" "$dest"
extra_script post install
fi
}
do_uninstall() {
local dry quiet all files
[[ $1 == dry ]] && dry=y && shift
[[ $1 == q ]] && quiet=y && shift
[[ $1 == all ]] && all=y && shift
if [[ $all ]]; then
mapfile -t files < <(files_exist -l "$@")
else
mapfile -t files < <(files_exist -l -b "$@")
fi
if [[ -n ${files[*]} ]]; then
[[ ! $quiet ]] && do_print_progress Running uninstall
if [[ $dry ]]; then
echo "rm -rf ${files[*]}"
else
rm -rf "${files[@]}"
fi
fi
}
do_pkgConfig() {
local pkg="${1%% *}"
local pc_check="${1#$pkg}"
local pkg_and_version="$pkg"
[[ $pkg == "$pc_check" ]] && pc_check=""
local version=$2
local deps=("${_deps[@]}") && unset _deps
[[ ! $version && $pc_check ]] && version="${pc_check#*= }"
[[ "$version" ]] && pkg_and_version="${pkg} ${version}"
if ! pc_exists "${pkg}"; then
do_print_status "${pkg_and_version}" "$red" "Not installed"
elif ! pc_exists "${pkg}${pc_check}"; then
do_print_status "${pkg_and_version}" "$orange" "Outdated"
elif [[ -n ${deps[*]} ]] && test_newer installed "${deps[@]}" "${pkg}.pc"; then
do_print_status "${pkg_and_version}" "$orange" "Newer dependencies"
elif [[ -n ${_check[*]} ]] && ! files_exist "${_check[@]}"; then
do_print_status "${pkg_and_version}" "$orange" "Files missing"
else
do_print_status "${pkg_and_version}" "$green" "Up-to-date"
return 1
fi
}
do_readoptionsfile() {
local filename="$1"
if [[ -f $filename ]]; then
sed -r '# remove commented text
s/#.*//
# delete empty lines
/^\s*$/d
# remove leading whitespace
s/^\s+//
# remove trailing whitespace
s/\s+$//
' "$filename" | tr -d '\r' # cut cr out from any crlf files
echo "Imported options from ${filename##*/}" >&2
fi
}
do_readbatoptions() {
local varname="$1"
# shellcheck disable=SC1117
printf '%s\n' "${bat[@]}" |
sed -En "/set ${varname}=/,/[^^]$/p" |
sed -E "/^:/d;s/(set ${varname}=| \\^|\")//g;s/ /\\n/g" |
sed -E '/^#/d;/^[^-]/{s/^/--enable-/g}'
}
do_getFFmpegConfig() {
local license="${1:-nonfree}"
FFMPEG_DEFAULT_OPTS=()
if [[ -f "/trunk/media-autobuild_suite.bat" && $ffmpegChoice =~ (n|z|f) ]]; then
IFS=$'\r\n' read -d '' -r -a bat < /trunk/media-autobuild_suite.bat
mapfile -t FFMPEG_DEFAULT_OPTS < <(do_readbatoptions "ffmpeg_options_(builtin|basic)")
local option
[[ $ffmpegChoice != n ]] && while read -r option; do
FFMPEG_DEFAULT_OPTS+=("$option")
done < <(do_readbatoptions "ffmpeg_options_zeranoe")
[[ $ffmpegChoice == f ]] && while read -r option; do
FFMPEG_DEFAULT_OPTS+=("$option")
done < <(do_readbatoptions "ffmpeg_options_full(|_shared)")
echo "Imported default FFmpeg options from .bat"
else
local custom_opts_file="$LOCALBUILDDIR/ffmpeg_options.txt"
if [[ -f "$LOCALBUILDDIR/ffmpeg_options_$bits.txt" ]]; then
custom_opts_file="$LOCALBUILDDIR/ffmpeg_options_$bits.txt"
fi
IFS=$'\n' read -d '' -r -a FFMPEG_DEFAULT_OPTS < <(do_readoptionsfile "$custom_opts_file")
unset FFMPEG_DEFAULT_OPTS_SHARED
if [[ -f "$LOCALBUILDDIR/ffmpeg_options_shared.txt" ]]; then
IFS=$'\n' read -d '' -r -a FFMPEG_DEFAULT_OPTS_SHARED < <(
do_readoptionsfile "$LOCALBUILDDIR/ffmpeg_options_shared.txt"
)
fi
fi
FFMPEG_OPTS=()
for opt in "${FFMPEG_BASE_OPTS[@]}" "${FFMPEG_DEFAULT_OPTS[@]}"; do
[[ -n $opt ]] && FFMPEG_OPTS+=("$opt")
done
echo "License: $license"
# we set these accordingly for static or shared
do_removeOption "--(en|dis)able-(shared|static)"
# OK to use GnuTLS for rtmpdump if not nonfree since GnuTLS was built for rtmpdump anyway
# If nonfree will use SChannel if neither openssl/libtls or gnutls are in the options
if ! enabled_any libtls openssl gnutls &&
{ enabled librtmp || [[ $rtmpdump == y ]]; }; then
if [[ $license == nonfree ]] ||
[[ $license == lgpl* && $rtmpdump == n ]]; then
do_addOption --enable-openssl
else
do_addOption --enable-gnutls
fi
do_removeOption "--enable-(gmp|gcrypt|mbedtls)"
fi
local _all_tls="--enable-(mbedtls|gnutls|openssl|libtls|schannel)"
if enabled_any libtls openssl && [[ $license != gpl* ]]; then
# prefer openssl/libtls if both are in options and not gpl
# prefer openssl over libtls if both enabled
local _prefer=libtls
if enabled openssl; then
_prefer=openssl
fi
do_removeOption "${_all_tls}"
do_addOption "--enable-${_prefer}"
elif enabled mbedtls; then
# prefer mbedtls if any other tls libs are enabled and gpl
do_removeOption "${_all_tls}"
do_addOption --enable-mbedtls
elif enabled gnutls; then
do_removeOption "${_all_tls}"
do_addOption --enable-gnutls
elif ! disabled schannel; then
# fallback to schannel if no other tls libs are enabled
do_addOption --enable-schannel
fi
enabled_any lib{vo-aacenc,aacplus,utvideo,dcadec,faac,ebur128,ndi_newtek,ndi-newtek,ssh,wavpack} netcdf &&
do_removeOption "--enable-(lib(vo-aacenc|aacplus|utvideo|dcadec|faac|ebur128|ndi_newtek|ndi-newtek|ssh|wavpack)|netcdf)" &&
sed -ri 's;--enable-(lib(vo-aacenc|aacplus|utvideo|dcadec|faac|ebur128|ndi_newtek|ndi-newtek|ssh|wavpack)|netcdf);;g' \
"$LOCALBUILDDIR/ffmpeg_options.txt"
}
do_changeFFmpegConfig() {
local license="${1:-nonfree}"
do_print_progress Changing options to comply to "$license"
# if w32threads is disabled, pthreads is used and needs this cflag
# decklink includes zvbi, which requires pthreads
if disabled w32threads || enabled pthreads || enabled_all decklink libzvbi || enabled libvmaf; then
do_removeOption --enable-w32threads
do_addOption --disable-w32threads
fi
# add options for static kvazaar
enabled libkvazaar && do_addOption --extra-cflags=-DKVZ_STATIC_LIB
# get libs restricted by license
local config_script=configure
[[ $(get_first_subdir) != "ffmpeg-git" ]] && config_script="$LOCALBUILDDIR/ffmpeg-git/configure"
[[ -f $config_script ]] || do_exit_prompt "There's no configure script to retrieve libs from"
eval "$(sed -n '/EXTERNAL_LIBRARY_GPL_LIST=/,/^"/p' "$config_script" | tr -s '\n' ' ')"
eval "$(sed -n '/HWACCEL_LIBRARY_NONFREE_LIST=/,/^"/p' "$config_script" | tr -s '\n' ' ')"
eval "$(sed -n '/EXTERNAL_LIBRARY_NONFREE_LIST=/,/^"/p' "$config_script" | tr -s '\n' ' ')"
eval "$(sed -n '/EXTERNAL_LIBRARY_VERSION3_LIST=/,/^"/p' "$config_script" | tr -s '\n' ' ')"
# handle gpl libs
local gpl
read -ra gpl <<< "${EXTERNAL_LIBRARY_GPL_LIST//_/-} gpl"
if [[ $license == gpl* || $license == nonfree ]] &&
{ enabled_any "${gpl[@]}" || ! disabled postproc; }; then
do_addOption --enable-gpl
else
do_removeOptions "${gpl[*]/#/--enable-} --enable-postproc --enable-gpl"
fi
# handle (l)gplv3 libs
local version3
read -ra version3 <<< "${EXTERNAL_LIBRARY_VERSION3_LIST//_/-}"
if [[ $license =~ (l|)gplv3 || $license == nonfree ]] && enabled_any "${version3[@]}"; then
do_addOption --enable-version3
else
do_removeOptions "${version3[*]/#/--enable-} --enable-version3"
fi
local nonfreehwaccel
read -ra nonfreehwaccel <<< "(${HWACCEL_LIBRARY_NONFREE_LIST//_/-}"
if [[ $license == "nonfree" ]] && enabled_any "${nonfreehwaccel[@]}"; then
do_addOption --enable-nonfree
else
do_removeOptions "${nonfreehwaccel[*]/#/--enable-} --enable-nonfree"
fi
# cuda-only workarounds
if verify_cuda_deps; then
if enabled libnpp; then
echo -e "${orange}FFmpeg and related apps will depend on CUDA SDK to run!${reset}"
local fixed_CUDA_PATH
fixed_CUDA_PATH="$(cygpath -sm "$CUDA_PATH")"
if [[ $fixed_CUDA_PATH != "${fixed_CUDA_PATH// /}" ]]; then
# Assumes CUDA_PATH backwards is version/CUDA/NVIDIA GPU Computing Toolkit/rest of the path
# Strips the onion to the rest of the path
{
cat << EOF
@echo off
fltmc > NUL 2>&1 || echo Elevation required, right click the script and click 'Run as administrator'. & echo/ & pause & exit /b 1
cd /d "$(dirname "$(dirname "$(dirname "$(cygpath -sw "$CUDA_PATH")")")")"
EOF
# Generate up to 4 shortnames
for _n in 1 2 3 4; do
printf 'fsutil file setshortname "NVIDIA GPU Computing Toolkit" NVIDIA~%d || ' "$_n"
done
echo 'echo Failed to set a shortname for your CUDA_PATH'
} > "$LOCALBUILDDIR/cuda.bat"
do_simple_print "${orange}Spaces detected in the CUDA path"'!'"$reset"
do_simple_print "Path returned by windows: ${bold}$fixed_CUDA_PATH${reset}"
do_simple_print "A script to create the missing short paths for your CUDA_PATH"
do_simple_print "was created at $(cygpath -m "$LOCALBUILDDIR/cuda.bat")"
do_simple_print "Please run that script as an administrator and rerun the suite"
do_simple_print "${red}This will break FFmpeg compilation, so aborting early"'!'"${reset}"