cmake, autotools: Add find package config support for cmake clients
authorRalf Habacker <ralf.habacker@freenet.de>
Fri, 3 Mar 2017 11:39:23 +0000 (11:39 +0000)
committerSimon McVittie <smcv@debian.org>
Mon, 20 Mar 2017 14:18:45 +0000 (14:18 +0000)
With this support cmake and autotools generates cmake equivalent of
pkgconfig files on configure time named DBus1Config*.cmake. These
files are installed into the related directory where cmake expects
find_package related config files.

For instructions how to use this feature with clients see readme.cmake.

With previous DBus versions each cmake client using DBus as dependency
needed a related FindDBus*.cmake in its source distribution or in
the cmake binary packages. With the 'config' find package style support
provided by this patch this requirement has been removed.

The generated config file uses pkgconfig on unix or autotools to
fetch package build flags, which is the prefered way. On Windows
we do not want to require CMake users to have pkg-config installed
so it uses cmake buildin target export support for exporting all
targets into DBus1ConfigTargets*.cmake.

[smcv: make sure variable substitution works in Autotools too]

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=99721
Reviewed-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
Signed-off-by: Simon McVittie <smcv@collabora.com>
Makefile.am
README.cmake
cmake/CMakeLists.txt
cmake/DBus1Config.cmake.in [new file with mode: 0644]
cmake/DBus1Config.pkgconfig.in [new file with mode: 0644]
cmake/DBus1ConfigVersion.cmake.in [new file with mode: 0644]
configure.ac

index db0d522..970b90d 100644 (file)
@@ -3,8 +3,14 @@ SUBDIRS=dbus bus tools test doc
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = dbus-1.pc
 
+cmakeconfigdir = $(libdir)/cmake/DBus1
+cmakeconfig_DATA = cmake/DBus1Config.cmake       \
+                   cmake/DBus1ConfigVersion.cmake
+
 DISTCLEANFILES =               \
-       dbus-1.pc
+       ${pkgconfig_DATA}       \
+       ${cmakeconfig_DATA}     \
+       $(NULL)
 
 EXTRA_DIST =                   \
        autogen.sh              \
index 55427df..facfaf0 100644 (file)
@@ -2,7 +2,7 @@ This file describes how to compile dbus using the cmake build system
 
 Requirements
 ------------
-- cmake version >= 2.4.4 see http://www.cmake.org
+- cmake version >= 2.6.0 see http://www.cmake.org
 - installed libexpat see http://sourceforge.net/projects/expat/ 
     unsupported RelWithDebInfo builds could be fetched 
     from http://sourceforge.net/projects/kde-windows/files/expat/
@@ -171,3 +171,27 @@ Note: The above mentioned options could be extracted after
 configuring from the output of running "<maketool> help-options" 
 in the build directory. The related entries start with 
 CMAKE_ or DBUS_. 
+
+
+How to compile in dbus into clients with cmake
+----------------------------------------------
+
+To compile dbus library into a client application with cmake
+the following cmake commands are required:
+
+1. call find_package to find dbus package
+
+find_package(DBus1)
+
+2. after specifing targets link dbus into target
+
+add_executable(test test.c)
+target_link_libraries(test ${DBus1_LIBRARIES})
+
+Adding ${DBus1_LIBRARIES} to targets also adds required dbus
+include dirs and compiler definitions by default. There is
+no need to add them with include_directories and add_definitions.
+
+To compile against dbus installed in a non standard location
+specify -DDBus1_DIR=<dbus-install-root>/lib[64]/cmake/DBus1
+on cmake command line.
index b62ee65..1d08c6a 100644 (file)
@@ -104,7 +104,7 @@ set(DBUS_DATADIR             ${CMAKE_INSTALL_FULL_DATADIR})
 #enable building of shared library
 SET(BUILD_SHARED_LIBS ON)
 
-set(INSTALL_TARGETS_DEFAULT_ARGS  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
+set(INSTALL_TARGETS_DEFAULT_ARGS EXPORT DBus1Targets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
 
 if (CYGWIN)
    set (WIN32)
@@ -523,6 +523,26 @@ endif()
 
 add_definitions(-DHAVE_CONFIG_H=1)
 
+#
+# create cmake find_package related files
+#
+set(INSTALL_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/DBus1)
+if(WIN32)
+   configure_file(DBus1Config.cmake.in "${CMAKE_BINARY_DIR}/DBus1Config.cmake" @ONLY)
+else()
+   configure_file(DBus1Config.pkgconfig.in "${CMAKE_BINARY_DIR}/DBus1Config.cmake" @ONLY)
+endif()
+configure_file(DBus1ConfigVersion.cmake.in "${CMAKE_BINARY_DIR}/DBus1ConfigVersion.cmake" @ONLY)
+install(FILES
+    "${CMAKE_BINARY_DIR}/DBus1Config.cmake"
+    "${CMAKE_BINARY_DIR}/DBus1ConfigVersion.cmake"
+    DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev
+)
+
+if(WIN32)
+    install(EXPORT DBus1Targets DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev)
+endif()
+
 ########### subdirs ###############
 
 add_subdirectory( dbus )
diff --git a/cmake/DBus1Config.cmake.in b/cmake/DBus1Config.cmake.in
new file mode 100644 (file)
index 0000000..c389d5e
--- /dev/null
@@ -0,0 +1,37 @@
+# - Config file for the DBus1 package
+# It defines the following variables
+#  DBus1_FOUND - Flag for indicating that DBus1 package has been found
+#  DBus1_DEFINITIONS  - compile definitions for DBus1 [1]
+#  DBus1_INCLUDE_DIRS - include directories for DBus1 [1]
+#  DBus1_LIBRARIES    - cmake targets to link against
+
+# [1] This variable is not required if DBus1_LIBRARIES is added
+#     to a target with target_link_libraries
+
+# Compute paths
+if(@DBUS_RELOCATABLE@)
+    get_filename_component(DBus1_INSTALL_DIR "${CMAKE_CURRENT_LIST_DIR}/../../.." ABSOLUTE)
+else()
+    set(DBus1_INSTALL_DIR "@DBUS_PREFIX@")
+endif()
+# Our library dependencies (contains definitions for IMPORTED targets)
+if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/DBus1Targets.cmake")
+    # do not additional search paths for implicit libraries
+    # see https://cmake.org/cmake/help/v3.0/policy/CMP0003.html
+    if(COMMAND cmake_policy)
+        cmake_policy(SET CMP0003 NEW)
+    endif(COMMAND cmake_policy)
+
+    if(NOT TARGET dbus-1)
+        include("${CMAKE_CURRENT_LIST_DIR}/DBus1Targets.cmake")
+    endif()
+
+    set(DBus1_INCLUDE_DIRS "${DBus1_INSTALL_DIR}/@CMAKE_INSTALL_INCLUDEDIR@/dbus-1.0" "${DBus1_INSTALL_DIR}/@CMAKE_INSTALL_LIBDIR@/dbus-1.0/include")
+    set(DBus1_DEFINITIONS)
+    set(DBus1_LIBRARIES dbus-1)
+
+    set_property(TARGET dbus-1 PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${DBus1_INCLUDE_DIRS})
+    set_property(TARGET dbus-1 PROPERTY INTERFACE_COMPILE_DEFINITIONS ${DBus1_DEFINITIONS})
+else()
+    message(FATAL_ERROR "Incomplete cmake support in DBus1 find_package configuration")
+endif()
diff --git a/cmake/DBus1Config.pkgconfig.in b/cmake/DBus1Config.pkgconfig.in
new file mode 100644 (file)
index 0000000..7e090aa
--- /dev/null
@@ -0,0 +1,48 @@
+# - Config file for the DBus1 package
+# It defines the following variables
+#  DBus1_FOUND - Flag for indicating that DBus1 package has been found
+#  DBus1_DEFINITIONS  - compile definitions for DBus1 [1]
+#  DBus1_INCLUDE_DIRS - include directories for DBus1 [1]
+#  DBus1_LIBRARIES    - cmake targets to link against
+
+# [1] This variable is not required if DBus1_LIBRARIES is added
+#     to a target with target_link_libraries
+
+get_filename_component(DBus1_PKGCONFIG_DIR "${CMAKE_CURRENT_LIST_DIR}/../../pkgconfig" ABSOLUTE)
+find_package(PkgConfig)
+set(ENV{PKG_CONFIG_DIR})
+set(ENV{PKG_CONFIG_PATH} ${DBus1_PKGCONFIG_DIR})
+set(ENV{PKG_CONFIG_LIBDIR} ${DBus1_PKGCONFIG_DIR})
+# for debugging
+#set(ENV{PKG_CONFIG_DEBUG_SPEW} 1)
+pkg_check_modules(PC_DBUS1 QUIET dbus-1)
+set(DBus1_DEFINITIONS ${PC_DBUS1_CFLAGS_OTHER})
+
+# find the real stuff and use pkgconfig variables as hints
+# because cmake provides more search options
+find_path(DBus1_INCLUDE_DIR dbus/dbus.h
+    HINTS ${PC_DBUS1_INCLUDEDIR} ${PC_DBUS1_INCLUDE_DIRS}
+    PATH_SUFFIXES dbus-1.0)
+find_path(DBus1_ARCH_INCLUDE_DIR dbus/dbus-arch-deps.h
+    HINTS ${PC_DBUS1_INCLUDE_DIRS}
+    PATH_SUFFIXES dbus-1.0)
+find_library(DBus1_LIBRARY NAMES ${PC_DBUS1_LIBRARIES}
+    HINTS ${PC_DBUS1_LIBDIR} ${PC_DBUS1_LIBRARY_DIRS})
+
+include(FindPackageHandleStandardArgs)
+# handle the QUIETLY and REQUIRED arguments and set DBus1_FOUND to TRUE
+# if all listed variables are TRUE
+find_package_handle_standard_args(DBus1 DEFAULT_MSG
+    DBus1_LIBRARY DBus1_INCLUDE_DIR DBus1_ARCH_INCLUDE_DIR)
+
+# make the mentioned variables only visible in cmake gui with "advanced" enabled
+mark_as_advanced(DBus1_INCLUDE_DIR DBus1_LIBRARY)
+
+set(DBus1_LIBRARIES dbus-1)
+set(DBus1_INCLUDE_DIRS "${DBus1_INCLUDE_DIR}" "${DBus1_ARCH_INCLUDE_DIR}")
+
+# setup imported target
+add_library(dbus-1 SHARED IMPORTED)
+set_property(TARGET dbus-1 APPEND PROPERTY IMPORTED_LOCATION ${DBus1_LIBRARY})
+set_property(TARGET dbus-1 APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${DBus1_INCLUDE_DIRS})
+set_property(TARGET dbus-1 APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS ${DBus1_DEFINITIONS})
diff --git a/cmake/DBus1ConfigVersion.cmake.in b/cmake/DBus1ConfigVersion.cmake.in
new file mode 100644 (file)
index 0000000..239bf59
--- /dev/null
@@ -0,0 +1,11 @@
+set(PACKAGE_VERSION @DBUS_VERSION@)
+
+# Check whether the requested PACKAGE_FIND_VERSION is compatible
+if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
+  set(PACKAGE_VERSION_COMPATIBLE FALSE)
+else()
+  set(PACKAGE_VERSION_COMPATIBLE TRUE)
+  if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
+    set(PACKAGE_VERSION_EXACT TRUE)
+  endif()
+endif()
index b0afc94..32c8e85 100644 (file)
@@ -1898,6 +1898,8 @@ doc/dbus-update-activation-environment.1.xml
 doc/dbus-uuidgen.1.xml
 dbus-1.pc
 dbus-1-uninstalled.pc
+cmake/DBus1Config.cmake:cmake/DBus1Config.pkgconfig.in
+cmake/DBus1ConfigVersion.cmake
 ])
 AC_OUTPUT