]> git.sesse.net Git - x264/blob - configure
Improve C99 support checks in configure
[x264] / 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 available options:
8
9   --help                   print this message
10   --disable-avs            disables avisynth support (windows only)
11   --disable-lavf           disables libavformat support
12   --disable-ffms           disables ffmpegsource support
13   --disable-gpac           disables gpac support
14   --disable-gpl            disables GPL-only features
15   --disable-thread         disables multithreaded encoding
16   --enable-win32thread     use win32threads (windows only)
17   --disable-swscale        disables swscale support
18   --disable-asm            disables platform-specific assembly optimizations
19   --enable-debug           adds -g, doesn't strip
20   --enable-gprof           adds -pg, doesn't strip
21   --enable-visualize       enables visualization (X11 only)
22   --enable-pic             build position-independent code
23   --enable-shared          build shared library
24   --bit-depth=BIT_DEPTH    sets output bit depth (8-10), default 8
25   --extra-asflags=EASFLAGS add EASFLAGS to ASFLAGS
26   --extra-cflags=ECFLAGS   add ECFLAGS to CFLAGS
27   --extra-ldflags=ELDFLAGS add ELDFLAGS to LDFLAGS
28   --host=HOST              build programs to run on HOST
29   --cross-prefix=PREFIX    use PREFIX for compilation tools
30   --sysroot=SYSROOT        root of cross-build tree
31
32 EOF
33 exit 1
34 fi
35
36 log_check() {
37     echo -n "checking $1... " >> config.log
38 }
39
40 log_ok() {
41     echo "yes" >> config.log
42 }
43
44 log_fail() {
45     echo "no" >> config.log
46 }
47
48 log_msg() {
49     echo "$1" >> config.log
50 }
51
52 intel_cflags() {
53     # Intel Compiler issues an incredibly large number of warnings on any warning level,
54     # suppress them by disabling all warnings rather than having to use #pragmas to disable most of them
55     for arg in $*; do
56         [ $arg = -ffast-math ] && arg=
57         [[ "$arg" = -falign-loops* ]] && arg=
58         [ "$arg" = -fno-tree-vectorize ] && arg=
59         [ "$arg" = -Wshadow ] && arg=
60         if [ $compiler = ICL ]; then
61             [ "$arg" = -Wall ] && arg=-W0
62             [ "$arg" = -g ] && arg=-Z7
63             [ "$arg" = -fomit-frame-pointer ] && arg=
64             [ "$arg" = -s ] && arg=
65             [ "$arg" = -fPIC ] && arg=
66         else
67             [ "$arg" = -Wall ] && arg=-w0
68         fi
69
70         # Intel Compiler on Mac OS X does not allow pre -msse3
71         [ $SYS = MACOSX -a \( "$arg" = -mia32 -o "$arg" = -msse -o "$arg" = -msse2 \) ] && arg=
72         [ -n "$arg" ] && echo -n "$arg "
73     done
74 }
75
76 icl_ldflags() {
77     for arg in $*; do
78         arg=${arg/LIBPATH/libpath}
79         [ ${arg#-libpath:} == $arg -a ${arg#-l} != $arg ] && arg=${arg#-l}.lib
80         [ ${arg#-L} != $arg ] && arg=-libpath:${arg#-L}
81         [ $arg = -Wl,--large-address-aware ] && arg=-largeaddressaware
82         [ $arg = -s ] && arg=
83         [ "$arg" = -Wl,-Bsymbolic ] && arg=
84
85         arg=${arg/pthreadGC/pthreadVC}
86         [ "$arg" = avifil32.lib ] && arg=vfw32.lib
87         [ "$arg" = gpac_static.lib ] && arg=libgpac_static.lib
88
89         [ -n "$arg" ] && echo -n "$arg "
90     done
91 }
92
93 cc_check() {
94     if [ -z "$3" ]; then
95         if [ -z "$1$2" ]; then
96             log_check "whether $CC works"
97         elif [ -z "$1" ]; then
98             log_check "for $2"
99         else
100             log_check "for $1"
101         fi
102     elif [ -z "$1" ]; then
103         if [ -z "$2" ]; then
104             log_check "whether $CC supports $3"
105         else
106             log_check "whether $CC supports $3 with $2"
107         fi
108     else
109         log_check "for $3 in $1";
110     fi
111     rm -f conftest.c
112     [ -n "$1" ] && echo "#include <$1>" > conftest.c
113     echo "int main () { $3 return 0; }" >> conftest.c
114     if [ $compiler = ICL ]; then
115         cc_cmd="$CC conftest.c $CFLAGS $2 -link $(icl_ldflags $2 $LDFLAGSCLI $LDFLAGS)"
116     else
117         cc_cmd="$CC conftest.c $CFLAGS $2 $LDFLAGSCLI $LDFLAGS -o conftest"
118     fi
119     if $cc_cmd >conftest.log 2>&1; then
120         res=$?
121         log_ok
122     else
123         res=$?
124         log_fail
125         log_msg "Failed commandline was:"
126         log_msg "--------------------------------------------------"
127         log_msg "$cc_cmd"
128         cat conftest.log >> config.log
129         log_msg "--------------------------------------------------"
130         log_msg "Failed program was:"
131         log_msg "--------------------------------------------------"
132         cat conftest.c >> config.log
133         log_msg "--------------------------------------------------"
134     fi
135     return $res
136 }
137
138 cpp_check() {
139     log_check "whether $3 is true"
140     rm -f conftest.c
141     [ -n "$1" ] && echo "#include <$1>" > conftest.c
142     echo -e "#if !($3) \n#error $4 \n#endif " >> conftest.c
143
144     if $CC conftest.c $CFLAGS $2 -E -o conftest >conftest.log 2>&1; then
145         res=$?
146         log_ok
147     else
148         res=$?
149         log_fail
150         log_msg "--------------------------------------------------"
151         cat conftest.log >> config.log
152         log_msg "--------------------------------------------------"
153         log_msg "Failed program was:"
154         log_msg "--------------------------------------------------"
155         cat conftest.c >> config.log
156         log_msg "--------------------------------------------------"
157     fi
158     return $res
159 }
160
161 as_check() {
162     log_check "whether $AS supports $1"
163     echo "$1" > conftest.asm
164     if $AS conftest.asm $ASFLAGS $2 -o conftest.o >conftest.log 2>&1; then
165         res=$?
166         log_ok
167     else
168         res=$?
169         log_fail
170         log_msg "Failed commandline was:"
171         log_msg "--------------------------------------------------"
172         log_msg "$AS conftest.asm $ASFLAGS $2 -o conftest.o"
173         cat conftest.log >> config.log
174         log_msg "--------------------------------------------------"
175         log_msg "Failed program was:"
176         log_msg "--------------------------------------------------"
177         cat conftest.asm >> config.log
178         log_msg "--------------------------------------------------"
179     fi
180     return $res
181 }
182
183 define() {
184     echo "#define $1$([ -n "$2" ] && echo " $2" || echo " 1")" >> config.h
185 }
186
187 die() {
188     log_msg "DIED: $@"
189     echo "$@"
190     exit 1
191 }
192
193 rm -f x264_config.h config.h config.mak config.log x264.pc x264.def conftest*
194
195 prefix='/usr/local'
196 exec_prefix='${prefix}'
197 bindir='${exec_prefix}/bin'
198 libdir='${exec_prefix}/lib'
199 includedir='${prefix}/include'
200 DEVNULL='/dev/null'
201
202 avs="auto"
203 lavf="auto"
204 ffms="auto"
205 gpac="auto"
206 gpl="yes"
207 thread="auto"
208 swscale="auto"
209 asm="auto"
210 debug="no"
211 gprof="no"
212 pic="no"
213 vis="no"
214 shared="no"
215 bit_depth="8"
216 compiler="GNU"
217
218 CFLAGS="$CFLAGS -Wall -I."
219 LDFLAGS="$LDFLAGS"
220 LDFLAGSCLI="$LDFLAGSCLI"
221 ASFLAGS="$ASFLAGS"
222 HAVE_GETOPT_LONG=1
223 cross_prefix=""
224
225 EXE=""
226
227 # list of all preprocessor HAVE values we can define
228 CONFIG_HAVE="MALLOC_H ALTIVEC ALTIVEC_H MMX ARMV6 ARMV6T2 NEON BEOSTHREAD POSIXTHREAD WIN32THREAD THREAD LOG2F VISUALIZE SWSCALE LAVF FFMS GPAC GF_MALLOC AVS GPL"
229
230 # parse options
231
232 for opt do
233     optarg="${opt#*=}"
234     case "$opt" in
235         --prefix=*)
236             prefix="$optarg"
237             ;;
238         --exec-prefix=*)
239             exec_prefix="$optarg"
240             ;;
241         --bindir=*)
242             bindir="$optarg"
243             ;;
244         --libdir=*)
245             libdir="$optarg"
246             ;;
247         --includedir=*)
248             includedir="$optarg"
249             ;;
250         --disable-asm)
251             asm="no"
252             ;;
253         --disable-avs)
254             avs="no"
255             ;;
256         --disable-lavf)
257             lavf="no"
258             ;;
259         --disable-ffms)
260             ffms="no"
261             ;;
262         --disable-gpac)
263             gpac="no"
264             ;;
265         --disable-gpl)
266             gpl="no"
267             ;;
268         --extra-asflags=*)
269             ASFLAGS="$ASFLAGS ${opt#--extra-asflags=}"
270             ;;
271         --extra-cflags=*)
272             CFLAGS="$CFLAGS ${opt#--extra-cflags=}"
273             ;;
274         --extra-ldflags=*)
275             LDFLAGS="$LDFLAGS ${opt#--extra-ldflags=}"
276             ;;
277         --disable-thread)
278             thread="no"
279             ;;
280         --enable-win32thread)
281             thread="win32"
282             ;;
283         --disable-swscale)
284             swscale="no"
285             ;;
286         --enable-debug)
287             debug="yes"
288             ;;
289         --enable-gprof)
290             CFLAGS="$CFLAGS -pg"
291             LDFLAGS="$LDFLAGS -pg"
292             gprof="yes"
293             ;;
294         --enable-pic)
295             pic="yes"
296             ;;
297         --enable-shared)
298             shared="yes"
299             ;;
300         --enable-visualize)
301             vis="yes"
302             ;;
303         --host=*)
304             host="${opt#--host=}"
305             ;;
306         --cross-prefix=*)
307             cross_prefix="${opt#--cross-prefix=}"
308             ;;
309         --sysroot=*)
310             CFLAGS="$CFLAGS --sysroot=${opt#--sysroot=}"
311             LDFLAGS="$LDFLAGS --sysroot=${opt#--sysroot=}"
312             ;;
313         --bit-depth=*)
314             bit_depth="${opt#--bit-depth=}"
315             if [ "$bit_depth" -lt "8" -o "$bit_depth" -gt "10" ]; then
316                 echo "Supplied bit depth must be in range [8,10]."
317                 exit 1
318             fi
319             bit_depth=`expr $bit_depth + 0`
320             ;;
321         *)
322             echo "Unknown option $opt, ignored"
323             ;;
324     esac
325 done
326
327 CC="${CC-${cross_prefix}gcc}"
328 AR="${AR-${cross_prefix}ar}"
329 RANLIB="${RANLIB-${cross_prefix}ranlib}"
330 STRIP="${STRIP-${cross_prefix}strip}"
331
332 if [ "x$host" = x ]; then
333     host=`./config.guess`
334 fi
335 # normalize a triplet into a quadruplet
336 host=`./config.sub $host`
337
338 # split $host
339 host_cpu="${host%%-*}"
340 host="${host#*-}"
341 host_vendor="${host%%-*}"
342 host_os="${host#*-}"
343
344 # test for use of Intel Compiler
345 if [[ $host_os = mingw* || $host_os = cygwin* ]]; then
346     if [[ `basename "$CC"` = icl* ]]; then
347         # Windows Intel Compiler creates dependency generation with absolute Windows paths, Cygwin's make does not support Windows paths.
348         [[ $host_os = cygwin* ]] && die "Windows Intel Compiler support requires MSYS"
349         ARCHPRE="-arch:"
350         compiler=ICL
351         CFLAGS="$CFLAGS -Qstd=c99 -nologo -Qms0 -DHAVE_STRING_H -Iextras"
352         QPRE="-Q"
353         `$CC 2>&1 | grep -q IA-32` && host_cpu=i486
354         `$CC 2>&1 | grep -q "Intel(R) 64"` && host_cpu=x86_64
355         cpp_check "" "" "_MSC_VER >= 1400" || die "Windows Intel Compiler support requires Visual Studio 2005 or newer"
356     fi
357 else
358     if [[ `basename "$CC"` = icc* ]]; then
359         AR="xiar"
360         ARCHPRE="-m"
361         compiler=ICC
362         QPRE="-"
363     fi
364 fi
365
366 case $host_os in
367     beos*)
368         SYS="BEOS"
369         define HAVE_MALLOC_H
370         ;;
371     darwin*)
372         SYS="MACOSX"
373         CFLAGS="$CFLAGS -falign-loops=16"
374         LDFLAGS="$LDFLAGS -lm"
375         if [ "$pic" = "no" ]; then
376             cc_check "" -mdynamic-no-pic && CFLAGS="$CFLAGS -mdynamic-no-pic"
377         fi
378         ;;
379     freebsd*)
380         SYS="FREEBSD"
381         LDFLAGS="$LDFLAGS -lm"
382         ;;
383     kfreebsd*-gnu)
384         SYS="FREEBSD"
385         define HAVE_MALLOC_H
386         LDFLAGS="$LDFLAGS -lm"
387         ;;
388     netbsd*)
389         SYS="NETBSD"
390         LDFLAGS="$LDFLAGS -lm"
391         ;;
392     openbsd*)
393         SYS="OPENBSD"
394         LDFLAGS="$LDFLAGS -lm"
395         ;;
396     *linux*)
397         SYS="LINUX"
398         define HAVE_MALLOC_H
399         LDFLAGS="$LDFLAGS -lm"
400         ;;
401     cygwin*)
402         EXE=".exe"
403         if cc_check "" -mno-cygwin; then
404             CFLAGS="$CFLAGS -mno-cygwin"
405             LDFLAGS="$LDFLAGS -mno-cygwin"
406         fi
407         if cpp_check "" "" "defined(__CYGWIN32__)" ; then
408             define HAVE_MALLOC_H
409             SYS="CYGWIN"
410         else
411             SYS="WINDOWS"
412             DEVNULL="NUL"
413         fi
414         ;;
415     mingw*)
416         SYS="WINDOWS"
417         EXE=".exe"
418         DEVNULL="NUL"
419         ;;
420     sunos*|solaris*)
421         SYS="SunOS"
422         define HAVE_MALLOC_H
423         LDFLAGS="$LDFLAGS -lm"
424         HAVE_GETOPT_LONG=0
425         ;;
426     *)
427         die "Unknown system $host, edit the configure"
428         ;;
429 esac
430
431 case $host_cpu in
432     i*86)
433         ARCH="X86"
434         AS="yasm"
435         ASFLAGS="$ASFLAGS -O2"
436         if [ $compiler = GNU ]; then
437             if [[ "$asm" == auto && "$CFLAGS" != *-march* ]]; then
438                 CFLAGS="$CFLAGS -march=i686"
439             fi
440             if [[ "$asm" == auto && "$CFLAGS" != *-mfpmath* ]]; then
441                 CFLAGS="$CFLAGS -mfpmath=sse -msse"
442             fi
443         else
444             # Intel Compiler >= 11 generally defaults to SSE2 optimization. -msse is also deprecated for -mia32
445             if cpp_check "" "" "__INTEL_COMPILER >= 1100" \
446              && ! (echo $CFLAGS | grep -Eiq "(${ARCHPRE}ia32|${ARCHPRE}sse|${ARCHPRE}ssse3|${ARCHPRE}avx)") \
447              && [[ "$CFLAGS" != *${QPRE}x* ]]; then
448                 CFLAGS="$CFLAGS ${ARCHPRE}ia32"
449             fi
450             # icc on linux has various degrees of mod16 stack support
451             if [ $SYS = LINUX ]; then
452                 # < 11 is completely incapable of keeping a mod16 stack
453                 if cpp_check "" "" "__INTEL_COMPILER < 1100" ; then
454                     define BROKEN_STACK_ALIGNMENT
455                 # 11 <= x < 12 is capable of keeping a mod16 stack, but defaults to not doing so.
456                 elif cpp_check "" "" "__INTEL_COMPILER < 1200" ; then
457                     CFLAGS="$CFLAGS -falign-stack=assume-16-byte"
458                 fi
459                 # >= 12 defaults to a mod16 stack
460             fi
461             # icl on windows has no mod16 stack support
462             [ $SYS = WINDOWS ] && define BROKEN_STACK_ALIGNMENT
463         fi
464         if [ "$SYS" = MACOSX ]; then
465             ASFLAGS="$ASFLAGS -f macho -DPREFIX"
466         elif [ "$SYS" = WINDOWS -o "$SYS" = CYGWIN ]; then
467             ASFLAGS="$ASFLAGS -f win32 -DPREFIX"
468             LDFLAGS="$LDFLAGS -Wl,--large-address-aware"
469         else
470             ASFLAGS="$ASFLAGS -f elf"
471         fi
472         ;;
473     x86_64)
474         ARCH="X86_64"
475         AS="yasm"
476         if [ "$SYS" = MACOSX ]; then
477             ASFLAGS="$ASFLAGS -f macho64 -m amd64 -DPIC -DPREFIX"
478             if cc_check '' "-arch x86_64"; then
479                 CFLAGS="$CFLAGS -arch x86_64"
480                 LDFLAGS="$LDFLAGS -arch x86_64"
481             fi
482         elif [ "$SYS" = WINDOWS ]; then
483             ASFLAGS="$ASFLAGS -f win32 -m amd64"
484             # only the GNU toolchain is inconsistent in prefixing function names with _
485             [ $compiler = GNU ] && cc_check "" "-S" && grep -q "_main:" conftest && ASFLAGS="$ASFLAGS -DPREFIX"
486         else
487             ASFLAGS="$ASFLAGS -f elf -m amd64"
488         fi
489         ;;
490     powerpc|powerpc64)
491         ARCH="PPC"
492         if [ $asm = auto ] ; then
493             define HAVE_ALTIVEC
494             AS="${AS-${cross_prefix}gcc}"
495             if [ $SYS = MACOSX ] ; then
496                 CFLAGS="$CFLAGS -faltivec -fastf -mcpu=G4"
497             else
498                 CFLAGS="$CFLAGS -maltivec -mabi=altivec"
499                 define HAVE_ALTIVEC_H
500             fi
501         fi
502         ;;
503     sparc)
504         ARCH="SPARC"
505         case $(uname -m) in
506             sun4u|sun4v)
507                 if [ $asm = auto ]; then
508                     ARCH="UltraSPARC"
509                     if ! echo $CFLAGS | grep -Eq '\-mcpu' ; then
510                         CFLAGS="$CFLAGS -mcpu=ultrasparc"
511                         LDFLAGS="$LDFLAGS -mcpu=ultrasparc"
512                     fi
513                     AS="${AS-${cross_prefix}as}"
514                     ASFLAGS="$ASFLAGS -xarch=v8plusa"
515                 fi
516                 ;;
517         esac
518         ;;
519     mips|mipsel|mips64|mips64el)
520         ARCH="MIPS"
521         ;;
522     arm*)
523         ARCH="ARM"
524         if [ "$SYS" = MACOSX ] ; then
525             AS="${AS-extras/gas-preprocessor.pl $CC}"
526             ASFLAGS="$ASFLAGS -DPREFIX -DPIC"  # apple's ld doesn't support movw/movt relocations at all
527             # build for armv7 by default
528             if ! echo $CFLAGS | grep -Eq '\-arch' ; then
529                 CFLAGS="$CFLAGS -arch armv7"
530                 LDFLAGS="$LDFLAGS -arch armv7"
531             fi
532         else
533             AS="${AS-${cross_prefix}gcc}"
534         fi
535         ;;
536     s390|s390x)
537         ARCH="S390"
538         ;;
539     parisc|parisc64)
540         ARCH="PARISC"
541         ;;
542     ia64)
543         ARCH="IA64"
544         ;;
545     *)
546         ARCH="$(echo $host_cpu | tr a-z A-Z)"
547         ;;
548 esac
549
550 log_msg "x264 configure script"
551 if [ -n "$*" ]; then
552     msg="Command line options:"
553     for i in $@; do
554         msg="$msg \"$i\""
555     done
556     log_msg "$msg"
557 fi
558 log_msg ""
559
560 # check requirements
561
562 cc_check || die "No working C compiler found."
563
564 if [ $compiler != ICL ]; then
565     if cc_check '' -std=gnu99 'for( int i = 0; i < 9; i++ );' ; then
566         CFLAGS="$CFLAGS -std=gnu99"
567     elif cc_check '' -std=c99 'for( int i = 0; i < 9; i++ );' ; then
568         CFLAGS="$CFLAGS -std=c99 -D_POSIX_C_SOURCE=200112L -D_BSD_SOURCE"
569     elif ! cc_check '' '' 'for( int i = 0; i < 9; i++ );' ; then
570         die "C99 compiler is needed for compilation."
571     fi
572 fi
573
574 if [ $shared = yes -a \( $ARCH = "X86_64" -o $ARCH = "PPC" -o $ARCH = "ALPHA" -o $ARCH = "ARM" -o $ARCH = "IA64" \) ] ; then
575     pic="yes"
576 fi
577
578 if [ $asm = auto -a \( $ARCH = X86 -o $ARCH = X86_64 \) ] ; then
579     if ! as_check "vpaddw xmm0, xmm0, xmm0" ; then
580         VER=`($AS --version || echo no assembler) 2>/dev/null | head -n 1`
581         echo "Found $VER"
582         echo "Minimum version is yasm-0.7.0"
583         echo "If you really want to compile without asm, configure with --disable-asm."
584         exit 1
585     fi
586     if ! cc_check '' '' '__asm__("pabsw %xmm0, %xmm0");' ; then
587         VER=`(${cross_prefix}as --version || echo no gnu as) 2>/dev/null | head -n 1`
588         echo "Found $VER"
589         echo "Minimum version is binutils-2.17"
590         echo "Your compiler can't handle inline SSSE3 asm."
591         echo "If you really want to compile without asm, configure with --disable-asm."
592         exit 1
593     fi
594     define HAVE_MMX
595 fi
596
597 if [ $asm = auto -a $ARCH = ARM ] ; then
598     # set flags so neon is built by default
599     echo $CFLAGS | grep -Eq '(-mcpu|-march|-mfpu|-mfloat-abi)' || CFLAGS="$CFLAGS -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=softfp"
600
601     if  cc_check '' '' '__asm__("rev ip, ip");' ; then      define HAVE_ARMV6
602         cc_check '' '' '__asm__("movt r0, #0");'         && define HAVE_ARMV6T2
603         cc_check '' '' '__asm__("vadd.i16 q0, q0, q0");' && define HAVE_NEON
604         ASFLAGS="$ASFLAGS $CFLAGS -c"
605     else
606         echo "You specified a pre-ARMv6 or Thumb-1 CPU in your CFLAGS."
607         echo "If you really want to run on such a CPU, configure with --disable-asm."
608         exit 1
609     fi
610 fi
611
612 [ $asm = no ] && AS=""
613 [ "x$AS" = x ] && asm="no" || asm="yes"
614
615 define ARCH_$ARCH
616 define SYS_$SYS
617
618 # skip endianness check for Intel Compiler, as all supported platforms are little. the -ipo flag will also cause the check to fail
619 if [ $compiler = GNU ]; then
620     echo "int i[2] = {0x42494745,0}; double f[2] = {0x1.0656e6469616ep+102,0};" > conftest.c
621     $CC $CFLAGS conftest.c -c -o conftest.o 2>/dev/null || die "endian test failed"
622     if (${cross_prefix}strings -a conftest.o | grep -q BIGE) && (${cross_prefix}strings -a conftest.o | grep -q FPendian) ; then
623         define WORDS_BIGENDIAN
624     elif !(${cross_prefix}strings -a conftest.o | grep -q EGIB && ${cross_prefix}strings -a conftest.o | grep -q naidnePF) ; then
625         die "endian test failed"
626     fi
627 fi
628
629 # autodetect options that weren't forced nor disabled
630
631 # pthread-win32 is lgpl, prevent its use if --disable-gpl is specified and targeting windows
632 [ "$SYS" = "WINDOWS" -a "$gpl" = "no" -a "$thread" = "auto" ] && thread="win32"
633
634 libpthread=""
635 if [ "$thread" = "auto" ]; then
636     thread="no"
637     case $SYS in
638         BEOS)
639             thread="beos"
640             define HAVE_BEOSTHREAD
641             ;;
642         WINDOWS)
643             if cc_check pthread.h -lpthread "pthread_create(0,0,0,0);" ; then
644                 thread="posix"
645                 libpthread="-lpthread"
646             elif cc_check pthread.h -lpthreadGC2 "pthread_create(0,0,0,0);" ; then
647                 thread="posix"
648                 libpthread="-lpthreadGC2"
649             elif cc_check pthread.h "-lpthreadGC2 -lwsock32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then
650                 thread="posix"
651                 libpthread="-lpthreadGC2 -lwsock32"
652                 define PTW32_STATIC_LIB
653             elif cc_check pthread.h "-lpthreadGC2 -lws2_32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then
654                 thread="posix"
655                 libpthread="-lpthreadGC2 -lws2_32"
656                 define PTW32_STATIC_LIB
657             else
658                 # default to native threading if pthread-win32 is unavailable
659                 thread="win32"
660             fi
661             ;;
662         OPENBSD)
663             cc_check pthread.h -pthread && thread="posix" && libpthread="-pthread"
664             ;;
665         *)
666             cc_check pthread.h -lpthread && thread="posix" && libpthread="-lpthread"
667             ;;
668     esac
669 fi
670 if [ "$thread" = "posix" ]; then
671     LDFLAGS="$LDFLAGS $libpthread"
672     define HAVE_POSIXTHREAD
673 fi
674 if [ "$thread" = "win32" ]; then
675     # cygwin does not support win32 threads
676     if [ "$SYS" = "WINDOWS" ]; then
677         define HAVE_WIN32THREAD
678     else
679         thread="no"
680     fi
681 fi
682 [ "$thread" != "no" ] && define HAVE_THREAD
683
684 if cc_check "math.h" "-Werror" "return log2f(2);" ; then
685     define HAVE_LOG2F
686 fi
687
688 if [ "$vis" = "yes" ] ; then
689     save_CFLAGS="$CFLAGS"
690     CFLAGS="$CFLAGS -I/usr/X11R6/include"
691     if cc_check "X11/Xlib.h" "-L/usr/X11R6/lib -lX11" "XOpenDisplay(0);" ; then
692         LDFLAGS="-L/usr/X11R6/lib -lX11 $LDFLAGS"
693         define HAVE_VISUALIZE
694     else
695         vis="no"
696         CFLAGS="$save_CFLAGS"
697    fi
698 fi
699
700 if [ "$swscale" = "auto" ] ; then
701     swscale="no"
702     if ${cross_prefix}pkg-config --exists libswscale 2>/dev/null; then
703         SWSCALE_LIBS="$SWSCALE_LIBS $(${cross_prefix}pkg-config --libs libswscale)"
704         SWSCALE_CFLAGS="$SWSCALE_CFLAGS $(${cross_prefix}pkg-config --cflags libswscale)"
705     fi
706     [ -z "$SWSCALE_LIBS" ] && SWSCALE_LIBS="-lswscale -lavutil"
707
708     error="swscale must be at least version 0.9.0"
709     if cc_check "libswscale/swscale.h" "$SWSCALE_CFLAGS $SWSCALE_LIBS" "sws_getContext(0,0,0,0,0,0,0,0,0,0);" ; then
710         if cpp_check "libswscale/swscale.h" "$SWSCALE_CFLAGS" "LIBSWSCALE_VERSION_INT >= AV_VERSION_INT(0,9,0)" "$error"; then
711             # we use colorspaces that were defined in libavutil r19775
712             if cc_check "libavutil/pixfmt.h" "$SWSCALE_CFLAGS" "enum PixelFormat pixfmt = PIX_FMT_YUV422P16LE;" ; then
713                 swscale="yes"
714             else
715                 echo "Warning: libavutil is too old, update to ffmpeg r19775+"
716             fi
717         else
718             echo "Warning: ${error}"
719         fi
720     fi
721 fi
722
723 if [ "$lavf" = "auto" ] ; then
724     lavf="no"
725     if ${cross_prefix}pkg-config --exists libavformat libavcodec libswscale 2>/dev/null; then
726         LAVF_LIBS="$LAVF_LIBS $(${cross_prefix}pkg-config --libs libavformat libavcodec libavutil libswscale)"
727         LAVF_CFLAGS="$LAVF_CFLAGS $(${cross_prefix}pkg-config --cflags libavformat libavcodec libavutil libswscale)"
728     fi
729     if [ -z "$LAVF_LIBS" -a -z "$LAVF_CFLAGS" ]; then
730         LAVF_LIBS="-lavformat"
731         for lib in -lpostproc -lavcodec -lavcore -lswscale -lavutil -lm -lz -lbz2 $libpthread -lavifil32; do
732             cc_check "" $lib && LAVF_LIBS="$LAVF_LIBS $lib"
733         done
734     fi
735     LAVF_LIBS="-L. $LAVF_LIBS"
736     if cc_check libavformat/avformat.h "$LAVF_CFLAGS $LAVF_LIBS" "avcodec_decode_video2(0,0,0,0);" ; then
737         # libvautil/pixdesc.h included the private header intreadwrite.h until r21854
738         if cc_check libavutil/pixdesc.h "$LAVF_CFLAGS $LAVF_LIBS" ; then
739             if [ "$swscale" = "yes" ]; then
740                 lavf="yes"
741             else
742                 echo "Warning: libavformat is not supported without swscale support"
743             fi
744         else
745             echo "Warning: libavutil is too old, update to ffmpeg r21854+"
746         fi
747     fi
748 fi
749
750 if [ "$ffms" = "auto" ] ; then
751     ffms_major="2"; ffms_minor="14"; ffms_micro="0"; ffms_bump="0"
752     ffms="no"
753
754     if ${cross_prefix}pkg-config --exists ffms2 2>/dev/null; then
755         FFMS2_LIBS="$FFMS2_LIBS $(${cross_prefix}pkg-config --libs ffms2)"
756         FFMS2_CFLAGS="$FFMS2_CFLAGS $(${cross_prefix}pkg-config --cflags ffms2)"
757     fi
758     [ -z "$FFMS2_LIBS" ] && FFMS2_LIBS="-lffms2"
759
760     if cc_check ffms.h "$FFMS2_CFLAGS $FFMS2_LIBS" "FFMS_DestroyVideoSource(0);" ; then
761         ffms="yes"
762     elif cc_check ffms.h "$FFMS2_CFLAGS $FFMS2_LIBS -lstdc++ $LAVF_LIBS" "FFMS_DestroyVideoSource(0);" ; then
763         ffms="yes"
764         FFMS2_LIBS="$FFMS2_LIBS -lstdc++ $LAVF_LIBS"
765     fi
766
767     error="ffms must be at least version $ffms_major.$ffms_minor.$ffms_micro.$ffms_bump"
768     if [ $ffms = "yes" ] && ! cpp_check "ffms.h" "$FFMS2_CFLAGS" "FFMS_VERSION >= (($ffms_major << 24) | ($ffms_minor << 16) | ($ffms_micro << 8) | $ffms_bump)" "$error"; then
769        ffms="no"
770        echo "Warning: $error"
771     fi
772     if [ "$ffms" = "yes" -a "$swscale" = "no" ]; then
773         echo "Warning: ffms is not supported without swscale support"
774         ffms="no"
775     fi
776 fi
777
778 if [ "$swscale" = "yes" ]; then
779     LDFLAGSCLI="$SWSCALE_LIBS $LDFLAGSCLI"
780     CFLAGS="$CFLAGS $SWSCALE_CFLAGS"
781     define HAVE_SWSCALE
782     if [ "$lavf" = "yes" ]; then
783         LDFLAGSCLI="$LAVF_LIBS $LDFLAGSCLI"
784         CFLAGS="$CFLAGS $LAVF_CFLAGS"
785         define HAVE_LAVF
786     fi
787     if [ "$ffms" = "yes" ]; then
788         LDFLAGSCLI="$FFMS2_LIBS $LDFLAGSCLI"
789         CFLAGS="$CFLAGS $FFMS2_CFLAGS"
790         define HAVE_FFMS
791     fi
792 fi
793
794 GPAC_LIBS="-lgpac_static"
795 if [ $SYS = WINDOWS ]; then
796     GPAC_LIBS="$GPAC_LIBS -lwinmm"
797 fi
798 if [ "$gpac" = "auto" ] ; then
799     gpac="no"
800     if cc_check gpac/isomedia.h "$GPAC_LIBS" ; then
801         if cc_check gpac/isomedia.h "$GPAC_LIBS" "gf_isom_set_pixel_aspect_ratio(0,0,0,0,0);" ; then
802             gpac="yes"
803         else
804             echo "Warning: gpac is too old, update to 2007-06-21 UTC or later"
805         fi
806     fi
807 fi
808 if [ "$gpac" = "yes" ] ; then
809     define HAVE_GPAC
810     if cc_check gpac/isomedia.h "-Werror $GPAC_LIBS" "gf_malloc(1); gf_free(NULL);" ; then
811         define HAVE_GF_MALLOC
812     fi
813     LDFLAGSCLI="$GPAC_LIBS $LDFLAGSCLI"
814 fi
815
816 if [ "$avs" = "auto" ] ; then
817     avs="no"
818     # cygwin can use avisynth if it can use LoadLibrary
819     if [ $SYS = WINDOWS ] || ([ $SYS = CYGWIN ] && cc_check windows.h "" "LoadLibrary(0);") ; then
820         avs="yes"
821         define HAVE_AVS
822     fi
823 fi
824
825 if [ "$pic" = "yes" ] ; then
826     CFLAGS="$CFLAGS -fPIC"
827     ASFLAGS="$ASFLAGS -DPIC"
828     # resolve textrels in the x86 asm
829     cc_check stdio.h -Wl,-Bsymbolic && LDFLAGS="$LDFLAGS -Wl,-Bsymbolic"
830 fi
831
832 if [ "$debug" != "yes" -a "$gprof" != "yes" ]; then
833     CFLAGS="$CFLAGS -s -fomit-frame-pointer"
834     LDFLAGS="$LDFLAGS -s"
835 fi
836
837 if [ "$debug" = "yes" ]; then
838     CFLAGS="-O1 -g $CFLAGS"
839 elif [ $ARCH = ARM ]; then
840     # arm-gcc-4.2 produces incorrect output with -ffast-math
841     # and it doesn't save any speed anyway on 4.4, so disable it
842     CFLAGS="-O3 -fno-fast-math $CFLAGS"
843 else
844     CFLAGS="-O3 -ffast-math $CFLAGS"
845 fi
846
847 if cc_check '' -fno-tree-vectorize ; then
848     CFLAGS="$CFLAGS -fno-tree-vectorize"
849 fi
850
851 if [ $SYS = WINDOWS -a $ARCH = X86 -a $compiler = GNU ] ; then
852     # workaround gcc/ld bug with alignment of static variables/arrays that are initialized to zero
853     cc_check '' -fno-zero-initialized-in-bss && CFLAGS="$CFLAGS -fno-zero-initialized-in-bss"
854 fi
855
856 if cc_check "stdio.h" "" "fseeko(stdin,0,0);" ; then
857     define fseek fseeko
858     define ftell ftello
859 elif cc_check "stdio.h" "" "fseeko64(stdin,0,0);" ; then
860     define fseek fseeko64
861     define ftell ftello64
862 elif cc_check "stdio.h" "" "_fseeki64(stdin,0,0);" ; then
863     define fseek _fseeki64
864     define ftell _ftelli64
865 fi
866
867 if cc_check '' -Wshadow ; then
868     CFLAGS="-Wshadow $CFLAGS"
869 fi
870
871 if [ "$bit_depth" -gt "8" ]; then
872     define HIGH_BIT_DEPTH
873     ASFLAGS="$ASFLAGS -DHIGH_BIT_DEPTH"
874 fi
875
876 ASFLAGS="$ASFLAGS -DBIT_DEPTH=$bit_depth"
877
878 [ $gpl = yes ] && define HAVE_GPL && x264_gpl=1 || x264_gpl=0
879
880 #define undefined vars as 0
881 for var in $CONFIG_HAVE; do
882     grep -q "HAVE_$var 1" config.h || define HAVE_$var 0
883 done
884
885 if [ $compiler = ICL ]; then
886     AR="xilib -nologo -out:"
887     DEPMM=-QMM
888     DEPMT=-QMT
889     HAVE_GETOPT_LONG=0
890     LD="xilink -out:"
891     LDFLAGS="-nologo -incremental:no $(icl_ldflags $LDFLAGS)"
892     LDFLAGSCLI="$(icl_ldflags $LDFLAGSCLI)"
893     LIBX264=libx264.lib
894     RANLIB=
895     STRIP=
896     if [ $debug = yes ]; then
897         LDFLAGS="-debug $LDFLAGS"
898         CFLAGS="-D_DEBUG $CFLAGS"
899     else
900         CFLAGS="-DNDEBUG $CFLAGS"
901     fi
902 else
903     AR="$AR rc "
904     DEPMM="-MM -g0"
905     DEPMT="-MT"
906     LD="$CC -o "
907     LIBX264=libx264.a
908 fi
909 if [ $compiler = GNU ]; then
910     PROF_GEN_CC="-fprofile-generate"
911     PROF_GEN_LD="-fprofile-generate"
912     PROF_USE_CC="-fprofile-use"
913     PROF_USE_LD="-fprofile-use"
914 else
915     CFLAGS="$(intel_cflags $CFLAGS)"
916     # icc does not define __SSE__ until SSE2 optimization and icl never defines it or _M_IX86_FP
917     [ \( $ARCH = X86_64 -o $ARCH = X86 \) -a $asm = yes ] && ! cpp_check "" "" "defined(__SSE__)" && define __SSE__
918     PROF_GEN_CC="${QPRE}prof-gen ${QPRE}prof-dir."
919     PROF_GEN_LD=
920     PROF_USE_CC="${QPRE}prof-use ${QPRE}prof-dir."
921     PROF_USE_LD=
922 fi
923
924 rm -f conftest*
925
926 # generate exported config file
927
928 cat > x264_config.h << EOF
929 #define X264_BIT_DEPTH $bit_depth
930 #define X264_GPL       $x264_gpl
931 EOF
932
933 # generate config files
934
935 cat > config.mak << EOF
936 prefix=$prefix
937 exec_prefix=$exec_prefix
938 bindir=$bindir
939 libdir=$libdir
940 includedir=$includedir
941 ARCH=$ARCH
942 SYS=$SYS
943 CC=$CC
944 CFLAGS=$CFLAGS
945 DEPMM=$DEPMM
946 DEPMT=$DEPMT
947 LD=$LD
948 LDFLAGS=$LDFLAGS
949 LDFLAGSCLI=$LDFLAGSCLI
950 LIBX264=$LIBX264
951 AR=$AR
952 RANLIB=$RANLIB
953 STRIP=$STRIP
954 AS=$AS
955 ASFLAGS=$ASFLAGS
956 EXE=$EXE
957 HAVE_GETOPT_LONG=$HAVE_GETOPT_LONG
958 DEVNULL=$DEVNULL
959 PROF_GEN_CC=$PROF_GEN_CC
960 PROF_GEN_LD=$PROF_GEN_LD
961 PROF_USE_CC=$PROF_USE_CC
962 PROF_USE_LD=$PROF_USE_LD
963 EOF
964
965 if [ $compiler = ICL ]; then
966     echo '%.o: %.c' >> config.mak
967     echo '      $(CC) $(CFLAGS) -c -Fo$@ $<' >> config.mak
968 fi
969
970 if [ "$shared" = "yes" ]; then
971     API=$(grep '#define X264_BUILD' < x264.h | cut -f 3 -d ' ')
972     if [ "$SYS" = "WINDOWS" -o "$SYS" = "CYGWIN" ]; then
973         echo "SONAME=libx264-$API.dll" >> config.mak
974         if [ $compiler = ICL ]; then
975             echo 'IMPLIBNAME=libx264.dll.lib' >> config.mak
976             # GNU ld on windows defaults to exporting all global functions if there are no explicit __declspec(dllexport) declarations
977             # 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
978             echo 'SOFLAGS=-dll -def:x264.def -implib:$(IMPLIBNAME)' >> config.mak
979             echo "EXPORTS" > x264.def
980             grep "^\(int\|void\|x264_t\|extern\).*x264.*[\[(;]" x264.h | sed -e "s/.*\(x264.*\)[\[(].*/\1/;s/.*\(x264.*\);/\1/;s/open/open_$API/g" >> x264.def
981         else
982             echo 'IMPLIBNAME=libx264.dll.a' >> config.mak
983             echo 'SOFLAGS=-shared -Wl,--out-implib,$(IMPLIBNAME) -Wl,--enable-auto-image-base' >> config.mak
984         fi
985     elif [ "$SYS" = "MACOSX" ]; then
986         echo "SOSUFFIX=dylib" >> config.mak
987         echo "SONAME=libx264.$API.dylib" >> config.mak
988         echo 'SOFLAGS=-shared -dynamiclib -Wl,-single_module -Wl,-read_only_relocs,suppress -install_name $(DESTDIR)$(libdir)/$(SONAME)' >> config.mak
989     elif [ "$SYS" = "SunOS" ]; then
990         echo "SOSUFFIX=so" >> config.mak
991         echo "SONAME=libx264.so.$API" >> config.mak
992         echo 'SOFLAGS=-shared -Wl,-h,$(SONAME)' >> config.mak
993     else
994         echo "SOSUFFIX=so" >> config.mak
995         echo "SONAME=libx264.so.$API" >> config.mak
996         echo 'SOFLAGS=-shared -Wl,-soname,$(SONAME)' >> config.mak
997     fi
998     echo 'default: $(SONAME)' >> config.mak
999 fi
1000
1001 ./version.sh >> config.h
1002
1003 pclibs="-L$libdir -lx264 $libpthread"
1004
1005 cat > x264.pc << EOF
1006 prefix=$prefix
1007 exec_prefix=$exec_prefix
1008 libdir=$libdir
1009 includedir=$includedir
1010
1011 Name: x264
1012 Description: H.264 (MPEG4 AVC) encoder library
1013 Version: $(grep POINTVER < config.h | sed -e 's/.* "//; s/".*//')
1014 Libs: $pclibs
1015 Cflags: -I$includedir
1016 EOF
1017
1018 filters="crop select_every"
1019 gpl_filters=""
1020 [ $swscale = yes ] && filters="resize $filters"
1021 [ $gpl = yes ] && filters="$filters $gpl_filters"
1022
1023 cat > conftest.log <<EOF
1024 Platform:   $ARCH
1025 System:     $SYS
1026 asm:        $asm
1027 avs:        $avs
1028 lavf:       $lavf
1029 ffms:       $ffms
1030 gpac:       $gpac
1031 gpl:        $gpl
1032 thread:     $thread
1033 filters:    $filters
1034 debug:      $debug
1035 gprof:      $gprof
1036 PIC:        $pic
1037 shared:     $shared
1038 visualize:  $vis
1039 bit depth:  $bit_depth
1040 EOF
1041
1042 echo >> config.log
1043 cat conftest.log >> config.log
1044 cat conftest.log
1045 rm conftest.log
1046
1047 echo
1048 echo "You can run 'make' or 'make fprofiled' now."
1049