From 1e28869c2db679f3a2873ea29a911b8f456efea8 Mon Sep 17 00:00:00 2001
From: Derek Foreman <derek.foreman@collabora.co.uk>
Date: Thu, 2 Jun 2011 10:42:42 -0400
Subject: [PATCH] Support building on android

---
 .gitignore               |    1 +
 Android.mk               |   40 ++++++++++++++++++++++++++++++++++++++++
 bus/Makefile.am          |    9 +++++++++
 bus/main.c               |    8 ++++++++
 dbus/Makefile.am         |   14 ++++++++++++++
 dbus/dbus-internals.c    |   23 +++++++++++++++--------
 dbus/dbus-message-util.c |    4 ++++
 dbus/dbus-transport.c    |    4 ++++
 dbus/sd-daemon.c         |    4 ++++
 tools/Makefile.am        |   17 +++++++++++++++++
 10 files changed, 116 insertions(+), 8 deletions(-)
 create mode 100644 Android.mk

diff --git a/.gitignore b/.gitignore
index db3db97..13dc88f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -41,3 +41,4 @@ tags
 /lcov.html/
 /lcov.info
 /lcov.info.tmp
+Android.mk
diff --git a/Android.mk b/Android.mk
new file mode 100644
index 0000000..0d03e8a
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,40 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+DBUS_BUILT_SOURCES := \
+	dbus/Android.mk \
+	bus/Android.mk \
+	tools/Android.mk
+
+DBUS_BUILT_SOURCES := $(patsubst %, $(abspath $(DBUS_TOP))/%, $(DBUS_BUILT_SOURCES))
+
+.PHONY: dbus-configure
+
+dbus-configure-real:
+	cd $(DBUS_TOP) ; \
+	CC="$(CONFIGURE_CC)" \
+	CFLAGS="$(CONFIGURE_CFLAGS)" \
+	LD=$(TARGET_LD) \
+	LDFLAGS="$(CONFIGURE_LDFLAGS)" \
+	CPP=$(CONFIGURE_CPP) \
+	CPPFLAGS="$(CONFIGURE_CPPFLAGS)" \
+	PKG_CONFIG_LIBDIR="$(CONFIGURE_PKG_CONFIG_LIBDIR)" \
+	PKG_CONFIG_TOP_BUILD_DIR=$(PKG_CONFIG_TOP_BUILD_DIR) \
+	ac_cv_func_posix_getpwnam_r=no \
+	ac_cv_lib_expat_XML_ParserCreate_MM=yes \
+	$(DBUS_TOP)/$(CONFIGURE) --host=arm-linux-androideabi && \
+	for file in $(DBUS_BUILT_SOURCES); do \
+		rm -f $$file && \
+		make -C $$(dirname $$file) $$(basename $$file) ; \
+	done
+
+$(BUILT_SOURCE):
+	make -C $$(dirname $(DBUS_TOP)/$@) $$(basename $@)
+
+dbus-configure: dbus-configure-real $(BUILT_SOURCES)
+
+CONFIGURE_TARGETS += dbus-configure
+
+-include $(DBUS_TOP)/dbus/Android.mk
+-include $(DBUS_TOP)/bus/Android.mk
+-include $(DBUS_TOP)/tools/Android.mk
diff --git a/bus/Makefile.am b/bus/Makefile.am
index 6cbc09a..37cdd82 100644
--- a/bus/Makefile.am
+++ b/bus/Makefile.am
@@ -168,6 +168,15 @@ if DBUS_UNIX
 libexec_PROGRAMS = dbus-daemon-launch-helper
 endif DBUS_UNIX
 
+Android.mk: Makefile.am
+	androgenizer -:PROJECT dbus -:SHARED libdbus-daemon -:TAGS eng debug \
+	 -:REL_TOP $(top_srcdir) -:ABS_TOP $(abs_top_srcdir) \
+	 -:SOURCES $(dbus_daemon_SOURCES) \
+	 -:CFLAGS $(DEFS) $(CFLAGS) -DBUILD_AS_ANDROID_SERVICE \
+	 -:CPPFLAGS $(CPPFLAGS) $(AM_CPPFLAGS) $(INCLUDES) $(DEFAULT_INCLUDES) \
+	 -:LDFLAGS $(dbus_daemon_LDADD) -llog \
+	> $@
+
 ## Note that TESTS has special meaning (stuff to use in make check).
 ## We don't actually want to run any of these tests until test/ has been
 ## compiled, so we don't put them in TESTS here; we run them in test/
diff --git a/bus/main.c b/bus/main.c
index a0bf07c..32d1c5b 100644
--- a/bus/main.c
+++ b/bus/main.c
@@ -44,6 +44,10 @@ static BusContext *context;
 
 #ifdef DBUS_UNIX
 
+#ifdef BUILD_AS_ANDROID_SERVICE
+int dbus_daemon_main (int argc, char **argv);
+#endif
+
 static int reload_pipe[2];
 #define RELOAD_READ_END 0
 #define RELOAD_WRITE_END 1
@@ -342,7 +346,11 @@ close_reload_pipe (DBusWatch **watch)
 #endif /* DBUS_UNIX */
 
 int
+#ifdef BUILD_AS_ANDROID_SERVICE
+dbus_daemon_main (int argc, char **argv)
+#else
 main (int argc, char **argv)
+#endif
 {
   DBusError error;
   DBusString config_file;
diff --git a/dbus/Makefile.am b/dbus/Makefile.am
index 3c44ae4..5135188 100644
--- a/dbus/Makefile.am
+++ b/dbus/Makefile.am
@@ -281,6 +281,20 @@ libdbus_internal_la_LIBADD=$(LIBDBUS_LIBS)
 
 noinst_PROGRAMS =
 
+Android.mk: Makefile.am
+	androgenizer -:PROJECT dbus -:SHARED libdbus-1 -:TAGS eng debug \
+	 -:REL_TOP $(top_srcdir) -:ABS_TOP $(abs_top_srcdir) \
+	 -:SOURCES $(libdbus_1_la_SOURCES) \
+	 -:CFLAGS $(DEFS) $(AM_CFLAGS) $(CFLAGS) \
+	 -:CPPFLAGS $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) -I$$\(GLIB_TOP\) \
+	 -:LDFLAGS $(libdbus_1_0_la_LIBADD) -llog \
+	 -:STATIC libdbus-internal -:TAGS eng debug \
+	 -:SOURCES $(libdbus_internal_la_SOURCES) \
+	 -:CFLAGS $(DEFS) $(AM_CFLAGS) $(CFLAGS) \
+	 -:CPPFLAGS $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) -I$$\(GLIB_TOP\) \
+	 -:LDFLAGS $(libdbus_internal_la_LIBADD) \
+	> $@
+
 if DBUS_BUILD_TESTS
 # We can't actually run this til we've reached test/
 noinst_PROGRAMS += dbus-test
diff --git a/dbus/dbus-internals.c b/dbus/dbus-internals.c
index fcea079..8d54da2 100644
--- a/dbus/dbus-internals.c
+++ b/dbus/dbus-internals.c
@@ -625,11 +625,23 @@ _dbus_read_uuid_file_without_creating (const DBusString *filename,
   DBusString contents;
   DBusString decoded;
   int end;
+  const char *tempenv;
   
-  if (!_dbus_string_init (&contents))
+  tempenv = _dbus_getenv("DBUS_MACHINE_ID");
+  if (tempenv)
+    _dbus_string_init_const (&contents, tempenv);
+  else
     {
-      _DBUS_SET_OOM (error);
-      return FALSE;
+      if (!_dbus_string_init (&contents))
+        {
+          _DBUS_SET_OOM (error);
+          return FALSE;
+        }
+
+      if (!_dbus_file_get_contents (&contents, filename, error))
+        goto error;
+
+      _dbus_string_chop_white (&contents);
     }
 
   if (!_dbus_string_init (&decoded))
@@ -638,11 +650,6 @@ _dbus_read_uuid_file_without_creating (const DBusString *filename,
       _DBUS_SET_OOM (error);
       return FALSE;
     }
-  
-  if (!_dbus_file_get_contents (&contents, filename, error))
-    goto error;
-
-  _dbus_string_chop_white (&contents);
 
   if (_dbus_string_get_length (&contents) != DBUS_UUID_LENGTH_HEX)
     {
diff --git a/dbus/dbus-message-util.c b/dbus/dbus-message-util.c
index f785952..2ca70e8 100644
--- a/dbus/dbus-message-util.c
+++ b/dbus/dbus-message-util.c
@@ -40,6 +40,10 @@
 #include <errno.h>
 #endif
 
+#ifdef __BIONIC__
+#include <sys/select.h>
+#include <linux/time.h>
+#endif
 /**
  * @addtogroup DBusMessage
  * @{
diff --git a/dbus/dbus-transport.c b/dbus/dbus-transport.c
index f743d01..0f6ac69 100644
--- a/dbus/dbus-transport.c
+++ b/dbus/dbus-transport.c
@@ -677,8 +677,12 @@ auth_via_default_rules (DBusTransport *transport)
                          " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
                          _dbus_credentials_get_unix_uid(auth_identity),
                          _dbus_credentials_get_unix_uid(our_identity));
+#ifdef __BIONIC__
+	allow = TRUE;
+#else
       _dbus_transport_disconnect (transport);
       allow = FALSE;
+#endif
     }  
 
   _dbus_credentials_unref (our_identity);
diff --git a/dbus/sd-daemon.c b/dbus/sd-daemon.c
index 9c23b91..048d844 100644
--- a/dbus/sd-daemon.c
+++ b/dbus/sd-daemon.c
@@ -32,7 +32,11 @@
 #include <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#ifdef __BIONIC__
+#include <linux/fcntl.h>
+#else
 #include <sys/fcntl.h>
+#endif
 #include <netinet/in.h>
 #include <stdlib.h>
 #include <errno.h>
diff --git a/tools/Makefile.am b/tools/Makefile.am
index 08b9023..60dfa14 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -74,3 +74,20 @@ CLEANFILES = 				\
 #create the /var/lib/data directory for dbus-uuidgen
 localstatelibdir = $(localstatedir)/lib/dbus
 localstatelib_DATA =
+
+Android.mk: Makefile.am
+	androgenizer -:PROJECT dbus \
+	 -:REL_TOP $(top_srcdir) -:ABS_TOP $(abs_top_srcdir) \
+	 -:EXECUTABLE dbus-send -:TAGS eng debug \
+	 -:SOURCES $(dbus_send_SOURCES) \
+	 -:CFLAGS $(DEFS) $(CFLAGS) $(DEFAULT_INCLUDES) $(INCLUDES) \
+	  $(AM_CFLAGS) \
+	 -:CPPFLAGS $(CPPFLAGS) $(AM_CPPFLAGS) \
+	 -:LDFLAGS $(dbus_send_LDADD) -llog \
+	 -:EXECUTABLE dbus-monitor -:TAGS eng debug \
+	 -:SOURCES $(dbus_monitor_SOURCES) \
+	 -:CFLAGS $(DEFS) $(CFLAGS) $(DEFAULT_INCLUDES) $(INCLUDES) \
+	  $(AM_CFLAGS) \
+	 -:CPPFLAGS $(CPPFLAGS) $(AM_CPPFLAGS) \
+	 -:LDFLAGS $(dbus_monitor_LDADD) -llog \
+	> $@
-- 
1.7.7