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