From 3d3964b1190c03ff7463aaae9f712dfc67291c91 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Jos=C3=A9=20Bollo?= <jose.bollo@iot.bzh>
Date: Thu, 16 Mar 2017 10:10:58 +0100
Subject: [PATCH] Allows to compile without libsystemd
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

This fixes a problem with bitbake when building
native packages.

Change-Id: I28586720010237db578ec55cb5254212c88240e7
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
---
 src/CMakeLists.txt  | 81 +++++++++++++++++++++++++++++++----------------------
 src/utils-systemd.c | 11 ++++++--
 2 files changed, 56 insertions(+), 36 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b4894b6..903a7de 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -41,7 +41,7 @@ set(CMAKE_C_FLAGS_CCOV         "-g -O2 --coverage")
 
 include(FindPkgConfig)
 
-pkg_check_modules(EXTRAS REQUIRED libxml-2.0 openssl xmlsec1 xmlsec1-openssl json-c libsystemd>=222)
+pkg_check_modules(EXTRAS REQUIRED libxml-2.0 openssl xmlsec1 xmlsec1-openssl json-c)
 add_compile_options(${EXTRAS_CFLAGS})
 include_directories(${EXTRAS_INCLUDE_DIRS})
 link_libraries(${EXTRAS_LIBRARIES})
@@ -56,6 +56,15 @@ else()
 	add_definitions(-DUSE_LIBZIP=0)
 endif()
 
+pkg_check_modules(libsystemd libsystemd>=222)
+if(libsystemd_FOUND)
+	add_compile_options(${libsystemd_CFLAGS})
+	include_directories(${libsystemd_INCLUDE_DIRS})
+	link_libraries(${libsystemd_LIBRARIES})
+else()
+	add_definitions(-DNO_LIBSYSTEMD)
+endif()
+
 ###########################################################################
 
 if(SIMULATE_SECMGR)
@@ -100,7 +109,6 @@ add_library(utils STATIC
 	utils-dir.c
 	utils-file.c
 	utils-json.c
-	utils-jbus.c
 	utils-systemd.c
 	verbose.c
 	)
@@ -144,38 +152,43 @@ target_link_libraries(wgtpkg-installer wgtpkg wgt secwrp utils)
 
 install(TARGETS wgtpkg-sign wgtpkg-pack wgtpkg-info wgtpkg-installer DESTINATION ${CMAKE_INSTALL_BINDIR})
 
-###########################################################################
-# the daemons
-MESSAGE(STATUS "Creating daemons")
-
-add_executable(afm-user-daemon afm-user-daemon.c)
-target_link_libraries(afm-user-daemon afm secwrp wgt utils)
-install(TARGETS afm-user-daemon DESTINATION ${CMAKE_INSTALL_BINDIR})
-
-add_executable(afm-system-daemon afm-system-daemon.c)
-target_link_libraries(afm-system-daemon wgtpkg afm secwrp wgt utils)
-install(TARGETS afm-system-daemon DESTINATION ${CMAKE_INSTALL_BINDIR})
-
-###########################################################################
-# the binding for afb
-
-pkg_check_modules(AFB afb-daemon)
-if(AFB_FOUND)
-	message(STATUS "Creation afm-main-binding for AFB-DAEMON")
-	###############################################################
-	pkg_get_variable(afb_binding_install_dir afb-daemon binding_install_dir)
-	###############################################################
-	add_library(afm-main-binding MODULE afm-main-binding.c)
-	target_compile_options(afm-main-binding PRIVATE ${AFB_CFLAGS})
-	target_include_directories(afm-main-binding PRIVATE ${AFB_INCLUDE_DIRS})
-	target_link_libraries(afm-main-binding utils ${AFB_LIBRARIES})
-	set_target_properties(afm-main-binding PROPERTIES
-		PREFIX ""
-		LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/afm-main-binding.export-map"
-	)
-	install(TARGETS afm-main-binding LIBRARY DESTINATION ${afb_binding_install_dir})
-else()
-	message(STATUS "Not creating the binding for AFB-DAEMON")
+if(libsystemd_FOUND)
+	###########################################################################
+	# the daemons
+
+	MESSAGE(STATUS "Creating daemons")
+
+	add_library(jbus STATIC utils-jbus.c)
+
+	add_executable(afm-user-daemon afm-user-daemon.c)
+	target_link_libraries(afm-user-daemon afm secwrp wgt utils jbus)
+	install(TARGETS afm-user-daemon DESTINATION ${CMAKE_INSTALL_BINDIR})
+
+	add_executable(afm-system-daemon afm-system-daemon.c)
+	target_link_libraries(afm-system-daemon wgtpkg afm secwrp wgt utils jbus)
+	install(TARGETS afm-system-daemon DESTINATION ${CMAKE_INSTALL_BINDIR})
+
+	###########################################################################
+	# the binding for afb
+
+	pkg_check_modules(AFB afb-daemon)
+	if(AFB_FOUND)
+		message(STATUS "Creation afm-main-binding for AFB-DAEMON")
+		###############################################################
+		pkg_get_variable(afb_binding_install_dir afb-daemon binding_install_dir)
+		###############################################################
+		add_library(afm-main-binding MODULE afm-main-binding.c)
+		target_compile_options(afm-main-binding PRIVATE ${AFB_CFLAGS})
+		target_include_directories(afm-main-binding PRIVATE ${AFB_INCLUDE_DIRS})
+		target_link_libraries(afm-main-binding utils jbus ${AFB_LIBRARIES})
+		set_target_properties(afm-main-binding PROPERTIES
+			PREFIX ""
+			LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/afm-main-binding.export-map"
+		)
+		install(TARGETS afm-main-binding LIBRARY DESTINATION ${afb_binding_install_dir})
+	else()
+		message(STATUS "Not creating the binding for AFB-DAEMON")
+	endif()
 endif()
 
 ###########################################################################
diff --git a/src/utils-systemd.c b/src/utils-systemd.c
index b8c3c0b..d804144 100644
--- a/src/utils-systemd.c
+++ b/src/utils-systemd.c
@@ -27,8 +27,15 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include <systemd/sd-bus.h>
-#include <systemd/sd-bus-protocol.h>
+#ifndef NO_LIBSYSTEMD
+# include <systemd/sd-bus.h>
+# include <systemd/sd-bus-protocol.h>
+#else
+  struct sd_bus;
+# define sd_bus_default_user(p)   ((*(p)=NULL),(-ENOTSUP))
+# define sd_bus_default_system(p) ((*(p)=NULL),(-ENOTSUP))
+# define sd_bus_call_method(...)  (-ENOTSUP)
+#endif
 
 #include "utils-systemd.h"
 
-- 
2.16.6