]> git.sesse.net Git - x264/blob - configure
Improve build system capabilities
[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-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     error="swscale must be at least version 0.9.0"
726     if cc_check "libswscale/swscale.h" "$SWSCALE_CFLAGS $SWSCALE_LIBS" "sws_getContext(0,0,0,0,0,0,0,0,0,0);" ; then
727         if cpp_check "libswscale/swscale.h" "$SWSCALE_CFLAGS" "LIBSWSCALE_VERSION_INT >= AV_VERSION_INT(0,9,0)" "$error"; then
728             # we use colorspaces that were defined in libavutil r19775
729             if cc_check "libavutil/pixfmt.h" "$SWSCALE_CFLAGS" "enum PixelFormat pixfmt = PIX_FMT_YUV422P16LE;" ; then
730                 swscale="yes"
731             else
732                 echo "Warning: libavutil is too old, update to ffmpeg r19775+"
733             fi
734         else
735             echo "Warning: ${error}"
736         fi
737     fi
738 fi
739
740 if [ "$lavf" = "auto" ] ; then
741     lavf="no"
742     if ${cross_prefix}pkg-config --exists libavformat libavcodec libswscale 2>/dev/null; then
743         LAVF_LIBS="$LAVF_LIBS $(${cross_prefix}pkg-config --libs libavformat libavcodec libavutil libswscale)"
744         LAVF_CFLAGS="$LAVF_CFLAGS $(${cross_prefix}pkg-config --cflags libavformat libavcodec libavutil libswscale)"
745     fi
746     if [ -z "$LAVF_LIBS" -a -z "$LAVF_CFLAGS" ]; then
747         LAVF_LIBS="-lavformat"
748         for lib in -lpostproc -lavcodec -lavcore -lswscale -lavutil -lm -lz -lbz2 $libpthread -lavifil32; do
749             cc_check "" $lib && LAVF_LIBS="$LAVF_LIBS $lib"
750         done
751     fi
752     LAVF_LIBS="-L. $LAVF_LIBS"
753     if cc_check libavformat/avformat.h "$LAVF_CFLAGS $LAVF_LIBS" "avcodec_decode_video2(0,0,0,0);" ; then
754         if cpp_check libavcodec/avcodec.h "$LAVF_CFLAGS $LAVF_LIBS" "LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52,64,0)" ; then
755             if [ "$swscale" = "yes" ]; then
756                 lavf="yes"
757             else
758                 echo "Warning: libavformat is not supported without swscale support"
759             fi
760         else
761             echo "Warning: libavcodec is too old, update to ffmpeg r22735+"
762         fi
763     fi
764 fi
765
766 if [ "$ffms" = "auto" ] ; then
767     ffms_major="2"; ffms_minor="14"; ffms_micro="0"; ffms_bump="0"
768     ffms="no"
769
770     if ${cross_prefix}pkg-config --exists ffms2 2>/dev/null; then
771         FFMS2_LIBS="$FFMS2_LIBS $(${cross_prefix}pkg-config --libs ffms2)"
772         FFMS2_CFLAGS="$FFMS2_CFLAGS $(${cross_prefix}pkg-config --cflags ffms2)"
773     fi
774     [ -z "$FFMS2_LIBS" ] && FFMS2_LIBS="-lffms2"
775
776     if cc_check ffms.h "$FFMS2_CFLAGS $FFMS2_LIBS" "FFMS_DestroyVideoSource(0);" ; then
777         ffms="yes"
778     elif cc_check ffms.h "$FFMS2_CFLAGS $FFMS2_LIBS -lstdc++ $LAVF_LIBS" "FFMS_DestroyVideoSource(0);" ; then
779         ffms="yes"
780         FFMS2_LIBS="$FFMS2_LIBS -lstdc++ $LAVF_LIBS"
781     fi
782
783     error="ffms must be at least version $ffms_major.$ffms_minor.$ffms_micro.$ffms_bump"
784     if [ $ffms = "yes" ] && ! cpp_check "ffms.h" "$FFMS2_CFLAGS" "FFMS_VERSION >= (($ffms_major << 24) | ($ffms_minor << 16) | ($ffms_micro << 8) | $ffms_bump)" "$error"; then
785        ffms="no"
786        echo "Warning: $error"
787     fi
788     if [ "$ffms" = "yes" -a "$swscale" = "no" ]; then
789         echo "Warning: ffms is not supported without swscale support"
790         ffms="no"
791     fi
792 fi
793
794 if [ "$swscale" = "yes" ]; then
795     LDFLAGSCLI="$SWSCALE_LIBS $LDFLAGSCLI"
796     CFLAGS="$CFLAGS $SWSCALE_CFLAGS"
797     define HAVE_SWSCALE
798     if [ "$lavf" = "yes" ]; then
799         LDFLAGSCLI="$LAVF_LIBS $LDFLAGSCLI"
800         CFLAGS="$CFLAGS $LAVF_CFLAGS"
801         define HAVE_LAVF
802     fi
803     if [ "$ffms" = "yes" ]; then
804         LDFLAGSCLI="$FFMS2_LIBS $LDFLAGSCLI"
805         CFLAGS="$CFLAGS $FFMS2_CFLAGS"
806         define HAVE_FFMS
807     fi
808 fi
809
810 if [ "$gpac" = "auto" ] ; then
811     gpac="no"
812     cc_check "" -lz && GPAC_LIBS="-lgpac_static -lz" || GPAC_LIBS="-lgpac_static"
813     if [ "$SYS" = "WINDOWS" ] ; then
814         GPAC_LIBS="$GPAC_LIBS -lwinmm"
815     fi
816     if cc_check gpac/isomedia.h "$GPAC_LIBS" ; then
817         if cc_check gpac/isomedia.h "$GPAC_LIBS" "gf_isom_set_pixel_aspect_ratio(0,0,0,0,0);" ; then
818             gpac="yes"
819         else
820             echo "Warning: gpac is too old, update to 2007-06-21 UTC or later"
821         fi
822     fi
823 fi
824 if [ "$gpac" = "yes" ] ; then
825     define HAVE_GPAC
826     if cc_check gpac/isomedia.h "-Werror $GPAC_LIBS" "gf_malloc(1); gf_free(NULL);" ; then
827         define HAVE_GF_MALLOC
828     fi
829     LDFLAGSCLI="$GPAC_LIBS $LDFLAGSCLI"
830 fi
831
832 if [ "$avs" = "auto" ] ; then
833     avs="no"
834     # cygwin can use avisynth if it can use LoadLibrary
835     if [ $SYS = WINDOWS ] || ([ $SYS = CYGWIN ] && cc_check windows.h "" "LoadLibrary(0);") ; then
836         avs="yes"
837         define HAVE_AVS
838     fi
839 fi
840
841 cc_check "stdint.h" "" "uint32_t test_vec __attribute__ ((vector_size (16))) = {0,1,2,3};" && define HAVE_VECTOREXT
842
843 if [ "$pic" = "yes" ] ; then
844     CFLAGS="$CFLAGS -fPIC"
845     ASFLAGS="$ASFLAGS -DPIC"
846     # resolve textrels in the x86 asm
847     cc_check stdio.h -Wl,-Bsymbolic && LDFLAGS="$LDFLAGS -Wl,-Bsymbolic"
848 fi
849
850 if [ "$debug" != "yes" -a "$gprof" != "yes" ]; then
851     CFLAGS="$CFLAGS -fomit-frame-pointer"
852 fi
853
854 if [ "$strip" = "yes" ]; then
855     CFLAGS="$CFLAGS -s"
856     LDFLAGS="$LDFLAGS -s"
857 fi
858
859 if [ "$debug" = "yes" ]; then
860     CFLAGS="-O1 -g $CFLAGS"
861 elif [ $ARCH = ARM ]; then
862     # arm-gcc-4.2 produces incorrect output with -ffast-math
863     # and it doesn't save any speed anyway on 4.4, so disable it
864     CFLAGS="-O3 -fno-fast-math $CFLAGS"
865 else
866     CFLAGS="-O3 -ffast-math $CFLAGS"
867 fi
868
869 if cc_check '' -fno-tree-vectorize ; then
870     CFLAGS="$CFLAGS -fno-tree-vectorize"
871 fi
872
873 if [ $SYS = WINDOWS -a $ARCH = X86 -a $compiler = GNU ] ; then
874     # workaround gcc/ld bug with alignment of static variables/arrays that are initialized to zero
875     cc_check '' -fno-zero-initialized-in-bss && CFLAGS="$CFLAGS -fno-zero-initialized-in-bss"
876 fi
877
878 if cc_check "stdio.h" "" "fseeko(stdin,0,0);" ; then
879     define fseek fseeko
880     define ftell ftello
881 elif cc_check "stdio.h" "" "fseeko64(stdin,0,0);" ; then
882     define fseek fseeko64
883     define ftell ftello64
884 elif cc_check "stdio.h" "" "_fseeki64(stdin,0,0);" ; then
885     define fseek _fseeki64
886     define ftell _ftelli64
887 fi
888
889 if cc_check '' -Wshadow ; then
890     CFLAGS="-Wshadow $CFLAGS"
891 fi
892
893 if [ "$bit_depth" -gt "8" ]; then
894     define HIGH_BIT_DEPTH
895     ASFLAGS="$ASFLAGS -DHIGH_BIT_DEPTH"
896 fi
897
898 ASFLAGS="$ASFLAGS -DBIT_DEPTH=$bit_depth"
899
900 [ $gpl = yes ] && define HAVE_GPL && x264_gpl=1 || x264_gpl=0
901
902 [ $interlaced = yes ] && define HAVE_INTERLACED && x264_interlaced=1 || x264_interlaced=0
903
904 #define undefined vars as 0
905 for var in $CONFIG_HAVE; do
906     grep -q "HAVE_$var 1" config.h || define HAVE_$var 0
907 done
908
909 if [ $compiler = ICL ]; then
910     AR="xilib -nologo -out:"
911     DEPMM=-QMM
912     DEPMT=-QMT
913     HAVE_GETOPT_LONG=0
914     LD="xilink -out:"
915     LDFLAGS="-nologo -incremental:no $(icl_ldflags $LDFLAGS)"
916     LDFLAGSCLI="$(icl_ldflags $LDFLAGSCLI)"
917     LIBX264=libx264.lib
918     RANLIB=
919     STRIP=
920     if [ $debug = yes ]; then
921         LDFLAGS="-debug $LDFLAGS"
922         CFLAGS="-D_DEBUG $CFLAGS"
923     else
924         CFLAGS="-DNDEBUG $CFLAGS"
925     fi
926 else
927     AR="$AR rc "
928     DEPMM="-MM -g0"
929     DEPMT="-MT"
930     LD="$CC -o "
931     LIBX264=libx264.a
932 fi
933 if [ $compiler = GNU ]; then
934     PROF_GEN_CC="-fprofile-generate"
935     PROF_GEN_LD="-fprofile-generate"
936     PROF_USE_CC="-fprofile-use"
937     PROF_USE_LD="-fprofile-use"
938 else
939     CFLAGS="$(intel_cflags $CFLAGS)"
940     # icc does not define __SSE__ until SSE2 optimization and icl never defines it or _M_IX86_FP
941     [ \( $ARCH = X86_64 -o $ARCH = X86 \) -a $asm = yes ] && ! cpp_check "" "" "defined(__SSE__)" && define __SSE__
942     PROF_GEN_CC="${QPRE}prof-gen ${QPRE}prof-dir."
943     PROF_GEN_LD=
944     PROF_USE_CC="${QPRE}prof-use ${QPRE}prof-dir."
945     PROF_USE_LD=
946 fi
947
948 rm -f conftest*
949
950 # generate exported config file
951
952 cat > x264_config.h << EOF
953 #define X264_BIT_DEPTH  $bit_depth
954 #define X264_GPL        $x264_gpl
955 #define X264_INTERLACED $x264_interlaced
956 EOF
957
958 # generate config files
959
960 cat > config.mak << EOF
961 prefix=$prefix
962 exec_prefix=$exec_prefix
963 bindir=$bindir
964 libdir=$libdir
965 includedir=$includedir
966 ARCH=$ARCH
967 SYS=$SYS
968 CC=$CC
969 CFLAGS=$CFLAGS
970 DEPMM=$DEPMM
971 DEPMT=$DEPMT
972 LD=$LD
973 LDFLAGS=$LDFLAGS
974 LIBX264=$LIBX264
975 AR=$AR
976 RANLIB=$RANLIB
977 STRIP=$STRIP
978 AS=$AS
979 ASFLAGS=$ASFLAGS
980 EXE=$EXE
981 HAVE_GETOPT_LONG=$HAVE_GETOPT_LONG
982 DEVNULL=$DEVNULL
983 PROF_GEN_CC=$PROF_GEN_CC
984 PROF_GEN_LD=$PROF_GEN_LD
985 PROF_USE_CC=$PROF_USE_CC
986 PROF_USE_LD=$PROF_USE_LD
987 EOF
988
989 if [ $compiler = ICL ]; then
990     echo '%.o: %.c' >> config.mak
991     echo '      $(CC) $(CFLAGS) -c -Fo$@ $<' >> config.mak
992 fi
993
994 if [ "$cli" = "yes" ]; then
995     echo 'default: cli' >> config.mak
996     echo 'install: install-cli' >> config.mak
997 fi
998
999 if [ "$shared" = "yes" ]; then
1000     API=$(grep '#define X264_BUILD' < x264.h | cut -f 3 -d ' ')
1001     if [ "$SYS" = "WINDOWS" -o "$SYS" = "CYGWIN" ]; then
1002         echo "SONAME=libx264-$API.dll" >> config.mak
1003         if [ $compiler = ICL ]; then
1004             echo 'IMPLIBNAME=libx264.dll.lib' >> config.mak
1005             # GNU ld on windows defaults to exporting all global functions if there are no explicit __declspec(dllexport) declarations
1006             # 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
1007             echo 'SOFLAGS=-dll -def:x264.def -implib:$(IMPLIBNAME)' >> config.mak
1008             echo "EXPORTS" > x264.def
1009             grep "^\(int\|void\|x264_t\|extern\).*x264.*[\[(;]" x264.h | sed -e "s/.*\(x264.*\)[\[(].*/\1/;s/.*\(x264.*\);/\1/;s/open/open_$API/g" >> x264.def
1010         else
1011             echo 'IMPLIBNAME=libx264.dll.a' >> config.mak
1012             echo 'SOFLAGS=-shared -Wl,--out-implib,$(IMPLIBNAME) -Wl,--enable-auto-image-base' >> config.mak
1013         fi
1014     elif [ "$SYS" = "MACOSX" ]; then
1015         echo "SOSUFFIX=dylib" >> config.mak
1016         echo "SONAME=libx264.$API.dylib" >> config.mak
1017         echo 'SOFLAGS=-shared -dynamiclib -Wl,-single_module -Wl,-read_only_relocs,suppress -install_name $(DESTDIR)$(libdir)/$(SONAME)' >> config.mak
1018     elif [ "$SYS" = "SunOS" ]; then
1019         echo "SOSUFFIX=so" >> config.mak
1020         echo "SONAME=libx264.so.$API" >> config.mak
1021         echo 'SOFLAGS=-shared -Wl,-h,$(SONAME)' >> config.mak
1022     else
1023         echo "SOSUFFIX=so" >> config.mak
1024         echo "SONAME=libx264.so.$API" >> config.mak
1025         echo 'SOFLAGS=-shared -Wl,-soname,$(SONAME)' >> config.mak
1026     fi
1027     echo 'default: lib-shared' >> config.mak
1028     echo 'install: install-lib-shared' >> config.mak
1029 fi
1030
1031 if [ "$static" = "yes" ]; then
1032     echo 'default: lib-static' >> config.mak
1033     echo 'install: install-lib-static' >> config.mak
1034 fi
1035
1036 if [ "$cli_libx264" = "system" ] ; then
1037     if [ "$shared" = "yes" ]; then
1038         CLI_LIBX264='$(SONAME)'
1039     elif ${cross_prefix}pkg-config --exists x264 2>/dev/null; then
1040         LDFLAGSCLI="$LDFLAGSCLI $(${cross_prefix}pkg-config --libs x264)"
1041         CLI_LIBX264=
1042     else
1043         die "Can not find system libx264"
1044     fi
1045 else
1046     CLI_LIBX264='$(LIBX264)'
1047 fi
1048 echo "LDFLAGSCLI = $LDFLAGSCLI" >> config.mak
1049 echo "CLI_LIBX264 = $CLI_LIBX264" >> config.mak
1050
1051 ./version.sh >> config.h
1052
1053 pclibs="-L$libdir -lx264 $libpthread"
1054
1055 cat > x264.pc << EOF
1056 prefix=$prefix
1057 exec_prefix=$exec_prefix
1058 libdir=$libdir
1059 includedir=$includedir
1060
1061 Name: x264
1062 Description: H.264 (MPEG4 AVC) encoder library
1063 Version: $(grep POINTVER < config.h | sed -e 's/.* "//; s/".*//')
1064 Libs: $pclibs
1065 Cflags: -I$includedir
1066 EOF
1067
1068 filters="crop select_every"
1069 gpl_filters=""
1070 [ $swscale = yes ] && filters="resize $filters"
1071 [ $gpl = yes ] && filters="$filters $gpl_filters"
1072
1073 cat > conftest.log <<EOF
1074 Platform:   $ARCH
1075 System:     $SYS
1076 cli:        $cli
1077 libx264:    $cli_libx264
1078 shared:     $shared
1079 static:     $static
1080 asm:        $asm
1081 interlaced: $interlaced
1082 avs:        $avs
1083 lavf:       $lavf
1084 ffms:       $ffms
1085 gpac:       $gpac
1086 gpl:        $gpl
1087 thread:     $thread
1088 filters:    $filters
1089 debug:      $debug
1090 gprof:      $gprof
1091 strip:      $strip
1092 PIC:        $pic
1093 visualize:  $vis
1094 bit depth:  $bit_depth
1095 EOF
1096
1097 echo >> config.log
1098 cat conftest.log >> config.log
1099 cat conftest.log
1100 rm conftest.log
1101
1102 echo
1103 echo "You can run 'make' or 'make fprofiled' now."
1104