1#!/bin/sh
2# some elements originated from qemu configure
3set -e
4
5TMPC="/tmp/picodrive-conf-${RANDOM}-$$-${RANDOM}.c"
6TMPO="/tmp/picodrive-conf-${RANDOM}-$$-${RANDOM}.o"
7TMPB="/tmp/picodrive-conf-${RANDOM}-$$-${RANDOM}"
8trap "rm -f $TMPC $TMPO $TMPB" EXIT INT QUIT TERM
9rm -f config.log
10
11compile_object()
12{
13  c="$CC $CFLAGS -c $TMPC -o $TMPO $@"
14  echo $c >> config.log
15  $c >> config.log 2>&1
16}
17
18compile_binary()
19{
20  c="$CC $CFLAGS $TMPC -o $TMPB $LDFLAGS $@"
21  echo $c >> config.log
22  $c >> config.log 2>&1
23}
24
25check_option()
26{
27  echo 'void test(void) { }' >$TMPC
28  compile_object $1 || return 1
29  return 0
30}
31
32check_define()
33{
34  $CC -E -dD $CFLAGS pico/arm_features.h | grep -q $1 || return 1
35  return 0
36}
37
38# setting options to "yes" or "no" will make that choice default,
39# "" means "autodetect".
40
41platform_list="generic pandora gp2x wiz caanoo opendingux gcw0 rg350 rpi1 rpi2"
42platform="generic"
43sound_driver_list="oss alsa sdl"
44sound_drivers=""
45have_armv5=""
46have_armv6=""
47have_armv7=""
48have_arm_oabi=""
49have_arm_neon=""
50have_libavcodec=""
51need_sdl="no"
52need_zlib="no"
53# these are for known platforms
54optimize_cortexa8="no"
55optimize_cortexa7="no"
56optimize_arm1176jzf="no"
57optimize_arm926ej="no"
58optimize_arm920="no"
59
60# hardcoded stuff
61CC="${CC-${CROSS_COMPILE}gcc}"
62CXX="${CXX-${CROSS_COMPILE}g++}"
63AS="${AS-${CROSS_COMPILE}as}"
64STRIP="${STRIP-${CROSS_COMPILE}strip}"
65SYSROOT=`$CC $CFLAGS $LDFLAGS --print-sysroot 2> /dev/null || true`
66test -n "$SDL_CONFIG" || SDL_CONFIG="$(ls $SYSROOT/*bin*/sdl-config 2>/dev/null | grep /bin/sdl-config | head -n 1)"
67test -n "$SDL_CONFIG" || SDL_CONFIG="$(ls $SYSROOT/*/*bin*/sdl-config 2>/dev/null | grep /bin/sdl-config | head -n 1)"
68#test -n "$SDL_CONFIG" || SDL_CONFIG="$(ls $SYSROOT/*bin*/sdl2-config 2>/dev/null | grep /bin/sdl2-config | head -n 1)"
69#test -n "$SDL_CONFIG" || SDL_CONFIG="$(ls $SYSROOT/*/*bin*/sdl2-config 2>/dev/null | grep /bin/sdl2-config | head -n 1)"
70SDLVERSION=sdl && echo $SDL_CONFIG | grep -q sdl2 && SDLVERSION=sdl2
71MAIN_LDLIBS="$LDLIBS -lm"
72config_mak="config.mak"
73
74fail()
75{
76  echo "$@"
77  exit 1
78}
79
80# call during arg parsing, so that cmd line can override platform defaults
81set_platform()
82{
83  platform=$1
84  case "$platform" in
85  rpi1)
86    optimize_arm1176jzf="yes"
87    ;;
88  rpi2)
89    optimize_cortexa7="yes"
90    have_arm_neon="yes"
91    ;;
92  generic)
93    ;;
94  opendingux | gcw0 | rg350)
95    sound_drivers="sdl"
96    # both are really an opendingux
97    CFLAGS="$CFLAGS -D__`echo $platform | tr '[a-z]' '[A-Z]'`__"
98    platform="opendingux"
99    ;;
100  pandora)
101    sound_drivers="oss alsa"
102    optimize_cortexa8="yes"
103    have_arm_neon="yes"
104    ;;
105  gp2x | wiz | caanoo)
106    sound_drivers="oss"
107    optimize_arm920="yes"
108    # compile for OABI if toolchain provides it (faster code on caanoo)
109    have_arm_oabi="yes"
110    # always use static linking, since caanoo doesn't have OABI libs. Moreover,
111    # dynamic linking slows Wiz 1-10%, and libm on F100 isn't compatible
112    LDFLAGS="$LDFLAGS -static"
113    # unified binary for all of them
114    CFLAGS="$CFLAGS -D__GP2X__"
115    platform="gp2x"
116    ;;
117  *)
118    fail "unsupported platform: $platform"
119    ;;
120  esac
121}
122
123for opt do
124  optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'` || true
125  case "$opt" in
126  --help|-h) show_help="yes"
127  ;;
128  --platform=*) set_platform "$optarg"
129  ;;
130  --sound-drivers=*) sound_drivers="$optarg"
131  ;;
132  *) echo "ERROR: unknown option $opt"; show_help="yes"
133  ;;
134  esac
135done
136
137if [ "$show_help" = "yes" ]; then
138  echo "options:"
139  echo "  --help                   print this message"
140  echo "  --platform=NAME          target platform [$platform]"
141  echo "                           available: $platform_list"
142  echo "  --sound-drivers=LIST     sound output drivers [guessed]"
143  echo "                           available: $sound_driver_list"
144  echo "influential environment variables:"
145  echo "  CROSS_COMPILE CC CXX AS STRIP CFLAGS ASFLAGS LDFLAGS LDLIBS"
146  exit 1
147fi
148
149# validate selections
150if [ "x$sound_drivers" != "x" ]; then
151  for d in $sound_drivers; do
152    if ! echo $sound_driver_list | grep -q "\<$d\>"; then
153      fail "unsupported sound driver: $sound_driver"
154    fi
155  done
156fi
157
158if ! test -f "platform/libpicofe/README"; then
159  fail "libpicofe is missing, please run 'git submodule update --init'"
160fi
161
162#if [ "$need_warm" = "yes" ]; then
163#  if ! test -f "frontend/warm/README"; then
164#    fail "wARM is missing, please run 'git submodule init && git submodule update'"
165#  fi
166#fi
167
168if [ -z "$ARCH" ]; then
169  ARCH=`$CC -dumpmachine | awk -F '-' '{print $1}'`
170fi
171
172# CPU/ABI stuff first, else compile test may fail
173case "$ARCH" in
174arm*)
175  # ARM stuff
176  ARCH="arm"
177
178  if [ "$optimize_cortexa8" = "yes" ]; then
179    CFLAGS="$CFLAGS -mcpu=cortex-a8 -mtune=cortex-a8"
180    ASFLAGS="$ASFLAGS -mcpu=cortex-a8"
181  fi
182  if [ "$optimize_cortexa7" = "yes" ]; then
183    CFLAGS="$CFLAGS -mcpu=cortex-a7"
184    ASFLAGS="$ASFLAGS -mcpu=cortex-a7"
185  fi
186  if [ "$optimize_arm1176jzf" = "yes" ]; then
187    CFLAGS="$CFLAGS -mcpu=arm1176jzf-s -mfloat-abi=hard"
188    ASFLAGS="$ASFLAGS -mcpu=arm1176jzf-s -mfloat-abi=hard"
189  fi
190  if [ "$optimize_arm926ej" = "yes" ]; then
191    CFLAGS="$CFLAGS -mcpu=arm926ej-s -mtune=arm926ej-s"
192    ASFLAGS="$ASFLAGS -mcpu=arm926ej-s -mfloat-abi=softfp"
193  fi
194  if [ "$optimize_arm920" = "yes" ]; then
195    CFLAGS="$CFLAGS -mcpu=arm920t -mtune=arm920t"
196    ASFLAGS="$ASFLAGS -mcpu=arm920t -mfloat-abi=soft"
197  fi
198
199  if [ "x$have_arm_neon" = "x" ]; then
200    # detect NEON from user-supplied cflags to enable asm code
201    have_arm_neon=`check_define __ARM_NEON__ && echo yes` || true
202  fi
203  if [ "x$have_armv7" = "x" ]; then
204    if check_define HAVE_ARMV7; then
205      have_armv7="yes"
206      have_armv6="yes"
207      have_armv5="yes"
208    fi
209  fi
210  if [ "x$have_armv6" = "x" ]; then
211    if check_define HAVE_ARMV6; then
212      have_armv6="yes"
213      have_armv5="yes"
214    fi
215  fi
216  if [ "x$have_armv5" = "x" ]; then
217    have_armv5=`check_define HAVE_ARMV5 && echo yes` || true
218  fi
219
220  # must disable thumb as recompiler can't handle it
221  if check_define __thumb__; then
222    CFLAGS="$CFLAGS -marm"
223  fi
224  # OABI/EABI selection
225  if [ "$have_arm_oabi" = "yes" ] &&  check_option -mabi=apcs-gnu; then
226    echo "$CFLAGS" | grep -q -- '-mabi=' || CFLAGS="$CFLAGS -mabi=apcs-gnu"
227    echo "$CFLAGS" | grep -q -- '-m\(no-\)*thumb-interwork' || CFLAGS="$CFLAGS -mno-thumb-interwork"
228    echo "$ASFLAGS" | grep -q -- '-mabi=' || ASFLAGS="$ASFLAGS -mabi=apcs-gnu"
229  fi
230
231  # automatically set mfpu and mfloat-abi if they are not set
232  if [ "$have_arm_neon" = "yes" ]; then
233    fpu="neon"
234    abi="hard"
235  elif [ "$have_armv6" = "yes" ]; then
236    fpu="vfp"
237    abi="softfp"
238  elif check_option -mfpu=fpa; then
239    fpu="fpa" # compatibility option for arm-linux-gnueabi-gcc on ubuntu
240    abi="soft"
241  fi
242  if [ "x$fpu" != "x" ]; then
243    echo "$CFLAGS" | grep -q -- '-mfpu=' || CFLAGS="$CFLAGS -mfpu=$fpu"
244    echo "$ASFLAGS" | grep -q -- '-mfpu=' || ASFLAGS="$ASFLAGS -mfpu=$fpu"
245    echo "$CFLAGS" | grep -q -- '-mfloat-abi=' || CFLAGS="$CFLAGS -mfloat-abi=$abi"
246    echo "$ASFLAGS" | grep -q -- '-mfloat-abi=' || ASFLAGS="$ASFLAGS -mfloat-abi=$abi"
247  fi
248
249  # add -ldl for helix support
250  case "$MAIN_LDLIBS" in
251    *"-ldl"*) ;;
252    *) MAIN_LDLIBS="-ldl $MAIN_LDLIBS" ;;
253  esac
254
255  # warn about common mistakes
256  if [ "$platform" != "gp2x" -a "$have_armv5" != "yes" ]; then
257    if ! echo "$CFLAGS" | grep -q -- '-mcpu=\|-march='; then
258      echo "Warning: compiling for ARMv4, is that really what you want?"
259      echo "You probably should specify -mcpu= or -march= like this:"
260      echo "  CFLAGS=-march=armv6 ./configure ..."
261    fi
262  fi
263  if [ "$have_arm_neon" = "yes" -a "$have_armv7" != "yes" ]; then
264    echo "Warning: compiling for NEON, but not ARMv7?"
265    echo "You probably want to specify -mcpu= or -march= like this:"
266    echo "  CFLAGS=-march=armv7-a ./configure ..."
267  fi
268  ;;
269*)
270  ;;
271esac
272
273case "$platform" in
274rpi1 | rpi2 | generic | opendingux)
275  need_sdl="yes"
276  ;;
277esac
278
279# basic compiler test
280cat > $TMPC <<EOF
281int main (int argc, char *argv[]) { return 0; }
282EOF
283if ! compile_binary; then
284  fail "compiler test failed, please check config.log"
285fi
286
287# header/library presence tests
288check_zlib()
289{
290  cat > $TMPC <<EOF
291  #include <zlib.h>
292  int main (int argc, char *argv[]) { uncompress(0, 0, 0, 0); }
293EOF
294  compile_binary "$@"
295}
296
297check_libpng()
298{
299  cat > $TMPC <<EOF
300  #include <png.h>
301  int main (int argc, char *argv[]) { png_init_io(0, 0); }
302EOF
303#  compile_binary
304  compile_object
305}
306
307check_oss()
308{
309  cat > $TMPC <<EOF
310  #include <sys/soundcard.h>
311  #include <sys/ioctl.h>
312  int main (int argc, char *argv[]) { int a=0; ioctl(0, SNDCTL_DSP_SETFMT, &a); }
313EOF
314  compile_binary
315}
316
317check_alsa()
318{
319  cat > $TMPC <<EOF
320  #include <alsa/asoundlib.h>
321  int main (int argc, char *argv[]) { snd_pcm_open(0, 0, 0, 0); }
322EOF
323  compile_binary "$@"
324}
325
326check_sdl()
327{
328  cat > $TMPC <<EOF
329  #include <SDL.h>
330  int main (int argc, char *argv[]) { SDL_OpenAudio(0, 0); }
331EOF
332  compile_binary "$@"
333}
334
335check_libavcodec()
336{
337  cat > $TMPC <<EOF
338  #include <libavcodec/avcodec.h>
339  int main (int argc, char *argv[]) { avcodec_decode_audio3(0, 0, 0, 0); }
340EOF
341  compile_object "$@"
342}
343
344check_zlib -lz &&MAIN_LDLIBS="$MAIN_LDLIBS -lz" || need_zlib="yes"
345
346MAIN_LDLIBS="-lpng $MAIN_LDLIBS"
347check_libpng || fail "please install libpng (libpng-dev)"
348
349if check_libavcodec; then
350  have_libavcodec="yes"
351  # add -ldl if needed
352  case "$MAIN_LDLIBS" in
353    *"-ldl"*) ;;
354    *) MAIN_LDLIBS="-ldl $MAIN_LDLIBS" ;;
355  esac
356fi
357
358# find what audio support we can compile
359if [ "x$sound_drivers" = "x" ]; then
360  if check_oss; then sound_drivers="$sound_drivers oss"; fi
361  if check_alsa -lasound; then
362    sound_drivers="$sound_drivers alsa"
363    MAIN_LDLIBS="-lasound $MAIN_LDLIBS"
364  fi
365  if [ "$need_sdl" = "yes" ] || check_sdl `$SDL_CONFIG --cflags --libs`; then
366    sound_drivers="$sound_drivers sdl"
367    need_sdl="yes"
368  fi
369else
370  if echo $sound_drivers | grep -q "\<oss\>"; then
371    check_oss || fail "oss support is missing"
372  fi
373  if echo $sound_drivers | grep -q "\<alsa\>"; then
374    MAIN_LDLIBS="-lasound $MAIN_LDLIBS"
375    check_alsa -lasound || fail "please install libasound2-dev"
376  fi
377fi
378
379if [ "$need_sdl" = "yes" ]; then
380  [ -x "$SDL_CONFIG" ] || \
381    fail "sdl-config is missing; please install libsdl (libsdl1.2-dev)"
382  CFLAGS="$CFLAGS `$SDL_CONFIG --cflags`"
383  MAIN_LDLIBS="`$SDL_CONFIG --libs` $MAIN_LDLIBS"
384  check_sdl `$SDL_CONFIG --libs` || fail "please install libsdl (libsdl1.2-dev)"
385  if [ "$SDLVERSION" = "sdl2" ]; then
386    CFLAGS="$CFLAGS -D__USE_SDL2__"
387  fi
388fi
389
390if check_option -Wno-unused_result; then
391  CFLAGS="$CFLAGS -Wno-unused-result"
392fi
393
394# set things that failed to autodetect to "no"
395test "x$have_armv6" != "x" || have_armv6="no"
396test "x$have_armv7" != "x" || have_armv7="no"
397test "x$have_libavcodec" != "x" || have_libavcodec="no"
398
399echo "architecture        $ARCH"
400echo "platform            $platform"
401echo "sound drivers       $sound_drivers"
402echo "C compiler          $CC"
403echo "C compiler flags    $CFLAGS"
404echo "libraries           $MAIN_LDLIBS"
405echo "linker flags        $LDFLAGS"
406echo "libavcodec (mp3)    $have_libavcodec"
407# echo "ARMv7 optimizations $have_armv7"
408
409echo "# Automatically generated by configure" > $config_mak
410printf "# Configured with:" >> $config_mak
411printf " '%s'" "$0" "$@" >> $config_mak
412echo >> $config_mak
413
414echo "CC = $CC" >> $config_mak
415echo "CXX = $CXX" >> $config_mak
416echo "AS = $AS" >> $config_mak
417echo "STRIP = $STRIP" >> $config_mak
418echo "CFLAGS += $CFLAGS" >> $config_mak
419echo "ASFLAGS += $ASFLAGS" >> $config_mak
420echo "LDFLAGS += $LDFLAGS" >> $config_mak
421echo "LDLIBS += $MAIN_LDLIBS" >> $config_mak
422echo >> $config_mak
423
424echo "ARCH = $ARCH" >> $config_mak
425echo "PLATFORM = $platform" >> $config_mak
426echo "SOUND_DRIVERS = $sound_drivers" >> $config_mak
427if [ "$have_libavcodec" = "yes" ]; then
428  echo "HAVE_LIBAVCODEC = 1" >> $config_mak
429fi
430if [ "$need_zlib" = "yes" ]; then
431  echo "PLATFORM_ZLIB = 1" >> $config_mak
432fi
433
434# GP2X toolchains are too old for UAL asm,
435# so add this here to not litter main Makefile
436#if [ "$platform" = "gp2x" ]; then
437#  echo >> $config_mak
438#  echo '%.o: %.S' >> $config_mak
439#  echo '	$(CC) $(CFLAGS) -E -c $^ -o /tmp/$(notdir $@).s' >> $config_mak
440#  echo '	$(AS) $(ASFLAGS) /tmp/$(notdir $@).s -o $@' >> $config_mak
441#fi
442
443# use pandora's skin (for now)
444test -e skin || ln -s platform/pandora/skin skin
445
446# vim:shiftwidth=2:expandtab
447