Build SDL from sources directly.

This removes the requirement to build the SDL library before the rest
of the emulator. The prebuilt SDL library and installation is now optional
and is only kept for the full Android build on Linux (mainly to avoid having
to install many dev libraries on build servers).

This patch affects both the Android and standalone build systems.

The script android-configure.sh will not use a prebuilt SDL installation
unless you explicitely use --sdl-config=<script>.

Tested for linux-x86 / darwin-x86 / cygwin / linux-mingw builds.

Change-Id: If973b2ad199f06aeeff43aec387d1ac629487529
diff --git a/.gitignore b/.gitignore
index e31aaec..db1f286 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,4 @@
 images/android_icon.o
 objs/*
 *.*~
-distrib
 
diff --git a/Makefile b/Makefile
index f39a14c..7b23968 100644
--- a/Makefile
+++ b/Makefile
@@ -46,11 +46,12 @@
 EXECUTABLES :=
 LIBRARIES   :=
 
-SDL_CONFIG ?= $(PREBUILT)/sdl/bin/sdl-config
+ifneq ($(SDL_CONFIG),)
 SDL_LIBS   := $(filter %.a,$(shell $(SDL_CONFIG) --static-libs))
 $(foreach lib,$(SDL_LIBS), \
     $(eval $(call copy-prebuilt-lib,$(lib))) \
 )
+endif
 
 clean: clean-intermediates
 
@@ -70,4 +71,4 @@
 
 # include dependency information
 DEPENDENCY_DIRS := $(sort $(DEPENDENCY_DIRS))
--include $(wildcard $(DEPENDENCY_DIRS:%=%/*.d))
\ No newline at end of file
+-include $(wildcard $(DEPENDENCY_DIRS:%=%/*.d))
diff --git a/Makefile.android b/Makefile.android
index 87da191..5519896 100644
--- a/Makefile.android
+++ b/Makefile.android
@@ -1,11 +1,21 @@
 ifeq ($(TARGET_ARCH),arm)
 LOCAL_PATH:= $(call my-dir)
 
+# determine the host tag to use
+QEMU_HOST_TAG := $(HOST_PREBUILT_TAG)
+ifneq ($(USE_MINGW),)
+    QEMU_HOST_TAG := windows
+endif
+
 # determine the location of platform-specific directories
 #
 CONFIG_DIRS     := \
     $(LOCAL_PATH)/android/config \
-    $(LOCAL_PATH)/android/config/$(HOST_PREBUILT_TAG)
+    $(LOCAL_PATH)/android/config/$(QEMU_HOST_TAG)
+
+ifeq ($(BUILD_STANDALONE_EMULATOR),true)
+    CONFIG_DIRS := $(LOCAL_PATH)/objs $(CONFIG_DIRS)
+endif
 
 CONFIG_INCLUDES := $(CONFIG_DIRS:%=-I%)
 
@@ -307,7 +317,20 @@
 # SDL-related definitions
 #
 
-SDL_CONFIG ?= prebuilt/$(HOST_PREBUILT_TAG)/sdl/bin/sdl-config
+# Build SDL from sources except on linux-x86, to avoid installing all
+# the X11 development packages on our build servers.
+#
+BUILD_SDL_FROM_SOURCES := true
+ifeq ($(QEMU_HOST_TAG),linux-x86)
+    BUILD_SDL_FROM_SOURCES := false
+endif
+ifneq ($(SDL_CONFIG),)
+    BUILD_SDL_FROM_SOURCES := false
+endif
+
+ifneq ($(BUILD_SDL_FROM_SOURCES),true)
+
+SDL_CONFIG ?= prebuilt/$(QEMU_HOST_TAG)/sdl/bin/sdl-config
 SDL_CFLAGS := $(shell $(SDL_CONFIG) --cflags)
 
 # We need to filter out the _GNU_SOURCE variable because it breaks recent
@@ -316,6 +339,16 @@
 SDL_CFLAGS := $(filter-out -D_GNU_SOURCE=1,$(SDL_CFLAGS))
 SDL_LDLIBS := $(filter-out %.a %.lib,$(shell $(SDL_CONFIG) --static-libs))
 
+# Circular dependencies between libSDL and libSDLmain;
+# We repeat the libraries in the final link to work around it.
+SDL_STATIC_LIBRARIES := libSDL libSDLmain libSDL libSDLmain
+
+else   # !BUILD_STANDALONE_EMULATOR
+
+SDL_DIR := distrib/sdl-1.2.12
+include $(LOCAL_PATH)/$(SDL_DIR)/sources.make
+
+endif  # !BUILD_STANDALONE_EMULATOR
 
 ##############################################################################
 # determine audio sources, build the prebuilt audio-library if needed
@@ -386,7 +419,7 @@
 
 ifneq ($(BUILD_STANDALONE_EMULATOR),true)
   QEMU_AUDIO_LIB := $(wildcard \
-	prebuilt/$(HOST_PREBUILT_TAG)/emulator/libqemu-audio.a)
+	prebuilt/$(QEMU_HOST_TAG)/emulator/libqemu-audio.a)
 endif
 
 ifeq ($(BUILD_QEMU_AUDIO_LIB),true)
@@ -463,6 +496,12 @@
 LOCAL_SRC_FILES += $(LIBPNG_SOURCES)
 LOCAL_CFLAGS    += $(LIBPNG_CFLAGS) -I$(LOCAL_PATH)/$(LIBPNG_DIR)
 
+# include the SDL sources
+#
+LOCAL_SRC_FILES += $(SDL_SOURCES) $(SDLMAIN_SOURCES)
+LOCAL_CFLAGS    += $(SDL_CFLAGS) -I$(LOCAL_PATH)/$(SDL_DIR)/include
+
+#
 LOCAL_CFLAGS    += -I$(LOCAL_PATH)/ \
                    -I$(LOCAL_PATH)/target-arm \
                    -I$(LOCAL_PATH)/fpu \
@@ -671,10 +710,7 @@
 #
 LOCAL_CFLAGS += $(SDL_CFLAGS)
 LOCAL_LDLIBS += $(SDL_LDLIBS)
-# Circular dependencies between libSDL and libSDLmain;
-# We repeat the libraries in the final link to work around it.
-LOCAL_STATIC_LIBRARIES += libSDL libSDLmain
-LOCAL_STATIC_LIBRARIES += libSDL libSDLmain
+LOCAL_STATIC_LIBRARIES += $(SDL_STATIC_LIBRARIES)
 
 # add ELFF-specific flags
 #
diff --git a/android-configure.sh b/android-configure.sh
index 0e95e04..1cf5008 100755
--- a/android-configure.sh
+++ b/android-configure.sh
@@ -4,11 +4,6 @@
 # in the current directory. It also contains logic to speed up the
 # rebuild if it detects that you're using the Android build system
 #
-# in this case, it will use prebuilt binaries for the compiler,
-# the audio library and the SDL library. You can disable this
-# by using the --no-prebuilt-libs and --cc=<compiler> options
-#
-#
 # here's the list of environment variables you can define before
 # calling this script to control it (besides options):
 #
@@ -139,13 +134,6 @@
         log "Prebuilt   : CCACHE=$CCACHE"
     fi
 
-    # if the user didn't specify an sdl-config script, get the prebuilt one
-    if [ -z "$SDL_CONFIG" -a "$OPTION_NO_PREBUILTS" = "no" ] ; then
-        # always use our own static libSDL by default
-        SDL_CONFIG=$ANDROID_PREBUILT/sdl/bin/sdl-config
-        log "Prebuilt   : SDL_CONFIG=$SDL_CONFIG"
-    fi
-
     # finally ensure that our new binary is copied to the 'out'
     # subdirectory as 'emulator'
     HOST_BIN=$(get_android_abs_build_var HOST_OUT_EXECUTABLES)
@@ -174,106 +162,83 @@
 ###  SDL Probe
 ###
 
-# if the user didn't specify a sdl-config script, get the prebuilt one
-if [ -z "$SDL_CONFIG" -a "$OPTION_NO_PREBUILTS" = "no" ] ; then
-    #  try to find one from our git repository
-    SDL_CONFIG=../sdl/out/$OS/bin/sdl-config
-    if [ -f $SDL_CONFIG ] ; then
-        log "Prebuilt   : SDL_CONFIG=$SDL_CONFIG"
-    else
-        echo "WARNING: YOU SHOULD USE THE --sdl-config OPTION"
-        SDL_CONFIG=
-    fi
-fi
+if [ -n "$SDL_CONFIG" ] ; then
 
-# For now, we require an external libSDL library, if SDL_CONFIG is not
-# defined, try to grab it from the environment
-#
-if [ -z "$SDL_CONFIG" ] ; then
-    SDL_CONFIG=`which sdl-config`
-    if [ $? != 0 ] ; then
-        echo "Please ensure that you have the emulator's patched libSDL"
-        echo "built somewhere and point to its sdl-config script either"
-        echo "with the SDL_CONFIG env. variable, or the --sdl-config=<script>"
-        echo "option."
-        clean_exit
-    fi
-fi
+	# check that we can link statically with the library.
+	#
+	SDL_CFLAGS=`$SDL_CONFIG --cflags`
+	SDL_LIBS=`$SDL_CONFIG --static-libs`
 
-# check that we can link statically with the library.
-#
-SDL_CFLAGS=`$SDL_CONFIG --cflags`
-SDL_LIBS=`$SDL_CONFIG --static-libs`
+	# quick hack, remove the -D_GNU_SOURCE=1 of some SDL Cflags
+	# since they break recent Mingw releases
+	SDL_CFLAGS=`echo $SDL_CFLAGS | sed -e s/-D_GNU_SOURCE=1//g`
 
-# quick hack, remove the -D_GNU_SOURCE=1 of some SDL Cflags
-# since they break recent Mingw releases
-SDL_CFLAGS=`echo $SDL_CFLAGS | sed -e s/-D_GNU_SOURCE=1//g`
-
-log "SDL-probe  : SDL_CFLAGS = $SDL_CFLAGS"
-log "SDL-probe  : SDL_LIBS   = $SDL_LIBS"
+	log "SDL-probe  : SDL_CFLAGS = $SDL_CFLAGS"
+	log "SDL-probe  : SDL_LIBS   = $SDL_LIBS"
 
 
-EXTRA_CFLAGS="$SDL_CFLAGS"
-EXTRA_LDFLAGS="$SDL_LIBS"
+	EXTRA_CFLAGS="$SDL_CFLAGS"
+	EXTRA_LDFLAGS="$SDL_LIBS"
 
-case "$OS" in
-    freebsd-*)
-    EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm -lpthread"
-    ;;
-esac
+	case "$OS" in
+		freebsd-*)
+		EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm -lpthread"
+		;;
+	esac
 
-cat > $TMPC << EOF
+	cat > $TMPC << EOF
 #include <SDL.h>
 #undef main
 int main( int argc, char** argv ) {
    return SDL_Init (SDL_INIT_VIDEO); 
 }
 EOF
-feature_check_link  SDL_LINKING
+	feature_check_link  SDL_LINKING
 
-if [ $SDL_LINKING != "yes" ] ; then
-    echo "You provided an explicit sdl-config script, but the corresponding library"
-    echo "cannot be statically linked with the Android emulator directly."
-    echo "Error message:"
-    cat $TMPL
-    clean_exit
-fi
-log "SDL-probe  : static linking ok"
+	if [ $SDL_LINKING != "yes" ] ; then
+		echo "You provided an explicit sdl-config script, but the corresponding library"
+		echo "cannot be statically linked with the Android emulator directly."
+		echo "Error message:"
+		cat $TMPL
+		clean_exit
+	fi
+	log "SDL-probe  : static linking ok"
 
-# now, let's check that the SDL library has the special functions
-# we added to our own sources
-#
-cat > $TMPC << EOF
+	# now, let's check that the SDL library has the special functions
+	# we added to our own sources
+	#
+	cat > $TMPC << EOF
 #include <SDL.h>
 #undef main
 int main( int argc, char** argv ) {
-    int  x, y;
-    SDL_Rect  r;
-    SDL_WM_GetPos(&x, &y);
-    SDL_WM_SetPos(x, y);
-    SDL_WM_GetMonitorDPI(&x, &y);
-    SDL_WM_GetMonitorRect(&r);
-    return SDL_Init (SDL_INIT_VIDEO); 
+	int  x, y;
+	SDL_Rect  r;
+	SDL_WM_GetPos(&x, &y);
+	SDL_WM_SetPos(x, y);
+	SDL_WM_GetMonitorDPI(&x, &y);
+	SDL_WM_GetMonitorRect(&r);
+	return SDL_Init (SDL_INIT_VIDEO); 
 }
 EOF
-feature_check_link  SDL_LINKING
+	feature_check_link  SDL_LINKING
 
-if [ $SDL_LINKING != "yes" ] ; then
-    echo "You provided an explicit sdl-config script in SDL_CONFIG, but the"
-    echo "corresponding library doesn't have the patches required to link"
-    echo "with the Android emulator. Unsetting SDL_CONFIG will use the"
-    echo "sources bundled with the emulator instead"
-    echo "Error:"
-    cat $TMPL
-    clean_exit
+	if [ $SDL_LINKING != "yes" ] ; then
+		echo "You provided an explicit sdl-config script in SDL_CONFIG, but the"
+		echo "corresponding library doesn't have the patches required to link"
+		echo "with the Android emulator. Unsetting SDL_CONFIG will use the"
+		echo "sources bundled with the emulator instead"
+		echo "Error:"
+		cat $TMPL
+		clean_exit
+	fi
+
+	log "SDL-probe  : extra features ok"
+	clean_temp
+
+	EXTRA_CFLAGS=
+	EXTRA_LDFLAGS=
 fi
 
-log "SDL-probe  : extra features ok"
-clean_temp
-
-EXTRA_CFLAGS=
-EXTRA_LDFLAGS=
-
 ###
 ###  Audio subsystems probes
 ###
@@ -384,11 +349,14 @@
 #
 
 create_config_mk
+add_android_config_mk
 
 PWD=`pwd`
 echo "TARGET_ARCH := arm" >> $config_mk
 echo "SRC_PATH          := $PWD" >> $config_mk
+if [ -n "$SDL_CONFIG" ] ; then
 echo "SDL_CONFIG         := $SDL_CONFIG" >> $config_mk
+fi
 echo "CONFIG_COREAUDIO  := $PROBE_COREAUDIO" >> $config_mk
 echo "CONFIG_WINAUDIO   := $PROBE_WINAUDIO" >> $config_mk
 echo "CONFIG_ESD        := $PROBE_ESD" >> $config_mk
diff --git a/android/build/common.sh b/android/build/common.sh
index 93ffba7..289e74a 100644
--- a/android/build/common.sh
+++ b/android/build/common.sh
@@ -134,6 +134,21 @@
 # define HOST_ARCH as the $CPU
 HOST_ARCH=$CPU
 
+# define HOST_TAG
+# special case: windows-x86 => windows
+compute_host_tag ()
+{
+    case $HOST_OS-$HOST_ARCH in
+        cygwin-x86|windows-x86)
+            HOST_TAG=windows
+            ;;
+        *)
+            HOST_TAG=$HOST_OS-$HOST_ARCH
+            ;;
+    esac
+}
+compute_host_tag
+
 #### Toolchain support
 ####
 
@@ -170,6 +185,7 @@
         esac
         HOST_ARCH=x86
         CPU=x86
+        compute_host_tag
         log "Check32Bits: Forcing generation of 32-bit binaries (--try-64 to disable)"
     fi
 }
@@ -504,6 +520,6 @@
 {
     echo "" >> $config_mk
     echo "TARGET_ARCH       := arm" >> $config_mk
-    echo "HOST_PREBUILT_TAG := $ANDROID_PREBUILT_HOST_TAG" >> $config_mk
+    echo "HOST_PREBUILT_TAG := $HOST_TAG" >> $config_mk
     echo "PREBUILT          := $ANDROID_PREBUILT" >> $config_mk
 }
diff --git a/distrib/sdl-1.2.12/android/build/darwin-x86/SDL_config.h b/android/config/darwin-x86/SDL_config.h
similarity index 100%
rename from distrib/sdl-1.2.12/android/build/darwin-x86/SDL_config.h
rename to android/config/darwin-x86/SDL_config.h
diff --git a/distrib/sdl-1.2.12/android/build/freebsd-x86/SDL_config.h b/android/config/freebsd-x86/SDL_config.h
similarity index 100%
rename from distrib/sdl-1.2.12/android/build/freebsd-x86/SDL_config.h
rename to android/config/freebsd-x86/SDL_config.h
diff --git a/distrib/sdl-1.2.12/android/build/linux-x86/SDL_config.h b/android/config/linux-x86/SDL_config.h
similarity index 100%
rename from distrib/sdl-1.2.12/android/build/linux-x86/SDL_config.h
rename to android/config/linux-x86/SDL_config.h
diff --git a/distrib/sdl-1.2.12/android/build/windows/SDL_config.h b/android/config/windows/SDL_config.h
similarity index 100%
rename from distrib/sdl-1.2.12/android/build/windows/SDL_config.h
rename to android/config/windows/SDL_config.h
diff --git a/android/main.c b/android/main.c
index bf3feec..c4e94c3 100644
--- a/android/main.c
+++ b/android/main.c
@@ -1301,9 +1301,6 @@
     return convertMBToBytes(imageMB);
 }
 
-#ifdef _WIN32
-#undef main  /* we don't want SDL to define main */
-#endif
 
 int main(int argc, char **argv)
 {
diff --git a/distrib/build-emulator.sh b/distrib/build-emulator.sh
index dad3dd1..e8d1749 100755
--- a/distrib/build-emulator.sh
+++ b/distrib/build-emulator.sh
@@ -26,13 +26,8 @@
 # directory where we'll place the temporary SDL binaries
 LOCAL=$CURDIR/local
 
-cd $CURDIR/sdl
-if ! (ANDROID_QEMU_PATH=$CURDIR/qemu ./android-configure.sh --prefix=$LOCAL && make && make install); then
-    echo "ERROR: could not build SDL library, please check their sources"
-fi
-
 cd $CURDIR/qemu
-if ! (./android-rebuild.sh --sdl-config=$LOCAL/bin/sdl-config); then
+if ! (./android-rebuild.sh); then
     echo "ERROR: could not build the emulator, please check the sources"
 fi
 
diff --git a/distrib/make-distrib.sh b/distrib/make-distrib.sh
index 2580837..e05117f 100755
--- a/distrib/make-distrib.sh
+++ b/distrib/make-distrib.sh
@@ -29,11 +29,6 @@
     echo "Could not clone sources"
 fi
 
-# move the SDL sources
-SDLDIR=$TMPDIR/sdl
-echo "Moving SDL sources to $SDLDIR"
-mv $TMPDIR/qemu/distrib/sdl-1.2.12 $SDLDIR
-
 echo "copying control scripts"
 mv $QEMUDIR/distrib/build-emulator.sh $TMPDIR/build-emulator.sh
 mv $QEMUDIR/distrib/README $TMPDIR/README
diff --git a/distrib/sdl-1.2.12/Makefile.android b/distrib/sdl-1.2.12/Makefile.android
index ab743e2..4e89fc7 100644
--- a/distrib/sdl-1.2.12/Makefile.android
+++ b/distrib/sdl-1.2.12/Makefile.android
@@ -1,5 +1,8 @@
 LOCAL_PATH:= $(call my-dir)
 
+SDL_DIR := $(LOCAL_PATH)
+include $(SDL_DIR)/sources.make
+
 CONFIG_DIRS     := \
     $(LOCAL_PATH)/objs \
     $(LOCAL_PATH)/include \
@@ -21,202 +24,7 @@
 
 LOCAL_MODULE := libSDL
 
-LOCAL_SRC_FILES :=
-
-# the main src/ sources
-#
-SRCS := SDL.c \
-        SDL_error.c \
-        SDL_fatal.c \
-
-SRCS += events/SDL_active.c \
-	events/SDL_events.c \
-	events/SDL_expose.c \
-	events/SDL_keyboard.c \
-	events/SDL_mouse.c \
-	events/SDL_quit.c \
-	events/SDL_resize.c \
-
-SRCS += file/SDL_rwops.c
-
-ifeq ($(CONFIG_LIBC),yes)
-  SRCS += stdlib/SDL_getenv.c \
-  	  stdlib/SDL_iconv.c \
-  	  stdlib/SDL_malloc.c \
-  	  stdlib/SDL_qsort.c \
-  	  stdlib/SDL_stdlib.c \
-  	  stdlib/SDL_string.c
-endif
-
-ifeq ($(CONFIG_CPUINFO),yes)
-  SRCS += cpuinfo/SDL_cpuinfo.c
-endif
-
-LOCAL_SRC_FILES += $(SRCS:%=src/%)
-
-# the Audio sources
-#
-SRCS := SDL_audio.c \
-	SDL_audiocvt.c \
-	SDL_audiodev.c \
-	SDL_mixer.c \
-	SDL_mixer_MMX.c \
-	dummy/SDL_dummyaudio.c \
-
-ifeq ($(CONFIG_AUDIO_ALSA),yes)
-  SRCS += alsa/SDL_alsa_audio.c
-endif
-
-ifeq ($(CONFIG_AUDIO_ESD),yes)
-  SRCS += esd/SDL_esdaudio.c
-endif
-
-ifeq ($(CONFIG_AUDIO_OSS),yes)
-  SRCS += dsp/SDL_dspaudio.c \
-          dma/SDL_dmaaudio.c
-endif
-
-ifeq ($(CONFIG_AUDIO_COREAUDIO),yes)
-  SRCS += macosx/SDL_coreaudio.c
-endif
-
-ifeq ($(CONFIG_AUDIO_WAVEOUT),yes)
-  SRCS += windib/SDL_dibaudio.c
-endif
-
-LOCAL_SRC_FILES += $(SRCS:%=src/audio/%)
-
-# the LoadSO sources
-#
-
-SRCS :=
-
-ifeq ($(CONFIG_LOADSO_DLOPEN),yes)
-  SRCS += dlopen/SDL_sysloadso.c
-endif
-
-ifeq ($(CONFIG_LOADSO_DLCOMPAT),yes)
-  SRCS += macosx/SDL_dlcompat.c
-endif
-
-ifeq ($(CONFIG_LOADSO_WIN32),yes)
-  SRCS += win32/SDL_sysloadso.c
-endif
-
-LOCAL_SRC_FILES += $(SRCS:%=src/loadso/%)
-
-# the Thread sources
-#
-
-SRCS := SDL_thread.c
-
-ifeq ($(CONFIG_THREAD_PTHREAD),yes)
-  SRCS += pthread/SDL_syscond.c \
-          pthread/SDL_sysmutex.c \
-          pthread/SDL_syssem.c \
-          pthread/SDL_systhread.c
-endif
-
-ifeq ($(CONFIG_THREAD_WIN32),yes)
-  SRCS += win32/SDL_sysmutex.c \
-          win32/SDL_syssem.c \
-          win32/SDL_systhread.c
-endif
-
-LOCAL_SRC_FILES += $(SRCS:%=src/thread/%)
-
-# the Timer sources
-#
-
-SRCS := SDL_timer.c
-
-ifeq ($(CONFIG_TIMER_UNIX),yes)
-  SRCS += unix/SDL_systimer.c
-endif
-
-ifeq ($(CONFIG_TIMER_WIN32),yes)
-  SRCS += win32/SDL_systimer.c
-endif
-
-LOCAL_SRC_FILES += $(SRCS:%=src/timer/%)
-
-# the Video sources
-#
-
-SRCS := SDL_RLEaccel.c \
-	SDL_blit.c \
-	SDL_blit_0.c \
-	SDL_blit_1.c \
-	SDL_blit_A.c \
-	SDL_blit_N.c \
-	SDL_bmp.c \
-	SDL_cursor.c \
-	SDL_gamma.c \
-	SDL_pixels.c \
-	SDL_stretch.c \
-	SDL_surface.c \
-	SDL_video.c \
-	SDL_yuv.c \
-	SDL_yuv_mmx.c \
-	SDL_yuv_sw.c \
-
-SRCS += dummy/SDL_nullevents.c \
-        dummy/SDL_nullmouse.c \
-        dummy/SDL_nullvideo.c
-
-ifeq ($(CONFIG_VIDEO_WINDIB),yes)
-  SRCS += windib/SDL_dibevents.c \
-          windib/SDL_dibvideo.c \
-          wincommon/SDL_sysevents.c \
-          wincommon/SDL_sysmouse.c \
-          wincommon/SDL_syswm.c \
-          wincommon/SDL_wingl.c
-endif
-
-ifeq ($(CONFIG_VIDEO_QUARTZ),yes)
-  SRCS += quartz/SDL_QuartzGL.m \
-          quartz/SDL_QuartzVideo.m \
-          quartz/SDL_QuartzWM.m \
-          quartz/SDL_QuartzWindow.m \
-          quartz/SDL_QuartzYUV.m \
-          quartz/SDL_QuartzEvents.m \
-          maccommon/SDL_macevents.c \
-          maccommon/SDL_macgl.c \
-          maccommon/SDL_macmouse.c \
-          maccommon/SDL_macwm.c
-endif
-
-ifeq ($(CONFIG_VIDEO_X11),yes)
-  SRCS += x11/SDL_x11dyn.c \
-          x11/SDL_x11dga.c \
-          x11/SDL_x11events.c \
-          x11/SDL_x11gamma.c \
-          x11/SDL_x11gl.c \
-          x11/SDL_x11image.c \
-          x11/SDL_x11modes.c \
-          x11/SDL_x11mouse.c \
-          x11/SDL_x11video.c \
-          x11/SDL_x11wm.c \
-          x11/SDL_x11yuv.c
-endif
-
-ifeq ($(CONFIG_VIDEO_X11_DGAMOUSE),yes)
-  SRCS += x11/SDL_x11dga.c
-endif
-
-ifeq ($(CONFIG_VIDEO_X11_XME),yes)
-  SRCS += Xext/XME/xme.c
-endif
-
-ifeq ($(CONFIG_VIDEO_X11_XINERAMA),yes)
-  SRCS += Xext/Xinerama/Xinerama.c
-endif
-
-ifeq ($(CONFIG_VIDEO_X11_XV),yes)
-  SRCS += Xext/Xv/Xv.c
-endif
-
-LOCAL_SRC_FILES += $(SRCS:%=src/video/%)
+LOCAL_SRC_FILES := $(SDL_SOURCES)
 
 include $(BUILD_HOST_STATIC_LIBRARY)
 
@@ -232,21 +40,7 @@
 
 LOCAL_MODULE := libSDLmain
 
-SRCS :=
-
-ifeq ($(CONFIG_MAIN_DUMMY),yes)
-  SRCS += dummy/SDL_dummy_main.c
-endif
-
-ifeq ($(CONFIG_MAIN_MACOSX),yes)
-  SRCS += macosx/SDLMain.m
-endif
-
-ifeq ($(HOST_OS),windows)
-  SRCS += win32/SDL_win32_main.c
-endif
-
-LOCAL_SRC_FILES := $(SRCS:%=src/main/%)
+LOCAL_SRC_FILES := $(SDLMAIN_SOURCES)
 
 include $(BUILD_HOST_STATIC_LIBRARY)
 
diff --git a/distrib/sdl-1.2.12/android-configure.sh b/distrib/sdl-1.2.12/android-configure.sh
index a61ad78..133daf4 100755
--- a/distrib/sdl-1.2.12/android-configure.sh
+++ b/distrib/sdl-1.2.12/android-configure.sh
@@ -157,7 +157,7 @@
         ;;
     darwin)
         SDL_CFLAGS="-D_GNU_SOURCE=1 -D_THREAD_SAFE"
-        SDL_STATIC_LIBS="-Wl,-framework,OpenGL -Wl,-framework,Cocoa -Wl,-framework,QuickTime -Wl,-framework,ApplicationServices -Wl,-framework,Carbon -Wl,-framework,AudioToolbox -Wl,-framework,AudioUnit -Wl,-framework,IOKit"
+        SDL_STATIC_LIBS="-Wl,-framework,OpenGL -Wl,-framework,Cocoa -Wl,-framework,QuickTime -Wl,-framework,ApplicationServices -Wl,-framework,Carbon -Wl,-framework,IOKit"
         ;;
     windows)
         SDL_CFLAGS="-D_GNU_SOURCE=1 -Dmain=SDL_main"
@@ -439,7 +439,7 @@
         driver_list="`var_value ${1}_default`"
     fi
     for driver_name in $driver_list; do
-        make_add "$2_${driver_name} := yes"
+        make_add "SDL_$2_${driver_name} := yes"
     done
 }
 
@@ -454,8 +454,8 @@
     make_add "SRC_PATH=$PWD"
     make_add "BUILD_SYSTEM=$ANDROID_QEMU_PATH/android/build"
 
-    make_add "CONFIG_LIBC := $CONFIG_LIBC"
-    make_add "CONFIG_CPUINFO := yes"
+    make_add "SDL_CONFIG_LIBC := $CONFIG_LIBC"
+    make_add "SDL_CONFIG_CPUINFO := yes"
 
     make_add_driver_macros DRIVERS_LOADSO CONFIG_LOADSO
     make_add_driver_macros DRIVERS_THREAD CONFIG_THREAD
diff --git a/distrib/sdl-1.2.12/sources.make b/distrib/sdl-1.2.12/sources.make
new file mode 100644
index 0000000..dadcb3f
--- /dev/null
+++ b/distrib/sdl-1.2.12/sources.make
@@ -0,0 +1,244 @@
+# This is included from the main Android emulator build script
+# to declare the SDL-related sources, compiler flags and libraries
+#
+
+SDL_CFLAGS :=
+SDL_LDLIBS :=
+SDL_STATIC_LIBRARIES :=
+
+SDL_SOURCES :=
+
+ifeq ($(HOST_OS),linux)
+    SDL_CONFIG_LOADSO_DLOPEN := yes
+    SDL_CONFIG_THREAD_PTHREAD := yes
+    SDL_CONFIG_THREAD_PTHREAD_RECURSIVE_MUTEX_NP := yes
+    SDL_CONFIG_TIMER_UNIX := yes
+    SDL_CONFIG_VIDEO_X11 := yes
+    SDL_CONFIG_VIDEO_X11_DPMS := yes
+    SDL_CONFIG_VIDEO_X11_XINERAMA := yes
+    SDL_CONFIG_VIDEO_X11_XME := yes
+    SDL_CONFIG_MAIN_DUMMY := yes
+
+    SDL_CFLAGS += -D_GNU_SOURCE=1 -D_REENTRANT
+    SDL_LDLIBS += -lm -ldl -lpthread -lrt
+endif
+
+ifeq ($(HOST_OS),freebsd)
+    SDL_CONFIG_LOADSO_DLOPEN := yes
+    SDL_CONFIG_THREAD_PTHREAD := yes
+    SDL_CONFIG_THREAD_PTHREAD_RECURSIVE_MUTEX := yes
+    SDL_CONFIG_TIMER_UNIX := yes
+    SDL_CONFIG_VIDEO_X11 := yes
+    SDL_CONFIG_VIDEO_X11_DPMS := yes
+    SDL_CONFIG_VIDEO_X11_XINERAMA := yes
+    SDL_CONFIG_VIDEO_X11_XME := yes
+    SDL_CONFIG_MAIN_DUMMY := yes
+
+    SDL_CFLAGS += -D_GNU_SOURCE=1 -D_REENTRANT
+    SDL_LDLIBS += -lm -ldl -lpthread
+endif
+
+ifeq ($(HOST_OS),darwin)
+    SDL_CONFIG_LOADSO_DLCOMPAT := yes
+    SDL_CONFIG_THREAD_PTHREAD := yes
+    SDL_CONFIG_THREAD_PTHREAD_RECURSIVE_MUTEX := yes
+    SDL_CONFIG_TIMER_UNIX := yes
+    SDL_CONFIG_VIDEO_QUARTZ := yes
+    SDL_CONFIG_MAIN_MACOSX := yes
+
+    SDL_CFLAGS += -D_GNU_SOURCE=1 -DTHREAD_SAFE
+    FRAMEWORKS := OpenGL Cocoa QuickTime ApplicationServices Carbon IOKit
+    SDL_LDLIBS += $(FRAMEWORKS:%=-Wl,-framework,%)
+endif
+
+ifeq ($(HOST_OS),windows)
+    SDL_CONFIG_LOADSO_WIN32 := yes
+    SDL_CONFIG_THREAD_WIN32 := yes
+    SDL_CONFIG_TIMER_WIN32 := yes
+    SDL_CONFIG_VIDEO_WINDIB := yes
+    SDL_CONFIG_MAIN_WIN32 := yes
+
+    SDL_CFLAGS += -D_GNU_SOURCE=1 -Dmain=SDL_main -DNO_STDIO_REDIRECT=1
+    SDL_LDLIBS += -luser32 -lgdi32 -lwinmm
+endif
+
+
+# the main src/ sources
+#
+SRCS := SDL.c \
+        SDL_error.c \
+        SDL_fatal.c \
+
+SRCS += events/SDL_active.c \
+	events/SDL_events.c \
+	events/SDL_expose.c \
+	events/SDL_keyboard.c \
+	events/SDL_mouse.c \
+	events/SDL_quit.c \
+	events/SDL_resize.c \
+
+SRCS += file/SDL_rwops.c
+
+SRCS += stdlib/SDL_getenv.c \
+        stdlib/SDL_iconv.c \
+        stdlib/SDL_malloc.c \
+        stdlib/SDL_qsort.c \
+        stdlib/SDL_stdlib.c \
+        stdlib/SDL_string.c
+
+SRCS += cpuinfo/SDL_cpuinfo.c
+
+SDL_SOURCES += $(SRCS:%=$(SDL_DIR)/src/%)
+
+# the LoadSO sources
+#
+
+SRCS :=
+
+ifeq ($(SDL_CONFIG_LOADSO_DLOPEN),yes)
+  SRCS += dlopen/SDL_sysloadso.c
+  SDL_LDLIBS += -ldl
+endif
+
+ifeq ($(SDL_CONFIG_LOADSO_DLCOMPAT),yes)
+  SRCS += macosx/SDL_dlcompat.c
+endif
+
+ifeq ($(SDL_CONFIG_LOADSO_WIN32),yes)
+  SRCS += win32/SDL_sysloadso.c
+endif
+
+SDL_SOURCES += $(SRCS:%=$(SDL_DIR)/src/loadso/%)
+
+# the Thread sources
+#
+
+SRCS := SDL_thread.c
+
+ifeq ($(SDL_CONFIG_THREAD_PTHREAD),yes)
+  SRCS += pthread/SDL_syscond.c \
+          pthread/SDL_sysmutex.c \
+          pthread/SDL_syssem.c \
+          pthread/SDL_systhread.c
+endif
+
+ifeq ($(SDL_CONFIG_THREAD_WIN32),yes)
+  SRCS += win32/SDL_sysmutex.c \
+          win32/SDL_syssem.c \
+          win32/SDL_systhread.c
+endif
+
+SDL_SOURCES += $(SRCS:%=$(SDL_DIR)/src/thread/%)
+
+# the Timer sources
+#
+
+SRCS := SDL_timer.c
+
+ifeq ($(SDL_CONFIG_TIMER_UNIX),yes)
+  SRCS += unix/SDL_systimer.c
+endif
+
+ifeq ($(SDL_CONFIG_TIMER_WIN32),yes)
+  SRCS += win32/SDL_systimer.c
+endif
+
+SDL_SOURCES += $(SRCS:%=$(SDL_DIR)/src/timer/%)
+
+# the Video sources
+#
+
+SRCS := SDL_RLEaccel.c \
+	SDL_blit.c \
+	SDL_blit_0.c \
+	SDL_blit_1.c \
+	SDL_blit_A.c \
+	SDL_blit_N.c \
+	SDL_bmp.c \
+	SDL_cursor.c \
+	SDL_gamma.c \
+	SDL_pixels.c \
+	SDL_stretch.c \
+	SDL_surface.c \
+	SDL_video.c \
+	SDL_yuv.c \
+	SDL_yuv_mmx.c \
+	SDL_yuv_sw.c \
+
+SRCS += dummy/SDL_nullevents.c \
+        dummy/SDL_nullmouse.c \
+        dummy/SDL_nullvideo.c
+
+ifeq ($(SDL_CONFIG_VIDEO_WINDIB),yes)
+  SRCS += windib/SDL_dibevents.c \
+          windib/SDL_dibvideo.c \
+          wincommon/SDL_sysevents.c \
+          wincommon/SDL_sysmouse.c \
+          wincommon/SDL_syswm.c \
+          wincommon/SDL_wingl.c
+endif
+
+ifeq ($(SDL_CONFIG_VIDEO_QUARTZ),yes)
+  SRCS += quartz/SDL_QuartzGL.m \
+          quartz/SDL_QuartzVideo.m \
+          quartz/SDL_QuartzWM.m \
+          quartz/SDL_QuartzWindow.m \
+          quartz/SDL_QuartzYUV.m \
+          quartz/SDL_QuartzEvents.m \
+          maccommon/SDL_macevents.c \
+          maccommon/SDL_macgl.c \
+          maccommon/SDL_macmouse.c \
+          maccommon/SDL_macwm.c
+
+endif
+
+ifeq ($(SDL_CONFIG_VIDEO_X11),yes)
+  SRCS += x11/SDL_x11dyn.c \
+          x11/SDL_x11dga.c \
+          x11/SDL_x11events.c \
+          x11/SDL_x11gamma.c \
+          x11/SDL_x11gl.c \
+          x11/SDL_x11image.c \
+          x11/SDL_x11modes.c \
+          x11/SDL_x11mouse.c \
+          x11/SDL_x11video.c \
+          x11/SDL_x11wm.c \
+          x11/SDL_x11yuv.c
+endif
+
+ifeq ($(SDL_CONFIG_VIDEO_X11_DGAMOUSE),yes)
+  SRCS += x11/SDL_x11dga.c
+endif
+
+ifeq ($(SDL_CONFIG_VIDEO_X11_XME),yes)
+  SRCS += Xext/XME/xme.c
+endif
+
+ifeq ($(SDL_CONFIG_VIDEO_X11_XINERAMA),yes)
+  SRCS += Xext/Xinerama/Xinerama.c
+endif
+
+ifeq ($(SDL_CONFIG_VIDEO_X11_XV),yes)
+  SRCS += Xext/Xv/Xv.c
+endif
+
+SDL_SOURCES += $(SRCS:%=$(SDL_DIR)/src/video/%)
+
+## Build libSDLmain
+##
+
+SRCS :=
+
+ifeq ($(SDL_CONFIG_MAIN_DUMMY),yes)
+  SRCS += dummy/SDL_dummy_main.c
+endif
+
+ifeq ($(SDL_CONFIG_MAIN_MACOSX),yes)
+  SRCS += macosx/SDLMain.m
+endif
+
+ifeq ($(SDL_CONFIG_MAIN_WIN32),yes)
+  SRCS += win32/SDL_win32_main.c
+endif
+
+SDLMAIN_SOURCES := $(SRCS:%=$(SDL_DIR)/src/main/%)