Fix OSX standalone build to use the right XCode SDK.

XCode changes the location of the SDKs, so use the same logic
as the platform build to detect it and select the "older" SDK
found on the machine.

 + Add missing configuration headers for the standalone
   Darwin 64-bits build.

Change-Id: I27a0f533732123044d0598a17301985c24256c68
diff --git a/Makefile.android b/Makefile.android
index 00039bd..a166ccb 100644
--- a/Makefile.android
+++ b/Makefile.android
@@ -61,13 +61,19 @@
 endif
 
 ifeq ($(HOST_OS),darwin)
-    MY_CFLAGS += -mdynamic-no-pic -D_DARWIN_C_SOURCE=1
+    MY_CFLAGS += -D_DARWIN_C_SOURCE=1
     ifneq ($(host_toolchain_header),)
+        MY_CFLAGS += -mdynamic-no-pic
         MY_CFLAGS += -isystem $(host_toolchain_header)
-    endif
-    MY_CFLAGS += -isysroot $(mac_sdk_root) -mmacosx-version-min=$(mac_sdk_version) -DMACOSX_DEPLOYMENT_TARGET=$(mac_sdk_version)
-    MY_LDLIBS += -isysroot $(mac_sdk_root) -Wl,-syslibroot,$(mac_sdk_root) -mmacosx-version-min=$(mac_sdk_version)
+    else
+        ifneq (,$(mac_sdk_root))
+            MY_CFLAGS += -isysroot $(mac_sdk_root) -mmacosx-version-min=$(mac_sdk_version) -DMACOSX_DEPLOYMENT_TARGET=$(mac_sdk_version)
+            MY_LDLIBS += -isysroot $(mac_sdk_root) -Wl,-syslibroot,$(mac_sdk_root) -mmacosx-version-min=$(mac_sdk_version)
 
+            # Clang complains about this flag being not useful anymore.
+            MY_CFLAGS := $(filter-out -falign-functions=0,$(MY_CFLAGS))
+        endif
+    endif
 endif
 
 # Some CFLAGS below use -Wno-missing-field-initializers but this is not
diff --git a/android-configure.sh b/android-configure.sh
index 1d65e1f..ad77bd2 100755
--- a/android-configure.sh
+++ b/android-configure.sh
@@ -321,6 +321,36 @@
     done
 fi
 
+# For OS X, detect the location of the SDK to use.
+if [ "$HOST_OS" = darwin ]; then
+    OSX_VERSION=$(sw_vers -productVersion)
+    OSX_SDK_SUPPORTED="10.6 10.7 10.8"
+    OSX_SDK_INSTALLED_LIST=$(xcodebuild -showsdks 2>/dev/null | grep macosx | sed -e "s/.*macosx//g" | sort -n)
+    if [ -z "$OSX_SDK_INSTALLED_LIST" ]; then
+        echo "ERROR: Please install XCode on this machine!"
+        exit 1
+    fi
+    log "OSX: Installed SDKs: $OSX_SDK_INSTALLED_LIST"
+
+    OSX_SDK_VERSION=$(echo "$OSX_SDK_INSTALLED_LIST" | tr ' ' '\n' | head -1)
+    log "OSX: Using SDK version $OSX_SDK_VERSION"
+
+    XCODE_PATH=$(xcode-select -print-path 2>/dev/null)
+    log "OSX: XCode path: $XCODE_PATH"
+
+    OSX_SDK_ROOT=$XCODE_PATH/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${OSX_SDK_VERSION}.sdk
+    log "OSX: Looking for $OSX_SDK_ROOT"
+    if [ ! -d "$OSX_SDK_ROOT" ]; then
+        OSX_SDK_ROOT=/Developer/SDKs/MaxOSX${OSX_SDK_VERSION}.sdk
+        log "OSX: Looking for $OSX_SDK_ROOT"
+        if [ ! -d "$OSX_SDK_ROOT" ]; then
+            echo "ERROR: Could not find SDK $OSX_SDK_VERSION at $OSX_SDK_ROOT"
+            exit 1
+        fi
+    fi
+    echo "OSX SDK   : Found at $OSX_SDK_ROOT"
+fi
+
 # we can build the emulator with Cygwin, so enable it
 enable_cygwin
 
@@ -574,6 +604,11 @@
     echo "QEMU_OPENGLES_LIBS       := $GLES_LIBS"    >> $config_mk
 fi
 
+if [ "$HOST_OS" = "darwin" ]; then
+    echo "mac_sdk_root := $OSX_SDK_ROOT" >> $config_mk
+    echo "mac_sdk_version := $OSX_SDK_VERSION" >> $config_mk
+fi
+
 # Build the config-host.h file
 #
 config_h=objs/config-host.h
diff --git a/android/build/common.sh b/android/build/common.sh
index e3e190b..230fa8a 100644
--- a/android/build/common.sh
+++ b/android/build/common.sh
@@ -110,6 +110,7 @@
 #   linux-x86
 #   linux-x86_64
 #   darwin-x86
+#   darwin-x86_64
 #   darwin-ppc
 #   windows  (MSys)
 #   cygwin
@@ -263,7 +264,6 @@
             # this is highly dependent on your GCC installation (and no, we can't set
             # this flag all the time)
             CFLAGS="$CFLAGS -Wa,--32"
-            compile
         fi
     fi
 
diff --git a/android/config/darwin-x86_64/SDL_config.h b/android/config/darwin-x86_64/SDL_config.h
new file mode 100644
index 0000000..48ca0c7
--- /dev/null
+++ b/android/config/darwin-x86_64/SDL_config.h
@@ -0,0 +1,120 @@
+/* This file was autogenerated by 'android-configure.sh' - do not edit */
+#ifndef _SDL_config_h
+#define _SDL_config_h
+
+#include "SDL_platform.h"
+
+#define SDL_HAS_64BIT_TYPE 1
+#define SDL_BYTEORDER 1234
+
+#define HAVE_LIBC 1
+#if HAVE_LIBC
+
+/* Useful headers */
+#define HAVE_ALLOCA_H 1
+#define HAVE_SYS_TYPES_H 1
+#define HAVE_STDIO_H 1
+#define STDC_HEADERS  1
+#define HAVE_STDLIB_H 1
+#define HAVE_STDARG_H 1
+/* #undef HAVE_MALLOC_H */
+#define HAVE_MEMORY_H 1
+#define HAVE_STRING_H 1
+#define HAVE_STRINGS_H 1
+#define HAVE_INTTYPES_H 1
+#define HAVE_STDINT_H 1
+#define HAVE_CTYPE_H 1
+#define HAVE_MATH_H 1
+#define HAVE_ICONV_H 1
+#define HAVE_SIGNAL_H 1
+/* #undef HAVE_ALTIVEC_H */
+/* C library functions */
+#define HAVE_MALLOC 1
+#define HAVE_CALLOC 1
+#define HAVE_REALLOC 1
+#define HAVE_FREE 1
+/* #undef HAVE_ALLOC */
+#ifndef _WIN32 /* Don't use on Windows */
+#define HAVE_GETENV 1
+#define HAVE_PUTENV 1
+#define HAVE_UNSETENV 1
+#endif
+#define HAVE_QSORT 1
+#define HAVE_ABS 1
+#define HAVE_BCOPY 1
+#define HAVE_MEMSET 1
+#define HAVE_MEMCPY 1
+#define HAVE_MEMMOVE 1
+#define HAVE_MEMCMP 1
+#define HAVE_STRLEN 1
+#define HAVE_STRLCPY 1
+#define HAVE_STRLCAT 1
+#define HAVE_STRDUP 1
+/* #undef HAVE__STRREV */
+/* #undef HAVE__STRUPR */
+/* #undef HAVE__STRLWR */
+#define HAVE_INDEX 1
+#define HAVE_RINDEX 1
+#define HAVE_STRCHR 1
+#define HAVE_STRRCHR 1
+/* #undef HAVE_ITOA */
+/* #undef HAVE__LTOA */
+/* #undef HAVE__UITOA */
+/* #undef HAVE__ULTOA */
+#define HAVE_STRTOL 1
+#define HAVE_STRTOUL 1
+/* #undef HAVE__I64TOA */
+/* #undef HAVE__UI64TOA */
+#define HAVE_STRTOLL 1
+#define HAVE_STRTOULL 1
+#define HAVE_STRTOD 1
+#define HAVE_ATOI 1
+#define HAVE_ATOF 1
+#define HAVE_STRCMP 1
+#define HAVE_STRNCMP 1
+/* #undef HAVE__STRICMP */
+#define HAVE_STRCASECMP 1
+/* #undef HAVE__STRNICMP */
+#define HAVE_VSNPRINTF 1
+/* #undef HAVE_ICONV */
+#define HAVE_SIGACTION 1
+#define HAVE_SETJMP 1
+#define HAVE_NANOSLEEP 1
+/* #undef HAVE_CLOCK_GETTIME */
+/* #undef HAVE_DLVSYM */
+#define HAVE_GETPAGESIZE 1
+#else
+/* We may need some replacement for stdarg.h here */
+#include <stdarg.h>
+#endif /* HAVE_LIBC */
+
+/* Allow disabling of core subsystems */
+#define SDL_AUDIO_DISABLED 1
+#define SDL_CDROM_DISABLED 1
+/* #undef SDL_CPUINFO_DISABLED */
+/* #undef SDL_EVENTS_DISABLED */
+#define SDL_FILE_DISABLED 1
+#define SDL_JOYSTICK_DISABLED 1
+/* #undef SDL_LOADSO_DISABLED */
+/* #undef SDL_THREADS_DISABLED */
+/* #undef SDL_TIMERS_DISABLED */
+/* #undef SDL_VIDEO_DISABLED */
+
+/* Enable various shared object loading systems */
+#define SDL_LOADSO_DLCOMPAT 1
+
+/* Enable various threading systems */
+#define SDL_THREAD_PTHREAD 1
+#define SDL_THREAD_PTHREAD_RECURSIVE_MUTEX 1
+
+/* Enable various timer systems */
+#define SDL_TIMER_UNIX 1
+
+/* Enable various video drivers */
+#define SDL_VIDEO_DRIVER_QUARTZ 1
+#define SDL_MAIN_MACOSX 1
+#define SDL_VIDEO_DRIVER_X11_DYNAMIC "libX11.so.6"
+#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT "libXext.so.6"
+#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR "libXrandr.so.2"
+#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRENDER "libXrender.so.1"
+#endif /* _SDL_config_h */
diff --git a/android/config/darwin-x86_64/config-host.h b/android/config/darwin-x86_64/config-host.h
new file mode 100644
index 0000000..f66a35d
--- /dev/null
+++ b/android/config/darwin-x86_64/config-host.h
@@ -0,0 +1,31 @@
+/* This file was autogenerated by 'android-configure.sh' */
+
+#define CONFIG_QEMU_SHAREDIR   "/usr/local/share/qemu"
+
+#if defined(__x86_64__)
+#define HOST_X86_64    1
+#define HOST_LONG_BITS  64
+#elif defined(__i386__)
+#define HOST_I386    1
+#define HOST_LONG_BITS  32
+#else
+#error Unknown architecture for codegen
+#endif
+
+#define CONFIG_FNMATCH  1
+#define CONFIG_GDBSTUB  1
+#define CONFIG_SLIRP    1
+#define CONFIG_SKINS    1
+#define CONFIG_TRACE    1
+#define CONFIG_POSIX  1
+#define CONFIG_MADVISE  1
+#define CONFIG_NAND_LIMITS  1
+#define QEMU_VERSION    "0.10.50"
+#define QEMU_PKGVERSION "Android"
+#define CONFIG_IOVEC 1
+#define CONFIG_DARWIN   1
+#define CONFIG_BSD       1
+#define O_LARGEFILE      0
+#define MAP_ANONYMOUS    MAP_ANON
+#define CONFIG_ANDROID       1
+#define CONFIG_ANDROID_OPENGLES 1