-
Notifications
You must be signed in to change notification settings - Fork 115
/
avbuild.sh
executable file
·1912 lines (1839 loc) · 80.7 KB
/
avbuild.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
# https://wiki.debian.org/Hardening#DEB_BUILD_HARDENING_RELRO_.28ld_-z_relro.29
# TODO: os arch-cc(e.g. winxp x86-gcc, winstore10 x86, win10 x64-clang, gcc is mingw or cygwin etc. depending on host env)
# TODO: link warning as error when checking ld flags. vc/lld-link: -WX
# TODO: cc_flags, linker_flags(linker only), os_flags, os_cc_flags, os_linker_flags, cc_linker_flags+=$(prepend_Wl linker_flags)
# remove -Wl, if LD_IS_LLD
# winphone clang+vs2013sdk: https://fate.libav.org/armv7-win32-clang-4.0/20190219150948 --arch=arm --cpu=armv7-a --as='clang -target armv7-win32-gnu' --cc='clang -target armv7-win32-msvc' --ld=lld-link --target-os=win32 --extra-cflags='-DWINAPI_FAMILY=WINAPI_FAMILY_PHONE_APP' --extra-ldflags='msvcrt.lib oldnames.lib -nodefaultlib:kernel32.lib -nodefaultlib:ole32.lib WindowsPhoneCore.lib' --enable-cross-compile --ar=llvm-ar --nm=llvm-nm
# wsl: https://docs.microsoft.com/en-us/archive/blogs/gillesk/building-ffmpeg-using-wsl
#PS4='+ $(gdate "+%s.%N")\011 '
#exec 3>&2 2>/tmp/bashstart.$$.log
#set -x
echo
echo "FFmpeg build tool for all platforms. Author: [email protected] 2013-2020"
echo "https://github.com/wang-bin/avbuild"
THIS_NAME=${0##*/}
THIS_DIR=$PWD
PLATFORMS="ios|iossimulator|tvos|tvossimulator|android|rpi|sunxi|vc|win|winrt|uwp|winphone|mingw"
echo "Usage:"
test -d $PWD/FFmpeg || echo " export FFSRC=/path/to/ffmpeg"
cat<<HELP
./$THIS_NAME [target_platform [target_architecture[-clang*/gcc*]]]
target_platform can be: ${PLATFORMS}
target_architecture can be: x86, x86_64, armv5, armv6, armv7, armv8, arm64
Build for host if no parameter is set.
Use a shortcut in tools dir if build for windows using MSVC.
Environment vars: USE_TOOLCHAIN(clang, gcc etc.), USE_LD(clang, lld etc.), USER_OPT, ANDROID_NDK, SYSROOT, ONECORE(="onecore", "onecore-$arch")
Add options via USER_OPT, \${platform}_OPT
config.sh and config-${target_platform}.sh is automatically included. config-lite.sh is for building smaller libraries.
HELP
test -f config.sh && . config.sh
USER_CONFIG=config-$1.sh
test -f $USER_CONFIG && . $USER_CONFIG
# TODO: use USER_OPT only
# set NDK_ROOT if compile for android
: ${NDK_ROOT:="$ANDROID_NDK"}
: ${LIB_OPT:="--enable-shared"}
#: ${FEATURE_OPT:="--enable-hwaccels"}
: ${DEBUG_OPT:="--disable-debug"}
: ${FORCE_LTO:=false}
: ${FFSRC:=$PWD/FFmpeg}
: ${LITE_BUILD:=false}
[[ "$LIB_OPT" == *"--disable-static"* ]] && FORCE_LTO=true
# other env vars to control build: NO_ENC, BITCODE, WINPHONE, VC_BUILD, FORCE_LTO (bool)
trap "kill -- -$$; rm -rf $THIS_DIR/.dir exit 3" SIGTERM SIGINT SIGKILL
export PATH_EXTRA="$PWD/tools/gas-preprocessor"
export PATH=$PWD/tools/gas-preprocessor:$PATH
: ${AMF_DIR:="$PWD/tools/dep/include"}
test -f $AMF_DIR/AMF/core/Version.h || unset AMF_DIR
echo FFSRC=$FFSRC
[[ "$FFSRC" == "$PWD" ]] && BUILD_DIR=$PWD
[ -f $FFSRC/configure ] && {
cd $FFSRC
export PATH=$PWD:$PATH # convert win path to unix path
export PATH_EXTRA="$PWD:$PATH_EXTRA"
cd -
echo "PATH: $PATH"
} || {
which configure &>/dev/null || {
echo 'ffmpeg configure script can not be found in "$PATH"'
exit 0
}
FFSRC=`which configure`
FFSRC=${FFSRC%/configure}
}
FFSRC_TOOLS=$FFSRC/fftools
if ! [ -d "$FFSRC_TOOLS" ]; then
FFSRC_TOOLS=$FFSRC_TOOLS/avtools
[ -d "$FFSRC_TOOLS" ] || FFSRC_TOOLS=$FFSRC
fi
# n1.2.8, 2.5.1, 2.5
cd $FFSRC
VER_SH=version.sh
[ -f $VER_SH ] || VER_SH=ffbuild/version.sh
[ -f $VER_SH ] || VER_SH=avbuild/version.sh
FFVERSION_FULL=`./$VER_SH`
FFMAJOR_GEN=`echo $FFVERSION_FULL |sed 's,[a-zA-Z]*\([0-9]*\)\..*,\1,'`
#echo $FFVERSION_FULL |grep '\.' || FFVERSION_FULL=`head -n 1 RELEASE` # stop updating in 6.x
echo $FFVERSION_FULL |grep '\.' || FFVERSION_FULL=`grep -m 1 'cut here' doc/APIchanges |sed 's,.* \([0-9]*\.[0-9]*\) .*,\1,'`
FFMAJOR=`echo $FFVERSION_FULL |sed 's,[a-zA-Z]*\([0-9]*\)\..*,\1,'`
FFMINOR=`echo $FFVERSION_FULL |sed 's,[a-zA-Z]*[0-9]*\.\([0-9]*\).*,\1,'`
MAJOR_GUESS=`cat libavutil/version.h |grep LIBAVUTIL_VERSION_MAJOR |head -n 1`
MAJOR_GUESS=${MAJOR_GUESS##* }
MAJOR_GUESS=$((MAJOR_GUESS-52))
FFGIT=false
echo $FFMAJOR_GEN |grep '\-' &>/dev/null && {
FFGIT=true
FFMAJOR=$MAJOR_GUESS
}
[ $MAJOR_GUESS -gt $FFMAJOR ] && {
echo "before major bump to $MAJOR_GUESS"
FFMAJOR=$MAJOR_GUESS
FFMINOR=0
}
PATCH_BRANCH=master
[ $FFMAJOR -lt 5 ] && PATCH_BRANCH=4.4
[ $FFMAJOR -eq 5 ] && PATCH_BRANCH=5.1
[ $FFMAJOR -eq 6 ] && PATCH_BRANCH=6.$FFMINOR
[ $FFMAJOR -eq 7 ] && {
$FFGIT || PATCH_BRANCH=7.$FFMINOR
}
echo "FFmpeg/Libav version: $FFMAJOR.$FFMINOR git: $FFGIT. patch set version: $PATCH_BRANCH"
USE_VK=$FFGIT
USE_VAAPI_WIN32=$FFGIT
if $FFGIT || [ ${FFMAJOR} -gt 3 ]; then
for p in $(find "$THIS_DIR/patches/$PATCH_BRANCH" -name "*.patch" |sort); do
echo $p
patch -p1 -N < $p
done
fi
[ $FFMAJOR -gt 6 ] || [ $FFMAJOR -eq 6 -a $FFMINOR -ge 1 ] && {
USE_VK=true
USE_VAAPI_WIN32=true
}
cd -
toupper(){
echo "$@" | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
}
tolower(){
echo "$@" | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
}
trim(){
local var="$@"
var="${var#"${var%%[![:space:]]*}"}" #bash4: ${a##+([[:space:]])}
var="${var%"${var##*[![:space:]]}"}"
echo -n "$var"
}
# why eval $v=$(trim_compress "\$$v") will have a leading white space (used in trim_vars)?
trim2(){
trim "$@" |tr -s ' '
}
host_is() {
uname |grep -iq $1 && return 0 || return 1
}
target_is() {
test "$TAGET_FLAG" = "$1" && return 0 || return 1
}
target_arch_is() {
test "$TAGET_ARCH_FLAG" = "$1" && return 0 || return 1
}
is_libav() {
test -f "$FFSRC_TOOLS/avconv.c" && return 0 || return 1
}
BUILD_TOOLS=gcc
host_is MinGW32 && BUILD_TOOLS=mingw-w64-i686-gcc
host_is MinGW64 && BUILD_TOOLS=mingw-w64-x86_64-gcc
host_is MinGW || host_is MSYS && {
echo "install msys2 packages: pacman -Sy --needed make diffutils patch pkg-config nasm yasm $BUILD_TOOLS"
which nasm || pacman -Sy --noconfirm --needed make diffutils patch pkg-config nasm yasm $BUILD_TOOLS
}
android_arch(){
# emulate hash in bash3
local armv5=armeabi # ${!armv5} is armeabi<=armeabi<=armv5
local armv7=armeabi-v7a
local arm64=arm64-v8a
# indirect reference
arch=${!1}
echo ${arch:=$1}
return 0
arch=`eval 'echo ${'$1'}'`
echo ${arch:=$1}
}
linux_arch(){
if [[ "$ARCH" == *ar*64 ]]; then
echo arm64
elif [[ "$ARCH" == *64 ]]; then
echo amd64
elif [[ "$ARCH" == *86 ]]; then
echo i386
elif [[ "$ARCH" == armel ]]; then
echo armel
elif [[ "$ARCH" == arm* ]]; then
echo armhf
fi
}
linux_gnu_triple(){
if [[ "$ARCH" == *ar*64 ]]; then
echo aarch64-linux-gnu
elif [[ "$ARCH" == *64 ]]; then
echo x86_64-linux-gnu
elif [[ "$ARCH" == *86 ]]; then
echo i386-linux-gnu
elif [[ "$ARCH" == arm* ]]; then
echo arm-linux-gnueabihf
fi
}
enable_cuda_llvm() {
grep -q "\-\-nvcc=" $FFSRC/configure && TOOLCHAIN_OPT+=" --nvcc=$USE_TOOLCHAIN"
}
#ffmpeg 1.2 autodetect dxva, vaapi, vdpau. manually enable vda before 2.3
enable_opt() {
# grep -m1
for OPT in $@; do
grep -q "\-\-enable\-$OPT" $FFSRC/configure && FEATURE_OPT="--enable-$OPT $FEATURE_OPT" # prepend to support override
done
}
disable_opt() {
# grep -m1
for OPT in $@; do
grep -qE "\-\-disable\-${OPT}|\-\-enable\-${OPT}.*\[autodetect\]" $FFSRC/configure && {
echo $FEATURE_OPT |grep -q "disable-$OPT" || FEATURE_OPT="--disable-$OPT $FEATURE_OPT" # prepend to support override
}
done
}
enable_libmfx(){
pkg-config --libs vpl && enable_opt libvpl || {
pkg-config --libs libmfx && enable_opt libmfx
}
}
enable_opt hwaccels
$USE_VK || disable_opt vulkan
add_elf_flags() {
HARDENED_CFLAGS="-fstack-protector-strong -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fPIE" # toolchain=hardened is -fstack-protector-all
HARDENED_LDFLAGS="-Wl,-z,relro -Wl,-z,now"
# -Wl,-z,noexecstack -Wl,--as-needed is added by configure
EXTRA_CFLAGS+=" -Wa,--noexecstack -fdata-sections -ffunction-sections $HARDENED_CFLAGS"
EXTRA_LDFLAGS+=" -Wl,--gc-sections $HARDENED_LDFLAGS"
# rpath
}
if [ -n "$PKG_CONFIG_PATH_MFX" -a -d "$PKG_CONFIG_PATH_MFX" ]; then
host_is MinGW || host_is MSYS || export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$PKG_CONFIG_PATH_MFX
fi
sed_bak=
host_is darwin && sed_bak=".bak"
CFLAGS_CLANG=
LFLAGS_CLANG=
CFLAGS_GCC=
LFLAGS_GCC=
CFLAG_IWITHSYSROOT="-I="
IS_CLANG=false
IS_APPLE_CLANG=false
IS_CLANG_CL=false
LD_IS_LLD=false
HAVE_LLD=false
LLVM_AR=llvm-ar
LLVM_NM=llvm-nm
LLVM_RANLIB=llvm-ranlib
LLVM_STRIP=llvm-strip
if [ -f "$PWD/tools/nv-codec-headers/ffnvcodec.pc.in" ]; then
sed 's/\(prefix=\).*/\1\${pcfiledir\}/' tools/nv-codec-headers/ffnvcodec.pc.in >tools/nv-codec-headers/ffnvcodec.pc
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$PWD/tools/nv-codec-headers
# cuGLGetDevices is a cuda8 api, never used
sed -i $sed_bak 's/LOAD_SYMBOL(cuGLGetDevices\(.*\)/LOAD_SYMBOL_OPT(cuGLGetDevices\1/;s/LOAD_SYMBOL(cuDeviceGetAttribute\(.*\)/LOAD_SYMBOL_OPT(cuDeviceGetAttribute\1/;s/LOAD_SYMBOL(cuCtxSetLimit\(.*\)/LOAD_SYMBOL_OPT(cuCtxSetLimit\1/' tools/nv-codec-headers/include/ffnvcodec/dynlink_loader.h
fi
ls "$PWD/tools/nv-codec-headers"
sed -i $sed_bak 's/-lmfplat/-lMfplat/g' "$FFSRC/configure"
sed -i $sed_bak '/check_cflags -Werror=partial-availability/d' "$FFSRC/configure" # for SSL, VT
use_clang() {
if [ -n "$CROSS_PREFIX" ]; then # TODO: "$CROSS_PREFIX" != $TARGET_TRIPLE
CLANG_TARGET=${CROSS_PREFIX%%-}
CLANG_TARGET=${CLANG_TARGET##*/}
CLANG_FLAGS="--target=$CLANG_TARGET" # apple clang uses -arch, but also supports -target. gcc cross prefix, clang use target value to find binutils, and set host triple
#CLANG_FLAGS="-fno-integrated-as $CLANG_FLAGS" # libswscale/arm/rgb2yuv_neon_{16,32}.o error. but using arm-linux-gnueabihf-gcc-7 asm from ubuntu results in bus error
fi
# TODO: add clang c/ld flags
}
probe_cc() {
local cc=$1
local flags=$2
$cc -v 2>&1 |grep -q clang && IS_CLANG=true
$cc -v 2>&1 |grep -q "Apple " && IS_APPLE_CLANG=true
$cc -v 2>/dev/null |grep msvc &>/dev/null && IS_CLANG_CL=true # /openmp:llvm ....
if $IS_CLANG_CL; then
IS_CLANG=true
HAVE_LLD=true
elif $IS_CLANG; then
LLD=$($cc -print-prog-name=lld)
# check file existence?
$LLD -flavor gnu -v >/dev/null && HAVE_LLD=true
$HAVE_LLD || {
LLD=$($cc -print-prog-name=ld.lld)
$LLD -flavor gnu -v >/dev/null && HAVE_LLD=true
}
fi
$IS_APPLE_CLANG && [ -f /usr/local/opt/llvm/bin/lld ] && HAVE_LLD=true
echo "compiler is clang: $IS_CLANG, apple clang: $IS_APPLE_CLANG, cl: $IS_CLANG_CL, have lld: $HAVE_LLD"
}
setup_cc() {
probe_cc $@
$IS_CLANG && use_clang
TOOLCHAIN_OPT+=" --cc=$USE_TOOLCHAIN"
}
to_unix_path() {
which wslpath &>/dev/null && { #preppend /mnt/ to the first path only. and always preppend /mnt/x before absolute path
wslpath -u "$1" | sed 's,\;,\;/mnt\/,g;s,:,,g'
exit 0
}
which cygpath &>/dev/null && cygpath -u "$1" && exit 0
echo "$1"
}
use_llvm_binutils() {
echo "detecting llvm tools..."
# use llvm-ar/ranlib, host ar/ranlib may not work for non-mac target(e.g. macOS)
local clang_dir=${USE_TOOLCHAIN%clang*}
local clang_name=${USE_TOOLCHAIN##*/}
local clang=$clang_dir${clang_name/-cl/}
local CLANG_FALLBACK=clang-${LLVM_VER:-18}
$IS_APPLE_CLANG && CLANG_FALLBACK=/usr/local/opt/llvm/bin/clang
echo "clang: `$clang --version`"
# -print-prog-name= prints native dir format(on windows) and `which` fails
`$clang -print-prog-name=llvm-ar` --version &>/dev/null || $(to_unix_path "`$clang -print-prog-name=llvm-ar`") --version &>/dev/null || clang=$CLANG_FALLBACK
echo clang=$clang
for tool in ar nm ranlib $@; do # strip
local tool_path=`eval 'which ${LLVM_'$(toupper $tool)'}'`
local tool_path_print=$($clang -print-prog-name=llvm-$tool)
# -print-prog-name= prints non-versioned path if exists, which may be wrong on linux
local tool_v=${clang//*-/llvm-$tool-}
[ "$tool_v" = "$clang" ] && tool_v=${clang%clang}llvm-$tool
echo "llvm-$tool: $tool_path_print -- `which $tool_v`"
which $tool_v &>/dev/null || tool_v=$clang_dir$tool_v
which $tool_v &>/dev/null && {
eval 'LLVM_'$(toupper $tool)'=$tool_v'
} || {
local clang_path=`which $clang`
local clang_dir=${clang_path%clang*}
tool_path=${clang_dir}llvm-$tool
if [ -x "$tool_path" -a "$tool_path" != "$tool_path_print" ]; then
eval 'LLVM_'$(toupper $tool)'="$tool_path"'
else
eval 'LLVM_'$(toupper $tool)'="\$($clang -print-prog-name=llvm-$tool)"'
fi
}
eval 'TOOLCHAIN_OPT="--$tool=${LLVM_'$(toupper $tool)'} $TOOLCHAIN_OPT"'
done
#EXTRA_LDFLAGS+=" -nodefaultlibs"; EXTRALIBS+=" -lc -lgcc_s"
# TODO: apple clang invoke ld64. --ld=${CROSS_PREFIX}ld ldflags are different from cc ld flags
}
use_lld() {
echo "using (clang)lld as linker..."
$IS_APPLE_CLANG && : ${USE_LD:="/usr/local/opt/llvm/bin/clang"}
[[ "${USE_LD##*/}" == *lld* ]] && LD_IS_LLD=true
# -flavor is passed in arguments and must be the 1st argument. configure will prepend flags before extra-ldflags.
# apple clang+lld can build for non-apple targets
[ -n "$USE_LD" ] && TOOLCHAIN_OPT+=" --ld=\"$USE_LD $@\"" # TODO: what if host strip does not supported target? -s may be not supported, e.g. -flavor darwin
$LD_IS_LLD || {
FUSE_LD="${USE_LD:=lld}"
[[ "${USE_LD##*/}" == *lld* ]] || FUSE_LD=lld # not lld or lld-link
EXTRA_LDFLAGS="-s -fuse-ld=$FUSE_LD $EXTRA_LDFLAGS" # -s: strip flag passing to lld. TODO: llvm-strip
USER_OPT="--disable-stripping $USER_OPT"; # disable strip command because cross gcc may be not installed
}
}
# or simply call "${CFLAG_IWITHSYSROOT}{dir1,dir2,...}"
include_with_sysroot() {
local dirs=($@)
EXTRA_CFLAGS+=" ${dirs[@]/#/${CFLAG_IWITHSYSROOT}}"
}
# compat for windows path (e.g. android gcc toolchain can not recognize dir in -isystem=), assume clang is fine(to be tested)
include_with_sysroot_compat() {
$IS_CLANG && [[ ! "$OSTYPE" == "msys"* ]] && { # android clang add msys install dir to value of -iwithsysroot
include_with_sysroot $@
return 0
}
local dirs=($@)
EXTRA_CFLAGS+=" ${dirs[@]/#/-isystem \$SYSROOT}"
}
check_cross_build() {
IS_CROSS=
IS_HOST=
IS_NATIVE=
}
# warnings are used by ffmpeg developer, some are enabled by configure: -Wl,--warn-shared-textrel
setup_win(){
WIN_VER_SET=false
local os=$2
local XP_VER=5.1
[[ $1 == *64 ]] && XP_VER=5.2
os=${os/xp/${XP_VER}}
echo os:$os
os=${os/vista/6.0}
os=${os/7/6.1}
os=${os/8.1/6.3}
os=${os/8/6.2}
local os_name=${os%%[0-9.]*}
local os_ver=${os##$os_name}
local os_major=${os_ver%%.*}
local os_minor=${os_ver##*.}
echo "$os_ver" |grep -q '\.' || os_minor=0
[ -n "$os_major" ] && {
WIN_VER_SET=true
WIN_VER_LD=${os_major}.0$os_minor
} || os_major=6
WIN_VER=`printf "0x%02X%02X" $os_major $os_minor`
echo WIN_VER_SET: $WIN_VER_SET WIN_VER:$WIN_VER
local win_cc=clang
cl.exe
which cl.exe &>/dev/null && {
win_cc=cl.exe # .exe: for wsl
} || {
: ${Platform:=x86} #Platform is empty(native) or x86(cross using 64bit toolchain)
Platform=${arch:-${Platform}} # arch is set, but may be null, so :-
local platform=$(tolower $Platform)
local PATH_arch=PATH_$platform
PATH_arch=${!PATH_arch}
PATH_arch=$(to_unix_path "$PATH_arch" |sed 's/\([a-zA-Z]\):/\/\1/g;s/\;/:/g;s/(/\\\(/g;s/)/\\\)/g;s/ /\\ /g')
PATH=$PATH_arch:$PATH which cl.exe &>/dev/null && win_cc=cl.exe
}
: ${USE_TOOLCHAIN:=$win_cc}
probe_cc $USE_TOOLCHAIN
enable_opt mediafoundation
disable_opt ptx-compression # libavfilter link error (zlib used in ff_cuda_load_module but no lib dep)
$USE_VK && EXTRA_CFLAGS+=" -I\$THIS_DIR/tools/Vulkan-Headers/include"
$USE_VAAPI_WIN32 || disable_opt vaapi
if $IS_CLANG ; then
setup_win_clang $@
else
setup_vc_env $@
fi
# FIXME: wslpath
WinSdkDir=$(to_unix_path $WindowsSdkDir)
dxva_h=$(find "$WinSdkDir/Include" -name dxva.h)
[ -f "$dxva_h" ] && {
grep -n DXVA_Tile_AV1 "$dxva_h" &>/dev/null || patch -p1 "$dxva_h" <patches/dxva-av1.diff
}
# LLVM for windows (on github ci /C/Program Files/LLVM/bin/clang) fail to build ffmpeg 4.4 libavfilter/vf_scale_cuda_bicubic.ptx.c(3625): fatal error C1060: compiler is out of heap space
# master libavfilter/vf_scale_cuda.ptx.c: fatal error C1033: cannot open program database 'D:\a\avbuild\avbuild\build_sdk-vc-x64-clvs2022\vc140.pdb'
[ -n "$MSYSTEM" ] && USER_OPT+=" --disable-filter=scale_cuda"
}
setup_win_clang(){
# -imsvc: add msvc system header path
: ${USE_TOOLCHAIN:=clang}
setup_cc $USE_TOOLCHAIN
local clang_dir=${USE_TOOLCHAIN%clang*}
local clang_name=${USE_TOOLCHAIN##*/}
local clang=$clang_dir${clang_name/-cl/}
USE_LD=$($clang -print-prog-name=lld-link) use_lld # lld 6.0 fixes undefined __enclave_config in msvcrt14.12. `lld -flavor link` just warns --version-script and results in link error
enable_pic=false
use_llvm_binutils windres
#use_lld # --target=i386-pc-windows-msvc -fuse-ld=lld: must use with -Wl,
enable_lto=false # ffmpeg: "LTO requires same compiler and linker"
# lto: link error if clang and lld version does not mach?
# TODO: patch ffmpeg. ffmpeg disables lto (enabled by --enable-lto) if "$cc_type" != "$ld_type"
#[[ "$LIB_OPT" == *"--enable-static"* ]] || FORCE_LTO=true
#$FORCE_LTO && LTO_CFLAGS=-flto=thin # win lto binary size is larger
LTO_LFLAGS="/opt:lldltojobs=`getconf _NPROCESSORS_ONLN`" # only affects thin lto?
enable_opt dxva2
# TODO: unify setup_vc/wnrt
: ${ONECORE:=}
STORE=
VS_VER=15
local IS_STORE=false
local HAS_CUDA=true
local os=$2
[[ "$os" == win*store || "$os" == win*phone || "${os:0:3}" == uwp || "${os:0:5}" == winrt ]] && IS_STORE=true
local WINPHONE=false
[[ "$os" == win*phone ]] && WINPHONE=true
: ${Platform:=$arch}
platform=$(tolower $Platform)
MACHINE=$Platform
LTL_Platform=$Platform
if [ "${platform:0:3}" = "arm" ]; then
$WIN_VER_SET || WIN_VER="0x0A00"
$IS_STORE || EXTRA_CFLAGS+=" -D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE=1"
# FIXME: clang armv7 does not support as_fpu_directive, and the alternative '@ .fpu neon' is not supported by --target=arm-pc-windows-msvc does not support
arch=$platform
# --as='clang -target aarch64-win32-gnu' --cc='clang -target aarch64-win32-msvc' : https://fate.libav.org/aarch64-win32-clang-6.0/20190219163918
[ -z "${platform/*64*/}" ] || MACHINE=arm
HAS_CUDA=false
LTL_Platform=$(toupper $MACHINE)
elif [ -z "${Platform/*64/}" ]; then
arch=x86_64
Platform=x64
LTL_Platform=x64
else
arch=x86
target_tripple_arch=i386
LTL_Platform=Win32
fi
: ${target_tripple_arch:=$arch}
: ${Platform:=$arch}
echo IS_STORE=$IS_STORE
$IS_STORE && {
HAS_CUDA=false
setup_winrt_env
# onecore/vcruntime.lib imports symbols from vcruntime140.dll, while store/vcruntime.lib imports them from vcruntime140_app.dll, both dlls can run in desktop mode
[[ "$ONECORE" == onecore || "$ONECORE" == *-"$Platform" ]] && EXTRALIBS+=" OneCoreUAP.Lib" || STORE=store
} || {
[[ "$ONECORE" == onecore || "$ONECORE" == *-"$Platform" ]] && EXTRALIBS+=" OneCore.Lib"
}
$HAS_CUDA && enable_cuda_llvm || disable_opt cuda-llvm
# environment var LIB is used by lld-link, in windows style, i.e. export LIB=dir1;dir2;...
# makedef: define env AR=llvm-ar, NM=llvm-nm
# --windres=rc option is broken and not recognized
TARGET_TRIPLE=${target_tripple_arch}-pc-windows-msvc # lto default vendor is empty, fix lld-link: warning: Linking two modules of different target triples
TARGET_OPT="--target=$TARGET_TRIPLE"
[ "$MACHINE" == arm ] && {
ASM_OPT+=" --enable-thumb --enable-neon --cpu=armv7-a"
# clang: FPU error. gas: vfp error
TOOLCHAIN_OPT+=" --as='$USE_TOOLCHAIN --target=armv7-win32-gnu -mfpu=neon'" # gas-preprocessor.pl -as-type clang -arch arm -- $USE_TOOLCHAIN'"
# cflags is appended to as flags, but arm as target tripple must be gnu not msvc
TARGET_OPT=
TOOLCHAIN_OPT=${TOOLCHAIN_OPT//--cc=$USE_TOOLCHAIN/--cc=\'$USE_TOOLCHAIN --target=$TARGET_TRIPLE\'}
USER_OPT=${USER_OPT//--cc=$USE_TOOLCHAIN/--cc=\'$USE_TOOLCHAIN --target=$TARGET_TRIPLE\'}
}
TOOLCHAIN_OPT+=" --enable-cross-compile --arch=$arch $ASM_OPT --target-os=win32 --disable-stripping"
[ -n "$WIN_VER_LD" ] && TOOLCHAIN_OPT+=" --extra-ldexeflags='-SUBSYSTEM:CONSOLE,$WIN_VER_LD'"
EXTRA_CFLAGS+=" $LTO_CFLAGS $TARGET_OPT -DWIN32 -D_WIN32 -D_WIN32_WINNT=$WIN_VER -Wno-nonportable-include-path -Wno-deprecated-declarations" # -Wno-deprecated-declarations: avoid clang crash
$FORCE_LTO || $enable_lto && EXTRA_LDFLAGS+=" -MACHINE:$MACHINE" # lto is compiled as ir but not coff object and lld can not determin thw target arch
EXTRA_LDFLAGS+=' -DEBUG -OPT:REF -SUBSYSTEM:CONSOLE -DEFAULTLIB:msvcrt'
EXTRALIBS+=" oldnames.lib" # fdopen, tempnam, close used in file_open.c
INSTALL_DIR="sdk-$2-$Platform-clang"
# pkgconf: check_func_headers() includes lflags "mfx.lib" which can not be in -c. fallbck to header and lib check.
[ -n "$PKG_CONFIG_PATH_MFX" ] && PKG_CONFIG_PATH_MFX_UNIX=`to_unix_path "$PKG_CONFIG_PATH_MFX"`
[ -d "$PKG_CONFIG_PATH_MFX_UNIX" ] || PKG_CONFIG_PATH_MFX_UNIX=${PKG_CONFIG_PATH_MFX_UNIX/\/lib\/pkgconfig/$Platform\/lib\/pkgconfig}
PKG_CONFIG_PATH_MFX=$PKG_CONFIG_PATH_MFX_UNIX
[ -d "$PKG_CONFIG_PATH_MFX_UNIX" ] && PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$PKG_CONFIG_PATH_MFX_UNIX"
echo PKG_CONFIG_PATH_MFX_UNIX=$PKG_CONFIG_PATH_MFX_UNIX PKG_CONFIG_PATH_MFX=$PKG_CONFIG_PATH_MFX PKG_CONFIG_PATH=$PKG_CONFIG_PATH
$IS_STORE || {
[ -d "$AMF_DIR" -a "${platform:0:3}" != "arm" ] && EXTRA_CFLAGS+=" -I$AMF_DIR"
[ -d "$SDL2_DIR" -a "${platform:0:3}" != "arm" ] && {
EXTRA_CFLAGS+=" -I$SDL2_DIR/include"
EXTRA_LDFLAGS+=" -L$SDL2_DIR/lib/${platform}"
}
enable_libmfx
}
[ -f "$WindowsSdkDir/Include/$WindowsSDKVersion/um/WINDOWS.H" ] || { # case sensitive file system
echo "CASE SENSITIVE FS!!!!!!!"
[ -f "$WindowsSdkDir/vfs.yaml" ] && EXTRA_CFLAGS+=" -Xclang -ivfsoverlay -Xclang \\\"\$WindowsSdkDir/vfs.yaml\\\""
# TODO: gen vfs.yaml
}
cfguard=true
# vcrt and win sdk dirs
win10inc=(shared ucrt um winrt)
win10inc=(${win10inc[@]/#/$WindowsSdkDir/Include/$WindowsSDKVersion/})
IFS=\; eval 'INCLUDE="${win10inc[*]}"'
WINDRESFLAGS="--target=$TARGET_TRIPLE ${win10inc[@]/#/-I}"
grep -q "\-\-extra\-windresflags" $FFSRC/configure && TOOLCHAIN_OPT+=" --extra-windresflags='$WINDRESFLAGS'"
local VCDIR_LIB=$VCDIR/lib/${ONECORE%%-*}/${MACHINE/86_/}/$STORE
ARCH120=${MACHINE/*86_*/amd64} #vc120 sdk layout
ARCH120=${ARCH120/x64/amd64}
ARCH120=${ARCH120/x86/}
[ ! -d "$VCDIR_LIB" ] && {
VCDIR_LIB=$VCDIR/lib/$STORE/${ARCH120}
cfguard=false # since vs2015. undefined ___guard_check_icall_fptr
cp -af patches/0001-define-timespec-for-vcrt-140.patch "$FFSRC/tmp.patch"
(cd "$FFSRC" && patch -p1 -N <"tmp.patch")
}
local VC_LTL_LIB=${VC_LTL_DIR}/TargetPlatform/${VC_LTL_VER:-10.0.19041.0}/lib/${LTL_Platform} #/CleanImport
$IS_CLANG_CL && {
if [ -d "$VC_LTL_LIB" ]; then
EXTRA_CFLAGS+=" -MT"
else
EXTRA_CFLAGS+=" -MD"
EXTRA_LDFLAGS+=" -NODEFAULTLIB:libcmt"
fi
[ "$MACHINE" == arm ] || EXTRA_CFLAGS+=" -Zi" # codeview is not implemented for arm(clang-10)
$cfguard && EXTRA_CFLAGS+=" /guard:cf"
} || {
EXTRA_CFLAGS+=" -D_DLL" # defined by cl /MD. fix _HUGE not defined using vcrt120
[ "$MACHINE" == arm ] || EXTRA_CFLAGS+=" -g -gcodeview"
$cfguard && EXTRA_CFLAGS+=" -Xclang -cfguard"
#EXTRA_LDFLAGS+=" -NODEFAULTLIB:libcmt" #?
}
$cfguard && EXTRA_LDFLAGS+=' -guard:cf'
mkdir -p $THIS_DIR/build_$INSTALL_DIR
cat > "$THIS_DIR/build_$INSTALL_DIR/.env.sh" <<EOF
export INCLUDE="$VCDIR/include;$INCLUDE;$PKG_CONFIG_PATH_MFX_UNIX/../../include"
export LIB="$VC_LTL_LIB;$VCDIR_LIB;$WindowsSdkDir/Lib/$WindowsSDKVersion/ucrt/${MACHINE/86_/};$WindowsSdkDir/Lib/$WindowsSDKVersion/um/${MACHINE/86_/};$PKG_CONFIG_PATH_MFX_UNIX/../../lib"
export AR=$LLVM_AR
export NM=$LLVM_NM
#export V=1 # FFmpeg BUG: AR is overriden in common.mak and becomes an invalid command in makedef(@printf works in makefiles but not sh scripts)
export PKG_CONFIG_PATH=${THIS_DIR}/tools/dep/windows/${MACHINE/86_/}/lib/pkgconfig:${THIS_DIR}/tools/dep_gpl/windows-desktop/${MACHINE/86_/}/lib/pkgconfig:${THIS_DIR}/tools/dep/windows/lib/pkgconfig:${THIS_DIR}/tools/dep_gpl/windows-desktop/lib/pkgconfig:$PKG_CONFIG_PATH
EOF
# [ expr1 ] && ... at end returns error if expr1 is false
}
setup_vc_env() {
local arch=$1
local osver=$2
grep -q "guard:cf is not recognized by armasm" "$FFSRC/configure" || sed -i $sed_bak "/-M\[TD\]\*)/a\\
\ -guard*) ;; # -guard:cf is not recognized by armasm\\
\ -FS) ;; # -FS is not recognized by armasm\\
" "$FFSRC/configure"
echo Call "set MSYS2_PATH_TYPE=inherit" before msys2 sh.exe if cl.exe is not found!
enable_lto=false # ffmpeg requires DCE, while vc with LTCG (-GL) does not support DCE
# dylink crt
if [ -d "$VC_LTL_DIR/TargetPlatform" ]; then
EXTRA_CFLAGS+=" -MT"
else
EXTRA_CFLAGS+=" -MD"
EXTRA_LDFLAGS+=" -NODEFAULTLIB:libcmt" #-NODEFAULTLIB:libcmt -winmd?
fi
EXTRA_CFLAGS+=" -Zi -FS -guard:cf" # /Zi: https://scc.ustc.edu.cn/zlsc/tc4600/intel/2017.0.098/compiler_f/common/core/GUID-CA811CC8-A2C1-4DFF-AC39-DF7E1EEAF30E.html
EXTRA_LDFLAGS+=" -DEBUG -guard:cf -OPT:REF -SUBSYSTEM:CONSOLE"
TOOLCHAIN_OPT+=" --toolchain=msvc"
VS_VER=${VisualStudioVersion:0:2}
: ${Platform:=x86} #Platform is empty(native) or x86(cross using 64bit toolchain)
Platform=${arch:-${Platform}} # arch is set, but may be null, so :-
local platform=$(tolower $Platform)
echo "VS version: $VS_VER, platform: $Platform" # Platform is from vsvarsall.bat
[ -n "$PKG_CONFIG_PATH_MFX" ] && PKG_CONFIG_PATH_MFX_UNIX=$(to_unix_path "$PKG_CONFIG_PATH_MFX")
[ -d "$PKG_CONFIG_PATH_MFX_UNIX" ] || PKG_CONFIG_PATH_MFX_UNIX=${PKG_CONFIG_PATH_MFX_UNIX/\/lib\/pkgconfig/$Platform\/lib\/pkgconfig}
PKG_CONFIG_PATH_MFX=$PKG_CONFIG_PATH_MFX_UNIX
[ -d "$PKG_CONFIG_PATH_MFX_UNIX" ] && PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$PKG_CONFIG_PATH_MFX_UNIX"
FAMILY=
local HAS_CUDA=true
[ "${platform:0:3}" = "arm" ] && HAS_CUDA=false
if ${WINRT:-false}; then
[ -z "$osver" ] && osver=winrt
setup_vc_winrt_env $arch
HAS_CUDA=false
else
[ -z "$osver" ] && osver=win
FAMILY=_DESKTOP
setup_vc_desktop_env $arch
fi
$HAS_CUDA || disable_opt cuda-llvm
EXTRA_CFLAGS+=" -D_WIN32_WINNT=$WIN_VER" # -DWINAPI_FAMILY=WINAPI_FAMILY${FAMILY}_APP is not required for desktop
INSTALL_DIR="`tolower sdk-$osver-$Platform-cl${VS_CL}`"
[ -n "$MSYSTEM" ] && {
mkdir -p /usr/local/bin
llvm_rc=`which llvm-rc.exe` && cp "$llvm_rc" /usr/local/bin/llvm-windres.exe
which llvm-windres
}
if [ "${platform:0:3}" = "arm" ]; then
target_tripple_arch=$platform
elif [ -z "${Platform/*64/}" ]; then
target_tripple_arch=x86_64
else
target_tripple_arch=i386
fi
TARGET_TRIPLE=${target_tripple_arch}-pc-windows-msvc
readarray -d ';' -t incs <<< "$INCLUDE"
echo incs: = ${incs[2]}
nb_incs=${#incs[@]}
WINDRESFLAGS="--target=$TARGET_TRIPLE ${win10inc[@]/#/-I}"
#incs_unix=`cygpath "${incs[@]}"`
#for inc in ${incs[@]}; do
for ((i=0;i<nb_incs;i++)); do
inc=${incs[$i]}
# WINDRESFLAGS+=" -I\"`cygpath "$inc"`\""
WINDRESFLAGS+=" -I`to_unix_path "$inc" |sed 's, ,\\\\ ,g;s,(,\\\\(,g;s,),\\\\),g'`"
done
grep -q "\-\-extra\-windresflags" $FFSRC/configure && TOOLCHAIN_OPT+=" --windres=llvm-windres --extra-windresflags='$WINDRESFLAGS'"
local BDIR=$BUILD_DIR
[ -d "$BDIR" ] || BDIR=$THIS_DIR/build_$INSTALL_DIR
rm -rf $BDIR/.env.sh
mkdir -p $BDIR
# get env vars for given arch, and export for build
local PATH_arch=PATH_$platform
PATH_arch=${!PATH_arch}
# LIB, LIBPATH, INCLUDE are used by vc compiler and linker only, so keep the original path style
local LIB_arch=LIB_$platform
LIB_arch=$(echo ${!LIB_arch} |sed 's/\\/\\\\/g;s/(/\\\(/g;s/)/\\\)/g;s/ /\\ /g;s/\;/\\\;/g')
local LIBPATH_arch=LIBPATH_$platform
LIBPATH_arch=$(echo ${!LIBPATH_arch} |sed 's/\\/\\\\/g;s/(/\\\(/g;s/)/\\\)/g;s/ /\\ /g;s/\;/\\\;/g')
local INCLUDE_arch=INCLUDE_$platform
INCLUDE_arch=$(echo ${!INCLUDE_arch} |sed 's/\\/\\\\/g;s/(/\\\(/g;s/)/\\\)/g;s/ /\\ /g;s/\;/\\\;/g')
[ -n "$PATH_arch" ] && {
# msvc exes are used by script, so must be converted to posix paths
PATH_arch=$(to_unix_path "$PATH_arch" |sed 's/\([a-zA-Z]\):/\/\1/g;s/\;/:/g;s/(/\\\(/g;s/)/\\\)/g;s/ /\\ /g')
# PATH_arch is set before bash environment, so must manually add bash paths
echo "export PATH=$PATH_EXTRA:/usr/local/bin:/usr/bin:/bin:/opt/bin:$PATH_arch" >"$BDIR/.env.sh"
}
[ -n "$LIB_arch" ] && echo "export LIB=$LIB_arch" >>"$BDIR/.env.sh"
[ -n "$LIBPATH_arch" ] && echo "export LIBPATH=$LIBPATH_arch" >>"$BDIR/.env.sh"
[ -n "$INCLUDE_arch" ] && echo "export INCLUDE=$INCLUDE_arch" >>"$BDIR/.env.sh"
cat >> "$BDIR/.env.sh" <<EOF
export PKG_CONFIG_PATH=${THIS_DIR}/tools/dep/windows/lib/pkgconfig:${THIS_DIR}/tools/dep_gpl/windows-desktop/lib/pkgconfig:$PKG_CONFIG_PATH
EOF
}
setup_vc_desktop_env() {
# http://ffmpeg.org/platform.html#Microsoft-Visual-C_002b_002b-or-Intel-C_002b_002b-Compiler-for-Windows
[ -z "$WSL_DISTRO_NAME" ] && [[ "$platform" != arm* ]] && enable_libmfx # msys2: -I -libpath path starts with X:/. wsl: -libpath:X:/ is ignored
[ -d "$AMF_DIR" -a "${platform:0:3}" != "arm" ] && EXTRA_CFLAGS+=" -I$AMF_DIR"
[ -d "$SDL2_DIR" -a "${platform:0:3}" != "arm" ] && {
EXTRA_CFLAGS+=" -I$SDL2_DIR/include"
EXTRA_LDFLAGS+=" -L$SDL2_DIR/lib/${platform}"
}
enable_opt dxva2
# ldflags prepends flags. extralibs appends libs and add to pkg-config
# can not use -luser32 because extralibs will not be filter -l to .lib (ldflags_filter is not ready, ffmpeg bug)
# TODO: check dxva2_extralibs="-luser32" in configure
grep -q " user32 " "$FFSRC/configure" || EXTRALIBS+=" user32.lib" # ffmpeg 3.x bug: hwcontext_dxva2 GetDesktopWindow()
[ -n "$WIN_VER_LD" ] && TOOLCHAIN_OPT+=" --extra-ldexeflags='-SUBSYSTEM:CONSOLE,$WIN_VER_LD'"
if [[ "$platform" == arm* ]]; then
$WIN_VER_SET || WIN_VER="0x0A00"
EXTRA_CFLAGS+=" -D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE=1"
TOOLCHAIN_OPT+=" --enable-cross-compile --target-os=win32"
setup_vc_common_env $@
fi
}
setup_winrt_env() {
if [ -f "$FFSRC/compat/w32dlfcn.h" ]; then
grep -q HAVE_WINRT "$FFSRC/compat/w32dlfcn.h" || {
echo "Patching LoadPackagedLibrary..."
cp -af patches/0001-winrt-use-LoadPackagedLibrary.patch "$FFSRC/tmp.patch"
cd "$FFSRC"
patch -p1 <"tmp.patch"
cd -
}
fi
if [ -f "$FFSRC/libavutil/hwcontext_d3d11va.c" ]; then
if ! `grep -q CreateMutexEx "$FFSRC/libavutil/hwcontext_d3d11va.c"`; then
echo "Patching CreateMutex..."
cp -af patches/0001-use-CreateMutexEx-instead-of-CreateMutex-to-fix-win8.patch "$FFSRC/tmp.patch"
cd "$FFSRC"
patch -p1 <"tmp.patch"
cd -
fi
fi
#http://fate.libav.org/arm-msvc-14-wp
disable_opt ffnvcodec
disable_opt vulkan
disable_opt programs avdevice
FEATURE_OPT+=" --disable-filter=ddagrab"
TOOLCHAIN_OPT+=" --enable-cross-compile --target-os=win32"
EXTRA_LDFLAGS+=" -APPCONTAINER"
$WIN_VER_SET || {
WIN_VER="0x0A00"
test $VS_VER -lt 14 && WIN_VER="0x0603" # vc can support multiple target (and sdk)
target_is winphone && WIN_VER="0x0603"
}
WIN10_VER_DEC=`printf "%d" 0x0A00`
WIN81_VER_DEC=`printf "%d" 0x0603`
WIN_VER_DEC=`printf "%d" $WIN_VER`
: ${WINPHONE:=false}
target_is winphone && WINPHONE=true
if $WINPHONE; then
# export dirs (lib, include)
FAMILY=_PHONE
# phone ldflags only for win8.1?
EXTRA_LDFLAGS+=" WindowsPhoneCore.lib RuntimeObject.lib PhoneAppModelHost.lib -NODEFAULTLIB:kernel32.lib -NODEFAULTLIB:ole32.lib"
fi
if [ $WIN_VER_DEC -gt ${WIN81_VER_DEC} ]; then
# store: can not use onecoreuap.lib, must use windowsapp.lib
# uwp: can use onecoreuap.lib
EXTRA_LDFLAGS+=" WindowsApp.lib"
fi
EXTRA_CFLAGS+=" -DWINAPI_FAMILY=WINAPI_FAMILY${FAMILY}_APP -DUNICODE -D_UNICODE" #-EHsc"
}
setup_vc_winrt_env() {
setup_winrt_env $@
setup_vc_common_env $@
}
setup_vc_common_env() {
local arch=x86_64 #used by configure --arch
if [ "${platform:0:3}" = "arm" ]; then
enable_pic=false # TODO: ffmpeg bug, should filter out -fPIC. armasm(gas) error (unsupported option) if pic is
type -a gas-preprocessor.pl
# vc only arm64_neon.h
[ -z "${platform/*64*/}" ] && {
BIT=64
TOOLCHAIN_OPT+=" --disable-pic" # arm64 pic is enabled by: enabled spic && enable_weak pic
} || ASM_OPT+=" --enable-thumb --cpu=armv7-a"
which cpp &>/dev/null && { # install gcc
ASM_OPT+=" --as=armasm$BIT"
} || {
echo "ASM is disabled: cpp is required by gas-preprocessor but it is missing. make sure (mingw) gcc is in your PATH"
ASM_OPT=--disable-asm
}
#gas-preprocessor.pl change open(INPUT, "-|", @preprocess_c_cmd) || die "Error running preprocessor"; to open(INPUT, "@preprocess_c_cmd|") || die "Error running preprocessor";
#EXTRA_CFLAGS+=" -D__ARM_PCS_VFP" # for gcc, hard float
EXTRA_LDFLAGS+=' -MACHINE:$Platform' # clang-cl: -pdb:\\$(NAME).pdb
arch="$platform"
TOOLCHAIN_OPT+=" $ASM_OPT"
else
[ -z "${Platform/*64/}" ] && arch=x86_64 || arch=x86
fi
TOOLCHAIN_OPT+=" --arch=$arch"
}
setup_mingw_env() {
enable_lto=false
local gcc=gcc
local arch=$1
local native_build=false # native build: use gcc instead of ${arch}-w64-mingw32-gcc
if [ -n "$arch" ]; then
[ -z "${arch/*64*/}" ] && BIT=64 || BIT=32
[[ $arch = *ar* ]] || arch=x86_$BIT
arch=${arch/*_32/i686}
fi
# msys2 /usr/bin/gcc is x86_64-pc-msys
$gcc -dumpmachine |grep -iq mingw && {
[ -z "$arch" ] && native_build=true || {
$gcc -dumpmachine |grep -iq "$arch" && native_build=true
}
}
$native_build && { # arch is not set. probe using gcc
$gcc -dumpmachine |grep -iqE "x86_64|aarch64|arm64" && BIT=64 || BIT=32
arch=x86_$BIT
arch=${arch/*_32/i686}
echo "mingw $arch host native build"
} || {
gcc=${arch}-w64-mingw32-gcc
host_is MinGW || host_is MSYS && {
echo "mingw host build for $arch" # TODO: -m32/64?
# mingw-w64-cross-gcc package has broken old mingw compilers with the same prefix, so prefer compilers in $MINGW_BIN
TOOLCHAIN_OPT+=" --cc=$gcc --target-os=mingw$BIT" # set target os recognized by configure. msys and mingw without 32/64 are rejected by configure
local MINGW_BIN=/mingw${BIT}/bin
[ -d $MINGW_BIN ] && export PATH=$MINGW_BIN:$PATH
} || {
echo "mingw cross build for $arch"
TOOLCHAIN_OPT+=" --enable-cross-compile --cross-prefix=${arch}-w64-mingw32- --target-os=mingw$BIT --arch=$arch --pkg-config=pkg-config"
}
}
[ -n "$PKG_CONFIG_PATH_MFX" ] && PKG_CONFIG_PATH_MFX_UNIX=$(to_unix_path "$PKG_CONFIG_PATH_MFX")
[ -d "$PKG_CONFIG_PATH_MFX_UNIX" ] || PKG_CONFIG_PATH_MFX_UNIX=${PKG_CONFIG_PATH_MFX_UNIX/\/lib\/pkgconfig/$BIT\/lib\/pkgconfig}
PKG_CONFIG_PATH_MFX=$PKG_CONFIG_PATH_MFX_UNIX
[ -d "$PKG_CONFIG_PATH_MFX_UNIX" ] && PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$PKG_CONFIG_PATH_MFX_UNIX"
[ -d "$AMF_DIR" ] && EXTRA_CFLAGS+=" -I$AMF_DIR"
$USE_VK && EXTRA_CFLAGS+=" -I\$THIS_DIR/tools/Vulkan-Headers/include"
enable_libmfx
enable_opt dxva2
#enable_opt mediafoundation # MFCreateAlignedMemoryBuffer
disable_opt iconv
EXTRA_LDFLAGS+=" -static-libgcc"
INSTALL_DIR="${INSTALL_DIR}-mingw-$1-gcc"
rm -rf $THIS_DIR/build_$INSTALL_DIR/.env.sh
mkdir -p $THIS_DIR/build_$INSTALL_DIR
[ -d "$MINGW_BIN" ] && cat>$THIS_DIR/build_$INSTALL_DIR/.env.sh<<EOF
export PATH=$MINGW_BIN:$PATH
shopt -s expand_aliases
#alias ${arch}-w64-mingw32-strip=$MINGW_BIN/strip # seems not work in sh used by ffmpeg
#alias ${arch}-w64-mingw32-nm=$MINGW_BIN/nm
EOF
}
# TOOLCHAIN_OPT+=" --enable-cross-compile --cross-prefix=arm-mingw32ce- --target-os=mingw32ce --arch=arm --cpu=arm"
setup_android_env() {
DEC_OPT=$DEC_OPT_MOBILE
DEMUX_OPT=$DEMUX_OPT_MOBILE
FILTER_OPT=$FILTER_OPT_MOBILE
ENC_OPT=$ENC_OPT_MOBILE
MUX_OPT=$MUX_OPT_MOBILE
# fbdev & v4l2 build error and not supported on android. camera requores api level 24, ./avbuild.sh android24
FEATURE_OPT+=" --disable-indevs --enable-indev=android_camera --disable-outdevs"
disable_opt v4l2_m2m v4l2-m2m
disable_opt vulkan # can not enable VK_ENABLE_BETA_EXTENSIONS for android
sed -i $sed_bak 's/^check_cc v4l2_m2m/enabled v4l2_m2m \&\& check_cc v4l2_m2m/' "$FFSRC/configure"
local ANDROID_ARCH=${1:=arm}
TRIPLE_ARCH=$ANDROID_ARCH
local ANDROID_TOOLCHAIN_PREFIX="${ANDROID_ARCH}-linux-android"
local CROSS_PREFIX=${ANDROID_TOOLCHAIN_PREFIX}-
local FFARCH=$ANDROID_ARCH
local API_LEVEL=${2#android}
local NDK_VER=`grep Pkg.Revision $NDK_ROOT/source.properties |cut -d ' ' -f 3 |cut -d '.' -f 1`
[ -z "$API_LEVEL" ] && {
API_LEVEL=14
[ $NDK_VER -gt 17 ] && API_LEVEL=16
[ $NDK_VER -gt 23 ] && API_LEVEL=19
[ $NDK_VER -gt 25 ] && API_LEVEL=21 # TODO: always neon
}
local UNIFIED_SYSROOT="$NDK_ROOT/sysroot"
[ -d "$UNIFIED_SYSROOT" ] || UNIFIED_SYSROOT=
add_elf_flags
EXTRA_CFLAGS+=" -ffast-math -fstrict-aliasing"
# -no-canonical-prefixes: results in "-mcpu= ", why?
TRY_FIX_CORTEX_A8=false
# TODO: clang lto in r14 (gcc?) except aarch64
if [ -z "${ANDROID_ARCH/*86/}" ]; then
ANDROID_ARCH=x86
TRIPLE_ARCH=i686
ANDROID_TOOLCHAIN_PREFIX=$ANDROID_ARCH
CLANG_TARGET="i686-none-linux-android"
# from ndk: x86 devices have stack alignment issues.
# clang error: inline assembly requires more registers than available ("movzbl "statep" , "ret")
CFLAGS_GCC+=" -mstackrealign"
enable_lto=false
TOOLCHAIN_OPT+=" --disable-asm" # FIXME: text relocations in x86/tx_float.asm
elif [ -z "${ANDROID_ARCH/x*64/}" ]; then
[ $API_LEVEL -lt 21 ] && API_LEVEL=21
ANDROID_ARCH=x86_64
TRIPLE_ARCH=$ANDROID_ARCH
ANDROID_TOOLCHAIN_PREFIX=$ANDROID_ARCH
CLANG_TARGET="x86_64-none-linux-android"
enable_lto=false
elif [ -z "${ANDROID_ARCH/a*r*64/}" ]; then
[ $API_LEVEL -lt 21 ] && API_LEVEL=21
ANDROID_ARCH=arm64
TRIPLE_ARCH=aarch64
ANDROID_TOOLCHAIN_PREFIX=${TRIPLE_ARCH}-linux-android
CLANG_TARGET="aarch64-none-linux-android"
elif [ -z "${ANDROID_ARCH/*arm*/}" ]; then
#https://wiki.debian.org/ArmHardFloatPort/VfpComparison
TRIPLE_ARCH=arm
ANDROID_TOOLCHAIN_PREFIX=${TRIPLE_ARCH}-linux-androideabi
FFARCH=arm
if [ -z "${ANDROID_ARCH/armv5*/}" ]; then
echo "armv5"
TOOLCHAIN_OPT+=" --cpu=armv5te"
CLANG_TARGET="armv5te-none-linux-androideabi"
: '
-mthumb error
selected processor does not support Thumb mode `itt gt
D:\msys2\tmp\ccXOcbBA.s:262: Error: instruction not supported in Thumb16 mode -- adds r3,r1,r0,lsr#31
use armv6t2 or -mthumb-interwork: https://gcc.gnu.org/onlinedocs/gcc-4.5.3/gcc/ARM-Options.html
'
# -msoft-float == -mfloat-abi=soft https://gcc.gnu.org/onlinedocs/gcc-4.5.3/gcc/ARM-Options.html
EXTRA_CFLAGS+=" -mtune=xscale -msoft-float" # -march=armv5te
CFLAGS_GCC+=" -mthumb-interwork"
else
TOOLCHAIN_OPT+=" --enable-thumb --enable-neon"
EXTRA_CFLAGS_FPU="-mfpu=vfpv3-d16"
if [ -z "${ANDROID_ARCH/*neon*/}" ]; then
enable_lto=false
echo "neon. can not run on Marvell and nVidia"
EXTRA_CFLAGS_FPU="-mfpu=neon"
CFLAGS_GCC+=" -mvectorize-with-neon-quad"
fi
CLANG_TARGET="armv7a-linux-androideabi"
EXTRA_CFLAGS+=" -march=armv7-a -mtune=cortex-a8 -mfloat-abi=softfp $EXTRA_CFLAGS_FPU" #-mcpu= is deprecated in gcc 3, use -mtune=cortex-a8 instead
TRY_FIX_CORTEX_A8=true
fi
ANDROID_ARCH=arm
fi
[ $NDK_VER -gt 17 ] && API_SUFFIX=$API_LEVEL
CLANG_FLAGS+=" --target=$CLANG_TARGET$API_SUFFIX" # TODO: api level suffix, and not manually set platform dir?
ANDROID_HEADER_TRIPLE=${TRIPLE_ARCH}-linux-android
[ "$ANDROID_ARCH" == "arm" ] && ANDROID_HEADER_TRIPLE=${ANDROID_HEADER_TRIPLE}eabi
CROSS_PREFIX=${ANDROID_HEADER_TRIPLE}-
local TOOLCHAIN=${ANDROID_TOOLCHAIN_PREFIX}-4.9
[ -d $NDK_ROOT/toolchains/${TOOLCHAIN} ] || TOOLCHAIN=${ANDROID_TOOLCHAIN_PREFIX}-4.8
local ANDROID_GCC_DIR="$NDK_ROOT/toolchains/${TOOLCHAIN}"
gxx=`find ${ANDROID_GCC_DIR} -name "*g++*" 2>/dev/null` # can not use "*-gcc*": can be -gcc-ar, stdint-gcc.h
as=`find ${ANDROID_GCC_DIR} -name "*-as" -o -name "*-as.exe" 2>/dev/null`
clangxxs=(`find $NDK_ROOT/toolchains/llvm/prebuilt -name "clang++*"`) # can not be "clang*": clang-tidy
gnu_nm=(`find $NDK_ROOT/toolchains/llvm/prebuilt -name "*-android*nm"`) # can not be "clang*": clang-tidy
clangxx=${clangxxs[0]}
ld_lld=${clangxx/clang++/ld.lld}
[ -f "$ld_lld" ] || ld_lld=
[ -f "$gxx" ] || {
IS_CLANG=true
echo "gxx not found!!!!"
}
echo "g++: $gxx, clang++: $clangxx IS_CLANG:$IS_CLANG, ld_lld: $ld_lld, as: $as"
$IS_CLANG && probe_cc $clangxx || probe_cc $gxx
ANDROID_GCC_DIR=${as%bin*}
[ -z "$gnu_nm" ] && TOOLCHAIN_OPT+=" --ar=llvm-ar --ranlib=llvm-ranlib --nm=llvm-nm"
local ANDROID_LLVM_DIR=${clangxx%bin*}
echo "ANDROID_GCC_DIR=${ANDROID_GCC_DIR}"
echo "ANDROID_LLVM_DIR=${ANDROID_LLVM_DIR}"
ANDROID_GCC_DIR_REL=${ANDROID_GCC_DIR#$NDK_ROOT}
[ -n "$ld_lld" ] && {
TRY_FIX_CORTEX_A8=false
TOOLCHAIN_OPT+=" --strip=$LLVM_STRIP" # https://github.com/android/ndk/issues/1148 TODO: add in use_llvm_binutils if llvm-strip works for other platforms
LFLAGS_CLANG+=" -fuse-ld=lld -rtlib=compiler-rt" # use compiler-rt instead of default libgcc.a so -gcc-toolchain is not required
} || {
LFLAGS_CLANG+=" -gcc-toolchain \$NDK_ROOT/$ANDROID_GCC_DIR_REL" # ld from gcc toolchain. TODO: lld?
}
$TRY_FIX_CORTEX_A8 && EXTRA_LDFLAGS+=" -Wl,--fix-cortex-a8"
$FFGIT || [ "$ANDROID_ARCH" == "arm" ] && [[ $FFMAJOR < 4 ]] && CFLAGS_CLANG="-fno-integrated-as -gcc-toolchain \$NDK_ROOT/$ANDROID_GCC_DIR_REL $CFLAGS_CLANG" # Disable integrated-as for better compatibility, but need as from gcc toolchain. from ndk cmake
local ANDROID_SYSROOT_LIB_REL="platforms/android-$API_LEVEL/arch-${ANDROID_ARCH}"
local ANDROID_SYSROOT_LIB="$NDK_ROOT/$ANDROID_SYSROOT_LIB_REL"
[ -d "$ANDROID_LLVM_DIR/sysroot" ] && UNIFIED_SYSROOT="$ANDROID_LLVM_DIR/sysroot"
if [ -d "$UNIFIED_SYSROOT" ]; then
[ $API_LEVEL -lt 21 ] && PATCH_MMAP="void* mmap(void*, size_t, int, int, int, __kernel_off_t);"
ANDROID_SYSROOT_REL=sysroot
SYSROOT=$NDK_ROOT/$ANDROID_SYSROOT_REL
if [ -d "$ANDROID_LLVM_DIR/sysroot" ]; then
SYSROOT="$ANDROID_LLVM_DIR/$ANDROID_SYSROOT_REL"
ANDROID_SYSROOT_LIB="$SYSROOT/usr/lib/$ANDROID_TOOLCHAIN_PREFIX/$API_LEVEL"
[ "$ANDROID_ARCH" == "arm" ] && EXE_FLAGS+=" -lunwind" #r21 undefined __aeabi_unwind_cpp_pr0 from compiler-rt. linking to libgcc auto add libunwind