configure: Detect the toolchain's sdl-config tool
[pcsx_rearmed.git] / configure
1 #!/bin/sh
2 # some elements originated from qemu configure
3 set -e
4
5 TMPC="/tmp/pcsx-conf-${RANDOM}-$$-${RANDOM}.c"
6 TMPO="/tmp/pcsx-conf-${RANDOM}-$$-${RANDOM}.o"
7 TMPB="/tmp/pcsx-conf-${RANDOM}-$$-${RANDOM}"
8 trap "rm -f $TMPC $TMPO $TMPB" EXIT INT QUIT TERM
9 rm -f config.log
10
11 compile_object()
12 {
13   c="$CC $CFLAGS -c $TMPC -o $TMPO $@"
14   echo $c >> config.log
15   $c >> config.log 2>&1
16 }
17
18 compile_binary()
19 {
20   c="$CC $CFLAGS $TMPC -o $TMPB $LDFLAGS $MAIN_LDLIBS $@"
21   echo $c >> config.log
22   $c >> config.log 2>&1
23 }
24
25 check_define()
26 {
27   $CC -E -dD $CFLAGS include/arm_features.h | grep -q "$1" || return 1
28   return 0
29 }
30
31 check_define_val()
32 {
33   $CC -E -dD $CFLAGS include/arm_features.h | grep "$1" | awk '{print $3}'
34   return 0
35 }
36
37 # setting options to "yes" or "no" will make that choice default,
38 # "" means "autodetect".
39
40 platform_list="generic pandora maemo caanoo libretro"
41 platform="generic"
42 builtin_gpu_list="peops unai neon senquack"
43 builtin_gpu=""
44 sound_driver_list="oss alsa pulseaudio sdl libretro"
45 sound_drivers=""
46 plugins="plugins/spunull/spunull.so \
47 plugins/dfxvideo/gpu_peops.so plugins/gpu_unai/gpu_unai.so plugins/gpu_senquack/gpu_senquack.so"
48 drc_cache_base="no"
49 have_armv5=""
50 have_armv6=""
51 have_armv7=""
52 have_arm_neon=""
53 have_tslib=""
54 have_gles=""
55 have_c64x_dsp=""
56 enable_dynarec="yes"
57 need_sdl="no"
58 need_xlib="no"
59 need_libpicofe="yes"
60 need_warm="no"
61 CFLAGS_GLES=""
62 LDLIBS_GLES=""
63 # these are for known platforms
64 optimize_cortexa8="no"
65 optimize_arm926ej="no"
66
67 # hardcoded stuff
68 CC="${CC-${CROSS_COMPILE}gcc}"
69 CXX="${CXX-${CROSS_COMPILE}g++}"
70 AS="${AS-${CROSS_COMPILE}as}"
71 AR="${AS-${CROSS_COMPILE}ar}"
72 MAIN_LDLIBS="$LDLIBS -ldl -lm -lpthread"
73 config_mak="config.mak"
74
75 SYSROOT="$(${CC} --print-sysroot)"
76 [ "x${SDL_CONFIG}" = "x" ] && SDL_CONFIG="${SYSROOT}/usr/bin/sdl-config"
77
78 fail()
79 {
80   echo "$@"
81   if test -n "$DUMP_CONFIG_LOG"; then cat config.log; fi
82   exit 1
83 }
84
85 # call during arg parsing, so that cmd line can override platform defaults
86 set_platform()
87 {
88   platform=$1
89   case "$platform" in
90   generic)
91     ;;
92   pandora)
93     sound_drivers="oss alsa"
94     drc_cache_base="yes"
95     optimize_cortexa8="yes"
96     have_arm_neon="yes"
97     need_xlib="yes"
98     ;;
99   maemo)
100     drc_cache_base="yes"
101     optimize_cortexa8="yes"
102     have_arm_neon="yes"
103     ;;
104   caanoo)
105     sound_drivers="oss"
106     drc_cache_base="yes"
107     optimize_arm926ej="yes"
108     need_warm="yes"
109     ;;
110   libretro)
111     sound_drivers="libretro"
112     need_libpicofe="no"
113     ;;
114   *)
115     fail "unsupported platform: $platform"
116     ;;
117   esac
118 }
119
120 for opt do
121   optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'` || true
122   case "$opt" in
123   --help|-h) show_help="yes"
124   ;;
125   --platform=*) set_platform "$optarg"
126   ;;
127   --gpu=*) builtin_gpu="$optarg"
128   ;;
129   --sound-drivers=*) sound_drivers="$optarg"
130   ;;
131   --enable-neon) have_arm_neon="yes"
132   ;;
133   --disable-neon) have_arm_neon="no"
134   ;;
135   --disable-dynarec) enable_dynarec="no"
136   ;;
137   *) echo "ERROR: unknown option $opt"; show_help="yes"
138   ;;
139   esac
140 done
141
142 if [ "$show_help" = "yes" ]; then
143   echo "options:"
144   echo "  --help                   print this message"
145   echo "  --platform=NAME          target platform [$platform]"
146   echo "                           available: $platform_list"
147   echo "  --gpu=NAME               builtin gpu plugin [guessed]"
148   echo "                           available: $builtin_gpu_list"
149   echo "  --sound-drivers=LIST     sound output drivers [guessed]"
150   echo "                           available: $sound_driver_list"
151   echo "  --enable-neon"
152   echo "  --disable-neon           enable/disable ARM NEON optimizations [guessed]"
153   echo "  --disable-dynarec        disable dynamic recompiler"
154   echo "                           (dynarec is only available and enabled on ARM)"
155   echo "  --disable-icache-emu     Disables the instruction cache emulation"
156   echo "influential environment variables:"
157   echo "  CROSS_COMPILE CC CXX AS AR CFLAGS ASFLAGS LDFLAGS LDLIBS"
158   exit 1
159 fi
160
161 # validate selections
162 if [ "x$builtin_gpu" != "x" ]; then
163   if ! echo $builtin_gpu_list | grep -q "\<$builtin_gpu\>"; then
164     fail "unsupported builtin gpu plugin: $builtin_gpu"
165   fi
166 fi
167
168 if [ "x$sound_drivers" != "x" ]; then
169   for d in $sound_drivers; do
170     if ! echo $sound_driver_list | grep -q "\<$d\>"; then
171       fail "unsupported sound driver: $sound_driver"
172     fi
173   done
174 fi
175
176 if [ "$need_libpicofe" = "yes" ]; then
177   if ! test -f "frontend/libpicofe/README"; then
178     fail "libpicofe is missing, please run 'git submodule init && git submodule update'"
179   fi
180 fi
181
182 if [ "$need_warm" = "yes" ]; then
183   if ! test -f "frontend/warm/README"; then
184     fail "wARM is missing, please run 'git submodule init && git submodule update'"
185   fi
186 fi
187
188 # basic compiler test
189 cat > $TMPC <<EOF
190 #include <zlib.h>
191 int main(void) { return 0; }
192 EOF
193 if ! compile_binary; then
194   fail "compiler test failed, please check config.log"
195 fi
196
197 if [ -z "$ARCH" ]; then
198   ARCH=`$CC -dumpmachine | awk -F '-' '{print $1}'`
199 fi
200
201 case "$ARCH" in
202 arm*)
203   # ARM stuff
204   ARCH="arm"
205
206   if [ "$optimize_cortexa8" = "yes" ]; then
207     CFLAGS="$CFLAGS -mcpu=cortex-a8 -mtune=cortex-a8"
208     ASFLAGS="$ASFLAGS -mcpu=cortex-a8"
209   fi
210   if [ "$optimize_arm926ej" = "yes" ]; then
211     CFLAGS="$CFLAGS -mcpu=arm926ej-s -mtune=arm926ej-s"
212     ASFLAGS="$ASFLAGS -mcpu=arm926ej-s -mfloat-abi=softfp"
213   fi
214
215   if [ "x$have_arm_neon" = "x" ]; then
216     # detect NEON from user-supplied cflags to enable asm code
217     have_arm_neon=`check_define __ARM_NEON__ && echo yes` || true
218   fi
219   if [ "x$have_armv7" = "x" ]; then
220     if check_define HAVE_ARMV7; then
221       have_armv7="yes"
222       have_armv6="yes"
223       have_armv5="yes"
224     fi
225   fi
226   if [ "x$have_armv6" = "x" ]; then
227     if check_define HAVE_ARMV6; then
228       have_armv6="yes"
229       have_armv5="yes"
230     fi
231   fi
232   if [ "x$have_armv5" = "x" ]; then
233     have_armv5=`check_define HAVE_ARMV5 && echo yes` || true
234   fi
235
236   if [ "x$builtin_gpu" = "x" ]; then
237     if [ "$have_arm_neon" = "yes" ]; then
238       builtin_gpu="neon"
239     elif [ "$have_armv7" != "yes" ]; then
240       # pre-ARMv7 hardware is usually not fast enough for peops
241       builtin_gpu="unai"
242     else
243       builtin_gpu="peops"
244     fi
245   fi
246
247   # automatically set mfpu and mfloat-abi if they are not set
248   if [ "$have_arm_neon" = "yes" ]; then
249     fpu="neon"
250   elif [ "$have_armv6" = "yes" ]; then
251     fpu="vfp"
252   fi
253   if [ "x$fpu" != "x" ]; then
254     echo "$CFLAGS" | grep -q -- '-mfpu=' || CFLAGS="$CFLAGS -mfpu=$fpu"
255     echo "$ASFLAGS" | grep -q -- '-mfpu=' || ASFLAGS="$ASFLAGS -mfpu=$fpu"
256     floatabi_set_by_gcc=`$CC -v 2>&1 | grep -q -- --with-float= && echo yes` || true
257     if [ "$floatabi_set_by_gcc" != "yes" ]; then
258       echo "$CFLAGS" | grep -q -- '-mfloat-abi=' || CFLAGS="$CFLAGS -mfloat-abi=softfp"
259       echo "$ASFLAGS" | grep -q -- '-mfloat-abi=' || ASFLAGS="$ASFLAGS -mfloat-abi=softfp"
260     fi
261   fi
262
263   # must disable thumb as recompiler can't handle it
264   if check_define __thumb__; then
265     CFLAGS="$CFLAGS -marm"
266   fi
267
268   # warn about common mistakes
269   if [ "$have_armv5" != "yes" ]; then
270     if ! echo "$CFLAGS" | grep -q -- '-mcpu=\|-march='; then
271       echo "Warning: compiling for ARMv4, is that really what you want?"
272       echo "You probably should specify -mcpu= or -march= like this:"
273       echo "  CFLAGS=-march=armv6 ./configure ..."
274     fi
275   fi
276   if [ "$have_arm_neon" = "yes" -a "$have_armv7" != "yes" ]; then
277     echo "Warning: compiling for NEON, but not ARMv7?"
278     echo "You probably want to specify -mcpu= or -march= like this:"
279     echo "  CFLAGS=-march=armv7-a ./configure ..."
280   fi
281   ;;
282 aarch64)
283   ;;
284 *)
285   # dynarec only available on ARM
286   enable_dynarec="no"
287   ;;
288 esac
289
290 if [ "x$builtin_gpu" = "x" ]; then
291   builtin_gpu="peops"
292 fi
293
294 # supposedly we can avoid -fPIC on armv5 for slightly better performace?
295 if [ "$ARCH" != "arm" -o "$have_armv6" = "yes" ]; then
296   PLUGIN_CFLAGS="$PLUGIN_CFLAGS -fPIC"
297 fi
298
299 case "$platform" in
300 generic)
301   need_sdl="yes"
302   ;;
303 maemo)
304   CFLAGS="$CFLAGS -DMAEMO -DMAEMO_CHANGES"
305   ;;
306 libretro)
307   CFLAGS="$CFLAGS -fPIC"
308   MAIN_LDFLAGS="$MAIN_LDFLAGS -shared -Wl,--no-undefined"
309   ;;
310 esac
311
312 # header/library presence tests
313 check_zlib()
314 {
315   cat > $TMPC <<EOF
316   #include <zlib.h>
317   int main(void) { uncompress(0, 0, 0, 0); }
318 EOF
319   compile_binary
320 }
321
322 check_libpng()
323 {
324   cat > $TMPC <<EOF
325   #include <png.h>
326   void main() { png_init_io(0, 0); }
327 EOF
328   compile_binary
329 }
330
331 check_oss()
332 {
333   cat > $TMPC <<EOF
334   #include <sys/soundcard.h>
335   #include <sys/ioctl.h>
336   void main() { int a=0; ioctl(0, SNDCTL_DSP_SETFMT, &a); }
337 EOF
338   compile_binary
339 }
340
341 check_alsa()
342 {
343   cat > $TMPC <<EOF
344   #include <alsa/asoundlib.h>
345   void main() { snd_pcm_open(0, 0, 0, 0); }
346 EOF
347   compile_binary "$@"
348 }
349
350 check_pulseaudio()
351 {
352   cat > $TMPC <<EOF
353   #include <pulse/pulseaudio.h>
354   void main() { pa_threaded_mainloop_new(); }
355 EOF
356   compile_binary "$@"
357 }
358
359 check_sdl()
360 {
361   cat > $TMPC <<EOF
362   #include <SDL.h>
363   void main() { SDL_OpenAudio(0, 0); }
364 EOF
365   compile_binary "$@"
366 }
367
368 check_xlib_headers()
369 {
370   cat > $TMPC <<EOF
371   #include <X11/Xlib.h>
372   void *f() { return XOpenDisplay(0); }
373 EOF
374   compile_object "$@"
375 }
376
377 # see if we have c64_tools for TI C64x DSP
378 check_c64_tools()
379 {
380   cat > $TMPC <<EOF
381   #include <inc_libc64_mini.h>
382   int f() { return dsp_open(); }
383 EOF
384   compile_object "$@"
385 }
386
387 MAIN_LDLIBS="$MAIN_LDLIBS -lz"
388 check_zlib || fail "please install zlib (libz-dev)"
389
390 MAIN_LDLIBS="-lpng $MAIN_LDLIBS"
391 check_libpng || fail "please install libpng (libpng-dev)"
392
393 # find what audio support we can compile
394 if [ "x$sound_drivers" = "x" ]; then
395   if check_oss; then sound_drivers="$sound_drivers oss"; fi
396   if check_alsa -lasound; then
397     sound_drivers="$sound_drivers alsa"
398     MAIN_LDLIBS="-lasound $MAIN_LDLIBS"
399   fi
400   if check_pulseaudio -lpulse; then
401     sound_drivers="$sound_drivers pulseaudio"
402     MAIN_LDLIBS="-lpulse $MAIN_LDLIBS"
403   fi
404   if [ "$need_sdl" = "yes" ] || check_sdl `${SDL_CONFIG} --cflags --libs`; then
405     sound_drivers="$sound_drivers sdl"
406     need_sdl="yes"
407   fi
408 else
409   if echo $sound_drivers | grep -q "\<oss\>"; then
410     check_oss || fail "oss support is missing"
411   fi
412   if echo $sound_drivers | grep -q "\<alsa\>"; then
413     MAIN_LDLIBS="-lasound $MAIN_LDLIBS"
414     check_alsa || fail "please install libasound2-dev"
415   fi
416   if echo $sound_drivers | grep -q "\<pulseaudio\>"; then
417     MAIN_LDLIBS="-lpulse $MAIN_LDLIBS"
418     check_pulseaudio || fail "pulseaudio support is missing"
419   fi
420 fi
421
422 if [ "$need_sdl" = "yes" ]; then
423   which ${SDL_CONFIG} > /dev/null || \
424     fail "sdl-config is missing; please install libsdl (libsdl1.2-dev)"
425   CFLAGS="$CFLAGS `${SDL_CONFIG} --cflags`"
426   MAIN_LDLIBS="`${SDL_CONFIG} --libs` $MAIN_LDLIBS"
427   check_sdl || fail "please install libsdl (libsdl1.2-dev)"
428 fi
429
430 # check for tslib (only headers needed)
431 if [ "x$have_tslib" = "x" ]; then
432   cat > $TMPC <<EOF
433   #include <tslib.h>
434   void test(struct ts_sample sample) {}
435 EOF
436   if compile_object; then
437     have_tslib="yes"
438   else
439     have_tslib="no"
440   fi
441 fi
442
443 # check for VideoCore stuff for Raspberry Pi
444 if [ -d /opt/vc/include -a -d /opt/vc/lib -a "$VIDEOCORE" != "no" ]; then
445   CFLAGS_GLES="$CFLAGS_GLES -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux"
446   LDLIBS_GLES="$LDLIBS_GLES -L/opt/vc/lib"
447   if [ -f /opt/vc/lib/libbcm_host.so ]; then
448     LDLIBS_GLES="$LDLIBS_GLES -lbcm_host"
449   fi
450   need_xlib="yes"
451   VIDEOCORE="yes"
452 fi
453
454 # check for GLES headers
455 cat > $TMPC <<EOF
456 #include <GLES/gl.h>
457 #include <EGL/egl.h>
458 int main(void) {
459   return (int)eglGetDisplay( (EGLNativeDisplayType)0 );
460 }
461 EOF
462 if [ "$VIDEOCORE" = "yes" ] && compile_binary $CFLAGS_GLES -lbrcmEGL -lbrcmGLESv2 $LDLIBS_GLES; then
463   have_gles="yes"
464   LDLIBS_GLES="-lbrcmEGL -lbrcmGLESv2 $LDLIBS_GLES"
465 elif compile_binary $CFLAGS_GLES -lEGL -lGLES_CM $LDLIBS_GLES; then
466   have_gles="yes"
467   LDLIBS_GLES="-lEGL -lGLES_CM $LDLIBS_GLES"
468 elif compile_binary $CFLAGS_GLES -lEGL -lGLESv1_CM $LDLIBS_GLES; then
469   have_gles="yes"
470   LDLIBS_GLES="-lEGL -lGLESv1_CM $LDLIBS_GLES"
471 fi
472
473 if check_c64_tools; then
474   have_c64x_dsp="yes"
475 fi
476
477 if [ "$have_gles" = "yes" ]; then
478   plugins="$plugins plugins/gpu-gles/gpu_gles.so"
479 fi
480 if [ "$have_arm_neon" = "yes" -a "$builtin_gpu" != "neon" ]; then
481   plugins="$plugins plugins/gpu_neon/gpu_neon.so"
482 fi
483
484 # check for xlib (only headers needed)
485 if [ "x$need_xlib" = "xyes" ]; then
486   check_xlib_headers || fail "please install libx11-dev"
487 fi
488
489 sizeof_long=`check_define_val __SIZEOF_LONG__`
490 if [ "x$sizeof_long" = "x4" ]; then
491   CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64"
492 fi
493
494 cat > $TMPC <<EOF
495 void test(void *f, void *d) { fread(d, 1, 1, f); }
496 EOF
497 if compile_object -Wno-unused-result; then
498   CFLAGS="$CFLAGS -Wno-unused-result"
499 fi
500
501 # short plugin list for display
502 for p in $plugins; do
503   p1=`basename $p`
504   plugins_short="$p1 $plugins_short"
505 done
506
507 # set things that failed to autodetect to "no"
508 test "x$have_armv6" != "x" || have_armv6="no"
509 test "x$have_armv7" != "x" || have_armv7="no"
510 test "x$have_arm_neon" != "x" || have_arm_neon="no"
511 test "x$have_gles" != "x" || have_gles="no"
512 test "x$have_c64x_dsp" != "x" || have_c64x_dsp="no"
513
514 echo "architecture        $ARCH"
515 echo "platform            $platform"
516 echo "built-in GPU        $builtin_gpu"
517 echo "sound drivers       $sound_drivers"
518 echo "plugins             $plugins_short"
519 echo "C compiler          $CC"
520 echo "C compiler flags    $CFLAGS"
521 echo "libraries           $MAIN_LDLIBS"
522 echo "linker flags        $LDFLAGS$MAIN_LDFLAGS"
523 echo "enable dynarec      $enable_dynarec"
524 if [ "$ARCH" = "arm" ]; then
525   echo "ARMv7 optimizations $have_armv7"
526   echo "enable ARM NEON     $have_arm_neon"
527   echo "TI C64x DSP support $have_c64x_dsp"
528 fi
529 echo "tslib support       $have_tslib"
530 if [ "$platform" = "generic" ]; then
531   echo "OpenGL ES output    $have_gles"
532 fi
533
534 echo "# Automatically generated by configure" > $config_mak
535 printf "# Configured with:" >> $config_mak
536 printf " '%s'" "$0" "$@" >> $config_mak
537 echo >> $config_mak
538
539 echo "CC = $CC" >> $config_mak
540 echo "CXX = $CXX" >> $config_mak
541 echo "AS = $AS" >> $config_mak
542 echo "CFLAGS += $CFLAGS" >> $config_mak
543 echo "ASFLAGS += $ASFLAGS" >> $config_mak
544 echo "LDFLAGS += $LDFLAGS" >> $config_mak
545 echo "MAIN_LDFLAGS += $MAIN_LDFLAGS" >> $config_mak
546 echo "MAIN_LDLIBS += $MAIN_LDLIBS" >> $config_mak
547 echo "PLUGIN_CFLAGS += $PLUGIN_CFLAGS" >> $config_mak
548 echo >> $config_mak
549
550 if [ "$platform" = "libretro" ]; then
551   echo "TARGET = libretro.so" >> $config_mak
552 fi
553 echo "ARCH = $ARCH" >> $config_mak
554 echo "PLATFORM = $platform" >> $config_mak
555 echo "BUILTIN_GPU = $builtin_gpu" >> $config_mak
556 echo "SOUND_DRIVERS = $sound_drivers" >> $config_mak
557 echo "PLUGINS = $plugins" >> $config_mak
558 if [ "$have_arm_neon" = "yes" ]; then
559   echo "HAVE_NEON = 1" >> $config_mak
560 fi
561 if [ "$have_tslib" = "yes" ]; then
562   echo "HAVE_TSLIB = 1" >> $config_mak
563 fi
564 if [ "$have_gles" = "yes" ]; then
565   echo "HAVE_GLES = 1" >> $config_mak
566   echo "CFLAGS_GLES = $CFLAGS_GLES" >> $config_mak
567   echo "LDLIBS_GLES = $LDLIBS_GLES" >> $config_mak
568 fi
569 if [ "$enable_dynarec" = "yes" ]; then
570   echo "USE_DYNAREC = 1" >> $config_mak
571 fi
572 if [ "$drc_cache_base" = "yes" ]; then
573   echo "BASE_ADDR_DYNAMIC = 1" >> $config_mak
574 fi
575 if [ "$have_c64x_dsp" = "yes" ]; then
576   echo "HAVE_C64_TOOLS = 1" >> $config_mak
577 fi
578
579 # use pandora's skin (for now)
580 test -e skin || ln -s frontend/pandora/skin skin
581
582 # vim:shiftwidth=2:expandtab