forked from slyfox1186/ffmpeg-build-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-ffmpeg.sh
executable file
·2859 lines (2561 loc) · 109 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.6.4
# Updated: 04.28.24
# Purpose: build ffmpeg from source code with addon development libraries
# also compiled from source to help ensure the latest functionality
# Supported Distros: Arch Linux
# Debian 11|12
# Ubuntu (20|22|23).04 & 23.10
# Supported architecture: x86_64
# CUDA SDK Toolkit: Updated to version 12.4.1
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.6.4"
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"
# 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 -I/usr/x86_64-linux-gnu/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 CPPFLAGS CXXFLAGS 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/script-repo"
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/script-repo/issues"
echo
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
}
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
execute cargo clean
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() {
get_rustc_ver=$(rustc --version | grep -oP '[0-9.]+' | head -n1)
if [[ ! "$get_rustc_ver" == "1.77.2" ]]; then
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"
fi
}
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 output_directory target_directory target_file
download_path="$packages"
download_url="$1"
download_file="${2:-"${1##*/}"}"
if [[ "$download_file" =~ tar. ]]; then
output_directory="${download_file%.*}"
output_directory="${3:-"${output_directory%.*}"}"
else
output_directory="${3:-"${download_file%.*}"}"
fi
target_file="$download_path/$download_file"
target_directory="$download_path/$output_directory"
if [[ -f "$target_file" ]]; then
echo "$download_file is already downloaded."
else
echo "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
echo "Download Completed"
fi
[[ -d "$target_directory" ]] && rm -fr "$target_directory"
mkdir -p "$target_directory"
if [[ -n "$3" ]]; then
if ! tar -xf "$target_file" -C "$target_directory" 2>/dev/null; then
rm "$target_file"
fail "Failed to extract the tarball \"$download_file\" and was deleted. Re-run the script to try again. Line: $LINENO"
fi
else
if ! tar -xf "$target_file" -C "$target_directory" --strip-components 1 2>/dev/null; then
rm "$target_file"
fail "Failed to extract the tarball \"$download_file\" and was deleted. Re-run the script to try again. Line: $LINENO"
fi
fi
printf "%s\n\n" "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
rm -fr "$target_directory" 2>/dev/null
if ! git clone --depth 1 $recurse -q "$repo_url" "$target_directory"; then
warn "Failed to clone \"$target_directory\". Second attempt in 3 seconds..."
sleep 3
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() {
version=$(curl -fsS "$1" | 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
curl_cmd=$(curl -fsSL "https://github.com/$repo/$url" | grep -o 'href="[^"]*\.tar\.gz"')
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 -o '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 base_url="$1"
local project="$2"
local api_path="$3"
local version_jq_filter="$4"
local short_id_jq_filter="$5"
local commit_id_jq_filter="$6"
local 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")
[[ "$base_url" != 536 ]] && while [[ "$version" =~ $git_regex ]]; do
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 url="$1"
local git_repo="$2"
local url_action="$3"
local 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
}
# Function to setup a python virtual environment and install packages with pip
setup_python_venv_and_install_packages() {
local parse_path="$1"
shift
local 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 " -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"
;;
-b|--build)
bflag="-b"
;;
-c|--cleanup)
cflag="-c"
cleanup
;;
-l|--latest)
LATEST="true"
;;
--compiler=gcc|--compiler=clang)
COMPILER_FLAG="${1#*=}"
shift
;;
-j|--jobs)
threads="$2"
shift 2
;;
*)
usage
exit 1
;;
esac
shift
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
if [[ -z "$bflag" ]]; then
if [[ -z "$cflag" ]]; then
usage
echo
exit 1
fi
exit 0
fi
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
# Set the path variable
if [[ -d "/usr/local/bin" ]]; then
cuda_bin_path="/usr/local/bin"
elif [[ -d "/opt/cuda/bin" ]]; then
cuda_bin_path="/opt/cuda/bin"
fi
if [[ -d /usr/lib/ccache/bin ]]; then
ccache_dir="/usr/lib/ccache/bin"
else
ccache_dir="/usr/lib/ccache"
fi
source_path() {
PATH="$ccache_dir:$cuda_bin_path:$workspace/bin:$HOME/.local/bin:/usr/local/ant/bin"
PATH+=":/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
export PATH
}
source_path
# 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
locate_java=$(find /usr/lib/jvm/ -type d -name "java-*-openjdk*" |
sort -ruV |
head -n1)
java_include=$(find /usr/lib/jvm/ -type f -name "javac" |
sort -ruV |
head -n1 |
xargs dirname |
sed 's/bin/include/')
CPPFLAGS+=" -I$java_include"
export CPPFLAGS
export JDK_HOME="$locate_java"
export JAVA_HOME="$locate_java"
export PATH="$PATH:$JAVA_HOME/bin"
}
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
}
cuda_download() {
local choice distro installer_path pin_file pkg_ext version version_serial
local cuda_version_number="$remote_cuda_version"
local cuda_pin_url="https://developer.download.nvidia.com/compute/cuda/repos"
local cuda_url="https://developer.download.nvidia.com/compute/cuda/$cuda_version_number"
echo
echo "Pick your Linux version from the list below:"
echo "Supported architecture: x86_64"
echo
options=(
"Debian 10"
"Debian 11"
"Debian 12"
"Ubuntu 20.04"
"Ubuntu 22.04"
"Ubuntu WSL"
"Arch Linux"
"Exit"
)
version_serial="12.4.1-550.54.15-1"
select choice in "${options[@]}"; do
case "$choice" in
"Debian 10") distro="debian10"; version="10-12-4"; pkg_ext="deb"; installer_path="local_installers/cuda-repo-debian${version}-local_${version_serial}_amd64.deb" ;;
"Debian 11") distro="debian11"; version="11-12-4"; pkg_ext="deb"; installer_path="local_installers/cuda-repo-debian${version}-local_${version_serial}_amd64.deb" ;;
"Debian 12") distro="debian12"; version="12-12-4"; pkg_ext="deb"; installer_path="local_installers/cuda-repo-debian${version}-local_${version_serial}_amd64.deb" ;;
"Ubuntu 20.04") distro="ubuntu2004"; version="12-4"; pkg_ext="pin"; pin_file="$distro/x86_64/cuda-ubuntu2004.pin"; installer_path="local_installers/cuda-repo-${distro}-${version}-local_${version_serial}_amd64.deb" ;;
"Ubuntu 22.04") distro="ubuntu2204"; version="12-4"; pkg_ext="pin"; pin_file="$distro/x86_64/cuda-ubuntu2204.pin"; installer_path="local_installers/cuda-repo-${distro}-${version}-local_${version_serial}_amd64.deb" ;;
"Ubuntu WSL") distro="wsl-ubuntu"; version="12-4"; version_ext="12.4.1-1"; pkg_ext="pin"; pin_file="$distro/x86_64/cuda-wsl-ubuntu.pin"; installer_path="local_installers/cuda-repo-${distro}-${version}-local_${version_ext}_amd64.deb" ;;
"Arch Linux")
git clone -q "https://gitlab.archlinux.org/archlinux/packaging/packages/cuda.git" || fail "Failed to clone Arch Linux CUDA repository"
cd cuda || fail "Unable to cd into the Arch Linux cuda directory"
makepkg -sif -C --needed --noconfirm || fail "The command makepkg failed to execute"
return
;;
"Exit") return ;;
*) echo "Invalid choice. Please try again."; continue ;;
esac
break
done
echo
echo "Downloading the CUDA SDK Toolkit - version $cuda_version_number"
mkdir -p "$packages/nvidia-cuda"
if [[ "$distro" == debian* ]]; then
distro="${distro//[0-9][0-9]}"
fi
if [[ "$pkg_ext" == "deb" ]]; then
package_name="$packages/nvidia-cuda/cuda-$distro-$cuda_version_number.$pkg_ext"
wget --show-progress -cqO "$package_name" "$cuda_url/$installer_path"
dpkg -i "$package_name"
cp -f "/var/cuda-repo-${distro}${version}-local/cuda-"*"-keyring.gpg" "/usr/share/keyrings/"
[[ "$distro" == "debian"* ]] && add-apt-repository -y contrib
elif [[ "$pkg_ext" == "pin" ]]; then
wget --show-progress -cqO "/etc/apt/preferences.d/cuda-repository-pin-600" "$cuda_pin_url/$pin_file"
package_name="$packages/nvidia-cuda/cuda-$distro-$cuda_version_number.deb"
wget --show-progress -cqO "$package_name" "$cuda_url/$installer_path"
dpkg -i "$package_name"
cp -f "/var/cuda-repo-${distro}-12-4-local/cuda-"*"-keyring.gpg" "/usr/share/keyrings/"
fi
apt update
apt install -y cuda-toolkit-12-4
}
# Function to detect the environment and check for an NVIDIA GPU
check_nvidia_gpu() {
local path_exists=0 found=0 gpu_info=""
if ! grep -Eiq '(microsoft|slyfox1186)' /proc/version; then
lspci | grep -qi nvidia && is_nvidia_gpu_present="NVIDIA GPU detected" || is_nvidia_gpu_present="NVIDIA GPU not detected"
else
for dir in "/mnt/c" "/c"; do
[[ -d "$dir/Windows/System32" ]] && { path_exists=1; [[ -f "$dir/Windows/System32/cmd.exe" ]] && { gpu_info=$("$dir/Windows/System32/cmd.exe" /d /c "wmic path win32_VideoController get name | findstr /i nvidia" 2>/dev/null); [[ -n "$gpu_info" ]] && { found=1; is_nvidia_gpu_present="NVIDIA GPU detected"; break; }; }; }
done
[[ "$path_exists" -eq 0 ]] && is_nvidia_gpu_present="C drive paths '/mnt/c/' and '/c/' do not exist." || [[ "$found" -eq 0 ]] && is_nvidia_gpu_present="NVIDIA GPU not detected"
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
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)$ ]] && cuda_download
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)$ ]] && cuda_download || return 0
fi
[[ "$OS" == "Arch" ]] && cuda_path=$(find /opt/cuda/ -type f -name "nvcc")
PATH+="$cuda_path:"
else
gpu_flag=1
fi
return 0
}
# Required build packages
apt_pkgs() {
local pkg available_packages unavailable_packages
local openjdk_pkg libcpp_pkg libcppabi_pkg libunwind_pkg
# Function to find the latest version of a package by pattern
find_latest_pkg_version() {
apt-cache search --names-only "$1" 2>/dev/null | awk '{print $1}' | grep -Eo "$2" | sort -ruV | head -n1
}
# Use the function to find the latest versions of specific packages
nvidia_driver=$(find_latest_pkg_version 'nvidia-driver-[0-9]+$' 'nvidia-driver-[0-9]+$')
nvidia_utils=$(find_latest_pkg_version 'nvidia-utils-[0-9]+$' 'nvidia-utils-[0-9]+$')
openjdk_pkg=$(find_latest_pkg_version '^openjdk-[0-9]+-jdk$' '^openjdk-[0-9]+-jdk')
libcpp_pkg=$(find_latest_pkg_version 'libc++*' 'libc\+\+-[0-9\-]+-dev')
libcppabi_pkg=$(find_latest_pkg_version 'libc++abi*' 'libc\+\+abi-[0-9]+-dev')
libunwind_pkg=$(find_latest_pkg_version 'libunwind*' 'libunwind-[0-9]+-dev')
gcc_plugin_pkg=$(find_latest_pkg_version 'gcc-1*-plugin-dev' 'gcc-1[0-9]+-plugin-dev')
# Define an array of apt package names
pkgs=(
$1 $libcppabi_pkg $libcpp_pkg $libunwind_pkg $nvidia_driver $nvidia_utils $openjdk_pkg $gcc_plugin_pkg
asciidoc autoconf autoconf-archive automake autopoint 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
google-perftools gperf gtk-doc-tools guile-3.0-dev help2man jq junit ladspa-sdk lib32stdc++6 libamd2 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 libgbm-dev libgdbm-dev libgegl-0.4-0 libgegl-common
libgimp2.0 libgl1-mesa-dev libgles2-mesa-dev libglib2.0-dev libgme-dev libgmock-dev libgnutls28-dev libgnutls30
libgoogle-perftools-dev libgoogle-perftools4 libgsm1-dev libgtest-dev libgvc6 libibus-1.0-dev libintl-perl
libladspa-ocaml-dev libldap2-dev libleptonica-dev liblilv-dev liblz-dev liblzma-dev liblzo2-dev libmp3lame-dev
libmathic-dev libmatroska-dev libmbedtls-dev libmetis5 libmfx-dev libmodplug-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 librsvg2-dev librtmp-dev librubberband-dev librust-gstreamer-base-sys-dev
libserd-dev libshine-dev libsmbclient-dev libsnappy-dev libsndio-dev libsoxr-dev libspeex-dev libsphinxbase-dev
libsqlite3-dev libsratom-dev libssh-dev libssl-dev libsystemd-dev libtalloc-dev libtesseract-dev libticonv-dev
libtool libtwolame-dev libudev-dev libv4l-dev libva-dev libvdpau-dev libvidstab-dev libvlccore-dev libvo-amrwbenc-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 libsctp-dev libflac-dev libglfw3-dev libgl1-mesa-dev
libglu1-mesa-dev
)
[[ "$OS" == "Debian" ]] && pkgs+=("nvidia-smi")
# Initialize arrays for missing, available, and unavailable packages
missing_packages=()
available_packages=()
unavailable_packages=()
log "Checking package installation status..."
# Loop through the array to find missing packages
for pkg in "${pkgs[@]}"; do
if ! dpkg-query -W -f='${Status}' "$pkg" 2>/dev/null | grep -q "ok installed"; then
missing_packages+=("$pkg")
fi
done
# Check availability of missing packages and categorize them
for pkg in "${missing_packages[@]}"; do
if apt-cache show "$pkg" >/dev/null 2>&1; then
available_packages+=("$pkg")
else
unavailable_packages+=("$pkg")
fi
done
# Print unavailable packages
if [[ "${#unavailable_packages[@]}" -gt 0 ]]; then
echo
warn "Unavailable packages:"
printf " %s\n" "${unavailable_packages[@]}"
fi
# Install available missing packages
if [[ "${#available_packages[@]}" -gt 0 ]]; then
echo
log "Installing available missing packages:"
printf " %s\n" "${available_packages[@]}"
echo
apt update
apt install "${available_packages[@]}"
apt -y autoremove
echo
else
log "No missing packages to install or all missing packages are unavailable."
echo
fi
}
check_avx512() {
# Checking if /proc/cpuinfo exists on the system
if [[ ! -f "/proc/cpuinfo" ]]; then
echo "Error: /proc/cpuinfo does not exist on this system." >&2
return 2
fi
# Search for AVX512 flag in cpuinfo
if grep -qo "avx512" "/proc/cpuinfo"; then
echo "ON"
else
echo "OFF"
fi
}
fix_libstd_libs() {
local libstdc_path
libstdc_path=$(find /usr/lib/x86_64-linux-gnu/ -type f -name 'libstdc++.so.6.0.*' | sort -ruV | head -n1)
if [[ ! -f "/usr/lib/x86_64-linux-gnu/libstdc++.so" ]] && [[ -f "$libstdc_path" ]]; then
exec ln -sf "$libstdc_path" "/usr/lib/x86_64-linux-gnu/libstdc++.so"
fi
}
fix_x265_libs() {
local x265_libs x265_libs_trim
x265_libs=$(find "$workspace"/lib/ -type f -name 'libx265.so.*' | sort -rV | head -n1)
x265_libs_trim=$(echo "$x265_libs" | sed "s:.*/::" | head -n1)
case "$OS" in
Arch)
cp -f "$x265_libs" "/usr/lib/$x265_libs_trim"
ln -sf "/usr/lib/$x265_libs_trim" "/usr/lib/libx265.so"
;;
*)
cp -f "$x265_libs" "/usr/lib/x86_64-linux-gnu"
ln -sf "/usr/lib/x86_64-linux-gnu/$x265_libs_trim" "/usr/lib/x86_64-linux-gnu/libx265.so"
;;
esac
}
fix_pulse_meson_build_file() {
# Replace the original pa_version_minor and pa_version_micro blocks
sed -i "/pa_version_major = version_split\[0\].split('v')\[0\]/a \\
if version_split.length() > 1\\n pa_version_minor = version_split[1]\\nelse\\n pa_version_minor = '0'\\nendif" "meson.build"