More work on getting Dalvik to build on the host (particularly OSX).

The dalvikvm executable still doesn't quite build (fails in linker), which
is probably due to the core library native code not getting compiled and
linked. Unfortunately, it looks like that will be pretty hairy to untangle.

Change-Id: Ib5de623e43a40116c4b7459a45a246d76886edf3
diff --git a/dalvikvm/Android.mk b/dalvikvm/Android.mk
index 216d08e..f463c85 100644
--- a/dalvikvm/Android.mk
+++ b/dalvikvm/Android.mk
@@ -14,20 +14,61 @@
 
 
 LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
 
-LOCAL_SRC_FILES:= \
+#
+# Common definitions.
+#
+
+dalvikvm_src_files := \
     Main.c
 
-LOCAL_C_INCLUDES := \
-	$(JNI_H_INCLUDE) \
-	dalvik/include
+dalvikvm_c_includes := \
+    $(JNI_H_INCLUDE) \
+    dalvik/include
+
+
+#
+# Build for the target (device).
+#
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(dalvikvm_src_files)
+LOCAL_C_INCLUDES := $(dalvikvm_c_includes)
 
 LOCAL_SHARED_LIBRARIES := \
-	libdvm \
-	libssl \
-	libz
+    libdvm \
+    libssl \
+    libz
 
-LOCAL_MODULE:= dalvikvm
+LOCAL_MODULE := dalvikvm
 
 include $(BUILD_EXECUTABLE)
+
+
+#
+# Build for the host.
+#
+
+ifeq ($(WITH_HOST_DALVIK),true)
+
+    include $(CLEAR_VARS)
+
+    LOCAL_SRC_FILES := $(dalvikvm_src_files)
+    LOCAL_C_INCLUDES := $(dalvikvm_c_includes)
+
+    LOCAL_STATIC_LIBRARIES := \
+        libdvm-host
+
+    ifeq ($(HOST_OS)-$(HOST_ARCH),darwin-x86)
+        # OSX comes with libz and libssl, so there is no need to build them.
+        LOCAL_LDLIBS := -lssl -lz
+    else
+        LOCAL_STATIC_LIBRARIES += libssl libz
+    endif
+
+    LOCAL_MODULE := dalvikvm-host
+
+    include $(BUILD_HOST_EXECUTABLE)
+
+endif
diff --git a/libnativehelper/Android.mk b/libnativehelper/Android.mk
index aa4be86..f030fea 100644
--- a/libnativehelper/Android.mk
+++ b/libnativehelper/Android.mk
@@ -55,8 +55,8 @@
 
 LOCAL_SRC_FILES := $(src_files)
 LOCAL_C_INCLUDES := $(c_includes)
-LOCAL_SHARED_LIBRARIES := $(shared_libraries)
 LOCAL_STATIC_LIBRARIES := $(static_libraries)
+LOCAL_SHARED_LIBRARIES := $(shared_libraries)
 
 # liblog and libcutils are shared for target.
 LOCAL_SHARED_LIBRARIES += \
@@ -77,15 +77,23 @@
 
     LOCAL_SRC_FILES := $(src_files)
     LOCAL_C_INCLUDES := $(c_includes)
-    LOCAL_SHARED_LIBRARIES := $(shared_libraries)
     LOCAL_STATIC_LIBRARIES := $(static_libraries)
 
+    ifeq ($(HOST_OS)-$(HOST_ARCH),darwin-x86)
+        # OSX has a lot of libraries built in, which we don't have to
+        # bother building; just include them on the ld line.
+        LOCAL_LDLIBS := -lexpat -lssl -lz -lcrypto -licucore -lsqlite3
+	LOCAL_STATIC_LIBRARIES += libutils
+    else
+        LOCAL_SHARED_LIBRARIES := $(shared_libraries)
+    endif
+
     # liblog and libcutils are static for host.
     LOCAL_STATIC_LIBRARIES += \
         liblog libcutils
 
     LOCAL_MODULE := libnativehelper-host
 
-    include $(BUILD_HOST_SHARED_LIBRARY)
+    include $(BUILD_HOST_STATIC_LIBRARY)
 
 endif
diff --git a/vm/Android.mk b/vm/Android.mk
index 5c276aa..6e71655 100644
--- a/vm/Android.mk
+++ b/vm/Android.mk
@@ -69,14 +69,21 @@
     LOCAL_STATIC_LIBRARIES += \
         liblog libcutils
 
-    # libffi is called libffi-host on the host. Similarly libnativehelper.
-    LOCAL_SHARED_LIBRARIES := \
-        $(patsubst libffi,libffi-host,$(LOCAL_SHARED_LIBRARIES))
-    LOCAL_SHARED_LIBRARIES := \
-        $(patsubst libnativehelper,libnativehelper-host,$(LOCAL_SHARED_LIBRARIES))
+    # libffi is called libffi-host on the host and should be staticly
+    # linked. Similarly libnativehelper.
+    ifneq (,$(findstring libffi,$(LOCAL_SHARED_LIBRARIES)))
+        LOCAL_SHARED_LIBRARIES := \
+            $(patsubst libffi, ,$(LOCAL_SHARED_LIBRARIES))
+        LOCAL_STATIC_LIBRARIES += libffi-host
+    endif
+    ifneq (,$(findstring libnativehelper,$(LOCAL_SHARED_LIBRARIES)))
+        LOCAL_SHARED_LIBRARIES := \
+            $(patsubst libnativehelper, ,$(LOCAL_SHARED_LIBRARIES))
+        LOCAL_STATIC_LIBRARIES += libnativehelper-host
+    endif
 
     LOCAL_MODULE := libdvm-host
 
-    include $(BUILD_HOST_SHARED_LIBRARY)
+    include $(BUILD_HOST_STATIC_LIBRARY)
 
 endif
diff --git a/vm/Dvm.mk b/vm/Dvm.mk
index 223b895..71ebf37 100644
--- a/vm/Dvm.mk
+++ b/vm/Dvm.mk
@@ -316,7 +316,13 @@
 ifeq ($(MTERP_ARCH_KNOWN),false)
   # unknown architecture, try to use FFI
   LOCAL_C_INCLUDES += external/libffi/$(dvm_os)-$(dvm_arch)
-  LOCAL_SHARED_LIBRARIES += libffi
+
+  ifeq ($(dvm_os)-$(dvm_arch),darwin-x86)
+      # OSX includes libffi, so just make the linker aware of it directly.
+      LOCAL_LDLIBS += -lffi
+  else
+      LOCAL_SHARED_LIBRARIES += libffi
+  endif
 
   LOCAL_SRC_FILES += \
 		arch/generic/Call.c \