1#/bin/sh
2#
3# this script is used to rebuild SDL from sources in the current
4# directory. It depends on the Android emulator build scripts
5# that should normally be located in ../emulator/android/build
6# but you can also use ANDROID_QEMU_PATH in your environment
7# to point to the top of the Android emulator sources
8#
9#
10
11# first, let's see which system we're running this on
12cd `dirname $0`
13
14# try to find the Android build directory automatically
15if [ -z "$ANDROID_QEMU_PATH" ] ; then
16    if [ -f ../../android/build/common.sh ] ; then
17        ANDROID_QEMU_PATH=../..
18    else
19        echo "You must define ANDROID_QEMU_PATH in your environment to point"
20        echo "to the directory containing the Android emulator's sources."
21        exit 1
22    fi
23fi
24
25if [ ! -d $ANDROID_QEMU_PATH ] ; then
26    echo "Your ANDROID_QEMU_PATH does not point to a valid directory."
27    exit 1
28fi
29
30if [ ! -f $ANDROID_QEMU_PATH/android/build/common.sh ] ; then
31    echo "Your ANDROID_QEMU_PATH does not point to a directory that"
32    echo "contains the Android emulator build scripts."
33    exit 1
34fi
35
36# source common functions definitions
37. $ANDROID_QEMU_PATH/android/build/common.sh
38
39# Parse options
40OPTION_TRY_64=no
41OPTION_HELP=no
42OPTION_TARGETS=
43OPTION_FORCE_AUTODETECT=no
44OPTION_NO_TIGER=no
45
46if [ -z "$CC" ] ; then
47  CC=gcc
48fi
49
50for opt do
51  optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
52  case "$opt" in
53  --help|-h|-\?) OPTION_HELP=yes
54  ;;
55  --verbose)
56    if [ "$VERBOSE" = "yes" ] ; then
57        VERBOSE2=yes
58    else
59        VERBOSE=yes
60    fi
61  ;;
62  --cc=*) CC="$optarg" ; HOSTCC=$CC
63  ;;
64  --try-64) OPTION_TRY_64=yes
65  ;;
66  --prefix=*) OPTION_TARGETS="$OPTION_TARGETS $optarg"
67  ;;
68  --force-autodetect) OPTION_FORCE_AUTODETECT=yes
69  ;;
70  --no-tiger) OPTION_NO_TIGER=yes
71  ;;
72  *)
73    echo "unknown option '$opt', use --help"
74    exit 1
75  esac
76done
77
78# Print the help message
79#
80if [ "$OPTION_HELP" = "yes" ] ; then
81    cat << EOF
82
83Usage: rebuild.sh [options]
84Options: [defaults in brackets after descriptions]
85EOF
86    echo "Standard options:"
87    echo "  --help                   print this message"
88    echo "  --cc=PATH                specify C compiler [$CC]"
89    echo "  --try-64                 try to build a 64-bit executable (may crash)"
90    echo "  --verbose                verbose configuration"
91    echo "  --force-autodetect       force feature auto-detection"
92    echo "  --no-tiger               do not generate Tiger-compatible binaries (OS X only)"
93    echo ""
94    exit 1
95fi
96
97# we only support generating 32-bit binaris on 64-bit systems.
98# And we may need to add a -Wa,--32 to CFLAGS to let the assembler
99# generate 32-bit binaries on Linux x86_64.
100#
101if [ "$OPTION_TRY_64" != "yes" ] ; then
102    force_32bit_binaries
103fi
104
105# default target
106if [ -z "$OPTION_TARGETS" ] ; then
107  OPTION_TARGETS=out/$OS
108fi
109
110# we can build SDL with Cygwin, so enable it
111enable_cygwin
112
113setup_toolchain
114
115###
116### Special compiler and linker flags
117###
118
119case "$OS" in
120    linux-*)
121        BUILD_CFLAGS="-D_GNU_SOURCE=1 -fvisibility=hidden -DXTHREADS -D_REENTRANT"
122        # prevent -fstack-protector or the generated binaries will not link
123        BUILD_CFLAGS="$BUILD_CFLAGS -fno-stack-protector"
124        ;;
125    darwin-*)
126        BUILD_CFLAGS="-D_GNU_SOURCE=1 -fvisibility=hidden -DTARGET_API_MAC_CARBON -DTARGET_API_MAC_OSX -D_THREAD_SAFE -force_cpusubtype_ALL -fpascal-strings"
127        # detect the 10.4 SDK and use it automatically
128        TIGER_SDK=/Developer/SDKs/MacOSX10.4u.sdk
129        if [ ! -d $TIGER_SDK -a $OPTION_NO_TIGER = no ] ; then
130            echo "Please install the 10.4 SDK at $TIGER_SDK to generate Tiger-compatible binaries."
131            echo "If you don't want compatibility, use --no-tiger option instead."
132            exit 1
133        fi
134        if [ -d $TIGER_SDK ] ; then
135            TIGER_OPTS="-isysroot $TIGER_SDK -mmacosx-version-min=10.4"
136            BUILD_CFLAGS="$BUILD_CFLAGS $TIGER_OPTS"
137            BUILD_LDFLAGS="$BUILD_LDFLAGS $TIGER_OPTS -Wl,-syslibroot,$(TIGER_SDK)"
138            echo "Using OS X 10.4 SDK to generate Tiger-compatible binaries."
139        else
140            echo "Warning: the generated binaries will not be compatible with Tiger."
141        fi
142        ;;
143    windows)
144        BUILD_CFLAGS="-D_GNU_SOURCE=1"
145        ;;
146    *)
147        BUILD_CFLAGS=
148esac
149
150# BUILD_CFLAGS are used to build SDL, but SDL_CFLAGS are used
151# when clients want to use its facilities
152#
153case "$HOST_OS" in
154    linux)
155        SDL_CFLAGS="-D_GNU_SOURCE=1 -D_REENTRANT"
156        SDL_STATIC_LIBS="-lm -ldl -lpthread -lrt"
157        ;;
158    darwin)
159        SDL_CFLAGS="-D_GNU_SOURCE=1 -D_THREAD_SAFE"
160        SDL_STATIC_LIBS="-Wl,-framework,OpenGL -Wl,-framework,Cocoa -Wl,-framework,QuickTime -Wl,-framework,ApplicationServices -Wl,-framework,Carbon -Wl,-framework,IOKit"
161        ;;
162    windows)
163        SDL_CFLAGS="-D_GNU_SOURCE=1 -Dmain=SDL_main"
164        SDL_STATIC_LIBS="-luser32 -lgdi32 -lwinmm"
165        ;;
166    *)
167        SDL_CFLAGS=
168        SDL_STATIC_LIBS=
169esac
170
171DOLLAR="$"
172SDL_STATIC_LIBS="$DOLLAR{libdir}/libSDLmain.a $DOLLAR{libdir}/libSDL.a $SDL_STATIC_LIBS"
173
174###
175### Features probes
176###
177
178CONFIG_LIBC=yes
179
180###
181### Configuration files generation
182###
183
184# create the objs directory that is going to contain all generated files
185# including the configuration ones
186#
187mkdir -p objs
188
189config_add ()
190{
191    echo "$1" >> $config_h
192}
193
194# used to add a macro define/undef line to the configuration
195# $1: macro name
196# $2: 1, 0, yes or no
197# $3: optional log prefix
198config_add_macro ()
199{
200    local logname
201    logname="${3:-CfgMacro   }"
202    case "$2" in
203        0|no|false)
204            config_add "/* #undef $1 */"
205            log "$logname: $1=0"
206            ;;
207        1|yes|true)
208            config_add "#define $1 1"
209            log "$logname: $1=1"
210            ;;
211    esac
212}
213
214# used to add host-os-specific driver macros to the config file
215# $1 : driver prefix, e.g. DRIVERS_VIDEO
216# $2 : macro prefix
217# this will look for DRIVERS_VIDEO_${HOST_OS}, and, if it is not
218# defined in DRIVERS_VIDEO_default
219#
220# then this will call 'config_add ${2}_${driver}' for each driver listed
221#
222config_add_driver_macros ()
223{
224    local  driver_list="`var_value ${1}_${HOST_OS}`"
225    local  driver_name
226    if [ -z "$driver_list" ] ; then
227        driver_list="`var_value ${1}_default`"
228    fi
229    for driver_name in $driver_list; do
230        config_add_macro "${2}_${driver_name}" yes
231    done
232}
233
234config_h=objs/SDL_config.h
235
236# a function used to check the availability of a given Standard C header
237# $1 : header name, without the .h suffix nor enclosing brackets (e.g. 'memory' for <memory.h>)
238# this will update the configuration with a relevant line
239sdl_check_header ()
240{
241    local result
242    local header
243    local macro
244    header="<$1.h>"
245    macro=`to_uppercase $1`
246    macro=`echo $macro | tr '/' '_'`
247    feature_check_header result "$header"
248    config_add_macro HAVE_${macro}_H $result "StdCHeader "
249}
250
251# call sdl_check_header with a list of header names
252sdl_check_headers ()
253{
254    for hh in $*; do
255        sdl_check_header "$hh"
256    done
257}
258
259# a function used to check that a given function is available in the C library
260# this will update the configuration with a relevant line
261# $1: function name
262# $2: optional, libraries to link against
263#
264sdl_check_func ()
265{
266    local result
267    local funcname
268    local macro
269    funcname="$1"
270    macro=`to_uppercase $1`
271    rm -f $TMPC
272    cat > $TMPC <<EOF
273#undef $funcname
274#define $funcname  _innocuous_$funcname
275#include <assert.h>
276#undef $funcname
277
278typedef void (*func_t)(void);
279extern void $funcname(void);
280func_t  _dummy = $funcname;
281int   main(void)
282{
283    return _dummy == $funcname;
284}
285EOF
286    EXTRA_LDFLAGS="$2"
287    feature_check_link result
288    config_add_macro HAVE_${macro} $result "StdCFunc   "
289}
290
291# call sdl_check_func with a list of functions
292sdl_check_funcs ()
293{
294    for ff in $*; do
295        sdl_check_func "$ff"
296    done
297}
298
299# check endianess of the host platform
300sdl_check_endianess ()
301{
302    cat > $TMPC <<EOF
303    int  main(void)
304    {
305        union {
306            unsigned short  v;
307            unsigned char   b[2];
308        } u;
309
310        u.v = 0x1234;
311        return (u.b[0] == 0x12);
312    }
313EOF
314    feature_run_exec test_e
315    if [ "$test_e" = "1" ] ; then
316        eval $1=4321
317    else
318        eval $1=1234
319    fi
320}
321
322
323generate_SDL_config_h ()
324{
325    echo "/* This file was autogenerated by '$PROGNAME' - do not edit */" > $config_h
326
327    config_add "#ifndef _SDL_config_h"
328    config_add "#define _SDL_config_h"
329    config_add ""
330    config_add "#include \"SDL_platform.h\""
331    config_add ""
332    
333    # true for all architectures these days
334    config_add_macro SDL_HAS_64BIT_TYPE  1
335
336    sdl_check_endianess ENDIANESS
337    config_add "#define SDL_BYTEORDER $ENDIANESS"
338    config_add ""
339
340    config_add_macro HAVE_LIBC $CONFIG_LIBC
341
342    config_add "#if HAVE_LIBC"
343    config_add ""
344    config_add "/* Useful headers */"
345
346    sdl_check_headers  alloca sys/types stdio
347
348    # just like configure - force it to 1
349    config_add "#define STDC_HEADERS  1"
350
351    sdl_check_headers  stdlib stdarg malloc memory string strings inttypes stdint ctype math iconv signal altivec
352
353    config_add "/* C library functions */"
354
355    sdl_check_funcs  malloc calloc realloc free alloc
356
357    config_add "#ifndef _WIN32 /* Don't use on Windows */"
358
359    sdl_check_funcs  getenv putenv unsetenv
360
361    config_add "#endif"
362    sdl_check_funcs qsort abs bcopy memset memcpy memmove memcmp strlen strlcpy strlcat
363    sdl_check_funcs strdup _strrev _strupr _strlwr index rindex strchr strrchr itoa _ltoa
364    sdl_check_funcs _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull
365    sdl_check_funcs strtod atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp
366    sdl_check_funcs vsnprintf iconv sigaction setjmp nanosleep
367
368    sdl_check_func clock_gettime -lrt
369    sdl_check_func dlvsym -ldl
370
371    sdl_check_funcs getpagesize
372
373    config_add "#else"
374    config_add "/* We may need some replacement for stdarg.h here */"
375    config_add "#include <stdarg.h>"
376    config_add "#endif /* HAVE_LIBC */"
377    config_add ""
378
379    config_add "/* Allow disabling of core subsystems */"
380
381    config_add_macro SDL_AUDIO_DISABLED yes
382    config_add_macro SDL_CDROM_DISABLED yes
383    config_add_macro SDL_CPUINFO_DISABLED no
384    config_add_macro SDL_EVENTS_DISABLED no
385    config_add_macro SDL_FILE_DISABLED yes
386    config_add_macro SDL_JOYSTICK_DISABLED yes
387    config_add_macro SDL_LOADSO_DISABLED no
388    config_add_macro SDL_THREADS_DISABLED no
389    config_add_macro SDL_TIMERS_DISABLED no
390    config_add_macro SDL_VIDEO_DISABLED no
391
392    config_add ""
393    config_add "/* Enable various shared object loading systems */"
394
395    config_add_driver_macros DRIVERS_LOADSO SDL_LOADSO
396
397    config_add ""
398    config_add "/* Enable various threading systems */"
399
400    config_add_driver_macros DRIVERS_THREAD SDL_THREAD
401
402    config_add ""
403    config_add "/* Enable various timer systems */"
404
405    config_add_driver_macros DRIVERS_TIMER SDL_TIMER
406
407    config_add ""
408    config_add "/* Enable various video drivers */"
409
410    config_add_driver_macros DRIVERS_VIDEO SDL_VIDEO_DRIVER
411
412    config_add_driver_macros DRIVERS_MAIN SDL_MAIN
413
414    # the following defines are good enough for all recent Unix distributions
415    config_add "#define SDL_VIDEO_DRIVER_X11_DYNAMIC \"libX11.so.6\""
416    config_add "#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT \"libXext.so.6\""
417    config_add "#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR \"libXrandr.so.2\""
418    config_add "#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER \"libXrender.so.1\""
419
420    # todo: OpenGL support ?
421    # todo: Assembly routines support ?
422
423    config_add "#endif /* _SDL_config_h */"
424}
425
426### Build the config sub-makefile
427###
428
429make_add ()
430{
431    echo "$1" >> $config_mk
432}
433
434make_add_driver_macros ()
435{
436    local driver_list="`var_value ${1}_${HOST_OS}`"
437    local driver_name
438    if [ -z "$driver_list" ] ; then
439        driver_list="`var_value ${1}_default`"
440    fi
441    for driver_name in $driver_list; do
442        make_add "SDL_$2_${driver_name} := yes"
443    done
444}
445
446generate_sdl_config_mk ()
447{
448    CFLAGS="$CFLAGS $BUILD_CFLAGS"
449    LDFLAGS="$LDFLAGS $BUILD_LDFLAGS"
450    create_config_mk
451
452    make_add ""
453    PWD=`pwd`
454    make_add "SRC_PATH=$PWD"
455    make_add "BUILD_SYSTEM=$ANDROID_QEMU_PATH/android/build"
456
457    make_add "SDL_CONFIG_LIBC := $CONFIG_LIBC"
458    make_add "SDL_CONFIG_CPUINFO := yes"
459
460    make_add_driver_macros DRIVERS_LOADSO CONFIG_LOADSO
461    make_add_driver_macros DRIVERS_THREAD CONFIG_THREAD
462    make_add_driver_macros DRIVERS_TIMER  CONFIG_TIMER
463    make_add_driver_macros DRIVERS_VIDEO  CONFIG_VIDEO
464
465    make_add_driver_macros DRIVERS_MAIN   CONFIG_MAIN
466
467    make_add "INSTALL_TARGETS := $OPTION_TARGETS"
468}
469
470### Build the final sdl-config script from the template
471###
472
473generate_sdl_config ()
474{
475    # build a sed script that will replace all @VARIABLES@ in the template
476    # with appropriate values
477    rm -f $TMPC
478
479    # replace @exec_prefix@ with "{prefix}", and @libdir@ with "{libdir}"
480    cat > $TMPC <<EOF
481s!@exec_prefix@!\$\{prefix\}!g
482s!@libdir@!\$\{exec_prefix\}/libs!g
483s!@includedir@!\$\{prefix\}/include!g
484EOF
485
486    # we want to enable static linking, do @ENABLE_STATIC_FALSE@ and @ENABLE_STATIC_TRUE@
487    cat >> $TMPC <<EOF
488s!@ENABLE_STATIC_FALSE@!\#!g
489s!@ENABLE_STATIC_TRUE@!!g
490s!@ENABLE_SHARED_TRUE@!\#!g
491s!@ENABLE_SHARED_FALSE@!!g
492EOF
493
494    # set the version to 1.2.12
495    cat >> $TMPC <<EOF
496s!@SDL_VERSION@!1.2.12!g
497EOF
498
499    #
500    cat >> $TMPC <<EOF
501s!@SDL_CFLAGS@!$SDL_CFLAGS!g
502s!@SDL_STATIC_LIBS@!$SDL_STATIC_LIBS!g
503EOF
504
505    cat sdl-config.in | sed -f $TMPC > objs/sdl-config
506    chmod +x objs/sdl-config
507}
508
509# copy a configuration file or perform auto-detection if one is not available
510# or the --force-autodetect option was used
511# $1: basename of config file
512# $2: command to run to perform auto-detection/generation
513#
514copy_or_autodetect ()
515{
516    if [ "$OPTION_FORCE_AUTODETECT" != "yes" -a -f android/build/$OS/$1 ] ; then
517        log "Setup      : Copying $1 from android/build/$OS"
518        cp -f android/build/$OS/$1 objs/$1
519    else
520        log "Setup      : Auto-generating $1"
521        eval $2
522    fi
523}
524
525generate_all ()
526{
527  copy_or_autodetect SDL_config.h generate_SDL_config_h
528  generate_sdl_config_mk
529  copy_or_autodetect sdl-config generate_sdl_config
530}
531
532DRIVERS_LOADSO_default=DLOPEN
533DRIVERS_LOADSO_darwin=DLCOMPAT
534DRIVERS_LOADSO_windows=WIN32
535
536# TODO: determine if PTHREAD_RECURSIVE or PTHREAD_RECURSIVE_NP is valid for the platform
537DRIVERS_THREAD_default="PTHREAD PTHREAD_RECURSIVE_MUTEX"
538DRIVERS_THREAD_linux="PTHREAD PTHREAD_RECURSIVE_MUTEX_NP"
539DRIVERS_THREAD_windows=WIN32
540
541DRIVERS_TIMER_default=UNIX
542DRIVERS_TIMER_windows=WIN32
543
544# TODO: actually compute this properly for X11 and dynamic loading !!
545DRIVERS_VIDEO_default="X11 X11_DPMS X11_XINERAMA X11_XME"
546DRIVERS_VIDEO_darwin="QUARTZ"
547DRIVERS_VIDEO_windows="WINDIB"
548
549DRIVERS_MAIN_default=DUMMY
550DRIVERS_MAIN_darwin=MACOSX
551DRIVERS_MAIN_windows=WIN32
552
553generate_all
554