Skip to content

Commit 762b4bd

Browse files
committed
cmake: allow including libfds as a subproject using cmake
1 parent 920359a commit 762b4bd

5 files changed

Lines changed: 58 additions & 25 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
cmake_minimum_required(VERSION 3.5...3.10)
22
project(libfds)
33

4+
# Check if libfds is the main project or it is a part of another project
5+
# (e.g. included via add_subdirectory() or FetchContent)
6+
if (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
7+
set(LIBFDS_TOP_LEVEL ON)
8+
else()
9+
set(LIBFDS_TOP_LEVEL OFF)
10+
endif()
11+
412
# Description of the project
513
set(PROJECT_DESCRIPTION
614
"Flow Data Storage library is set of tools for processing flow records."
@@ -25,7 +33,7 @@ include(CheckCCompilerFlag)
2533
include(CheckCXXCompilerFlag)
2634

2735
# Include custom FindXXX modules
28-
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules")
36+
list(INSERT CMAKE_MODULE_PATH 0 "${PROJECT_SOURCE_DIR}/CMakeModules")
2937

3038
CHECK_C_COMPILER_FLAG(-std=gnu11 COMPILER_SUPPORT_GNU11)
3139
if (NOT COMPILER_SUPPORT_GNU11)
@@ -50,13 +58,25 @@ if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
5058
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
5159
endif()
5260

53-
option(ENABLE_DOC "Enable documentation building" OFF)
54-
option(ENABLE_TESTS "Build Unit tests (make test)" OFF)
55-
option(ENABLE_TESTS_INTERNAL "Build Unit tests that don't use public API" OFF)
56-
option(ENABLE_TESTS_VALGRIND "Build Unit tests with Valgrind Memcheck" OFF)
57-
option(ENABLE_TESTS_COVERAGE "Enable support for code coverage" OFF)
58-
option(PACKAGE_BUILDER_RPM "Enable RPM package builder (make rpm)" OFF)
59-
option(PACKAGE_BUILDER_DEB "Enable DEB package builder (make deb)" OFF)
61+
if (LIBFDS_TOP_LEVEL)
62+
option(ENABLE_DOC "Enable documentation building" OFF)
63+
option(ENABLE_TESTS "Build Unit tests (make test)" OFF)
64+
option(ENABLE_TESTS_INTERNAL "Build Unit tests that don't use public API" OFF)
65+
option(ENABLE_TESTS_VALGRIND "Build Unit tests with Valgrind Memcheck" OFF)
66+
option(ENABLE_TESTS_COVERAGE "Enable support for code coverage" OFF)
67+
option(PACKAGE_BUILDER_RPM "Enable RPM package builder (make rpm)" OFF)
68+
option(PACKAGE_BUILDER_DEB "Enable DEB package builder (make deb)" OFF)
69+
else()
70+
# Options of the same name in the parent project must not affect libfds.
71+
# Documentation, tests and packages are available only in standalone builds.
72+
set(ENABLE_DOC OFF)
73+
set(ENABLE_TESTS OFF)
74+
set(ENABLE_TESTS_INTERNAL OFF)
75+
set(ENABLE_TESTS_VALGRIND OFF)
76+
set(ENABLE_TESTS_COVERAGE OFF)
77+
set(PACKAGE_BUILDER_RPM OFF)
78+
set(PACKAGE_BUILDER_DEB OFF)
79+
endif()
6080
option(USE_SYSTEM_LZ4 "Use system-installed LZ4 library" ON)
6181
option(USE_SYSTEM_ZSTD "Use system-installed ZSTD library" ON)
6282

‎CMakeModules/git_info.cmake‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if (GIT_FOUND)
1414
# Get an abbreviated commit hash of the latest commit
1515
execute_process(
1616
COMMAND ${GIT_EXECUTABLE} "log" "-1" "--format=%h"
17-
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
17+
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
1818
RESULT_VARIABLE TMP_GIT_RET_CODE
1919
OUTPUT_VARIABLE TMP_GIT_HASH
2020
ERROR_QUIET
@@ -28,7 +28,7 @@ if (GIT_FOUND)
2828
# Get a user name
2929
execute_process(
3030
COMMAND ${GIT_EXECUTABLE} "config" "--get" "user.name"
31-
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
31+
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
3232
RESULT_VARIABLE TMP_GIT_RET_CODE
3333
OUTPUT_VARIABLE TMP_GIT_NAME
3434
ERROR_QUIET
@@ -42,7 +42,7 @@ if (GIT_FOUND)
4242
# Get a user email
4343
execute_process(
4444
COMMAND ${GIT_EXECUTABLE} "config" "--get" "user.email"
45-
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
45+
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
4646
RESULT_VARIABLE TMP_GIT_RET_CODE
4747
OUTPUT_VARIABLE TMP_GIT_EMAIL
4848
ERROR_QUIET

‎CMakeModules/install_dirs.cmake‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ endif()
4040

4141
set(INSTALL_DIR_INFO "${INSTALL_DIR_SHARE}/info/")
4242
set(INSTALL_DIR_MAN "${INSTALL_DIR_SHARE}/man/")
43-
set(INSTALL_DIR_DOC "${INSTALL_DIR_SHARE}/doc/${CMAKE_PROJECT_NAME}/")
43+
set(INSTALL_DIR_DOC "${INSTALL_DIR_SHARE}/doc/${PROJECT_NAME}/")
4444

‎pkg/CMakeLists.txt‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1+
find_package(PkgConfig QUIET)
2+
if (PKG_CONFIG_FOUND)
3+
# Generate and install pkg-config file
4+
configure_file(
5+
"${CMAKE_CURRENT_SOURCE_DIR}/libfds.pc.in"
6+
"${CMAKE_CURRENT_BINARY_DIR}/libfds.pc"
7+
@ONLY)
8+
install(
9+
FILES "${CMAKE_CURRENT_BINARY_DIR}/libfds.pc"
10+
DESTINATION "${INSTALL_DIR_LIB}/pkgconfig/")
11+
endif()
12+
13+
# CPack configuration and package builders are available only in standalone
14+
# builds as they would collide with the parent project
15+
if (NOT LIBFDS_TOP_LEVEL)
16+
return()
17+
endif()
18+
119
# Common parameters (based on CPack variables)
2-
set(CPACK_PACKAGE_NAME ${CMAKE_PROJECT_NAME})
20+
set(CPACK_PACKAGE_NAME ${PROJECT_NAME})
321
set(CPACK_PACKAGE_VERSION ${LIBFDS_VERSION})
422
if (NOT CPACK_PACKAGE_RELEASE)
523
# User specified
@@ -58,18 +76,6 @@ endif()
5876

5977
message(STATUS "Maintainer contact for packages: " ${CPACK_PACKAGE_CONTACT})
6078

61-
find_package(PkgConfig QUIET)
62-
if (PKG_CONFIG_FOUND)
63-
# Generate and install pkg-config file
64-
configure_file(
65-
"${CMAKE_CURRENT_SOURCE_DIR}/libfds.pc.in"
66-
"${CMAKE_CURRENT_BINARY_DIR}/libfds.pc"
67-
@ONLY)
68-
install(
69-
FILES "${CMAKE_CURRENT_BINARY_DIR}/libfds.pc"
70-
DESTINATION "${INSTALL_DIR_LIB}/pkgconfig/")
71-
endif()
72-
7379
# Package builders
7480
add_subdirectory(tgz)
7581

‎src/CMakeLists.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ add_library(
5252
${PROJECT_SOURCE_DIR}/include/libfds/
5353
)
5454

55+
# Public headers for projects that use libfds as a subproject
56+
target_include_directories(fds PUBLIC
57+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
58+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
59+
)
60+
add_library(libfds::fds ALIAS fds)
61+
5562
target_link_libraries(fds ${LIBXML2_LIBRARIES})
5663
if (LIBRT)
5764
target_link_libraries(fds ${LIBRT})

0 commit comments

Comments
 (0)