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