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