summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Willers <M.Willers@gmx.net>2021-05-10 03:02:08 +0200
committerGitHub <noreply@github.com>2021-05-10 10:02:08 +0900
commitb95044abb202838837b0566efc40ae26308a4bb4 (patch)
tree901351ddcf4f2b3f9fce5ad51166527d9c1cc56f
parente969bed805f7dba9d2fa35c56a7ace28efb67e4e (diff)
downloadDLT-daemon-b95044abb202838837b0566efc40ae26308a4bb4.tar.gz
Export cmake config file (#289)
Create and install proper CMake *-target.cmake and *-config.cmake files, for use by other CMake-using projects. It installs a file called automotive-dlt-targets.cmake into a common location for such files, namely <prefix>/lib/cmake/automotive-dlt/automotive-dlt-targets.cmake They can now call find_package(automotive-dlt) and obtain a target called Genivi::dlt that they can link against with target_link_libraries(), by which they automatically gain all necessary attributes, including libdlt's include directories. Signed-off-by: Martin Willers <M.Willers@gmx.net>
-rw-r--r--automotive-dlt-config.cmake.in32
-rw-r--r--doc/dlt_for_developers.md69
-rw-r--r--src/lib/CMakeLists.txt38
3 files changed, 133 insertions, 6 deletions
diff --git a/automotive-dlt-config.cmake.in b/automotive-dlt-config.cmake.in
new file mode 100644
index 0000000..5013b54
--- /dev/null
+++ b/automotive-dlt-config.cmake.in
@@ -0,0 +1,32 @@
+#######
+# SPDX license identifier: MPL-2.0
+#
+# Copyright (C) 2021, Martin Willers <M.Willers@gmx.net>
+#
+# This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
+#
+# This Source Code Form is subject to the terms of the
+# Mozilla Public License (MPL), v. 2.0.
+# If a copy of the MPL was not distributed with this file,
+# You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# For further information see http://www.genivi.org/.
+#######
+
+# Config file for the Genivi::dlt package.
+
+# This file exports the Genivi::dlt CMake target which should be passed to the
+# target_link_libraries command.
+#
+# In addition, the following variable is defined:
+# @PROJECT_NAME@_FOUND - TRUE if headers and library were found
+
+include(CMakeFindDependencyMacro)
+
+find_dependency(Threads)
+
+@PACKAGE_INIT@
+
+include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake")
+
+check_required_components(@PROJECT_NAME@)
diff --git a/doc/dlt_for_developers.md b/doc/dlt_for_developers.md
index cd85de0..1b30f86 100644
--- a/doc/dlt_for_developers.md
+++ b/doc/dlt_for_developers.md
@@ -25,9 +25,15 @@ within the standard include directory.
This example gives an overview of DLT usage inside an application by using a
minimal code example. Detailed information about the API can be found later in
this document.
+Please note that the #include statement depends on the means by which you are
+incorporating the DLT library into your project. The `<dlt/dlt.h>` form (i.e.
+with a directory prefix) seen here is necessary when you are using the CMake
+Config file (see below). If you are using pkg-config instead, then this #include
+statement needs to refer to only `<dlt.h>`, due to the way the pkg-config module
+exports the include directory.
```
-#include <dlt.h>
+#include <dlt/dlt.h>
DLT_DECLARE_CONTEXT(ctx); /* declare context */
@@ -62,14 +68,55 @@ string. On application cleanup, all DLT contexts, as well as the DLT
application have to be unregistered.
### DLT with cmake
-To use DLT with cmake, the following lines are the important ones:
+
+To use DLT with CMake, the recommended way is to use the CMake Config file
+that is being generated as part of installation.
+
+You can thus:
+```
+find_package(automotive-dlt)
+...
+target_link_libraries(myapp PRIVATE Genivi::DLT)
+```
+which lets your project automatically gain all necessary compile and link flags
+needed by libdlt, including the include directories.
+
+The generated CMake Config file follows "Modern CMake" convention and only
+exports an IMPORTED CMake target; it does not set any variables.
+
+### DLT with pkg-config
+
+Alternatively to the CMake integration detailed above, it is also possible
+to use DLT via pkg-config. This can also be done with CMake's PkgConfig
+module as well.
+
+#### PkgConfig usage with "Modern CMake"
+
+Here, you let the PkgConfig module create targets as well; the target's name
+is however determined by the PkgConfig module:
+
+```
+find_package(PkgConfig)
+pkg_check_modules(DLT REQUIRED IMPORTED_TARGET automotive-dlt)
+```
+
+As per "Modern CMake", there are again no variables to be added, but only
+a CMake target to be added to the link libraries:
+
+```
+target_link_libraries(myapp PRIVATE PkgConfig::DLT)
+```
+
+#### PkgConfig usage with "Legacy CMake" (<3.0)
+
+Here, you let the PkgConfig module only create variables, but not targets:
```
find_package(PkgConfig)
pkg_check_modules(DLT REQUIRED automotive-dlt)
```
-to INCLUDE\_DIRECTORIES, add
+to INCLUDE\_DIRECTORIES (or, since CMake 2.8.11, TARGET\_INCLUDE\_DIRECTORIES), add
```
${DLT_INCLUDE_DIRS}
@@ -78,7 +125,21 @@ ${DLT_INCLUDE_DIRS}
and to TARGET\_LINK\_LIBRARIES:
```
-${DLT_LIBRARIES}
+${DLT_LINK_LIBRARIES} (preferred, for CMake >= 3.12)
+${DLT_LIBRARIES} (otherwise)
+```
+
+The contents of `${DLT_LIBRARIES}` do not include the library's path
+(e.g. `-L/path/to/lib`), so if the library resides in a location that is not
+on the linker's default search path, you'll either have to add that path
+to LINK\_DIRECTORIES:
+```
+link_directories(${DLT_LIBRARY_DIRS})
+```
+or, alternatively, not use `${DLT_LIBRARIES}`, but `${DLT_LDFLAGS}` instead,
+which combines `${DLT_LIBRARIES}` and `${DLT_LIBRARY_DIRS}`:
+```
+target_link_libraries(myapp ${DLT_LDFLAGS})
```
### Limitation
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index 6d20c64..5a94bef 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -44,8 +44,14 @@ else()
message(STATUS "pthread_setname_np API not available on this platform")
endif()
-target_link_libraries(dlt ${RT_LIBRARY} ${SOCKET_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
-target_include_directories(dlt PUBLIC ${PROJECT_SOURCE_DIR}/include/dlt ${PROJECT_BINARY_DIR}/include/dlt)
+target_link_libraries(dlt ${RT_LIBRARY} ${SOCKET_LIBRARY} Threads::Threads)
+
+target_include_directories(dlt
+ PUBLIC
+ $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/dlt>
+ $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/dlt>
+ $<INSTALL_INTERFACE:include>
+)
if(WITH_LIB_SHORT_VERSION)
set_target_properties(dlt PROPERTIES VERSION ${PROJECT_VERSION_MAJOR} SOVERSION ${PROJECT_VERSION_MAJOR})
@@ -53,7 +59,11 @@ else()
set_target_properties(dlt PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
endif()
+add_library(Genivi::dlt ALIAS dlt)
+
install(TARGETS dlt
+ EXPORT
+ ${PROJECT_NAME}-targets
RUNTIME
DESTINATION bin
COMPONENT base
@@ -63,3 +73,27 @@ install(TARGETS dlt
ARCHIVE
DESTINATION ${CMAKE_INSTALL_LIBDIR}/static
COMPONENT base)
+
+# Install *-targets.cmake file
+install(
+ EXPORT ${PROJECT_NAME}-targets
+ DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
+ NAMESPACE Genivi::
+)
+
+# Create *-config.cmake file
+include(CMakePackageConfigHelpers)
+configure_package_config_file(
+ ${PROJECT_SOURCE_DIR}/${PROJECT_NAME}-config.cmake.in
+ ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
+ INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
+ NO_SET_AND_CHECK_MACRO
+)
+
+# Install *-config.cmake file
+install(
+ FILES
+ ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
+ DESTINATION
+ ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
+)