cmake: Add support for systemd integration on Linux operating systems

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.
This commit is contained in:
Ralf Habacker 2020-05-18 12:44:37 +02:00 committed by Simon McVittie
parent eb6d4a1439
commit 97bdefd4e2
6 changed files with 110 additions and 2 deletions

View 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})

View 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

View 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

View file

@ -227,6 +227,7 @@
#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

View 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()

View 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()