Totally new preset system for x264.c (not libx264), new defaults
[x262.git] / configure
1 #!/bin/bash
2
3 if test x"$1" = x"-h" -o x"$1" = x"--help" ; then
4
5 echo "Usage: ./configure [options]"
6 echo ""
7 echo "available options:"
8 echo ""
9 echo "  --help                   print this message"
10 echo "  --disable-avis-input     disables avisynth input (win32 only)"
11 echo "  --disable-mp4-output     disables mp4 output (using gpac)"
12 echo "  --disable-pthread        disables multithreaded encoding"
13 echo "  --disable-asm            disables assembly optimizations on x86"
14 echo "  --enable-debug           adds -g, doesn't strip"
15 echo "  --enable-gprof           adds -pg, doesn't strip"
16 echo "  --enable-visualize       enables visualization (X11 only)"
17 echo "  --enable-pic             build position-independent code"
18 echo "  --enable-shared          build libx264.so"
19 echo "  --extra-asflags=EASFLAGS add EASFLAGS to ASFLAGS"
20 echo "  --extra-cflags=ECFLAGS   add ECFLAGS to CFLAGS"
21 echo "  --extra-ldflags=ELDFLAGS add ELDFLAGS to LDFLAGS"
22 echo "  --host=HOST              build programs to run on HOST"
23 echo "  --cross-prefix=PREFIX    use PREFIX for compilation tools"
24 echo ""
25 exit 1
26 fi
27
28 cc_check() {
29     rm -f conftest.c
30     [ -n "$1" ] && echo "#include <$1>" > conftest.c
31     echo "int main () { $3 return 0; }" >> conftest.c
32     $CC conftest.c $CFLAGS $LDFLAGS $2 -o conftest 2>$DEVNULL
33 }
34
35 as_check() {
36     echo "$1" > conftest.asm
37     $AS conftest.asm $ASFLAGS $2 -o conftest.o 2>$DEVNULL
38 }
39
40 die() {
41     echo "$@"
42     exit 1
43 }
44
45 rm -f config.h config.mak x264.pc conftest*
46
47 prefix='/usr/local'
48 exec_prefix='${prefix}'
49 bindir='${exec_prefix}/bin'
50 libdir='${exec_prefix}/lib'
51 includedir='${prefix}/include'
52 DEVNULL='/dev/null'
53
54 avis_input="auto"
55 mp4_output="auto"
56 pthread="auto"
57 asm="yes"
58 debug="no"
59 gprof="no"
60 pic="no"
61 vis="no"
62 shared="no"
63
64 CFLAGS="$CFLAGS -Wall -I."
65 LDFLAGS="$LDFLAGS"
66 ASFLAGS="$ASFLAGS"
67 HAVE_GETOPT_LONG=1
68 cross_prefix=""
69
70 EXE=""
71
72 # parse options
73
74 for opt do
75     optarg="${opt#*=}"
76     case "$opt" in
77         --prefix=*)
78             prefix="$optarg"
79             ;;
80         --exec-prefix=*)
81             exec_prefix="$optarg"
82             ;;
83         --bindir=*)
84             bindir="$optarg"
85             ;;
86         --libdir=*)
87             libdir="$optarg"
88             ;;
89         --includedir=*)
90             includedir="$optarg"
91             ;;
92         --enable-asm)
93             asm="yes"
94             ;;
95         --disable-asm)
96             asm="no"
97             ;;
98         --enable-avis-input)
99             avis_input="yes"
100             ;;
101         --disable-avis-input)
102             avis_input="no"
103             ;;
104         --enable-mp4-output)
105             mp4_output="yes"
106             ;;
107         --disable-mp4-output)
108             mp4_output="no"
109             ;;
110         --extra-asflags=*)
111             ASFLAGS="$ASFLAGS ${opt#--extra-asflags=}"
112             ;;
113         --extra-cflags=*)
114             CFLAGS="$CFLAGS ${opt#--extra-cflags=}"
115             ;;
116         --extra-ldflags=*)
117             LDFLAGS="$LDFLAGS ${opt#--extra-ldflags=}"
118             ;;
119         --enable-pthread)
120             pthread="auto" # can't skip detection, since it differs by OS
121             ;;
122         --disable-pthread)
123             pthread="no"
124             ;;
125         --enable-debug)
126             debug="yes"
127             ;;
128         --enable-gprof)
129             CFLAGS="$CFLAGS -pg"
130             LDFLAGS="$LDFLAGS -pg"
131             gprof="yes"
132             ;;
133         --enable-pic)
134             pic="yes"
135             ;;
136         --enable-shared)
137             shared="yes"
138             ;;
139         --enable-visualize)
140             LDFLAGS="$LDFLAGS -L/usr/X11R6/lib -lX11"
141             CFLAGS="$CFLAGS -DVISUALIZE=1"
142             vis="yes"
143             ;;
144         --host=*)
145             host="${opt#--host=}"
146             ;;
147         --cross-prefix=*)
148             cross_prefix="${opt#--cross-prefix=}"
149             ;;
150         *)
151             echo "Unknown option $opt, ignored"
152             ;;
153     esac
154 done
155
156 CC="${CC-${cross_prefix}gcc}"
157 AR="${AR-${cross_prefix}ar}"
158 RANLIB="${RANLIB-${cross_prefix}ranlib}"
159 STRIP="${STRIP-${cross_prefix}strip}"
160 AS=""
161
162 if [ "x$host" = x ]; then
163     host=`./config.guess`
164 fi
165 # normalize a triplet into a quadruplet
166 host=`./config.sub $host`
167
168 # split $host
169 host_cpu="${host%%-*}"
170 host="${host#*-}"
171 host_vendor="${host%%-*}"
172 host_os="${host#*-}"
173
174 case $host_os in
175   beos*)
176     SYS="BEOS"
177     CFLAGS="$CFLAGS -DHAVE_MALLOC_H"
178     ;;
179   darwin*)
180     SYS="MACOSX"
181     CFLAGS="$CFLAGS -falign-loops=16"
182     LDFLAGS="$LDFLAGS -lm -lmx"
183     if [ "$pic" = "no" ]; then
184         CFLAGS="$CFLAGS -mdynamic-no-pic"
185     fi
186     ;;
187   freebsd*)
188     SYS="FREEBSD"
189     LDFLAGS="$LDFLAGS -lm"
190     ;;
191   kfreebsd*-gnu)
192     SYS="FREEBSD"
193     CFLAGS="$CFLAGS -DHAVE_MALLOC_H"
194     LDFLAGS="$LDFLAGS -lm"
195     ;;
196   netbsd*)
197     SYS="NETBSD"
198     LDFLAGS="$LDFLAGS -lm"
199     ;;
200   openbsd*)
201     SYS="OPENBSD"
202     CFLAGS="$CFLAGS -I/usr/X11R6/include"
203     LDFLAGS="$LDFLAGS -lm"
204     ;;
205   *linux*)
206     SYS="LINUX"
207     CFLAGS="$CFLAGS -DHAVE_MALLOC_H"
208     LDFLAGS="$LDFLAGS -lm"
209     ;;
210   cygwin*)
211     SYS="MINGW"
212     EXE=".exe"
213     DEVNULL="NUL"
214     if cc_check "" -mno-cygwin; then
215         CFLAGS="$CFLAGS -mno-cygwin"
216         LDFLAGS="$LDFLAGS -mno-cygwin"
217     fi
218     ;;
219   mingw*)
220     SYS="MINGW"
221     EXE=".exe"
222     DEVNULL="NUL"
223     ;;
224   sunos*|solaris*)
225     SYS="SunOS"
226     CFLAGS="$CFLAGS -DHAVE_MALLOC_H"
227     LDFLAGS="$LDFLAGS -lm"
228     HAVE_GETOPT_LONG=0
229     ;;
230   *)
231     die "Unknown system $host, edit the configure"
232     ;;
233 esac
234
235 case $host_cpu in
236   i*86)
237     ARCH="X86"
238     AS="yasm"
239     ASFLAGS="$ASFLAGS -O2"
240     if [[ "$asm" == yes && "$CFLAGS" != *-march* ]]; then
241       CFLAGS="$CFLAGS -march=i686"
242     fi
243     if [ "$SYS" = MACOSX ]; then
244       ASFLAGS="$ASFLAGS -f macho -DPREFIX"
245     elif [ "$SYS" = MINGW ]; then
246       ASFLAGS="$ASFLAGS -f win32 -DPREFIX"
247     else
248       ASFLAGS="$ASFLAGS -f elf"
249     fi
250     ;;
251   x86_64)
252     ARCH="X86_64"
253     AS="yasm"
254     if [ "$SYS" = MACOSX ];then
255       ASFLAGS="$ASFLAGS -f macho64 -m amd64 -DPIC -DPREFIX"
256       CFLAGS="$CFLAGS -arch x86_64"
257       LDFLAGS="$LDFLAGS -arch x86_64"
258     elif [ "$SYS" = MINGW ]; then
259       ASFLAGS="$ASFLAGS -f win32 -m amd64 -DPREFIX"
260     else
261       ASFLAGS="$ASFLAGS -f elf -m amd64"
262     fi
263     ;;
264   powerpc|powerpc64)
265     ARCH="PPC"
266     if [ $SYS = MACOSX ]
267     then
268       ALTIVECFLAGS="$ALTIVECFLAGS -faltivec -fastf -mcpu=G4"
269     else
270       ALTIVECFLAGS="$ALTIVECFLAGS -maltivec -mabi=altivec -DHAVE_ALTIVEC_H"
271     fi
272     ;;
273   sparc)
274     if test "$(uname -m)" = "sun4u"; then
275       ARCH="UltraSparc"
276       CFLAGS="$CFLAGS -mcpu=ultrasparc"
277       LDFLAGS="$LDFLAGS -mcpu=ultrasparc"
278       AS="${cross_prefix}as"
279       ASFLAGS="$ASFLAGS -xarch=v8plusa"
280     else
281       ARCH="Sparc"
282     fi
283     ;;
284   mips|mipsel|mips64|mips64el)
285     ARCH="MIPS"
286     ;;
287   arm*)
288     ARCH="ARM"
289     ;;
290   s390|s390x)
291     ARCH="S390"
292     ;;
293   parisc|parisc64)
294     ARCH="PARISC"
295     ;;
296   *)
297     ARCH="$(echo $host_cpu | tr a-z A-Z)"
298     ;;
299 esac
300
301 # check requirements
302
303 cc_check || die "No working C compiler found."
304
305 if [ $shared = yes -a \( $ARCH = "X86_64" -o $ARCH = "PPC" -o $ARCH = "ALPHA" \) ] ; then
306     pic="yes"
307 fi
308
309 if [ $asm = yes -a \( $ARCH = X86 -o $ARCH = X86_64 \) ] ; then
310     if ! as_check "pinsrd xmm0, [esp], 0" ; then
311         VER=`($AS --version || echo no assembler) 2>$DEVNULL | head -n 1`
312         echo "Found $VER"
313         echo "Minimum version is yasm-0.6.1"
314         echo "If you really want to compile without asm, configure with --disable-asm."
315         exit 1
316     fi
317     if ! cc_check '' '' 'asm("pabsw %xmm0, %xmm0");' ; then
318         VER=`(as --version || echo no gnu as) 2>$DEVNULL | head -n 1`
319         echo "Found $VER"
320         echo "Minimum version is binutils-2.17"
321         echo "Your compiler can't handle inline SSSE3 asm."
322         echo "If you really want to compile without asm, configure with --disable-asm."
323         exit 1
324     fi
325     CFLAGS="$CFLAGS -DHAVE_MMX"
326 fi
327 [ $asm = no ] && AS=""
328 [ "x$AS" = x ] && asm="no"
329
330 CFLAGS="$CFLAGS -DARCH_$ARCH -DSYS_$SYS"
331
332 echo "unsigned int endian = 'B' << 24 | 'I' << 16 | 'G' << 8 | 'E';" > conftest.c
333 $CC $CFLAGS conftest.c -c -o conftest.o 2>$DEVNULL || die "endian test failed"
334 grep -q BIGE conftest.o && CFLAGS="$CFLAGS -DWORDS_BIGENDIAN"
335
336 # autodetect options that weren't forced nor disabled
337
338 libpthread=""
339 if test "$pthread" = "auto" ; then
340     pthread="no"
341     case $SYS in
342         BEOS)
343             pthread="yes"
344             ;;
345         MINGW)
346             if cc_check pthread.h -lpthread "pthread_create(0,0,0,0);" ; then
347                 pthread="yes"
348                 libpthread="-lpthread"
349             elif cc_check pthread.h -lpthreadGC2 "pthread_create(0,0,0,0);" ; then
350                 pthread="yes"
351                 libpthread="-lpthreadGC2"
352             elif cc_check pthread.h "-lpthreadGC2 -lwsock32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then
353                 pthread="yes"
354                 libpthread="-lpthreadGC2 -lwsock32"
355                 CFLAGS="$CFLAGS -DPTW32_STATIC_LIB"
356             elif cc_check pthread.h "-lpthreadGC2 -lws2_32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then
357                 pthread="yes"
358                 libpthread="-lpthreadGC2 -lws2_32"
359                 CFLAGS="$CFLAGS -DPTW32_STATIC_LIB"
360             fi
361             ;;
362         OPENBSD)
363             cc_check pthread.h -pthread && pthread="yes" && libpthread="-pthread"
364             ;;
365         *)
366             cc_check pthread.h -lpthread && pthread="yes" && libpthread="-lpthread"
367             ;;
368     esac
369 fi
370 if test "$pthread" = "yes" ; then
371     CFLAGS="$CFLAGS -DHAVE_PTHREAD"
372     LDFLAGS="$LDFLAGS $libpthread"
373 fi
374
375 MP4_LDFLAGS="-lgpac_static"
376 if [ $SYS = MINGW ]; then
377     MP4_LDFLAGS="$MP4_LDFLAGS -lwinmm"
378 fi
379 if [ "$mp4_output" = "auto" ] ; then
380     mp4_output="no"
381     cc_check gpac/isomedia.h "$MP4_LDFLAGS" && mp4_output="yes"
382 fi
383 if [ "$mp4_output" = "yes" ] ; then
384     echo "#define MP4_OUTPUT" >> config.h
385     LDFLAGS="$LDFLAGS $MP4_LDFLAGS"
386 fi
387
388 if [ "$avis_input" = "auto" ] ; then
389     if [ $SYS = MINGW ]; then
390         avis_input="yes"
391     else
392         avis_input="no";
393     fi
394 fi
395 if [ "$avis_input" = "yes" ] ; then
396     if cc_check "stdlib.h" -lvfw32 ; then
397         echo "#define AVIS_INPUT" >> config.h
398         LDFLAGS="$LDFLAGS -lvfw32"
399     elif cc_check "stdlib.h" -lavifil32 ; then
400         echo "#define AVIS_INPUT" >> config.h
401         LDFLAGS="$LDFLAGS -lavifil32"
402     else
403         avis_input="no";
404     fi
405 fi
406
407 if [ "$pic" = "yes" ] ; then
408     CFLAGS="$CFLAGS -fPIC"
409     ASFLAGS="$ASFLAGS -DPIC"
410     # resolve textrels in the x86 asm
411     cc_check stdio.h -Wl,-Bsymbolic && LDFLAGS="$LDFLAGS -Wl,-Bsymbolic"
412 fi
413
414 if [ "$debug" != "yes" -a "$gprof" != "yes" ]; then
415     CFLAGS="$CFLAGS -s -fomit-frame-pointer"
416     LDFLAGS="$LDFLAGS -s"
417 fi
418
419 if [ "$debug" = "yes" ]; then
420     CFLAGS="-O1 -g $CFLAGS"
421 else
422     CFLAGS="-O4 -ffast-math $CFLAGS"
423 fi
424
425 if cc_check "stdio.h" "" "fseeko(stdin,0,0);" ; then
426     echo "#define fseek fseeko" >> config.h
427     echo "#define ftell ftello" >> config.h
428 elif cc_check "stdio.h" "" "fseeko64(stdin,0,0);" ; then
429     echo "#define fseek fseeko64" >> config.h
430     echo "#define ftell ftello64" >> config.h
431 fi
432
433 rm -f conftest*
434
435 # generate config files
436
437 cat > config.mak << EOF
438 prefix=$prefix
439 exec_prefix=$exec_prefix
440 bindir=$bindir
441 libdir=$libdir
442 includedir=$includedir
443 ARCH=$ARCH
444 SYS=$SYS
445 CC=$CC
446 CFLAGS=$CFLAGS
447 ALTIVECFLAGS=$ALTIVECFLAGS
448 LDFLAGS=$LDFLAGS
449 AR=$AR
450 RANLIB=$RANLIB
451 STRIP=$STRIP
452 AS=$AS
453 ASFLAGS=$ASFLAGS
454 EXE=$EXE
455 VIS=$vis
456 HAVE_GETOPT_LONG=$HAVE_GETOPT_LONG
457 DEVNULL=$DEVNULL
458 EOF
459
460 if [ "$shared" = "yes" ]; then
461     API=$(grep '#define X264_BUILD' < x264.h | cut -f 3 -d ' ')
462     if [ "$SYS" = "MINGW" ]; then
463         echo "SONAME=libx264-$API.dll" >> config.mak
464         echo 'IMPLIBNAME=libx264.dll.a' >> config.mak
465         echo 'SOFLAGS=-Wl,--out-implib,$(IMPLIBNAME) -Wl,--enable-auto-image-base' >> config.mak
466     elif [ "$SYS" = "MACOSX" ]; then
467         echo "SOSUFFIX=dylib" >> config.mak
468         echo "SONAME=libx264.$API.dylib" >> config.mak
469         echo 'SOFLAGS=-dynamiclib -Wl,-single_module -Wl,-read_only_relocs,suppress -install_name $(DESTDIR)$(libdir)/$(SONAME)' >> config.mak
470     elif [ "$SYS" = "SunOS" ]; then
471         echo "SOSUFFIX=so" >> config.mak
472         echo "SONAME=libx264.so.$API" >> config.mak
473         echo 'SOFLAGS=-Wl,-h,$(SONAME)' >> config.mak
474     else
475         echo "SOSUFFIX=so" >> config.mak
476         echo "SONAME=libx264.so.$API" >> config.mak
477         echo 'SOFLAGS=-Wl,-soname,$(SONAME)' >> config.mak
478     fi
479     echo 'default: $(SONAME)' >> config.mak
480 fi
481
482 ./version.sh
483
484 pclibs="-L$libdir -lx264 $libpthread"
485
486 cat > x264.pc << EOF
487 prefix=$prefix
488 exec_prefix=$exec_prefix
489 libdir=$libdir
490 includedir=$includedir
491
492 Name: x264
493 Description: H.264 (MPEG4 AVC) encoder library
494 Version: $(grep POINTVER < config.h | sed -e 's/.* "//; s/".*//')
495 Libs: $pclibs
496 Cflags: -I$includedir
497 EOF
498
499
500 echo "Platform:   $ARCH"
501 echo "System:     $SYS"
502 echo "asm:        $asm"
503 echo "avis input: $avis_input"
504 echo "mp4 output: $mp4_output"
505 echo "pthread:    $pthread"
506 echo "debug:      $debug"
507 echo "gprof:      $gprof"
508 echo "PIC:        $pic"
509 echo "shared:     $shared"
510 echo "visualize:  $vis"
511 echo
512 echo "You can run 'make' or 'make fprofiled' now."
513