Skip to content

Commit

Permalink
Add option to build libsystemd automatically as integral part of sdbu…
Browse files Browse the repository at this point in the history
…s-c++
  • Loading branch information
sangelovic committed Apr 28, 2019
1 parent 3626989 commit 0cffed4
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 12 deletions.
23 changes: 19 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ project(sdbus-c++ VERSION 0.6.1 LANGUAGES C CXX)
include(GNUInstallDirs) # Installation directories for `install` command and pkgconfig file

#-------------------------------
# PERFORMING CHECKS
# PERFORMING CHECKS & PREPARING THE DEPENDENCIES
#-------------------------------

find_package(PkgConfig REQUIRED)
pkg_check_modules(SYSTEMD REQUIRED libsystemd>=236)
option(BUILD_LIBSYSTEMD "Build libsystemd static library and incorporate it into libsdbus-c++" OFF)

if(NOT BUILD_LIBSYSTEMD)
find_package(PkgConfig REQUIRED)
pkg_check_modules(SYSTEMD libsystemd>=236)
if(NOT SYSTEMD_FOUND)
message(FATAL_ERROR "libsystemd of version at least 236 is required, but was not found "
"(you may turn BUILD_LIBSYSTEMD on for sdbus-c++ to try downloading "
"and building libsystemd in as part of sdbus-c++ during configuration)")
endif()
else()
# Build static libsystemd library as an external project
include(cmake/LibsystemdExternalProject.cmake)
endif()

#-------------------------------
# SOURCE FILES CONFIGURATION
Expand Down Expand Up @@ -93,6 +105,9 @@ target_compile_definitions(sdbus-cpp PRIVATE BUILDLIB=1)
if(BUILD_SHARED_LIBS)
set_target_properties(sdbus-cpp PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()
if(BUILD_LIBSYSTEMD)
add_dependencies(sdbus-cpp LibsystemdBuildProject)
endif()

add_library(sdbus-c++ $<TARGET_OBJECTS:sdbus-cpp>)
set_target_properties(sdbus-c++
Expand Down Expand Up @@ -139,9 +154,9 @@ endif()
#----------------------------------

option(BUILD_DOC "Build documentation for sdbus-c++" ON)
option(BUILD_DOXYGEN_DOC "Build doxygen documentation for sdbus-c++ API" OFF)

if(BUILD_DOC)
option(BUILD_DOXYGEN_DOC "Build doxygen documentation for sdbus-c++ API" OFF)
add_subdirectory("${CMAKE_SOURCE_DIR}/docs")
install(FILES README README.md NEWS COPYING ChangeLog AUTHORS DESTINATION ${CMAKE_INSTALL_DOCDIR})
endif()
Expand Down
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,10 @@ v0.6.0
- Extend stress tests to allow testing safe initialization/deinitialization of objects and proxies
- Fix gcc warnings
- Use release v1.8.1 of googletest for tests

vX.Y.Z
- sdbus-c++ now offers automatic download&build of libsystemd dependency, and making it part of sdbus-c++ library, which might be very helpful in non-systemd environments
- sdbus-c++ file organization has been adjusted to comply to de-facto OSS project standards
- Using googletest from master branch instead of v1.8.1 which has THREADS_PTHREAD_ARG issue when cross-compiling
- Documentation improvements
- Other minor fixes and improvements
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
sdbus-c++
=========

sdbus-c++ is a C++ D-Bus library meant to provide high-level, expressive, easy-to-use interfaces in modern C++. It adds another layer of abstraction on top of sd-bus, a nice, fresh C D-Bus implementation by systemd.
sdbus-c++ is a high-level C++ D-Bus library for Linux designed to provide expressive, easy-to-use API in modern C++. It adds another layer of abstraction on top of sd-bus, a nice, fresh C D-Bus implementation by systemd.

sdbus-c++ has been written primarily as a replacement of dbus-c++, which currently suffers from a number of (unsolved) bugs, concurrency issues and inherent design limitations. sdbus-c++ has learned from dbus-c++ and tries to come up with a simple design that is flexible and friendly to the user and inherently free of those bugs.
sdbus-c++ has been written primarily as a replacement of dbus-c++, which currently suffers from a number of (unresolved) bugs, concurrency issues and inherent design complexities and limitations. sdbus-c++ has learned from dbus-c++ and has chosen a different path, a path of simple yet powerful design that is intuitive and friendly to the user and inherently free of those bugs.

Even though sdbus-c++ uses sd-bus library, it is not necessarily constrained to systemd and can perfectly be used in non-systemd environments as well.

Building and installing the library
-----------------------------------
Expand All @@ -13,12 +15,12 @@ The library is built using CMake:
```bash
$ mkdir build
$ cd build
$ cmake .. ${CONFIGURE_FLAGS_IF_NECESSARY}
$ cmake .. -DCMAKE_BUILD_TYPE=Release ${OTHER_CONFIG_FLAGS}
$ make
$ sudo make install
```

### CMake configuration flags
### CMake configuration flags for sdbus-c++

* `BUILD_CODE_GEN` [boolean]

Expand Down Expand Up @@ -48,11 +50,23 @@ $ sudo make install

Path where the test binaries shall get installed. Default value: `/opt/test/bin`.

* `BUILD_LIBSYSTEMD` [boolean]

Option for building libsystemd dependency library automatically when sdbus-c++ is built, and making libsystemd an integral part of sdbus-c++ library. Default value: `OFF`. Might be very helpful in non-systemd environments where libsystemd shared library is unavailable (see [Solving libsystemd dependency](docs/using-sdbus-c++.md#solving-libsystemd-dependency) for more information). With this option turned on, you may also provide the following configuration flag:

* `LIBSYSTEMD_VERSION` [string]

Defines version of systemd to be downloaded, built and integrated into sdbus-c++. Default value: `v239`.

* `CMAKE_BUILD_TYPE` [string]

This is a CMake-builtin option. Set to `Release` to build sdbus-c++ for production use. Set to `Debug` if you want to help further develop (and debug) the library :)

Dependencies
------------

* `C++17` - the library uses C++17 `std::uncaught_exceptions()` feature. When building sdbus-c++ manually, make sure you use a compiler that supports that feature (gcc >= 6, clang >= 3.7)
* `libsystemd` - systemd library containing sd-bus implementation. This library is part of systemd. Systemd at least v236 is needed. (Non-systemd environments are also supported, see the [tutorial](docs/using-sdbus-c++.md#solving-libsystemd-dependency) for more information.)
* `libsystemd` - systemd library containing sd-bus implementation. This library is part of systemd. Systemd at least v236 is needed. (In case you have a non-systemd environment, don't worry, see [Solving libsystemd dependency](docs/using-sdbus-c++.md#solving-libsystemd-dependency) for more information.)
* `googletest` - google unit testing framework, only necessary when building tests, will be downloaded and built automatically.

Licensing
Expand Down
46 changes: 46 additions & 0 deletions cmake/LibsystemdExternalProject.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
find_program(MESON meson)
find_program(NINJA ninja)

if((NOT MESON) OR (NOT NINJA))
message(FATAL_ERROR "Meson and Ninja are required to build libsystemd")
endif()

find_library(GLIBC_RT_LIBRARY rt)
find_package(PkgConfig REQUIRED)
pkg_check_modules(MOUNT REQUIRED mount)
pkg_check_modules(CAP REQUIRED libcap)
if (NOT CAP_FOUND)
find_library(CAP_LIBRARIES cap) # Compat with Ubuntu 14.04 which ships libcap w/o .pc file
endif()

set(LIBSYSTEMD_VERSION v239 CACHE STRING "libsystemd version (>=v239) to build and incorporate into libsdbus-c++")

if(NOT CMAKE_BUILD_TYPE)
set(LIBSYSTEMD_BUILD_TYPE "plain")
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(LIBSYSTEMD_BUILD_TYPE "debug")
else()
set(LIBSYSTEMD_BUILD_TYPE "release")
endif()

include(ExternalProject)
ExternalProject_Add(LibsystemdBuildProject
PREFIX libsystemd
GIT_REPOSITORY https://github.com/systemd/systemd.git
GIT_TAG ${LIBSYSTEMD_VERSION}
UPDATE_COMMAND ""
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E remove <BINARY_DIR>/*
COMMAND ${MESON} --buildtype=${LIBSYSTEMD_BUILD_TYPE} -Dstatic-libsystemd=pic <SOURCE_DIR> <BINARY_DIR>
BUILD_COMMAND ${NINJA} -C <BINARY_DIR> libsystemd.a
INSTALL_COMMAND ""
LOG_DOWNLOAD 1 LOG_UPDATE 1 LOG_CONFIGURE 1 LOG_BUILD 1)

ExternalProject_Get_property(LibsystemdBuildProject SOURCE_DIR)
set(SYSTEMD_INCLUDE_DIRS ${SOURCE_DIR}/src)
ExternalProject_Get_property(LibsystemdBuildProject BINARY_DIR)
set(SYSTEMD_LIBRARY_DIRS ${BINARY_DIR})

#add_library(libsystemd-static STATIC IMPORTED)
#set_target_properties(libsystemd-static PROPERTIES IMPORTED_LOCATION ${SYSTEMD_LIBRARY_DIRS}/libsystemd.a)
#target_link_libraries(libsystemd-static ${MOUNT_LIBRARIES} ${CAP_LIBRARIES} ${GLIBC_RT_LIBRARY})
set(SYSTEMD_LIBRARIES ${SYSTEMD_LIBRARY_DIRS}/libsystemd.a ${MOUNT_LIBRARIES} ${CAP_LIBRARIES} ${GLIBC_RT_LIBRARY})
16 changes: 13 additions & 3 deletions docs/using-sdbus-c++.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,30 @@ sdbus-c++ depends on libsystemd, a C library that is part of [systemd](https://g

Minimum required libsystemd shared library version is 0.20.0 (which corresponds to minimum systemd version 236).

If your target Linux distribution is already based on systemd ecosystem of version 236 and higher, then there is no additional effort, just make sure you have corresponding systemd header files installed, and you should build sdbus-c++ seamlessly.
If your target Linux distribution is already based on systemd ecosystem of version 236 and higher, then there is no additional effort, just make sure you have corresponding systemd header files available (provided by `libsystemd-dev` package on Debian/Ubuntu, for example), and you may go on building sdbus-c++ seamlessly.

sdbus-c++ can also be used in non-systemd environments. Fortunately, libsystemd is rather self-contained and can be built and used independently of the rest of systemd ecosystem. To build libsystemd shared library for sdbus-c++:
sdbus-c++ can perfectly be used in non-systemd environments as well. There are two ways to approach this:

### Building and distributing libsystemd as a shared library yourself

Fortunately, libsystemd is rather self-contained and can be built and used independently of the rest of systemd ecosystem. To build libsystemd shared library for sdbus-c++:

```shell
$ git clone https://github.com/systemd/systemd
$ cd systemd
$ git checkout v242 # or any other recent stable version
$ meson build/ # solve systemd dependencies if any pop up, e.g. libmount-dev...
$ meson build/ # solve systemd dependencies if any pop up, e.g. libmount-dev, libcap, librt...
$ ninja -C build version.h
$ ninja -C build libsystemd.so.0.26.0 # or another version number depending which systemd version you have
# finally, manually install the library, header files and libsystemd.pc pkgconfig file
```

### Building libsystemd as part of sdbus-c++

sdbus-c++ provides `BUILD_LIBSYSTEMD` configuration option. When turned on, sdbus-c++ will automatically download, build and integrate libsystemd as a static library into sdbus-c++ for you. This is the most convenient and effective approach to build, distribute and use sdbus-c++ as a self-contained, systemd-independent library in non-systemd enviroments. Just make sure your build machine has all dependencies needed by libsystemd build process. That includes `meson`, `ninja`, `git` programs and mainly libraries `libmount`, `libcap` and `librt` (part of glibc).

You may additionally use `LIBSYSTEMD_VERSION` configuration flag to fine-tune the version of systemd to be used.

Header files and namespaces
---------------------------

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
#----------------------------------

add_executable(sdbus-c++-unit-tests ${UNITTESTS_SRCS} $<TARGET_OBJECTS:sdbus-cpp>)
target_include_directories(sdbus-c++-unit-tests PUBLIC ${SYSTEMD_INCLUDE_DIRS})
target_link_libraries(sdbus-c++-unit-tests ${SYSTEMD_LIBRARIES} gmock gmock_main)

add_executable(sdbus-c++-integration-tests ${INTEGRATIONTESTS_SRCS})
Expand Down

0 comments on commit 0cffed4

Please sign in to comment.