x86inc: Fix call with memory operands
[x264.git] / configure
1 #!/bin/bash
2
3 if test x"$1" = x"-h" -o x"$1" = x"--help" ; then
4 cat <<EOF
5 Usage: ./configure [options]
6
7 Help:
8   -h, --help               print this message
9
10 Standard options:
11   --prefix=PREFIX          install architecture-independent files in PREFIX
12                            [/usr/local]
13   --exec-prefix=EPREFIX    install architecture-dependent files in EPREFIX
14                            [PREFIX]
15   --bindir=DIR             install binaries in DIR [EPREFIX/bin]
16   --libdir=DIR             install libs in DIR [EPREFIX/lib]
17   --includedir=DIR         install includes in DIR [PREFIX/include]
18   --extra-asflags=EASFLAGS add EASFLAGS to ASFLAGS
19   --extra-cflags=ECFLAGS   add ECFLAGS to CFLAGS
20   --extra-ldflags=ELDFLAGS add ELDFLAGS to LDFLAGS
21   --extra-rcflags=ERCFLAGS add ERCFLAGS to RCFLAGS
22
23 Configuration options:
24   --disable-cli            disable cli
25   --system-libx264         use system libx264 instead of internal
26   --enable-shared          build shared library
27   --enable-static          build static library
28   --disable-opencl         disable OpenCL features
29   --disable-gpl            disable GPL-only features
30   --disable-thread         disable multithreaded encoding
31   --disable-win32thread    disable win32threads (windows only)
32   --disable-interlaced     disable interlaced encoding support
33   --bit-depth=BIT_DEPTH    set output bit depth (8-10) [8]
34   --chroma-format=FORMAT   output chroma format (420, 422, 444, all) [all]
35
36 Advanced options:
37   --disable-asm            disable platform-specific assembly optimizations
38   --enable-lto             enable link-time optimization
39   --enable-debug           add -g
40   --enable-gprof           add -pg
41   --enable-strip           add -s
42   --enable-pic             build position-independent code
43
44 Cross-compilation:
45   --host=HOST              build programs to run on HOST
46   --cross-prefix=PREFIX    use PREFIX for compilation tools
47   --sysroot=SYSROOT        root of cross-build tree
48
49 External library support:
50   --disable-avs            disable avisynth support
51   --disable-swscale        disable swscale support
52   --disable-lavf           disable libavformat support
53   --disable-ffms           disable ffmpegsource support
54   --disable-gpac           disable gpac support
55   --disable-lsmash         disable lsmash support
56
57 EOF
58 exit 1
59 fi
60
61 log_check() {
62     echo -n "checking $1... " >> config.log
63 }
64
65 log_ok() {
66     echo "yes" >> config.log
67 }
68
69 log_fail() {
70     echo "no" >> config.log
71 }
72
73 log_msg() {
74     echo "$1" >> config.log
75 }
76
77 cc_cflags() {
78     # several non gcc compilers issue an incredibly large number of warnings on high warning levels,
79     # suppress them by reducing the warning level rather than having to use #pragmas
80     for arg in $*; do
81         [[ "$arg" = -falign-loops* ]] && arg=
82         [ "$arg" = -fno-tree-vectorize ] && arg=
83         [ "$arg" = -Wshadow ] && arg=
84         [ "$arg" = -Wno-maybe-uninitialized ] && arg=
85         [[ "$arg" = -mpreferred-stack-boundary* ]] && arg=
86         [[ "$arg" = -l* ]] && arg=
87         [[ "$arg" = -L* ]] && arg=
88         if [ $compiler_style = MS ]; then
89             [ "$arg" = -ffast-math ] && arg="-fp:fast"
90             [ "$arg" = -Wall ] && arg=
91             [ "$arg" = -Werror ] && arg="-W3 -WX"
92             [ "$arg" = -g ] && arg=-Z7
93             [ "$arg" = -fomit-frame-pointer ] && arg=
94             [ "$arg" = -s ] && arg=
95             [ "$arg" = -fPIC ] && arg=
96         else
97             [ "$arg" = -ffast-math ] && arg=
98             [ "$arg" = -Wall ] && arg=
99             [ "$arg" = -Werror ] && arg="-w3 -Werror"
100         fi
101         [ $compiler = CL -a "$arg" = -O3 ] && arg=-O2
102
103         [ -n "$arg" ] && echo -n "$arg "
104     done
105 }
106
107 cl_ldflags() {
108     for arg in $*; do
109         arg=${arg/LIBPATH/libpath}
110         [ "${arg#-libpath:}" == "$arg" -a "${arg#-l}" != "$arg" ] && arg=${arg#-l}.lib
111         [ "${arg#-L}" != "$arg" ] && arg=-libpath:${arg#-L}
112         [ "$arg" = -Wl,--large-address-aware ] && arg=-largeaddressaware
113         [ "$arg" = -s ] && arg=
114         [ "$arg" = -Wl,-Bsymbolic ] && arg=
115         [ "$arg" = -fno-tree-vectorize ] && arg=
116         [ "$arg" = -Werror ] && arg=
117         [ "$arg" = -Wshadow ] && arg=
118         [ "$arg" = -Wmaybe-uninitialized ] && arg=
119         [[ "$arg" = -Qdiag-error* ]] && arg=
120
121         arg=${arg/pthreadGC/pthreadVC}
122         [ "$arg" = avifil32.lib ] && arg=vfw32.lib
123         [ "$arg" = gpac_static.lib ] && arg=libgpac_static.lib
124         [ "$arg" = x264.lib ] && arg=libx264.lib
125
126         [ -n "$arg" ] && echo -n "$arg "
127     done
128 }
129
130 cc_check() {
131     if [ -z "$3" ]; then
132         if [ -z "$1$2" ]; then
133             log_check "whether $CC works"
134         elif [ -z "$1" ]; then
135             log_check "for $2"
136         else
137             log_check "for $1"
138         fi
139     elif [ -z "$1" ]; then
140         if [ -z "$2" ]; then
141             log_check "whether $CC supports $3"
142         else
143             log_check "whether $CC supports $3 with $2"
144         fi
145     else
146         log_check "for $3 in $1";
147     fi
148     rm -f conftest.c
149     for arg in $1; do
150         echo "#include <$arg>" >> conftest.c
151     done
152     echo "int main (void) { $3 return 0; }" >> conftest.c
153     if [ $compiler_style = MS ]; then
154         cc_cmd="$CC conftest.c $(cc_cflags $CFLAGS $CHECK_CFLAGS $2) -link $(cl_ldflags $2 $LDFLAGSCLI $LDFLAGS)"
155     else
156         cc_cmd="$CC conftest.c $CFLAGS $CHECK_CFLAGS $2 $LDFLAGSCLI $LDFLAGS -o conftest"
157     fi
158     if $cc_cmd >conftest.log 2>&1; then
159         res=$?
160         log_ok
161     else
162         res=$?
163         log_fail
164         log_msg "Failed commandline was:"
165         log_msg "--------------------------------------------------"
166         log_msg "$cc_cmd"
167         cat conftest.log >> config.log
168         log_msg "--------------------------------------------------"
169         log_msg "Failed program was:"
170         log_msg "--------------------------------------------------"
171         cat conftest.c >> config.log
172         log_msg "--------------------------------------------------"
173     fi
174     return $res
175 }
176
177 cpp_check() {
178     log_check "whether $3 is true"
179     rm -f conftest.c
180     for arg in $1; do
181         echo "#include <$arg>" >> conftest.c
182     done
183     echo -e "#if !($3) \n#error $4 \n#endif " >> conftest.c
184     if [ $compiler_style = MS ]; then
185         cpp_cmd="$CC conftest.c $(cc_cflags $CFLAGS $2) -P"
186     else
187         cpp_cmd="$CC conftest.c $CFLAGS $2 -E -o conftest"
188     fi
189     if $cpp_cmd >conftest.log 2>&1; then
190         res=$?
191         log_ok
192     else
193         res=$?
194         log_fail
195         log_msg "--------------------------------------------------"
196         cat conftest.log >> config.log
197         log_msg "--------------------------------------------------"
198         log_msg "Failed program was:"
199         log_msg "--------------------------------------------------"
200         cat conftest.c >> config.log
201         log_msg "--------------------------------------------------"
202     fi
203     return $res
204 }
205
206 as_check() {
207     log_check "whether $AS supports $1"
208     echo "$1" > conftest$AS_EXT
209     as_cmd="$AS conftest$AS_EXT $ASFLAGS $2 -o conftest.o"
210     if $as_cmd >conftest.log 2>&1; then
211         res=$?
212         log_ok
213     else
214         res=$?
215         log_fail
216         log_msg "Failed commandline was:"
217         log_msg "--------------------------------------------------"
218         log_msg "$as_cmd"
219         cat conftest.log >> config.log
220         log_msg "--------------------------------------------------"
221         log_msg "Failed program was:"
222         log_msg "--------------------------------------------------"
223         cat conftest$AS_EXT >> config.log
224         log_msg "--------------------------------------------------"
225     fi
226     return $res
227 }
228
229 rc_check() {
230     log_check "whether $RC works"
231     echo "$1" > conftest.rc
232     if [ $compiler = GNU ]; then
233         rc_cmd="$RC $RCFLAGS -o conftest.o conftest.rc"
234     else
235         rc_cmd="$RC $RCFLAGS -foconftest.o conftest.rc"
236     fi
237     if $rc_cmd >conftest.log 2>&1; then
238         res=$?
239         log_ok
240     else
241         res=$?
242         log_fail
243         log_msg "Failed commandline was:"
244         log_msg "--------------------------------------------------"
245         log_msg "$rc_cmd"
246         cat conftest.log >> config.log
247         log_msg "--------------------------------------------------"
248         log_msg "Failed program was:"
249         log_msg "--------------------------------------------------"
250         cat conftest.rc >> config.log
251         log_msg "--------------------------------------------------"
252     fi
253     return $res
254 }
255
256 define() {
257     echo "#define $1$([ -n "$2" ] && echo " $2" || echo " 1")" >> config.h
258 }
259
260 die() {
261     log_msg "DIED: $@"
262     echo "$@"
263     exit 1
264 }
265
266 configure_system_override() {
267     log_check "system libx264 configuration"
268     x264_config_path="$1/x264_config.h"
269     if [ -e "$x264_config_path" ]; then
270         res=$?
271         log_ok
272         arg="$(grep '#define X264_GPL ' $x264_config_path | sed -e 's/#define X264_GPL *//; s/ *$//')"
273         if [ -n "$arg" ]; then
274             [ "$arg" = 0 ] && arg="no" || arg="yes"
275             [ "$arg" != "$gpl" ] && die "Incompatible license with system libx264"
276         fi
277         arg="$(grep '#define X264_BIT_DEPTH ' $x264_config_path | sed -e 's/#define X264_BIT_DEPTH *//; s/ *$//')"
278         if [ -n "$arg" ]; then
279             if [ "$arg" != "$bit_depth" ]; then
280                 echo "Override output bit depth with system libx264 configuration"
281                 bit_depth="$arg"
282             fi
283         fi
284         arg="$(grep '#define X264_CHROMA_FORMAT ' $x264_config_path | sed -e 's/#define X264_CHROMA_FORMAT *//; s/ *$//')"
285         if [ -n "$arg" ]; then
286             [ "$arg" = 0 ] && arg="all" || arg="${arg#X264_CSP_I}"
287             if [ "$arg" != "$chroma_format" ]; then
288                 echo "Override output chroma format with system libx264 configuration"
289                 chroma_format="$arg"
290             fi
291         fi
292         arg="$(grep '#define X264_INTERLACED ' $x264_config_path | sed -e 's/#define X264_INTERLACED *//; s/ *$//')"
293         if [ -n "$arg" ]; then
294             [ "$arg" = 0 ] && arg="no" || arg="yes"
295             if [ "$arg" != "$interlaced" ]; then
296                 echo "Override interlaced encoding support with system libx264 configuration"
297                 interlaced="$arg"
298             fi
299         fi
300     else
301         res=$?
302         log_fail
303         log_msg "Failed search path was: $x264_config_path"
304     fi
305     return $res
306 }
307
308 rm -f x264_config.h config.h config.mak config.log x264.pc x264.def
309 rm -rf conftest*
310
311 # Construct a path to the specified directory relative to the working directory
312 relative_path() {
313     local base="${PWD%/}"
314     local path="$(cd "$1" >/dev/null; printf '%s/.' "${PWD%/}")"
315     local up=''
316
317     while [[ $path != "$base/"* ]]; do
318         base="${base%/*}"
319         up="../$up"
320     done
321
322     dirname "$up${path#"$base/"}"
323 }
324
325 SRCPATH="$(relative_path "$(dirname "$0")")"
326 echo "$SRCPATH" | grep -q ' ' && die "Out of tree builds are impossible with whitespace in source path."
327 [ -e "$SRCPATH/config.h" -o -e "$SRCPATH/x264_config.h" ] && die "Out of tree builds are impossible with config.h/x264_config.h in source dir."
328
329 prefix='/usr/local'
330 exec_prefix='${prefix}'
331 bindir='${exec_prefix}/bin'
332 libdir='${exec_prefix}/lib'
333 includedir='${prefix}/include'
334 DEVNULL='/dev/null'
335
336 cli="yes"
337 cli_libx264="internal"
338 shared="no"
339 static="no"
340 avs="auto"
341 lavf="auto"
342 ffms="auto"
343 gpac="auto"
344 lsmash="auto"
345 mp4="no"
346 gpl="yes"
347 thread="auto"
348 swscale="auto"
349 asm="auto"
350 interlaced="yes"
351 lto="no"
352 debug="no"
353 gprof="no"
354 strip="no"
355 pic="no"
356 bit_depth="8"
357 chroma_format="all"
358 compiler="GNU"
359 compiler_style="GNU"
360 opencl="yes"
361 vsx="auto"
362
363 CFLAGS="$CFLAGS -Wall -I. -I\$(SRCPATH)"
364 LDFLAGS="$LDFLAGS"
365 LDFLAGSCLI="$LDFLAGSCLI"
366 ASFLAGS="$ASFLAGS -I. -I\$(SRCPATH)"
367 RCFLAGS="$RCFLAGS"
368 CHECK_CFLAGS=""
369 HAVE_GETOPT_LONG=1
370 cross_prefix=""
371
372 EXE=""
373 AS_EXT=".S"
374 NL="
375 "
376
377 # list of all preprocessor HAVE values we can define
378 CONFIG_HAVE="MALLOC_H ALTIVEC ALTIVEC_H MMX ARMV6 ARMV6T2 NEON BEOSTHREAD POSIXTHREAD WIN32THREAD THREAD LOG2F SWSCALE \
379              LAVF FFMS GPAC AVS GPL VECTOREXT INTERLACED CPU_COUNT OPENCL THP LSMASH X86_INLINE_ASM AS_FUNC INTEL_DISPATCHER \
380              MSA MMAP WINRT VSX"
381
382 # parse options
383
384 for opt do
385     optarg="${opt#*=}"
386     case "$opt" in
387         --prefix=*)
388             prefix="$optarg"
389             ;;
390         --exec-prefix=*)
391             exec_prefix="$optarg"
392             ;;
393         --bindir=*)
394             bindir="$optarg"
395             ;;
396         --libdir=*)
397             libdir="$optarg"
398             ;;
399         --includedir=*)
400             includedir="$optarg"
401             ;;
402         --disable-cli)
403             cli="no"
404             ;;
405         --system-libx264)
406             cli_libx264="system"
407             ;;
408         --enable-shared)
409             shared="yes"
410             ;;
411         --enable-static)
412             static="yes"
413             ;;
414         --disable-asm)
415             asm="no"
416             ;;
417         --disable-interlaced)
418             interlaced="no"
419             ;;
420         --disable-avs)
421             avs="no"
422             ;;
423         --disable-lavf)
424             lavf="no"
425             ;;
426         --disable-ffms)
427             ffms="no"
428             ;;
429         --disable-gpac)
430             gpac="no"
431             ;;
432         --disable-lsmash)
433             lsmash="no"
434             ;;
435         --disable-gpl)
436             gpl="no"
437             ;;
438         --extra-asflags=*)
439             ASFLAGS="$ASFLAGS $optarg"
440             ;;
441         --extra-cflags=*)
442             CFLAGS="$CFLAGS $optarg"
443             ;;
444         --extra-ldflags=*)
445             LDFLAGS="$LDFLAGS $optarg"
446             ;;
447         --extra-rcflags=*)
448             RCFLAGS="$RCFLAGS $optarg"
449             ;;
450         --disable-thread)
451             thread="no"
452             ;;
453         --disable-win32thread)
454             [ "$thread" != "no" ] && thread="posix"
455             ;;
456         --disable-swscale)
457             swscale="no"
458             ;;
459         --enable-lto)
460             lto="auto"
461             ;;
462         --enable-debug)
463             debug="yes"
464             ;;
465         --enable-gprof)
466             CFLAGS="$CFLAGS -pg"
467             LDFLAGS="$LDFLAGS -pg"
468             gprof="yes"
469             ;;
470         --enable-strip)
471             strip="yes"
472             ;;
473         --enable-pic)
474             pic="yes"
475             ;;
476         --host=*)
477             host="$optarg"
478             ;;
479         --disable-vsx)
480             vsx="no"
481             ;;
482         --disable-opencl)
483             opencl="no"
484             ;;
485         --cross-prefix=*)
486             cross_prefix="$optarg"
487             ;;
488         --sysroot=*)
489             CFLAGS="$CFLAGS --sysroot=$optarg"
490             LDFLAGS="$LDFLAGS --sysroot=$optarg"
491             ;;
492         --bit-depth=*)
493             bit_depth="$optarg"
494             if [ "$bit_depth" -lt "8" -o "$bit_depth" -gt "10" ]; then
495                 echo "Supplied bit depth must be in range [8,10]."
496                 exit 1
497             fi
498             bit_depth=`expr $bit_depth + 0`
499             ;;
500         --chroma-format=*)
501             chroma_format="$optarg"
502             if [ $chroma_format != "420" -a $chroma_format != "422" -a $chroma_format != "444" -a $chroma_format != "all" ]; then
503                 echo "Supplied chroma format must be 420, 422, 444 or all."
504                 exit 1
505             fi
506             ;;
507         *)
508             echo "Unknown option $opt, ignored"
509             ;;
510     esac
511 done
512
513 [ "$cli" = "no" -a "$shared" = "no" -a "$static" = "no" ] && die "Nothing to build. Enable cli, shared or static."
514
515 CC="${CC-${cross_prefix}gcc}"
516 STRIP="${STRIP-${cross_prefix}strip}"
517 INSTALL="${INSTALL-install}"
518 PKGCONFIG="${PKGCONFIG-${cross_prefix}pkg-config}"
519
520 # ar and ranlib doesn't load the LTO plugin by default, prefer the gcc-prefixed wrappers which does.
521 if ${cross_prefix}gcc-ar --version >/dev/null 2>&1; then
522     AR="${AR-${cross_prefix}gcc-ar}"
523 else
524     AR="${AR-${cross_prefix}ar}"
525 fi
526 if ${cross_prefix}gcc-ranlib --version >/dev/null 2>&1; then
527     RANLIB="${RANLIB-${cross_prefix}gcc-ranlib}"
528 else
529     RANLIB="${RANLIB-${cross_prefix}ranlib}"
530 fi
531
532 if [ "x$host" = x ]; then
533     host=`${SRCPATH}/config.guess`
534 fi
535 # normalize a triplet into a quadruplet
536 host=`${SRCPATH}/config.sub $host`
537
538 # split $host
539 host_cpu="${host%%-*}"
540 host="${host#*-}"
541 host_vendor="${host%%-*}"
542 host_os="${host#*-}"
543
544 trap 'rm -rf conftest*' EXIT
545
546 # test for use of compilers that require specific handling
547 cc_base=`basename "$CC"`
548 QPRE="-"
549 if [[ $host_os = mingw* || $host_os = cygwin* ]]; then
550     if [[ "$cc_base" = icl || "$cc_base" = icl[\ .]* ]]; then
551         # Windows Intel Compiler creates dependency generation with absolute Windows paths, Cygwin's make does not support Windows paths.
552         [[ $host_os = cygwin* ]] && die "Windows Intel Compiler support requires MSYS"
553         compiler=ICL
554         compiler_style=MS
555         CFLAGS="$CFLAGS -Qstd=c99 -nologo -Qms0 -DHAVE_STRING_H -I\$(SRCPATH)/extras"
556         QPRE="-Q"
557         `$CC 2>&1 | grep -q IA-32` && host_cpu=i486
558         `$CC 2>&1 | grep -q "Intel(R) 64"` && host_cpu=x86_64
559         cpp_check "" "" "_MSC_VER >= 1400" || die "Windows Intel Compiler support requires Visual Studio 2005 or newer"
560         if cc_check '' -Qdiag-error:10006,10157 ; then
561             CHECK_CFLAGS="$CHECK_CFLAGS -Qdiag-error:10006,10157"
562         fi
563     elif [[ "$cc_base" = cl || "$cc_base" = cl[\ .]* ]]; then
564         # Standard Microsoft Visual Studio
565         compiler=CL
566         compiler_style=MS
567         CFLAGS="$CFLAGS -nologo -GS- -DHAVE_STRING_H -I\$(SRCPATH)/extras"
568         `$CC 2>&1 | grep -q 'x86'` && host_cpu=i486
569         `$CC 2>&1 | grep -q 'x64'` && host_cpu=x86_64
570         cpp_check '' '' '_MSC_VER > 1800 || (_MSC_VER == 1800 && _MSC_FULL_VER >= 180030324)' || die "Microsoft Visual Studio support requires Visual Studio 2013 Update 2 or newer"
571     else
572         # MinGW uses broken pre-VS2015 Microsoft printf functions unless it's told to use the POSIX ones.
573         CFLAGS="$CFLAGS -D_POSIX_C_SOURCE=200112L"
574     fi
575 else
576     if [[ "$cc_base" = icc || "$cc_base" = icc[\ .]* ]]; then
577         AR="xiar"
578         compiler=ICC
579     fi
580 fi
581
582 if [[ "$cc_base" = clang* ]]; then
583     if cc_check '' -Werror=unknown-warning-option ; then
584         CHECK_CFLAGS="$CHECK_CFLAGS -Werror=unknown-warning-option"
585     fi
586 fi
587
588 libm=""
589 case $host_os in
590     beos*)
591         SYS="BEOS"
592         define HAVE_MALLOC_H
593         ;;
594     darwin*)
595         SYS="MACOSX"
596         libm="-lm"
597         if [ "$pic" = "no" ]; then
598             cc_check "" -mdynamic-no-pic && CFLAGS="$CFLAGS -mdynamic-no-pic"
599         fi
600         ;;
601     freebsd*)
602         SYS="FREEBSD"
603         libm="-lm"
604         ;;
605     kfreebsd*-gnu)
606         SYS="FREEBSD"
607         define HAVE_MALLOC_H
608         libm="-lm"
609         ;;
610     netbsd*)
611         SYS="NETBSD"
612         libm="-lm"
613         ;;
614     openbsd*)
615         SYS="OPENBSD"
616         libm="-lm"
617         ;;
618     *linux*)
619         SYS="LINUX"
620         define HAVE_MALLOC_H
621         libm="-lm"
622         ;;
623     gnu*)
624         SYS="HURD"
625         define HAVE_MALLOC_H
626         libm="-lm"
627         ;;
628     cygwin*|mingw*|msys*)
629         EXE=".exe"
630         if [[ $host_os = cygwin* ]] && cpp_check "" "" "defined(__CYGWIN__)" ; then
631             SYS="CYGWIN"
632             define HAVE_MALLOC_H
633         else
634             SYS="WINDOWS"
635             DEVNULL="NUL"
636             LDFLAGSCLI="$LDFLAGSCLI -lshell32"
637             [ $compiler = GNU ] && RC="${RC-${cross_prefix}windres}" || RC="${RC-rc}"
638         fi
639         ;;
640     sunos*|solaris*)
641         SYS="SunOS"
642         define HAVE_MALLOC_H
643         libm="-lm"
644         if cc_check "" /usr/lib/64/values-xpg6.o; then
645             LDFLAGS="$LDFLAGS /usr/lib/64/values-xpg6.o"
646         else
647             LDFLAGS="$LDFLAGS /usr/lib/values-xpg6.o"
648         fi
649         if test -x /usr/ucb/install ; then
650             INSTALL=/usr/ucb/install
651         elif test -x /usr/bin/ginstall ; then
652             # OpenSolaris
653             INSTALL=/usr/bin/ginstall
654         elif test -x /usr/gnu/bin/install ; then
655             # OpenSolaris
656             INSTALL=/usr/gnu/bin/install
657         fi
658         HAVE_GETOPT_LONG=0
659         ;;
660     *qnx*)
661         SYS="QNX"
662         define HAVE_MALLOC_H
663         libm="-lm"
664         HAVE_GETOPT_LONG=0
665         CFLAGS="$CFLAGS -I\$(SRCPATH)/extras"
666         ;;
667     *haiku*)
668         SYS="HAIKU"
669         ;;
670     *)
671         die "Unknown system $host, edit the configure"
672         ;;
673 esac
674
675 LDFLAGS="$LDFLAGS $libm"
676
677 stack_alignment=4
678 case $host_cpu in
679     i*86)
680         ARCH="X86"
681         AS="${AS-yasm}"
682         AS_EXT=".asm"
683         ASFLAGS="$ASFLAGS -DARCH_X86_64=0 -I\$(SRCPATH)/common/x86/"
684         if [ $compiler = GNU ]; then
685             if [[ "$asm" == auto && "$CFLAGS" != *-march* ]]; then
686                 CFLAGS="$CFLAGS -march=i686"
687             fi
688             if [[ "$asm" == auto && "$CFLAGS" != *-mfpmath* ]]; then
689                 CFLAGS="$CFLAGS -mfpmath=sse -msse -msse2"
690             fi
691             CFLAGS="-m32 $CFLAGS"
692             LDFLAGS="-m32 $LDFLAGS"
693         fi
694         if [ "$SYS" = MACOSX ]; then
695             ASFLAGS="$ASFLAGS -f macho32 -DPREFIX"
696         elif [ "$SYS" = WINDOWS -o "$SYS" = CYGWIN ]; then
697             ASFLAGS="$ASFLAGS -f win32 -DPREFIX"
698             LDFLAGS="$LDFLAGS -Wl,--large-address-aware"
699             [ $compiler = GNU ] && LDFLAGS="$LDFLAGS -Wl,--dynamicbase,--nxcompat,--tsaware"
700             [ $compiler = GNU ] && RCFLAGS="--target=pe-i386 $RCFLAGS"
701         else
702             ASFLAGS="$ASFLAGS -f elf32"
703         fi
704         ;;
705     x86_64)
706         ARCH="X86_64"
707         AS="${AS-yasm}"
708         AS_EXT=".asm"
709         ASFLAGS="$ASFLAGS -DARCH_X86_64=1 -I\$(SRCPATH)/common/x86/"
710         stack_alignment=16
711         [ $compiler = GNU ] && CFLAGS="-m64 $CFLAGS" && LDFLAGS="-m64 $LDFLAGS"
712         if [ "$SYS" = MACOSX ]; then
713             ASFLAGS="$ASFLAGS -f macho64 -DPIC -DPREFIX"
714             if cc_check '' "-arch x86_64"; then
715                 CFLAGS="$CFLAGS -arch x86_64"
716                 LDFLAGS="$LDFLAGS -arch x86_64"
717             fi
718         elif [ "$SYS" = WINDOWS -o "$SYS" = CYGWIN ]; then
719             ASFLAGS="$ASFLAGS -f win64"
720             if [ $compiler = GNU ]; then
721                 # only the GNU toolchain is inconsistent in prefixing function names with _
722                 cc_check "" "-S" && grep -q "_main:" conftest && ASFLAGS="$ASFLAGS -DPREFIX"
723                 cc_check "" "-Wl,--high-entropy-va" && LDFLAGS="$LDFLAGS -Wl,--high-entropy-va"
724                 LDFLAGS="$LDFLAGS -Wl,--dynamicbase,--nxcompat,--tsaware"
725                 LDFLAGSCLI="$LDFLAGSCLI -Wl,--image-base,0x140000000"
726                 SOFLAGS="$SOFLAGS -Wl,--image-base,0x180000000"
727                 RCFLAGS="--target=pe-x86-64 $RCFLAGS"
728             fi
729         else
730             ASFLAGS="$ASFLAGS -f elf64"
731         fi
732         ;;
733     powerpc*)
734         ARCH="PPC"
735         if [ $asm = auto ] ; then
736             define HAVE_ALTIVEC
737             AS="${AS-${CC}}"
738             AS_EXT=".c"
739             if [ $SYS = MACOSX ] ; then
740                 CFLAGS="$CFLAGS -faltivec -fastf -mcpu=G4"
741             else
742                 CFLAGS="$CFLAGS -maltivec -mabi=altivec"
743                 define HAVE_ALTIVEC_H
744             fi
745             if [ "$vsx" != "no" ] ; then
746                 vsx="no"
747                 if cc_check "" "-mvsx" ; then
748                     CFLAGS="$CFLAGS -mvsx"
749                     define HAVE_VSX
750                     vsx="yes"
751                 fi
752             fi
753         fi
754         ;;
755     sparc)
756         ARCH="SPARC"
757         ;;
758     mips*)
759         ARCH="MIPS"
760         AS="${AS-${CC}}"
761         AS_EXT=".c"
762         ;;
763     arm*)
764         ARCH="ARM"
765         if [ "$SYS" = MACOSX ] ; then
766             AS="${AS-${SRCPATH}/tools/gas-preprocessor.pl -arch arm -- ${CC}}"
767             ASFLAGS="$ASFLAGS -DPREFIX -DPIC"  # apple's ld doesn't support movw/movt relocations at all
768             # build for armv7 by default
769             if ! echo $CFLAGS | grep -Eq '\-arch' ; then
770                 CFLAGS="$CFLAGS -arch armv7"
771                 LDFLAGS="$LDFLAGS -arch armv7"
772             fi
773         else
774             AS="${AS-${CC}}"
775         fi
776         ;;
777     aarch64)
778         ARCH="AARCH64"
779         stack_alignment=16
780         if [ "$SYS" = MACOSX ] ; then
781             AS="${AS-${SRCPATH}/tools/gas-preprocessor.pl -arch aarch64 -- ${CC}}"
782             ASFLAGS="$ASFLAGS -DPREFIX -DPIC"
783         else
784             AS="${AS-${CC}}"
785         fi
786         ;;
787     s390|s390x)
788         ARCH="S390"
789         ;;
790     hppa*|parisc*)
791         ARCH="PARISC"
792         ;;
793     ia64)
794         ARCH="IA64"
795         ;;
796     alpha*)
797         ARCH="ALPHA"
798         ;;
799     *)
800         ARCH="$(echo $host_cpu | tr a-z A-Z)"
801         ;;
802 esac
803
804 [ "$vsx" != "yes" ] && vsx="no"
805
806 if [ $SYS = WINDOWS ]; then
807     if ! rc_check "0 RCDATA {0}" ; then
808         RC=""
809     fi
810
811     if cpp_check "winapifamily.h" "" "!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)" ; then
812         [ $compiler = CL ] || die "WinRT requires MSVC"
813         define HAVE_WINRT
814         CFLAGS="$CFLAGS -MD"
815         LDFLAGS="$LDFLAGS -appcontainer"
816         if ! cpp_check "" "" "defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603" ; then
817             die "_WIN32_WINNT must be defined to at least 0x0603 (Windows 8.1) for WinRT"
818         elif cpp_check "" "" "_WIN32_WINNT >= 0x0A00" ; then
819             # Universal Windows Platform (Windows 10)
820             LDFLAGS="$LDFLAGS -lWindowsApp"
821         fi
822         cli="no"
823         opencl="no"
824     fi
825 fi
826
827 log_msg "x264 configure script"
828 if [ -n "$*" ]; then
829     msg="Command line options:"
830     for i in $@; do
831         msg="$msg \"$i\""
832     done
833     log_msg "$msg"
834 fi
835 log_msg ""
836
837 # check requirements
838
839 cc_check || die "No working C compiler found."
840
841 if [ $compiler_style = GNU ]; then
842     if cc_check '' -std=gnu99 'for( int i = 0; i < 9; i++ );' ; then
843         CFLAGS="$CFLAGS -std=gnu99 -D_GNU_SOURCE"
844     elif cc_check '' -std=c99 'for( int i = 0; i < 9; i++ );' ; then
845         CFLAGS="$CFLAGS -std=c99 -D_POSIX_C_SOURCE=200112L -D_BSD_SOURCE"
846     elif ! cc_check '' '' 'for( int i = 0; i < 9; i++ );' ; then
847         die "C99 compiler is needed for compilation."
848     fi
849 fi
850
851 if [ $shared = yes -a \( $ARCH = "X86_64" -o $ARCH = "PPC" -o $ARCH = "ALPHA" -o $ARCH = "ARM" -o $ARCH = "IA64" -o $ARCH = "PARISC" -o $ARCH = "MIPS" -o $ARCH = "AARCH64" \) ] ; then
852     pic="yes"
853 fi
854
855 if [ $compiler = GNU -a \( $ARCH = X86 -o $ARCH = X86_64 \) ] ; then
856     if cc_check '' -mpreferred-stack-boundary=5 ; then
857         CFLAGS="$CFLAGS -mpreferred-stack-boundary=5"
858         stack_alignment=32
859     elif [ $stack_alignment -lt 16 ] && cc_check '' -mpreferred-stack-boundary=4 ; then
860         CFLAGS="$CFLAGS -mpreferred-stack-boundary=4"
861         stack_alignment=16
862     fi
863 elif [ $compiler = ICC -a $ARCH = X86 ]; then
864     # icc on linux has various degrees of mod16 stack support
865     if [ $SYS = LINUX ]; then
866         # >= 12 defaults to a mod16 stack
867         if cpp_check "" "" "__INTEL_COMPILER >= 1200" ; then
868             stack_alignment=16
869         # 11 <= x < 12 is capable of keeping a mod16 stack, but defaults to not doing so.
870         elif cpp_check "" "" "__INTEL_COMPILER >= 1100" ; then
871             CFLAGS="$CFLAGS -falign-stack=assume-16-byte"
872             stack_alignment=16
873         fi
874         # < 11 is completely incapable of keeping a mod16 stack
875     fi
876 fi
877
878 if [ $asm = auto -a \( $ARCH = X86 -o $ARCH = X86_64 \) ] ; then
879     if ! as_check "vpmovzxwd ymm0, xmm0" ; then
880         VER=`($AS --version || echo no assembler) 2>/dev/null | head -n 1`
881         echo "Found $VER"
882         echo "Minimum version is yasm-1.2.0"
883         echo "If you really want to compile without asm, configure with --disable-asm."
884         exit 1
885     fi
886     cc_check '' '' '__asm__("pabsw %xmm0, %xmm0");' && define HAVE_X86_INLINE_ASM
887     ASFLAGS="$ASFLAGS -Worphan-labels"
888     define HAVE_MMX
889 fi
890
891 if [ $asm = auto -a $ARCH = ARM ] ; then
892     # set flags so neon is built by default
893     echo $CFLAGS | grep -Eq '(-mcpu|-march|-mfpu)' || CFLAGS="$CFLAGS -mcpu=cortex-a8 -mfpu=neon"
894
895     if  cc_check '' '' '__asm__("rev ip, ip");' ; then      define HAVE_ARMV6
896         cc_check '' '' '__asm__("movt r0, #0");'         && define HAVE_ARMV6T2
897         cc_check '' '' '__asm__("vadd.i16 q0, q0, q0");' && define HAVE_NEON
898         ASFLAGS="$ASFLAGS -c"
899     else
900         echo "You specified a pre-ARMv6 or Thumb-1 CPU in your CFLAGS."
901         echo "If you really want to run on such a CPU, configure with --disable-asm."
902         exit 1
903     fi
904 fi
905
906 if [ $asm = auto -a $ARCH = AARCH64 ] ; then
907     if  cc_check '' '' '__asm__("cmeq v0.8h, v0.8h, #0");' ; then define HAVE_NEON
908         ASFLAGS="$ASFLAGS -c"
909     else
910         echo "no NEON support, try adding -mfpu=neon to CFLAGS"
911         echo "If you really want to run on such a CPU, configure with --disable-asm."
912         exit 1
913     fi
914 fi
915
916 if [ $asm = auto -a \( $ARCH = ARM -o $ARCH = AARCH64 \) ] ; then
917     # check if the assembler supports '.func' (clang 3.5 does not)
918     as_check ".func test${NL}.endfunc" && define HAVE_AS_FUNC 1
919 fi
920
921 if [ $asm = auto -a $ARCH = MIPS ] ; then
922     if ! echo $CFLAGS | grep -Eq '(-march|-mmsa|-mno-msa)' ; then
923         cc_check '' '-mmsa -mfp64 -mhard-float' && CFLAGS="-mmsa -mfp64 -mhard-float $CFLAGS"
924     fi
925
926     if cc_check '' '' '__asm__("addvi.b $w0, $w1, 1");' ; then
927         define HAVE_MSA
928     else
929         echo "You specified a pre-MSA CPU in your CFLAGS."
930         echo "If you really want to run on such a CPU, configure with --disable-asm."
931         exit 1
932     fi
933 fi
934
935 [ $asm = no ] && AS=""
936 [ "x$AS" = x ] && asm="no" || asm="yes"
937
938 define ARCH_$ARCH
939 define SYS_$SYS
940
941 define STACK_ALIGNMENT $stack_alignment
942 ASFLAGS="$ASFLAGS -DSTACK_ALIGNMENT=$stack_alignment"
943
944 # skip endianness check for Intel Compiler and MSVS, as all supported platforms are little. each have flags that will cause the check to fail as well
945 CPU_ENDIAN="little-endian"
946 if [ $compiler = GNU ]; then
947     echo "int i[2] = {0x42494745,0}; double f[2] = {0x1.0656e6469616ep+102,0};" > conftest.c
948     $CC $CFLAGS conftest.c -c -o conftest.o 2>/dev/null || die "endian test failed"
949     if (${cross_prefix}strings -a conftest.o | grep -q BIGE) && (${cross_prefix}strings -a conftest.o | grep -q FPendian) ; then
950         define WORDS_BIGENDIAN
951         CPU_ENDIAN="big-endian"
952     elif !(${cross_prefix}strings -a conftest.o | grep -q EGIB && ${cross_prefix}strings -a conftest.o | grep -q naidnePF) ; then
953         die "endian test failed"
954     fi
955 fi
956
957 if [ "$cli_libx264" = "system" -a "$shared" != "yes" ] ; then
958     [ "$static" = "yes" ] && die "Option --system-libx264 can not be used together with --enable-static"
959     if $PKGCONFIG --exists x264 2>/dev/null; then
960         X264_LIBS="$($PKGCONFIG --libs x264)"
961         X264_INCLUDE_DIR="${X264_INCLUDE_DIR-$($PKGCONFIG --variable=includedir x264)}"
962         configure_system_override "$X264_INCLUDE_DIR" || die "Detection of system libx264 configuration failed"
963     else
964         die "Can not find system libx264"
965     fi
966 fi
967
968 # autodetect options that weren't forced nor disabled
969
970 libpthread=""
971 if [ "$SYS" = "WINDOWS" -a "$thread" = "posix" ] ; then
972     if [ "$gpl" = "no" ] ; then
973         echo "Warning: pthread-win32 is LGPL and is therefore not supported with --disable-gpl"
974         thread="no"
975     elif cc_check pthread.h -lpthread "pthread_create(0,0,0,0);" ; then
976         libpthread="-lpthread"
977     elif cc_check pthread.h -lpthreadGC2 "pthread_create(0,0,0,0);" ; then
978         libpthread="-lpthreadGC2"
979     elif cc_check pthread.h "-lpthreadGC2 -lwsock32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then
980         libpthread="-lpthreadGC2 -lwsock32"
981         define PTW32_STATIC_LIB
982     elif cc_check pthread.h "-lpthreadGC2 -lws2_32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then
983         libpthread="-lpthreadGC2 -lws2_32"
984         define PTW32_STATIC_LIB
985     else
986         thread="no"
987     fi
988 elif [ "$thread" != "no" ] ; then
989     thread="no"
990     case $SYS in
991         BEOS)
992             thread="beos"
993             define HAVE_BEOSTHREAD
994             ;;
995         WINDOWS)
996             thread="win32"
997             define HAVE_WIN32THREAD
998             ;;
999         QNX)
1000             cc_check pthread.h -lc "pthread_create(0,0,0,0);" && thread="posix" && libpthread="-lc"
1001             ;;
1002         *)
1003             if cc_check pthread.h -lpthread "pthread_create(0,0,0,0);" ; then
1004                thread="posix"
1005                libpthread="-lpthread"
1006             else
1007                 cc_check pthread.h "" "pthread_create(0,0,0,0);" && thread="posix" && libpthread=""
1008             fi
1009             ;;
1010     esac
1011 fi
1012 if [ "$thread" = "posix" ]; then
1013     LDFLAGS="$LDFLAGS $libpthread"
1014     define HAVE_POSIXTHREAD
1015     if [ "$SYS" = "LINUX" ] && cc_check sched.h "-D_GNU_SOURCE -Werror" "cpu_set_t p_aff; return CPU_COUNT(&p_aff);" ; then
1016         define HAVE_CPU_COUNT
1017     fi
1018 fi
1019 [ "$thread" != "no" ] && define HAVE_THREAD
1020
1021 if cc_check "math.h" "-Werror" "return log2f(2);" ; then
1022     define HAVE_LOG2F
1023 fi
1024
1025 if [ "$SYS" != "WINDOWS" ] && cpp_check "sys/mman.h unistd.h" "" "defined(MAP_PRIVATE)"; then
1026     define HAVE_MMAP
1027 fi
1028
1029 if [ "$SYS" = "LINUX" -a \( "$ARCH" = "X86" -o "$ARCH" = "X86_64" \) ] && cc_check "sys/mman.h" "" "MADV_HUGEPAGE;" ; then
1030     define HAVE_THP
1031 fi
1032
1033 if [ "$cli" = "no" ] ; then
1034     avs="no"
1035     lavf="no"
1036     ffms="no"
1037     gpac="no"
1038     lsmash="no"
1039     mp4="no"
1040     swscale="no"
1041 fi
1042
1043 if [ "$swscale" = "auto" ] ; then
1044     swscale="no"
1045     if $PKGCONFIG --exists libswscale 2>/dev/null; then
1046         SWSCALE_LIBS="$SWSCALE_LIBS $($PKGCONFIG --libs libswscale libavutil)"
1047         SWSCALE_CFLAGS="$SWSCALE_CFLAGS $($PKGCONFIG --cflags libswscale libavutil)"
1048     fi
1049     [ -z "$SWSCALE_LIBS" ] && SWSCALE_LIBS="-lswscale -lavutil"
1050
1051     if cc_check "libswscale/swscale.h" "$SWSCALE_CFLAGS $SWSCALE_LIBS" "sws_init_context(0,0,0);" ; then
1052         if cpp_check "libavutil/pixdesc.h" "$SWSCALE_CFLAGS $SWSCALE_LIBS" "defined(AV_PIX_FMT_FLAG_RGB)" ; then
1053             swscale="yes"
1054         else
1055             echo "Warning: AV_PIX_FMT_FLAG_RGB is missing from libavutil, update for swscale support"
1056         fi
1057     fi
1058 fi
1059
1060 if [ "$lavf" = "auto" ] ; then
1061     lavf="no"
1062     if $PKGCONFIG --exists libavformat libavcodec libswscale 2>/dev/null; then
1063         LAVF_LIBS="$LAVF_LIBS $($PKGCONFIG --libs libavformat libavcodec libavutil libswscale)"
1064         LAVF_CFLAGS="$LAVF_CFLAGS $($PKGCONFIG --cflags libavformat libavcodec libavutil libswscale)"
1065     fi
1066     if [ -z "$LAVF_LIBS" -a -z "$LAVF_CFLAGS" ]; then
1067         LAVF_LIBS="-lavformat"
1068         for lib in -lpostproc -lavcodec -lswscale -lavutil -lm -lz -lbz2 $libpthread -lavifil32 -lws2_32; do
1069             cc_check "" $lib && LAVF_LIBS="$LAVF_LIBS $lib"
1070         done
1071     fi
1072     LAVF_LIBS="-L. $LAVF_LIBS"
1073     if cc_check libavformat/avformat.h "$LAVF_CFLAGS $LAVF_LIBS" "av_frame_free(0);" ; then
1074         if [ "$swscale" = "yes" ]; then
1075             lavf="yes"
1076         else
1077             echo "Warning: libavformat is not supported without swscale support"
1078         fi
1079     fi
1080 fi
1081
1082 if [ "$ffms" = "auto" ] ; then
1083     ffms_major="2"; ffms_minor="21"; ffms_micro="0"; ffms_bump="0"
1084     ffms="no"
1085
1086     if $PKGCONFIG --exists ffms2 2>/dev/null; then
1087         FFMS2_LIBS="$FFMS2_LIBS $($PKGCONFIG --libs ffms2)"
1088         FFMS2_CFLAGS="$FFMS2_CFLAGS $($PKGCONFIG --cflags ffms2)"
1089     fi
1090     [ -z "$FFMS2_LIBS" ] && FFMS2_LIBS="-lffms2"
1091
1092     if cc_check ffms.h "$FFMS2_CFLAGS $FFMS2_LIBS" "FFMS_DestroyVideoSource(0);" ; then
1093         ffms="yes"
1094     elif cc_check ffms.h "$FFMS2_CFLAGS $FFMS2_LIBS -lstdc++ $LAVF_LIBS" "FFMS_DestroyVideoSource(0);" ; then
1095         ffms="yes"
1096         FFMS2_LIBS="$FFMS2_LIBS -lstdc++ $LAVF_LIBS"
1097     fi
1098
1099     error="ffms must be at least version $ffms_major.$ffms_minor.$ffms_micro.$ffms_bump"
1100     if [ $ffms = "yes" ] && ! cpp_check "ffms.h" "$FFMS2_CFLAGS" "FFMS_VERSION >= (($ffms_major << 24) | ($ffms_minor << 16) | ($ffms_micro << 8) | $ffms_bump)" "$error"; then
1101        ffms="no"
1102        echo "Warning: $error"
1103     fi
1104     if [ "$ffms" = "yes" -a "$swscale" = "no" ]; then
1105         echo "Warning: ffms is not supported without swscale support"
1106         ffms="no"
1107     fi
1108 fi
1109
1110 if [ "$swscale" = "yes" ]; then
1111     LDFLAGSCLI="$SWSCALE_LIBS $LDFLAGSCLI"
1112     CFLAGS="$CFLAGS $SWSCALE_CFLAGS"
1113     define HAVE_SWSCALE
1114     if [ "$lavf" = "yes" ]; then
1115         LDFLAGSCLI="$LAVF_LIBS $LDFLAGSCLI"
1116         CFLAGS="$CFLAGS $LAVF_CFLAGS"
1117         define HAVE_LAVF
1118     fi
1119     if [ "$ffms" = "yes" ]; then
1120         LDFLAGSCLI="$FFMS2_LIBS $LDFLAGSCLI"
1121         CFLAGS="$CFLAGS $FFMS2_CFLAGS"
1122         define HAVE_FFMS
1123     fi
1124 fi
1125
1126 if [ "$lsmash" = "auto" ] ; then
1127     lsmash="no"
1128     if $PKGCONFIG --exists liblsmash 2>/dev/null; then
1129         LSMASH_LIBS="$LSMASH_LIBS $($PKGCONFIG --libs liblsmash)"
1130         LSMASH_CFLAGS="$LSMASH_CFLAGS $($PKGCONFIG --cflags liblsmash)"
1131     fi
1132     [ -z "$LSMASH_LIBS" ] && LSMASH_LIBS="-llsmash"
1133
1134     if cc_check lsmash.h "$LSMASH_CFLAGS $LSMASH_LIBS" ; then
1135         if cpp_check lsmash.h "$LSMASH_CFLAGS" "LSMASH_VERSION_MAJOR > 1 || (LSMASH_VERSION_MAJOR == 1 && LSMASH_VERSION_MINOR >= 5)" ; then
1136             lsmash="yes"
1137         else
1138             echo "Warning: lsmash is too old, update to rev.895 or later"
1139         fi
1140     fi
1141 fi
1142
1143 if [ "$gpac" = "auto" -a "$lsmash" != "yes" ] ; then
1144     gpac="no"
1145     GPAC_LIBS="-lgpac_static"
1146     cc_check "" -lz && GPAC_LIBS="$GPAC_LIBS -lz"
1147     if [ "$SYS" = "WINDOWS" ] ; then
1148         cc_check "" -lws2_32 && GPAC_LIBS="$GPAC_LIBS -lws2_32"
1149         cc_check "" -lwinmm && GPAC_LIBS="$GPAC_LIBS -lwinmm"
1150     fi
1151     if cc_check gpac/isomedia.h "$GPAC_LIBS" ; then
1152         if cc_check gpac/isomedia.h "$GPAC_LIBS" "gf_isom_set_pixel_aspect_ratio(0,0,0,0,0);" ; then
1153             gpac="yes"
1154         else
1155             echo "Warning: gpac is too old, update to 2007-06-21 UTC or later"
1156         fi
1157     fi
1158 fi
1159
1160 if [ "$lsmash" = "yes" ] ; then
1161     mp4="lsmash"
1162     LDFLAGSCLI="$LSMASH_LIBS $LDFLAGSCLI"
1163     CFLAGS="$CFLAGS $LSMASH_CFLAGS"
1164     define HAVE_LSMASH
1165 elif [ "$gpac" = "yes" ] ; then
1166     mp4="gpac"
1167     define HAVE_GPAC
1168     LDFLAGSCLI="$GPAC_LIBS $LDFLAGSCLI"
1169 fi
1170
1171 if [ "$avs" = "auto" ] ; then
1172     avs="no"
1173     # cygwin can use avisynth if it can use LoadLibrary
1174     if [ $SYS = WINDOWS ] || ([ $SYS = CYGWIN ] && cc_check windows.h "" "LoadLibraryW(0);") ; then
1175         avs="avisynth"
1176         define HAVE_AVS
1177         define USE_AVXSYNTH 0
1178     elif [ "$SYS" = "LINUX" -o "$SYS" = "MACOSX" ] ; then
1179     # AvxSynth currently only supports Linux and OSX
1180         avs="avxsynth"
1181         define HAVE_AVS
1182         define USE_AVXSYNTH 1
1183         AVS_LIBS="-ldl"
1184         LDFLAGSCLI="$AVS_LIBS $LDFLAGSCLI"
1185     fi
1186 fi
1187
1188 cc_check "stdint.h" "" "uint32_t test_vec __attribute__ ((vector_size (16))) = {0,1,2,3};" && define HAVE_VECTOREXT
1189
1190 if [ "$pic" = "yes" ] ; then
1191     [ "$SYS" != WINDOWS -a "$SYS" != CYGWIN ] && CFLAGS="$CFLAGS -fPIC"
1192     ASFLAGS="$ASFLAGS -DPIC"
1193     # resolve textrels in the x86 asm
1194     cc_check stdio.h "-shared -Wl,-Bsymbolic" && SOFLAGS="$SOFLAGS -Wl,-Bsymbolic"
1195     [ $SYS = SunOS -a "$ARCH" = "X86" ] && SOFLAGS="$SOFLAGS -mimpure-text"
1196 fi
1197
1198 if [ "$debug" != "yes" -a "$gprof" != "yes" ]; then
1199     CFLAGS="$CFLAGS -fomit-frame-pointer"
1200 fi
1201
1202 if [ "$strip" = "yes" ]; then
1203     LDFLAGS="$LDFLAGS -s"
1204 fi
1205
1206 if [ "$debug" = "yes" ]; then
1207     CFLAGS="-O1 -g $CFLAGS"
1208     RCFLAGS="$RCFLAGS -DDEBUG"
1209 else
1210     CFLAGS="-O3 -ffast-math $CFLAGS"
1211     if [ "$lto" = "auto" ] && [ $compiler = GNU ] && cc_check "" "-flto" ; then
1212         lto="yes"
1213         CFLAGS="$CFLAGS -flto"
1214         LDFLAGS="$LDFLAGS -O3 -flto"
1215     fi
1216 fi
1217 [ "$lto" = "auto" ] && lto="no"
1218
1219 if cc_check '' -fno-tree-vectorize ; then
1220     CFLAGS="$CFLAGS -fno-tree-vectorize"
1221 fi
1222
1223 if [ $SYS = WINDOWS -a $ARCH = X86 -a $compiler = GNU ] ; then
1224     # workaround gcc/ld bug with alignment of static variables/arrays that are initialized to zero
1225     cc_check '' -fno-zero-initialized-in-bss && CFLAGS="$CFLAGS -fno-zero-initialized-in-bss"
1226 fi
1227
1228 if cc_check "stdio.h" "" "fseeko(stdin,0,0);" ; then
1229     define fseek fseeko
1230     define ftell ftello
1231 elif cc_check "stdio.h" "" "fseeko64(stdin,0,0);" ; then
1232     define fseek fseeko64
1233     define ftell ftello64
1234 elif cc_check "stdio.h" "" "_fseeki64(stdin,0,0);" ; then
1235     define fseek _fseeki64
1236     define ftell _ftelli64
1237 fi
1238
1239 if cc_check '' -Wshadow ; then
1240     CFLAGS="-Wshadow $CFLAGS"
1241 fi
1242
1243 if cc_check '' -Wmaybe-uninitialized ; then
1244     CFLAGS="-Wno-maybe-uninitialized $CFLAGS"
1245 fi
1246
1247 if [ $compiler = ICC -o $compiler = ICL ] ; then
1248     if cc_check 'extras/intel_dispatcher.h' '' 'x264_intel_dispatcher_override();' ; then
1249         define HAVE_INTEL_DISPATCHER
1250     fi
1251 fi
1252
1253 if [ "$bit_depth" -gt "8" ]; then
1254     define HIGH_BIT_DEPTH
1255     ASFLAGS="$ASFLAGS -DHIGH_BIT_DEPTH=1"
1256     opencl="no"
1257 else
1258     ASFLAGS="$ASFLAGS -DHIGH_BIT_DEPTH=0"
1259 fi
1260
1261 if [ "$chroma_format" != "all" ]; then
1262     define CHROMA_FORMAT CHROMA_$chroma_format
1263 fi
1264
1265 ASFLAGS="$ASFLAGS -DBIT_DEPTH=$bit_depth"
1266
1267 [ $gpl = yes ] && define HAVE_GPL && x264_gpl=1 || x264_gpl=0
1268
1269 [ $interlaced = yes ] && define HAVE_INTERLACED && x264_interlaced=1 || x264_interlaced=0
1270
1271 libdl=""
1272 if [ "$opencl" = "yes" ]; then
1273     opencl="no"
1274     # cygwin can use opencl if it can use LoadLibrary
1275     if [ $SYS = WINDOWS ] || ([ $SYS = CYGWIN ] && cc_check windows.h "" "LoadLibraryW(0);") ; then
1276         opencl="yes"
1277         define HAVE_OPENCL
1278     elif [ "$SYS" = "LINUX" -o "$SYS" = "MACOSX" ] ; then
1279         opencl="yes"
1280         define HAVE_OPENCL
1281         libdl="-ldl"
1282     fi
1283     LDFLAGS="$LDFLAGS $libdl"
1284 fi
1285
1286 #define undefined vars as 0
1287 for var in $CONFIG_HAVE; do
1288     grep -q "HAVE_$var 1" config.h || define HAVE_$var 0
1289 done
1290
1291 # generate exported config file
1292
1293 config_chroma_format="X264_CSP_I$chroma_format"
1294 [ "$config_chroma_format" == "X264_CSP_Iall" ] && config_chroma_format="0"
1295 cat > x264_config.h << EOF
1296 #define X264_BIT_DEPTH     $bit_depth
1297 #define X264_GPL           $x264_gpl
1298 #define X264_INTERLACED    $x264_interlaced
1299 #define X264_CHROMA_FORMAT $config_chroma_format
1300 EOF
1301
1302 ${SRCPATH}/version.sh >> x264_config.h
1303
1304 if [ "$cli_libx264" = "system" ] ; then
1305     if [ "$shared" = "yes" ]; then
1306         CLI_LIBX264='$(SONAME)'
1307     else
1308         CLI_LIBX264=
1309         LDFLAGSCLI="$X264_LIBS $LDFLAGSCLI"
1310         cc_check 'stdint.h x264.h' '' 'x264_encoder_open(0);' || die "System libx264 can't be used for compilation of this version"
1311     fi
1312 else
1313     CLI_LIBX264='$(LIBX264)'
1314 fi
1315
1316 DEPMM="${QPRE}MM"
1317 DEPMT="${QPRE}MT"
1318 if [ $compiler_style = MS ]; then
1319     AR="lib -nologo -out:"
1320     LD="link -out:"
1321     if [ $compiler = ICL ]; then
1322         AR="xi$AR"
1323         LD="xi$LD"
1324     else
1325         mslink="$(dirname "$(command -v cl 2>/dev/null)")/link"
1326         [ -x "$mslink" ] && LD="\"$mslink\" -out:"
1327     fi
1328     HAVE_GETOPT_LONG=0
1329     LDFLAGS="-nologo -incremental:no $(cl_ldflags $LDFLAGS)"
1330     LDFLAGSCLI="$(cl_ldflags $LDFLAGSCLI)"
1331     LIBX264=libx264.lib
1332     RANLIB=
1333     [ -n "$RC" ] && RCFLAGS="$RCFLAGS -nologo -I. -I\$(SRCPATH)/extras -fo"
1334     STRIP=
1335     if [ $debug = yes ]; then
1336         LDFLAGS="-debug $LDFLAGS"
1337         CFLAGS="-D_DEBUG $CFLAGS"
1338     else
1339         CFLAGS="-DNDEBUG $CFLAGS"
1340     fi
1341 else # gcc/icc
1342     DEPMM="$DEPMM -g0"
1343     AR="$AR rc "
1344     LD="$CC -o "
1345     LIBX264=libx264.a
1346     [ -n "$RC" ] && RCFLAGS="$RCFLAGS -I. -o "
1347 fi
1348 [ $compiler != GNU ] && CFLAGS="$(cc_cflags $CFLAGS)"
1349 if [ $compiler = ICC -o $compiler = ICL ]; then
1350     # icc does not define __SSE__ until SSE2 optimization and icl never defines it or _M_IX86_FP
1351     [ \( $ARCH = X86_64 -o $ARCH = X86 \) -a $asm = yes ] && ! cpp_check "" "" "defined(__SSE__)" && define __SSE__
1352     PROF_GEN_CC="${QPRE}prof-gen ${QPRE}prof-dir."
1353     PROF_GEN_LD=
1354     PROF_USE_CC="${QPRE}prof-use ${QPRE}prof-dir."
1355     PROF_USE_LD=
1356 elif [ $compiler = CL ]; then
1357     # Visual Studio
1358     # _M_IX86_FP is only defined on x86
1359     [ $ARCH = X86 ] && cpp_check '' '' '_M_IX86_FP >= 1' && define __SSE__
1360     [ $ARCH = X86_64 ] && define __SSE__
1361     # As long as the cli application can't link against the dll, the dll can not be pgo'd.
1362     # pgds are link flag specific and the -dll flag for creating the dll makes it unshareable with the cli
1363     PROF_GEN_CC="-GL"
1364     PROF_GEN_LD="-LTCG:PGINSTRUMENT"
1365     PROF_USE_CC="-GL"
1366     PROF_USE_LD="-LTCG:PGOPTIMIZE"
1367 else
1368     PROF_GEN_CC="-fprofile-generate"
1369     PROF_GEN_LD="-fprofile-generate"
1370     PROF_USE_CC="-fprofile-use"
1371     PROF_USE_LD="-fprofile-use"
1372 fi
1373
1374 # generate config files
1375
1376 cat > config.mak << EOF
1377 SRCPATH=$SRCPATH
1378 prefix=$prefix
1379 exec_prefix=$exec_prefix
1380 bindir=$bindir
1381 libdir=$libdir
1382 includedir=$includedir
1383 SYS_ARCH=$ARCH
1384 SYS=$SYS
1385 CC=$CC
1386 CFLAGS=$CFLAGS
1387 COMPILER=$compiler
1388 COMPILER_STYLE=$compiler_style
1389 DEPMM=$DEPMM
1390 DEPMT=$DEPMT
1391 LD=$LD
1392 LDFLAGS=$LDFLAGS
1393 LIBX264=$LIBX264
1394 AR=$AR
1395 RANLIB=$RANLIB
1396 STRIP=$STRIP
1397 INSTALL=$INSTALL
1398 AS=$AS
1399 ASFLAGS=$ASFLAGS
1400 RC=$RC
1401 RCFLAGS=$RCFLAGS
1402 EXE=$EXE
1403 HAVE_GETOPT_LONG=$HAVE_GETOPT_LONG
1404 DEVNULL=$DEVNULL
1405 PROF_GEN_CC=$PROF_GEN_CC
1406 PROF_GEN_LD=$PROF_GEN_LD
1407 PROF_USE_CC=$PROF_USE_CC
1408 PROF_USE_LD=$PROF_USE_LD
1409 HAVE_OPENCL=$opencl
1410 EOF
1411
1412 if [ $compiler_style = MS ]; then
1413     echo '%.o: %.c' >> config.mak
1414     echo '      $(CC) $(CFLAGS) -c -Fo$@ $<' >> config.mak
1415 fi
1416
1417 if [ "$cli" = "yes" ]; then
1418     echo 'default: cli' >> config.mak
1419     echo 'install: install-cli' >> config.mak
1420 fi
1421
1422 if [ "$shared" = "yes" ]; then
1423     API=$(grep '#define X264_BUILD' < ${SRCPATH}/x264.h | cut -f 3 -d ' ')
1424     if [ "$SYS" = "WINDOWS" -o "$SYS" = "CYGWIN" ]; then
1425         echo "SONAME=libx264-$API.dll" >> config.mak
1426         if [ $compiler_style = MS ]; then
1427             echo 'IMPLIBNAME=libx264.dll.lib' >> config.mak
1428             # GNU ld on windows defaults to exporting all global functions if there are no explicit __declspec(dllexport) declarations
1429             # MSVC link does not act similarly, so it is required to make an export definition out of x264.h and use it at link time
1430             echo "SOFLAGS=-dll -def:x264.def -implib:\$(IMPLIBNAME) $SOFLAGS" >> config.mak
1431             echo "EXPORTS" > x264.def
1432             # export API functions
1433             grep "^\(int\|void\|x264_t\).*x264" ${SRCPATH}/x264.h | sed -e "s/.*\(x264.*\)(.*/\1/;s/open/open_$API/g" >> x264.def
1434             # export API variables/data. must be flagged with the DATA keyword
1435             grep "extern.*x264" ${SRCPATH}/x264.h | sed -e "s/.*\(x264\w*\)\W.*/\1 DATA/;" >> x264.def
1436         else
1437             echo 'IMPLIBNAME=libx264.dll.a' >> config.mak
1438             echo "SOFLAGS=-shared -Wl,--out-implib,\$(IMPLIBNAME) $SOFLAGS" >> config.mak
1439         fi
1440     elif [ "$SYS" = "MACOSX" ]; then
1441         echo "SOSUFFIX=dylib" >> config.mak
1442         echo "SONAME=libx264.$API.dylib" >> config.mak
1443         echo "SOFLAGS=-shared -dynamiclib -Wl,-single_module -Wl,-read_only_relocs,suppress -install_name \$(DESTDIR)\$(libdir)/\$(SONAME) $SOFLAGS" >> config.mak
1444     elif [ "$SYS" = "SunOS" ]; then
1445         echo "SOSUFFIX=so" >> config.mak
1446         echo "SONAME=libx264.so.$API" >> config.mak
1447         echo "SOFLAGS=-shared -Wl,-h,\$(SONAME) $SOFLAGS" >> config.mak
1448     else
1449         echo "SOSUFFIX=so" >> config.mak
1450         echo "SONAME=libx264.so.$API" >> config.mak
1451         echo "SOFLAGS=-shared -Wl,-soname,\$(SONAME) $SOFLAGS" >> config.mak
1452     fi
1453     echo 'default: lib-shared' >> config.mak
1454     echo 'install: install-lib-shared' >> config.mak
1455 fi
1456
1457 if [ "$static" = "yes" ]; then
1458     echo 'default: lib-static' >> config.mak
1459     echo 'install: install-lib-static' >> config.mak
1460 fi
1461
1462 echo "LDFLAGSCLI = $LDFLAGSCLI" >> config.mak
1463 echo "CLI_LIBX264 = $CLI_LIBX264" >> config.mak
1464
1465 cat > x264.pc << EOF
1466 prefix=$prefix
1467 exec_prefix=$exec_prefix
1468 libdir=$libdir
1469 includedir=$includedir
1470
1471 Name: x264
1472 Description: H.264 (MPEG4 AVC) encoder library
1473 Version: $(grep POINTVER < x264_config.h | sed -e 's/.* "//; s/".*//')
1474 Libs: -L$libdir -lx264 $([ "$shared" = "yes" ] || echo $libpthread $libm $libdl)
1475 Libs.private: $([ "$shared" = "yes" ] && echo $libpthread $libm $libdl)
1476 Cflags: -I$includedir
1477 EOF
1478
1479 filters="crop select_every"
1480 gpl_filters=""
1481 [ $swscale = yes ] && filters="resize $filters"
1482 [ $gpl = yes ] && filters="$filters $gpl_filters"
1483
1484 cat > conftest.log <<EOF
1485 platform:      $ARCH
1486 byte order:    $CPU_ENDIAN
1487 system:        $SYS
1488 cli:           $cli
1489 libx264:       $cli_libx264
1490 shared:        $shared
1491 static:        $static
1492 asm:           $asm
1493 interlaced:    $interlaced
1494 avs:           $avs
1495 lavf:          $lavf
1496 ffms:          $ffms
1497 mp4:           $mp4
1498 gpl:           $gpl
1499 thread:        $thread
1500 opencl:        $opencl
1501 filters:       $filters
1502 lto:           $lto
1503 debug:         $debug
1504 gprof:         $gprof
1505 strip:         $strip
1506 PIC:           $pic
1507 bit depth:     $bit_depth
1508 chroma format: $chroma_format
1509 EOF
1510
1511 echo >> config.log
1512 cat conftest.log >> config.log
1513 cat conftest.log
1514
1515 [ "$SRCPATH" != "." ] && ln -sf ${SRCPATH}/Makefile ./Makefile
1516 mkdir -p common/{aarch64,arm,mips,ppc,x86} encoder extras filters/video input output tools
1517
1518 echo
1519 echo "You can run 'make' or 'make fprofiled' now."
1520