forked from slyfox1186/ffmpeg-build-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-ffmpeg.sh
executable file
·2634 lines (2344 loc) · 105 KB
/
build-ffmpeg.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
#!/usr/bin/env bash
# shellcheck disable=SC2068,SC2162,SC2317 source=/dev/null
# GitHub: https://github.com/slyfox1186/ffmpeg-build-script
#
# Script version: 3.9.7
# Updated: 08.25.24
#
# Purpose: build ffmpeg from source code with addon development libraries
# also compiled from source to help ensure the latest functionality
# Supported Distros: Debian 11|12
# Ubuntu (20|22|24).04
# Linux Mint 21.x
# Zorin OS 16.x
# (Other Ubuntu-based distributions may also work)
# Supported architecture: x86_64
# CUDA SDK Toolkit: Updated to version 12.6.0
if [[ "$EUID" -ne 0 ]]; then
echo "You must run this script as root or with sudo."
exit 1
fi
# Define global variables
script_name="${0##*/}"
script_version="3.9.7"
cwd="$PWD/ffmpeg-build-script"
mkdir -p "$cwd"; cd "$cwd" || exit 1
if [[ "$PWD" =~ ffmpeg-build-script\/ffmpeg-build-script ]]; then
cd ../
rm -fr ffmpeg-build-script
cwd="$PWD"
fi
packages="$cwd/packages"
workspace="$cwd/workspace"
google_speech_flag=false
# Set a regex string to match and then exclude any found release candidate versions of a program. Utilize stable releases only.
git_regex='(Rc|rc|rC|RC|alpha|beta|early|init|next|pending|pre|tentative)+[0-9]*$'
debug=OFF
# Pre-defined color variables
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
NC='\033[0m'
# Print script banner
echo
box_out_banner() {
input_char=$(echo "$@" | wc -c)
line=$(for i in $(seq 0 "$input_char"); do printf "-"; done)
tput bold
line="$(tput setaf 3)$line"
space="${line//-/ }"
echo " $line"
printf "|" ; echo -n "$space" ; printf "%s\n" "|";
printf "| " ;tput setaf 4; echo -n "$@"; tput setaf 3 ; printf "%s\n" " |";
printf "|" ; echo -n "$space" ; printf "%s\n" "|";
echo " $line"
tput sgr 0
}
box_out_banner "FFmpeg Build Script - v$script_version"
# Create output directories
mkdir -p "$packages" "$workspace"
# Set the CC/CPP compilers + customized compiler optimization flags
source_compiler_flags() {
CFLAGS="-O3 -pipe -fPIC -march=native"
CXXFLAGS="$CFLAGS"
CPPFLAGS="-I$workspace/include -D_FORTIFY_SOURCE=2"
LDFLAGS="-L$workspace/lib64 -L$workspace/lib -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now"
EXTRALIBS="-ldl -lpthread -lm -lz"
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
}
log() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_update() {
echo -e "${GREEN}[UPDATE]${NC} $1"
}
warn() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
exit_fn() {
echo
echo -e "${GREEN}[INFO]${NC} Make sure to ${YELLOW}star${NC} this repository to show your support!"
echo -e "${GREEN}[INFO]${NC} https://github.com/slyfox1186/ffmpeg-build-script"
echo
exit 0
}
fail() {
echo
echo -e "${RED}[ERROR]${NC} $1"
echo
echo -e "${GREEN}[INFO]${NC} For help or to report a bug create an issue at: https://github.com/slyfox1186/ffmpeg-build-script/issues"
echo
if $google_speech_flag; then
google_speech "The FFmpeg build script encountered a fatal error." &>/dev/null
fi
exit 1
}
cleanup() {
local choice
echo
read -p "Do you want to clean up the build files? (yes/no): " choice
case "$choice" in
[yY]*|[yY][eE][sS]*)
rm -fr "$cwd"
;;
[nN]*|[nN][oO]*)
;;
*) unset choice
cleanup
;;
esac
}
disk_space_requirements() {
# Set the required install directory size in megabytes
INSTALL_DIR_SIZE=7001
log "Required install directory size: $(echo "$INSTALL_DIR_SIZE / 1024" | bc -l | awk '{printf "%.2f", $1}')G"
# Calculate the minimum required disk space with a 20% buffer
MIN_DISK_SPACE=$(echo "$INSTALL_DIR_SIZE * 1.2" | bc -l | awk '{print int($1)}')
warn "Minimum required disk space (including 20% buffer): $(echo "$MIN_DISK_SPACE / 1024" | bc -l | awk '{printf "%.2f", $1}')G"
# Get the available disk space in megabytes
AVAILABLE_DISK_SPACE=$(df -BM . | awk '{print $4}' | tail -n1 | sed 's/M//')
warn "Available disk space: $(echo "$AVAILABLE_DISK_SPACE / 1024" | bc -l | awk '{printf "%.2f", $1}')G"
# Compare the available disk space with the minimum required
if (( $(echo "$AVAILABLE_DISK_SPACE < $MIN_DISK_SPACE" | bc -l) )); then
warn "Insufficient disk space."
warn "Minimum required (including 20% buffer): $(echo "$MIN_DISK_SPACE / 1024" | bc -l | awk '{printf "%.2f", $1}')G"
fail "Available disk space: $(echo "$AVAILABLE_DISK_SPACE / 1024" | bc -l | awk '{printf "%.2f", $1}')G"
else
log "Sufficient disk space available."
fi
}
display_ffmpeg_versions() {
local file files
files=( [0]=ffmpeg [1]=ffprobe [2]=ffplay )
echo
for file in ${files[@]}; do
if command -v "$file" >/dev/null 2>&1; then
"$file" -version
echo
fi
done
}
show_versions() {
local choice
echo
read -p "Display the installed versions? (yes/no): " choice
case "$choice" in
[yY]*|[yY][eE][sS]*|"")
display_ffmpeg_versions
;;
[nN]*|[nN][oO]*)
;;
*) unset choice
show_versions
;;
esac
}
# Function to ensure no cargo or rustc processes are running
ensure_no_cargo_or_rustc_processes() {
local running_processes
running_processes=$(pgrep -fl 'cargo|rustc')
if [[ -n "$running_processes" ]]; then
warn "Waiting for cargo or rustc processes to finish..."
while pgrep -x cargo &>/dev/null || pgrep -x rustc &>/dev/null; do
sleep 3
done
log "No cargo or rustc processes running."
fi
}
# Function to check if cargo-c is installed and install it if not
check_and_install_cargo_c() {
if ! command -v cargo-cinstall &>/dev/null; then
warn "cargo-c could not be found and will be installed..."
ensure_no_cargo_or_rustc_processes
# Perform cleanup only when it's safe
cargo clean 2>/dev/null
find "$HOME/.cargo/registry/index" -type f -name ".cargo-lock" -delete
# Install cargo-c
execute cargo install cargo-c
fi
}
install_windows_hardware_acceleration() {
local file
declare -A files=(
["objbase.h"]="https://raw.githubusercontent.com/wine-mirror/wine/master/include/objbase.h"
["dxva2api.h"]="https://download.videolan.org/pub/contrib/dxva2api.h"
["windows.h"]="https://raw.githubusercontent.com/tpn/winsdk-10/master/Include/10.0.10240.0/um/Windows.h"
["direct.h"]="https://raw.githubusercontent.com/tpn/winsdk-10/master/Include/10.0.10240.0/km/crt/direct.h"
["dxgidebug.h"]="https://raw.githubusercontent.com/apitrace/dxsdk/master/Include/dxgidebug.h"
["dxva.h"]="https://raw.githubusercontent.com/nihon-tc/Rtest/master/header/Microsoft%20SDKs/Windows/v7.0A/Include/dxva.h"
["intrin.h"]="https://raw.githubusercontent.com/yuikns/intrin/master/intrin.h"
["arm_neon.h"]="https://raw.githubusercontent.com/gcc-mirror/gcc/master/gcc/config/arm/arm_neon.h"
["conio.h"]="https://raw.githubusercontent.com/zoelabbb/conio.h/master/conio.h"
)
for file in "${!files[@]}"; do
curl -LSso "$workspace/include/$file" "${files[$file]}"
done
}
install_rustc() {
echo "Installing RustUp"
curl -fsS --proto '=https' --tlsv1.2 'https://sh.rustup.rs' | sh -s -- --default-toolchain stable -y &>/dev/null
source "$HOME/.cargo/env"
[[ -f "$HOME/.zshrc" ]] && source "$HOME/.zshrc"
[[ -f "$HOME/.bashrc" ]] && source "$HOME/.bashrc"
}
check_ffmpeg_version() {
local ffmpeg_repo
ffmpeg_repo="$1"
ffmpeg_git_version=$(git ls-remote --tags "$ffmpeg_repo" |
awk -F'/' '/n[0-9]+(\.[0-9]+)*(-dev)?$/ {print $3}' |
grep -Ev '\-dev' | sort -ruV | head -n1)
echo "$ffmpeg_git_version"
}
download() {
local download_file download_path download_url giflib_regex output_directory target_directory target_file
download_path="$packages"
download_url="$1"
download_file="${2:-"${1##*/}"}"
giflib_regex='cfhcable\.dl\.sourceforge\.net'
if [[ "$download_file" =~ tar\. ]]; then
output_directory="${download_file%.*}"
output_directory="${output_directory%.*}"
else
output_directory="${download_file%.*}"
fi
target_file="$download_path/$download_file"
target_directory="$download_path/$output_directory"
if [[ -f "$target_file" ]]; then
log "$download_file is already downloaded."
else
log "Downloading \"$download_url\" saving as \"$download_file\""
if ! curl -LSso "$target_file" "$download_url"; then
warn "Failed to download \"$download_file\". Second attempt in 3 seconds..."
sleep 3
curl -LSso "$target_file" "$download_url" || fail "Failed to download \"$download_file\". Exiting... Line: $LINENO"
fi
log "Download Completed"
fi
[[ -d "$target_directory" ]] && rm -fr "$target_directory"
mkdir -p "$target_directory"
if ! tar -xf "$target_file" -C "$target_directory" --strip-components 1 2>/dev/null; then
rm "$target_file"
[[ "$download_url" =~ $giflib_regex ]] && return 0
fail "Failed to extract the tarball \"$download_file\" and was deleted. Re-run the script to try again. Line: $LINENO"
fi
log "File extracted: $download_file"
cd "$target_directory" || fail "Failed to cd into \"$target_directory\". Line: $LINENO"
}
git_caller() {
git_url="$1"
repo_name="$2"
third_flag="$3"
recurse_flag=0
[[ "$3" == "recurse" ]] && recurse_flag=1
version=$(git_clone "$git_url" "$repo_name" "$third_flag")
version="${version//Cloning completed: /}"
}
git_clone() {
local repo_flag repo_name repo_url target_directory version
repo_url="$1"
repo_name="${2:-${1##*/}}"
repo_name="${repo_name//\./-}"
repo_flag="$3"
target_directory="$packages/$repo_name"
case "$repo_flag" in
ant)
version=$(git ls-remote --tags "https://github.com/apache/ant.git" |
awk -F'/' '/\/v?[0-9]+\.[0-9]+(\.[0-9]+)?(\^\{\})?$/ {tag = $4; sub(/^v/, "", tag); if (tag !~ /\^\{\}$/) print tag}' |
sort -ruV | head -n1)
;;
ffmpeg)
version=$(git ls-remote --tags "https://git.ffmpeg.org/ffmpeg.git" |
awk -F/ '/\/n?[0-9]+\.[0-9]+(\.[0-9]+)?(\^\{\})?$/ {tag = $3; sub(/^[v]/, "", tag); print tag}' |
grep -v '\^{}' | sort -ruV | head -n1)
;;
*)
version=$(git ls-remote --tags "$repo_url" |
awk -F'/' '/\/v?[0-9]+\.[0-9]+(\.[0-9]+)?(-[0-9]+)?(\^\{\})?$/ {tag = $3; sub(/^v/, "", tag); print tag}' |
grep -v '\^{}' | sort -ruV | head -n1)
[[ -z "$version" ]] && version=$(git ls-remote "$repo_url" | awk '/HEAD/ {print substr($1,1,7)}')
[[ -z "$version" ]] && version="unknown"
;;
esac
[[ -f "$packages/$repo_name.done" ]] && store_prior_version=$(cat "$packages/$repo_name.done")
if [[ ! "$version" == "$store_prior_version" ]]; then
if [[ "$recurse_flag" -eq 1 ]]; then
recurse="--recursive"
elif [[ -n "$3" ]]; then
target_directory="$download_path/$3"
fi
[[ -d "$target_directory" ]] && rm -fr "$target_directory"
if ! git clone --depth 1 $recurse -q "$repo_url" "$target_directory"; then
warn "Failed to clone \"$target_directory\". Second attempt in 5 seconds..."
sleep 5
git clone --depth 1 $recurse -q "$repo_url" "$target_directory" || fail "Failed to clone \"$target_directory\". Exiting script. Line: $LINENO"
fi
cd "$target_directory" || fail "Failed to cd into \"$target_directory\". Line: $LINENO"
fi
echo "Cloning completed: $version"
}
gnu_repo() {
local repo
repo="$1"
repo_version=$(curl -fsS "$repo" | grep -oP '[a-z]+-\K(([0-9.]*[0-9]+)){2,}' | sort -ruV | head -n1)
}
github_repo() {
local count max_attempts repo url url_flag
repo="$1"
url="$2"
url_flag="$3"
count=1
max_attempts=10
[[ -z "$repo" || -z "$url" ]] && fail "Git repository and URL are required. Line: $LINENO"
while [[ "$count" -le "$max_attempts" ]]; do
if [[ "$url_flag" -eq 1 ]]; then
repo_version=$(
curl -fsSL "https://github.com/xiph/rav1e/tags/" |
grep -oP 'p[0-9]+\.tar\.gz' | sed 's/\.tar\.gz//g' |
head -n1
)
if [[ -n "$repo_version" ]]; then
return 0
else
continue
fi
else
if [[ "$repo" == "FFmpeg/FFmpeg" ]]; then
curl_cmd=$(curl -fsSL "https://github.com/FFmpeg/FFmpeg/tags/" | grep -oP 'href="[^"]*[6-9]\..*\.tar\.gz"' | grep -v '\-dev' | sort -un)
else
curl_cmd=$(curl -fsSL "https://github.com/$repo/$url" | grep -oP 'href="[^"]*\.tar\.gz"')
fi
fi
line=$(echo "$curl_cmd" | grep -oP 'href="[^"]*\.tar\.gz"' | sed -n "${count}p")
if echo "$line" | grep -qP 'v*(\d+[._]\d+(?:[._]\d*){0,2})\.tar\.gz'; then
repo_version=$(echo "$line" | grep -oP '(\d+[._]\d+(?:[._]\d+){0,2})')
break
else
((count++))
fi
done
while [[ "$repo_version" =~ $git_regex ]]; do
curl_cmd=$(curl -fsSL "https://github.com/$repo/$url" | grep -oP 'href="[^"]*\.tar\.gz"')
line=$(echo "$curl_cmd" | grep -oP 'href="[^"]*\.tar\.gz"' | sed -n "${count}p")
if echo "$line" | grep -qP 'v*(\d+[._]\d+(?:[._]\d*){0,2})\.tar\.gz'; then
repo_version=$(echo "$line" | grep -oP '(\d+[._]\d+(?:[._]\d+){0,2})')
break
else
((count++))
fi
done
}
fetch_repo_version() {
local api_path base_url commit_id_jq_filter count project short_id_jq_filter version_jq_filter
base_url="$1"
project="$2"
api_path="$3"
version_jq_filter="$4"
short_id_jq_filter="$5"
commit_id_jq_filter="$6"
count=0
response=$(curl -fsS "$base_url/$project/$api_path") || fail "Failed to fetch data from $base_url/$project/$api_path in the function \"fetch_repo_version\". Line: $LINENO"
version=$(echo "$response" | jq -r ".[$count]$version_jq_filter")
while [[ "$version" =~ $git_regex ]]; do
((++count))
version=$(echo "$response" | jq -r ".[$count]$version_jq_filter")
[[ -z "$version" || "$version" == "null" ]] && fail "No suitable release version found in the function \"fetch_repo_version\". Line: $LINENO"
done
short_id=$(echo "$response" | jq -r ".[$count]$short_id_jq_filter")
commit_id=$(echo "$response" | jq -r ".[$count]$commit_id_jq_filter")
repo_version="${version#v}"
repo_version_1="$commit_id"
repo_short_version_1="$short_id"
}
find_git_repo() {
local git_repo url url_action url_flag
url="$1"
git_repo="$2"
url_action="$3"
url_flag="$4"
case "$url_flag" in
enabled) set_url_flag=1 ;;
*) set_url_flag=0 ;;
esac
case "$url_action" in
B) set_type="branches" ;;
T) set_type="tags" ;;
*) set_type="$3" ;;
esac
case "$git_repo" in
1) set_repo="github_repo" ;;
2) fetch_repo_version "https://code.videolan.org/api/v4/projects" "$url" "repository/$set_type" ".name" ".commit.short_id" ".commit.id"; return 0 ;;
3) fetch_repo_version "https://gitlab.com/api/v4/projects" "$url" "repository/tags" ".name" ".commit.short_id" ".commit.id"; return 0 ;;
4) fetch_repo_version "https://gitlab.freedesktop.org/api/v4/projects" "$url" "repository/tags" ".name" ".commit.short_id" ".commit.id"; return 0 ;;
5) fetch_repo_version "https://gitlab.gnome.org/api/v4/projects" "$url" "repository/tags" ".name" ".commit.short_id" ".commit.id"; return 0 ;;
6) fetch_repo_version "https://salsa.debian.org/api/v4/projects" "$url" "repository/tags" ".name" ".commit.short_id" ".commit.id"; return 0 ;;
*) fail "Unsupported repository type in the function \"find_git_repo\". Line: $LINENO" ;;
esac
"$set_repo" "$url" "$set_type" "$set_url_flag" 2>/dev/null
}
execute() {
echo "$ $*"
if [[ "$debug" == "ON" ]]; then
if ! output=$("$@"); then
notify-send -t 5000 "Failed to execute $*" 2>/dev/null
fail "Failed to execute $*"
fi
else
if ! output=$("$@" 2>/dev/null); then
notify-send -t 5000 "Failed to execute $*" 2>/dev/null
fail "Failed to execute $*"
fi
fi
}
build() {
echo
echo -e "${GREEN}Building${NC} ${YELLOW}$1${NC} - ${GREEN}version ${YELLOW}$2${NC}"
echo "========================================================"
if [[ -f "$packages/$1.done" ]]; then
if grep -Fx "$2" "$packages/$1.done" >/dev/null; then
echo "$1 version $2 already built. Remove $packages/$1.done lockfile to rebuild it."
return 1
elif "$LATEST"; then
echo "$1 is outdated and will be rebuilt with latest version $2"
return 0
else
echo "$1 is outdated, but will not be rebuilt. Pass in --latest to rebuild it or remove $packages/$1.done lockfile."
return 1
fi
fi
return 0
}
build_done() {
echo "$2" > "$packages/$1.done"
}
library_exists() {
if ! [[ -x $(pkg-config --exists --print-errors "$1" 2>&1) ]]; then
return 1
fi
return 0
}
determine_libtool_version() {
case "$STATIC_VER" in
20.04|22.04|23.04|23.10)
libtool_version="2.4.6"
;;
11|12|24.04|msft)
libtool_version="2.4.7"
;;
esac
}
# Function to setup a python virtual environment and install packages with pip
setup_python_venv_and_install_packages() {
local -a parse_package
local parse_path="$1"
shift
parse_package=("$@")
echo "Creating a Python virtual environment at $parse_path..."
python3 -m venv "$parse_path" || fail "Failed to create virtual environment"
echo "Activating the virtual environment..."
source "$parse_path/bin/activate" || fail "Failed to activate virtual environment"
echo "Installing Python packages: ${parse_package[*]}..."
pip install "${parse_package[@]}" || fail "Failed to install packages"
echo "Deactivating the virtual environment..."
deactivate
echo "Python virtual environment setup and package installation completed."
}
find_cuda_json_file() {
if [[ -f "/opt/cuda/version.json" ]]; then
locate_cuda_json_file="/opt/cuda/version.json"
elif [[ -f "/usr/local/cuda/version.json" ]]; then
locate_cuda_json_file="/usr/local/cuda/version.json"
fi
echo "$locate_cuda_json_file"
}
# PRINT THE SCRIPT OPTIONS
usage() {
echo
echo "Usage: $script_name [options]"
echo
echo "Options:"
echo " -h, --help Display usage information"
echo " --compiler=<gcc|clang> Set the default CC and CXX compiler (default: gcc)"
echo " -b, --build Starts the build process"
echo " -c, --cleanup Remove all working dirs"
echo " -g, --google-speech Enable Google Speech for audible error messages (google_speech must already be installed to work)."
echo " -j, --jobs <number> Set the number of CPU threads for parallel processing"
echo " -l, --latest Force the script to build the latest version of dependencies if newer version is available"
echo " -n, --enable-gpl-and-non-free Enable GPL and non-free codecs - https://ffmpeg.org/legal.html"
echo " -v, --version Display the current script version"
echo
echo "Example: bash $script_name --build --compiler=clang -j 8"
echo
}
COMPILER_FLAG=""
CONFIGURE_OPTIONS=()
LATEST=false
LDEXEFLAGS=""
NONFREE_AND_GPL=false
while (("$#" > 0)); do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--version)
echo
log "The script version is: $script_version"
exit 0
;;
-n|--enable-gpl-and-non-free)
CONFIGURE_OPTIONS+=("--enable-"{gpl,libsmbclient,libcdio,nonfree})
NONFREE_AND_GPL=true
shift
;;
-b|--build)
bflag="-b"
shift
;;
-c|--cleanup)
cflag="-c"
cleanup
shift
;;
-l|--latest)
LATEST=true
shift
;;
--compiler=gcc|--compiler=clang)
COMPILER_FLAG="${1#*=}"
shift
;;
-j|--jobs)
threads="$2"
shift 2
;;
-g|--google-speech)
google_speech_flag=true
shift
;;
*)
usage
exit 1
;;
esac
done
if [[ -z "$threads" ]]; then
# Set the available CPU thread and core count for parallel processing (speeds up the build process)
if [[ -f /proc/cpuinfo ]]; then
threads=$(grep --count ^processor /proc/cpuinfo)
else
threads=$(nproc --all)
fi
fi
MAKEFLAGS="-j$threads"
if [[ -z "$COMPILER_FLAG" ]] || [[ "$COMPILER_FLAG" == "gcc" ]]; then
CC="gcc"
CXX="g++"
elif [[ "$COMPILER_FLAG" == "clang" ]]; then
CC="clang"
CXX="clang++"
else
fail "Invalid compiler specified. Valid options are 'gcc' or 'clang'."
fi
export CC CXX MAKEFLAGS
echo
log "Utilizing $threads CPU threads"
echo
if "$NONFREE_AND_GPL"; then
warn "With GPL and non-free codecs enabled"
echo
fi
if [[ -n "$LDEXEFLAGS" ]]; then
echo "The script has been configured to run in full static mode."
echo
fi
source_path() {
ccache_dir="/usr/lib/ccache"
PATH="$ccache_dir:/usr/local/cuda/bin:$workspace/bin:$HOME/.local/bin:$PATH"
export PATH
}
source_path
remove_duplicate_paths() {
local -a path_array
local IFS new_path seen
IFS=':'
path_array=("$PATH")
declare -A seen
for path in "${path_array[@]}"; do
if [[ -n "$path" && ! -v seen[$path] ]]; then
seen[$path]=1
if [[ -z "$new_path" ]]; then
new_path="$path"
else
new_path="$new_path:$path"
fi
fi
done
PATH="$new_path"
export PATH
}
remove_duplicate_paths
# Set the pkg_config_path variable
PKG_CONFIG_PATH="$workspace/lib64/pkgconfig:$workspace/lib/x86_64-linux-gnu/pkgconfig:$workspace/lib/pkgconfig:$workspace/share/pkgconfig"
PKG_CONFIG_PATH+=":/usr/local/lib64/x86_64-linux-gnu:/usr/local/lib64/pkgconfig:/usr/local/lib/x86_64-linux-gnu/pkgconfig:/usr/local/lib/pkgconfig"
PKG_CONFIG_PATH+=":/usr/local/share/pkgconfig:/usr/lib/x86_64-linux-gnu/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig"
export PKG_CONFIG_PATH
check_amd_gpu() {
if lshw -C display 2>&1 | grep -Eioq "amdgpu|amd"; then
echo "AMD GPU detected"
elif dpkg -l 2>&1 | grep -iq "amdgpu"; then
echo "AMD GPU detected"
elif lspci 2>&1 | grep -i "amd"; then
echo "AMD GPU detected"
else
echo "No AMD GPU detected"
fi
}
check_remote_cuda_version() {
# Use curl to fetch the HTML content of the page
local base_version cuda_regex html update_version
html=$(curl -fsS "https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html")
# Parse the version directly from the fetched content
cuda_regex='CUDA\ ([0-9]+\.[0-9]+)(\ Update\ ([0-9]+))?'
if [[ "$html" =~ $cuda_regex ]]; then
base_version="${BASH_REMATCH[1]}"
update_version="${BASH_REMATCH[3]}"
remote_cuda_version="$base_version"
# Append the update number if present
if [[ -n "$update_version" ]]; then
remote_cuda_version+=".$update_version"
else
remote_cuda_version+=".0"
fi
fi
}
set_java_variables() {
source_path
if [[ -d "/usr/lib/jvm/" ]]; then
locate_java=$(
find /usr/lib/jvm/ -type d -name "java-*-openjdk*" |
sort -ruV | head -n1
)
else
latest_openjdk_version=$(
apt-cache search '^openjdk-[0-9]+-jdk-headless$' |
sort -ruV | head -n1 | awk '{print $1}'
)
if apt -y install $latest_openjdk_version; then
set_java_variables
else
fail "Could not install openjdk. Line: $LINENO"
fi
fi
java_include=$(
find /usr/lib/jvm/ -type f -name "javac" |
sort -ruV | head -n1 | xargs dirname |
sed 's/bin/include/'
)
CPPFLAGS+=" -I$java_include"
JDK_HOME="$locate_java"
JAVA_HOME="$locate_java"
PATH="$PATH:$JAVA_HOME/bin"
export CPPFLAGS JDK_HOME JAVA_HOME PATH
remove_duplicate_paths
}
set_ant_path() {
export ANT_HOME="$workspace/ant"
if [[ ! -d "$workspace/ant/bin" ]] || [[ ! -d "$workspace/ant/lib" ]]; then
mkdir -p "$workspace/ant/bin" "$workspace/ant/lib" 2>/dev/null
fi
}
nvidia_architecture() {
if [[ -n $(find_cuda_json_file) ]]; then
gpu_name=$(nvidia-smi --query-gpu=gpu_name --format=csv,noheader | head -n1)
case "$gpu_name" in
"Quadro P2000"|"NVIDIA GeForce GT 1010"|"NVIDIA GeForce GTX 1030"|"NVIDIA GeForce GTX 1050"|"NVIDIA GeForce GTX 1060"|"NVIDIA GeForce GTX 1070"|"NVIDIA GeForce GTX 1080"|"NVIDIA TITAN Xp"|"NVIDIA Tesla P40"|"NVIDIA Tesla P4")
nvidia_arch_type="compute_61,code=sm_61"
;;
"NVIDIA GeForce GTX 1180"|"NVIDIA GeForce GTX Titan V"|"Quadro GV100"|"NVIDIA Tesla V100")
nvidia_arch_type="compute_70,code=sm_70"
;;
"NVIDIA GeForce GTX 1660 Ti"|"NVIDIA GeForce RTX 2060"|"NVIDIA GeForce RTX 2070"|"NVIDIA GeForce RTX 2080"|"Quadro 4000"|"Quadro 5000"|"Quadro 6000"|"Quadro 8000"|"NVIDIA T1000"|"NVIDIA T2000"|"NVIDIA Tesla T4")
nvidia_arch_type="compute_75,code=sm_75"
;;
"NVIDIA GeForce RTX 3050"|"NVIDIA GeForce RTX 3060"|"NVIDIA GeForce RTX 3070"|"NVIDIA GeForce RTX 3080"|"NVIDIA GeForce RTX 3080 Ti"|"NVIDIA GeForce RTX 3090"|"NVIDIA RTX A2000"|"NVIDIA RTX A3000"|"NVIDIA RTX A4000"|"NVIDIA RTX A5000"|"NVIDIA RTX A6000")
nvidia_arch_type="compute_86,code=sm_86"
;;
"NVIDIA GeForce RTX 4080"|"NVIDIA GeForce RTX 4090")
nvidia_arch_type="compute_89,code=sm_89"
;;
"NVIDIA H100")
nvidia_arch_type="compute_90,code=sm_90"
;;
*) echo "If you get a driver version \"mismatch\" when executing the command \"nvidia-smi\", reboot your PC and rerun the script."
echo
fail "Failed to set the variable \"nvidia_arch_type\". Line: $LINENO"
;;
esac
else
return 1
fi
}
download_cuda() {
local -a options
local choice distro cuda_version="12.6.1"
echo
echo "Pick your Linux version from the list below:"
echo "Supported architecture: x86_64"
echo
options=(
"Debian 11"
"Debian 12"
"Ubuntu 20.04"
"Ubuntu 22.04"
"Ubuntu 24.04"
"Ubuntu WSL"
"Skip"
)
select choice in "${options[@]}"; do
case "$choice" in
"Debian 11") distro="debian11" ;;
"Debian 12") distro="debian12" ;;
"Ubuntu 20.04") distro="ubuntu2004" ;;
"Ubuntu 22.04") distro="ubuntu2204" ;;
"Ubuntu 24.04") distro="ubuntu2404" ;;
"Ubuntu WSL") distro="wsl-ubuntu" ;;
Skip) return ;;
*) echo "Invalid choice. Please try again."; continue ;;
esac
break
done
mkdir -p "$packages/nvidia-cuda"
if [[ "$distro" == debian* ]]; then
wget --show-progress -cqO "$packages/nvidia-cuda/cuda-repo-$distro-12-6-local_$cuda_version-560.35.03-1_amd64.deb" \
"https://developer.download.nvidia.com/compute/cuda/$cuda_version/local_installers/cuda-repo-$distro-12-6-local_$cuda_version-560.35.03-1_amd64.deb"
sudo dpkg -i "$packages/nvidia-cuda/cuda-repo-$distro-12-6-local_$cuda_version-560.35.03-1_amd64.deb"
sudo cp /var/cuda-repo-$distro-12-6-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo add-apt-repository contrib
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-6
elif [[ "$distro" == ubuntu* || "$distro" == "wsl-ubuntu" ]]; then
wget --show-progress -cqO "$packages/nvidia-cuda/cuda-$distro.pin" \
"https://developer.download.nvidia.com/compute/cuda/repos/$distro/x86_64/cuda-$distro.pin"
sudo mv "$packages/nvidia-cuda/cuda-$distro.pin" /etc/apt/preferences.d/cuda-repository-pin-600
if [[ "$distro" == "wsl-ubuntu" ]]; then
wget --show-progress -cqO "$packages/nvidia-cuda/cuda-repo-$distro-12-6-local_$cuda_version-1_amd64.deb" \
"https://developer.download.nvidia.com/compute/cuda/$cuda_version/local_installers/cuda-repo-$distro-12-6-local_$cuda_version-1_amd64.deb"
sudo dpkg -i "$packages/nvidia-cuda/cuda-repo-$distro-12-6-local_$cuda_version-1_amd64.deb"
else
wget --show-progress -cqO "$packages/nvidia-cuda/cuda-repo-$distro-12-6-local_$cuda_version-560.35.03-1_amd64.deb" \
"https://developer.download.nvidia.com/compute/cuda/$cuda_version/local_installers/cuda-repo-$distro-12-6-local_$cuda_version-560.35.03-1_amd64.deb"
sudo dpkg -i "$packages/nvidia-cuda/cuda-repo-$distro-12-6-local_$cuda_version-560.35.03-1_amd64.deb"
fi
sudo cp /var/cuda-repo-$distro-12-6-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-6
fi
echo "CUDA SDK Toolkit version $cuda_version has been installed."
}
# Function to detect the environment and check for an NVIDIA GPU
check_nvidia_gpu() {
local found
path_exists=0
found=0
gpu_info=""
if ! grep -Eiq '(microsoft|slyfox1186)' /proc/version; then
if lspci | grep -qi nvidia; then
is_nvidia_gpu_present="NVIDIA GPU detected"
else
is_nvidia_gpu_present="NVIDIA GPU not detected"
fi
else
for dir in "/mnt/c" "/c"; do
if [[ -d "$dir/Windows/System32" ]]; then
path_exists=1
if [[ -f "$dir/Windows/System32/cmd.exe" ]]; then
gpu_info=$("$dir/Windows/System32/cmd.exe" /d /c "wmic path win32_VideoController get name | findstr /i nvidia" 2>/dev/null)
if [[ -n "$gpu_info" ]]; then
found=1
is_nvidia_gpu_present="NVIDIA GPU detected"
break
fi
fi
fi
done
if [[ "$path_exists" -eq 0 ]]; then
is_nvidia_gpu_present="C drive paths '/mnt/c/' and '/c/' do not exist."
elif [[ "$found" -eq 0 ]]; then
is_nvidia_gpu_present="NVIDIA GPU not detected"
fi
fi
}
get_local_cuda_version() {
[[ -f "/usr/local/cuda/version.json" ]] && jq -r '.cuda.version' < "/usr/local/cuda/version.json"
}
# Required Geforce CUDA development packages
install_cuda() {
local choice
echo "Checking GPU Status"
echo "========================================================"
amd_gpu_test=$(check_amd_gpu)
check_nvidia_gpu
if [[ -n "$amd_gpu_test" ]] && [[ "$is_nvidia_gpu_present" == "NVIDIA GPU not detected" ]]; then
log "AMD GPU detected."
log "Nvidia GPU not detected"
warn "CUDA Hardware Acceleration will not be enabled"
return 0
elif [[ "$is_nvidia_gpu_present" == "NVIDIA GPU detected" ]]; then
log "Nvidia GPU detected"
log "Determining if CUDA is installed..."
check_remote_cuda_version
local_cuda_version=$(get_local_cuda_version)
if [[ -z "$local_cuda_version" ]]; then
echo "The latest CUDA version available is: $remote_cuda_version"
echo "CUDA is not currently installed."
echo
read -p "Do you want to install the latest CUDA version? (yes/no): " choice
[[ "$choice" =~ ^(yes|y)$ ]] && download_cuda
elif [[ "$local_cuda_version" == "$remote_cuda_version" ]]; then
log "CUDA is already installed and up to date."
return 0
else
echo "The installed CUDA version is: $local_cuda_version"
echo "The latest CUDA version available is: $remote_cuda_version"
read -p "Do you want to update/reinstall CUDA to the latest version? (yes/no): " choice
[[ "$choice" =~ ^(yes|y)$ ]] && download_cuda || return 0
fi
else
gpu_flag=1
fi
return 0
}
# Required build packages
apt_pkgs() {
local -a pkgs=() missing_packages=() available_packages=() unavailable_packages=()
local pkg
# Function to find the latest version of a package by pattern
find_latest_version() {
apt-cache search "^$1" | sort -ruV | head -n1 | awk '{print $1}'
}
# Define an array of apt package names
pkgs=(
$1 $(find_latest_version 'libc\+\+abi-[0-9]+-dev') $(find_latest_version 'libc\+\+-[0-9]+-dev')
$(find_latest_version 'libunwind-[0-9]+-dev') $(find_latest_version 'nvidia-driver[0-9-]*')
$(find_latest_version 'openjdk-[0-9]+-jdk') $(find_latest_version 'gcc-[1-3]*-plugin-dev')
asciidoc autoconf autoconf-archive automake autopoint bc binutils bison build-essential cargo ccache checkinstall
curl doxygen fcitx-libs-dev flex flite1-dev gawk gcc gettext gimp-data git gnome-desktop-testing gnustep-gui-runtime
golang-go google-perftools gperf gtk-doc-tools guile-3.0-dev help2man imagemagick jq junit ladspa-sdk lib32stdc++6
libasound2-dev libass-dev libaudio-dev libavfilter-dev libbabl-0.1-0 libbluray-dev libbpf-dev libbs2b-dev libbz2-dev
libc6 libc6-dev libcaca-dev libcairo2-dev libcdio-dev libcdio-paranoia-dev libcdparanoia-dev libchromaprint-dev
libcjson-dev libcodec2-dev libcrypto++-dev libcurl4-openssl-dev libdav1d-dev libdbus-1-dev libde265-dev libdevil-dev
libdmalloc-dev libdrm-dev libdvbpsi-dev libebml-dev libegl1-mesa-dev libffi-dev libflac-dev libgbm-dev libgdbm-dev
libgegl-common libgl1-mesa-dev libgles2-mesa-dev libglfw3-dev libglib2.0-dev libglu1-mesa-dev libgme-dev libgmock-dev
libgnutls28-dev libgoogle-perftools-dev libgsm1-dev libgtest-dev libgvc6 libibus-1.0-dev libintl-perl libjack-dev
libladspa-ocaml-dev libldap2-dev libleptonica-dev liblilv-dev liblz-dev liblzma-dev liblzo2-dev libmathic-dev libmatroska-dev
libmbedtls-dev libmetis5 libmfx-dev libmodplug-dev libmp3lame-dev libmusicbrainz5-dev libnuma-dev libpango1.0-dev libperl-dev
libplacebo-dev libpocketsphinx-dev libportaudio-ocaml-dev libpsl-dev libpstoedit-dev libpulse-dev librabbitmq-dev libraw-dev
librtmp-dev librubberband-dev librust-gstreamer-base-sys-dev libsctp-dev libserd-dev libshine-dev libsmbclient-dev libsnappy-dev
libsndio-dev libspeex-dev libsphinxbase-dev libsqlite3-dev libsratom-dev libssh-dev libssl-dev libsystemd-dev libtalloc-dev
libtesseract-dev libticonv-dev libtool libwavpack-dev libtwolame-dev libudev-dev libv4l-dev libva-dev libvdpau-dev libvidstab-dev
libvlccore-dev libvo-amrwbenc-dev libvpl-dev libx11-dev libxcursor-dev libxext-dev libxfixes-dev libxi-dev libxkbcommon-dev libxrandr-dev
libxss-dev libxvidcore-dev libzmq3-dev libzvbi-dev libzzip-dev lsb-release lshw lzma-dev m4 mesa-utils pandoc python3 python3-pip
python3-venv ragel re2c scons texi2html texinfo tk-dev unzip valgrind wget xmlto ccache and libssl-dev
)
[[ "$OS" == "Debian" && "$is_nvidia_gpu_present" == "NVIDIA GPU detected" ]] && pkgs+=("nvidia-smi")
log "Checking package installation status..."
# Find missing and categorize packages in one loop
for pkg in "${pkgs[@]}"; do
if ! dpkg-query -W -f='${Status}' "$pkg" 2>/dev/null | grep -q "ok installed"; then
if apt-cache show "$pkg" >/dev/null 2>&1; then
available_packages+=("$pkg")
else
unavailable_packages+=("$pkg")
fi
fi
done