]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
cmake: Add support for systemd integration on Linux operating systems
authorRalf Habacker <ralf.habacker@freenet.de>
Mon, 18 May 2020 10:44:37 +0000 (12:44 +0200)
committerSimon McVittie <smcv@collabora.com>
Wed, 10 Jun 2020 18:13:47 +0000 (18:13 +0000)
Previously, only the Autotools build system could do this. This commit
includes most of the same features as in the Autotools build, although
not the user-session semantics, which will be added separately.

Systemd support is controlled by the cmake variable ENABLE_SYSTEMD, which can
have the values OFF, ON and AUTO, the latter enabling support by default if
the required libraries are available.

With WITH_SYSTEMD_SYSTEMUNITDIR a custom installation location can be specified.
If it is not specified, the related install path is determined from the installed
systemd package, if present.

CMakeLists.txt
README.cmake
bus/CMakeLists.txt
cmake/config.h.cmake
cmake/modules/Macros.cmake
dbus/CMakeLists.txt

index a0095ebbb55023529a91e9b5ad2a83ddcebc4e3d..48cf967571991cdfda8a278f4e8649ac91a64ad2 100644 (file)
@@ -52,6 +52,10 @@ set(BUILD_TIMESTAMP ${DBUS_BUILD_TIMESTAMP})
 
 ########### basic vars ###############
 
+if(UNIX AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
+    set(DBUS_LINUX 1)
+endif()
+
 include(GNUInstallDirs)
 
 if(DBUSDIR)
@@ -135,6 +139,35 @@ option(DBUS_DISABLE_ASSERT "Disable assertion checking" OFF)
 option(DBUS_ENABLE_STATS "enable bus daemon usage statistics" OFF)
 option(DBUS_ENABLE_CONTAINERS "enable restricted servers for app-containers" OFF)
 
+if(DBUS_LINUX)
+    add_auto_option(ENABLE_SYSTEMD "build with systemd at_console support" AUTO)
+    include(FindPkgConfig)
+    pkg_check_modules(SYSTEMD libsystemd>=209)
+    if(NOT SYSTEMD_FOUND)
+        pkg_check_modules(SYSTEMD libsystemd-login>=32 libsystemd-daemon>=32 libsystemd-journal>=32)
+    endif()
+    check_auto_option(ENABLE_SYSTEMD "systemd support" SYSTEMD_FOUND "systemd")
+    if(ENABLE_SYSTEMD AND SYSTEMD_FOUND)
+        set(DBUS_BUS_ENABLE_SYSTEMD ON)
+        set(HAVE_SYSTEMD ${SYSTEMD_FOUND})
+    endif()
+    add_path_option(WITH_SYSTEMD_SYSTEMUNITDIR "Directory for systemd service files" "")
+    # get defaults
+    pkg_check_modules(_SYSTEMD systemd)
+    if(_SYSTEMD_FOUND)
+        pkg_get_variable(_SYSTEMD_PREFIX systemd prefix)
+        pkg_get_variable(_SYSTEMD_SYSTEMUNITDIR systemd systemdsystemunitdir)
+        pkg_get_variable(_SYSTEMD_USERUNITDIR systemd systemduserunitdir)
+        # remove install prefix, which may not match the current prefix
+        string(REPLACE "${_SYSTEMD_PREFIX}/" "" DBUS_SYSTEMD_SYSTEMUNITDIR ${_SYSTEMD_SYSTEMUNITDIR})
+    else()
+        set(DBUS_SYSTEMD_SYSTEMUNITDIR lib/systemd/system)
+    endif()
+    if(WITH_SYSTEMD_SYSTEMUNITDIR)
+        set(DBUS_SYSTEMD_SYSTEMUNITDIR ${WITH_SYSTEMD_SYSTEMUNITDIR})
+    endif()
+endif()
+
 if(WIN32)
     set(FD_SETSIZE "8192" CACHE STRING "The maximum number of connections that can be handled at once")
 endif()
@@ -609,6 +642,8 @@ message("        Building bus stats API:   ${DBUS_ENABLE_STATS}                "
 message("        installing system libs:   ${DBUS_INSTALL_SYSTEM_LIBS}         ")
 message("        Building inotify support: ${DBUS_BUS_ENABLE_INOTIFY}          ")
 message("        Building kqueue support:  ${DBUS_BUS_ENABLE_KQUEUE}           ")
+message("        Building systemd support: ${DBUS_BUS_ENABLE_SYSTEMD}          ")
+message("        systemd system install dir:${DBUS_SYSTEMD_SYSTEMUNITDIR}      ")
 message("        Building Doxygen docs:    ${DBUS_ENABLE_DOXYGEN_DOCS}         ")
 message("        Building Qt help docs:    ${DBUS_ENABLE_QTHELP_DOCS}          ")
 message("        Building XML docs:        ${DBUS_ENABLE_XML_DOCS}             ")
@@ -679,7 +714,6 @@ add_custom_target(help-options
 #
 if(DBUS_ENABLE_PKGCONFIG)
     set(PLATFORM_LIBS pthread ${LIBRT})
-    include(FindPkgConfig QUIET)
     if(PKG_CONFIG_FOUND)
         # convert lists of link libraries into -lstdc++ -lm etc..
         foreach(LIB ${CMAKE_C_IMPLICIT_LINK_LIBRARIES} ${PLATFORM_LIBS})
index e6bc9725cfafc43f1ec3e12051b4bfbba2a9d793..0b32fdca42fac966d3bac4227a7ee42432d39282 100644 (file)
@@ -149,6 +149,12 @@ DBUS_ENABLE_STATS:BOOL=OFF
 // enable restricted servers for app containers
 DBUS_ENABLE_CONTAINERS:BOOL=OFF
 
+// build with systemd at_console support
+ENABLE_SYSTEMD:STRING=AUTO
+
+// Directory for systemd service files
+WITH_SYSTEMD_SYSTEMUNITDIR:STRING=
+
 // support verbose debug mode
 DBUS_ENABLE_VERBOSE_MODE:BOOL=ON
 
index 5d76573d0037ae7eb979b8b7def4ef379d60399b..2aa6068d19f272fde91b9a2c582119db60d5c234 100644 (file)
@@ -169,6 +169,13 @@ if(NOT WIN32)
     install_example_file(example-system-hardening-without-traditional-activation.conf)
 endif()
 
+if(DBUS_BUS_ENABLE_SYSTEMD)
+    configure_file(dbus.socket.in ${CMAKE_CURRENT_BINARY_DIR}/dbus.socket)
+    configure_file(dbus.service.in ${CMAKE_CURRENT_BINARY_DIR}/dbus.service)
+    add_systemd_service(${CMAKE_CURRENT_BINARY_DIR}/dbus.socket PATH ${DBUS_SYSTEMD_SYSTEMUNITDIR} LINKS sockets.target.wants)
+    add_systemd_service(${CMAKE_CURRENT_BINARY_DIR}/dbus.service PATH ${DBUS_SYSTEMD_SYSTEMUNITDIR} LINKS multi-user.target.wants)
+endif()
+
 ## mop up the gcov files
 #clean-local:
 #      /bin/rm *.bb *.bbg *.da *.gcov || true
index 1bc545a99a7c50d72210c62c40bb6a4095313187..cbffcfa91c9a6f2761cee52eb97db362542c7eae 100644 (file)
 #cmakedefine HAVE_RAISE 1
 #cmakedefine HAVE_SETRLIMIT 1
 #cmakedefine HAVE_UNIX_FD_PASSING 1
+#cmakedefine HAVE_SYSTEMD
 
 /* Define to use epoll(4) on Linux */
 #cmakedefine DBUS_HAVE_LINUX_EPOLL 1
index 9db3bfb0e7b906f5d49c55560a749f5901b4007f..caf4372c8f18b6ccaae66569a279d34f8bac2b02 100644 (file)
@@ -233,3 +233,63 @@ macro(check_auto_option _name _text _var _vartext)
         message(FATAL_ERROR "${_text} requested but ${_vartext} not found")
     endif()
 endmacro()
+
+#
+# Provide option that takes a path
+#
+macro(add_path_option _name _text _default)
+    if(NOT DEFINED ${_name})
+        set(${_name} ${_default} CACHE STRING "${_text}" FORCE)
+    else()
+        set(${_name} ${_default} CACHE STRING "${_text}")
+    endif()
+endmacro()
+
+#
+# create directory on install
+#
+macro(install_dir filepath)
+    install(CODE "
+    set(_path \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${filepath}\")
+    if(NOT EXISTS \"\${_path}\")
+        execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory \"\${_path}\")
+        message(\"-- Creating directory: \${_path}\")
+    else()
+        message(\"-- Up-to-date: \${_path}\")
+    endif()
+    ")
+endmacro()
+
+#
+# create symlink on install
+#
+macro(install_symlink filepath sympath)
+    install(CODE "
+    set(_sympath \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${sympath}\")
+    file(REMOVE \"\${_sympath}\")
+    execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink \"${filepath}\" \"\${_sympath}\" RESULT_VARIABLE result)
+    if(NOT result)
+        message(\"-- Creating symlink: \${_sympath} -> ${filepath}\")
+    else()
+        message(FATAL ERROR \"-- Failed to create symlink: \${_sympath} -> ${filepath}\")
+    endif()
+    ")
+endmacro()
+
+#
+# add system service <file> PATH <install path> LINKS [multi-user.target.wants [...]]
+#
+macro(add_systemd_service file)
+    set(options)
+    set(oneValueArgs PATH)
+    set(multiValueArgs LINKS)
+    cmake_parse_arguments(_ "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
+    set(_targetdir ${__PATH})
+    install(FILES ${file} DESTINATION ${_targetdir})
+    get_filename_component(_name ${file} NAME)
+    foreach(l ${__LINKS})
+        set(_linkdir ${_targetdir}/${l})
+        install_dir(${_linkdir})
+        install_symlink(../${_name} ${_linkdir}/${_name})
+    endforeach()
+endmacro()
index 972e05c15db09e32a2261059358fe82552199bab..63070b69b5af9fa0f059d37749c5525111ba9920 100644 (file)
@@ -281,7 +281,7 @@ else(WIN32)
     if(DEFINED DBUS_LIBRARY_REVISION)
         set_target_properties(dbus-1 PROPERTIES VERSION ${DBUS_LIBRARY_MAJOR}.${DBUS_LIBRARY_AGE}.${DBUS_LIBRARY_REVISION} SOVERSION ${DBUS_LIBRARY_MAJOR})
     endif()
-    target_link_libraries(dbus-1 ${CMAKE_THREAD_LIBS_INIT})
+    target_link_libraries(dbus-1 ${CMAKE_THREAD_LIBS_INIT} ${SYSTEMD_LIBRARIES})
     if(LIBRT)
         target_link_libraries(dbus-1 ${LIBRT})
     endif()