Skip to content

Commit d20cab6

Browse files
author
Alexander Damian
committed
Add CMake configuration file and export installed targets
1 parent 07b3c49 commit d20cab6

File tree

12 files changed

+213
-78
lines changed

12 files changed

+213
-78
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ script:
3737
- ./configure --prefix=./install && make libs && make install
3838
- cd ..
3939
- mkdir build && cd build
40-
- cmake .. -DRDKAFKA_ROOT_DIR=../librdkafka/install/ -DKAFKA_TEST_INSTANCE=localhost:9092
40+
- cmake .. -DCPPKAFKA_CMAKE_VERBOSE=ON -DRDKAFKA_ROOT=./librdkafka/install -DKAFKA_TEST_INSTANCE=localhost:9092
4141
- make examples
4242
- make tests
4343
- ./tests/cppkafka_tests

CMakeLists.txt

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
cmake_minimum_required(VERSION 2.8.1)
2-
project(cppkafka)
1+
cmake_minimum_required(VERSION 3.9.2)
2+
project(CppKafka)
3+
4+
#Allows using '<Package>_ROOT' to point to a package install dir. After CMake v3.14 this becomes default behavior.
5+
#cmake_policy(SET CMP0074 NEW)
6+
7+
include(GNUInstallDirs)
8+
include(CMakePackageConfigHelpers)
39

410
# Set the version number.
511
set(CPPKAFKA_VERSION_MAJOR 0)
612
set(CPPKAFKA_VERSION_MINOR 3)
713
set(CPPKAFKA_VERSION_REVISION 1)
814
set(CPPKAFKA_VERSION "${CPPKAFKA_VERSION_MAJOR}.${CPPKAFKA_VERSION_MINOR}.${CPPKAFKA_VERSION_REVISION}")
9-
set(RDKAFKA_MIN_VERSION 0x00090400)
15+
set(RDKAFKA_MIN_VERSION "0.9.4")
16+
set(RDKAFKA_MIN_VERSION_HEX 0x00090400)
1017

1118
if (NOT CMAKE_CXX_FLAGS)
1219
# Set default compile flags for the project
@@ -23,7 +30,6 @@ if (NOT CMAKE_CXX_FLAGS)
2330
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall")
2431
endif()
2532
endif()
26-
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
2733

2834
# Set output directories
2935
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
@@ -38,13 +44,27 @@ option(CPPKAFKA_BOOST_STATIC_LIBS "Link with Boost static libraries." ON)
3844
option(CPPKAFKA_BOOST_USE_MULTITHREADED "Use Boost multithreaded libraries." ON)
3945
option(CPPKAFKA_RDKAFKA_STATIC_LIB "Link with Rdkafka static library." OFF)
4046

41-
math(EXPR BITS "8*${CMAKE_SIZEOF_VOID_P}")
47+
# Add FindRdKafka.cmake
48+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
4249

43-
# Properly set the output directory
44-
if (${BITS} EQUAL 64)
45-
set(LIBDIR "lib64")
46-
else()
47-
set(LIBDIR "lib")
50+
# Maintain previous compatibility
51+
if (RDKAFKA_ROOT_DIR)
52+
set(RdKafka_ROOT ${RDKAFKA_ROOT_DIR})
53+
elseif (RDKAFKA_ROOT)
54+
set(RdKafka_ROOT ${RDKAFKA_ROOT})
55+
endif()
56+
57+
if (RdKafka_ROOT)
58+
if (NOT IS_ABSOLUTE ${RdKafka_ROOT})
59+
set(RdKafka_ROOT "${CMAKE_SOURCE_DIR}/${RdKafka_ROOT}")
60+
endif()
61+
endif()
62+
63+
if (RDKAFKA_DIR)
64+
set(RdKafka_DIR ${RDKAFKA_DIR}) # For older versions of find_package
65+
if (NOT IS_ABSOLUTE ${RdKafka_ROOT})
66+
set(RdKafka_DIR "${CMAKE_SOURCE_DIR}/${RdKafka_DIR}")
67+
endif()
4868
endif()
4969

5070
# Disable output from find_package macro
@@ -60,19 +80,23 @@ else()
6080
message(STATUS "Build will generate a static library.")
6181
set(CPPKAFKA_LIBRARY_TYPE STATIC)
6282
add_definitions("-DCPPKAFKA_STATIC=1")
83+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
6384
endif()
6485

6586
if (CPPKAFKA_RDKAFKA_STATIC_LIB)
6687
add_definitions("-DLIBRDKAFKA_STATICLIB")
6788
endif()
6889

90+
if (NOT CPPKAFKA_CONFIG_DIR)
91+
set(CPPKAFKA_CONFIG_DIR lib/cmake/${PROJECT_NAME})
92+
endif()
93+
6994
if (NOT CPPKAFKA_PKGCONFIG_DIR)
7095
set(CPPKAFKA_PKGCONFIG_DIR share/pkgconfig)
7196
endif()
7297

7398
# Look for Boost (just need boost.optional headers here)
7499
find_package(Boost REQUIRED ${FIND_PACKAGE_QUIET})
75-
find_package(RdKafka REQUIRED ${FIND_PACKAGE_QUIET})
76100

77101
if (Boost_FOUND)
78102
find_package(Boost COMPONENTS program_options ${FIND_PACKAGE_QUIET})
@@ -89,8 +113,25 @@ if (Boost_FOUND)
89113
endif()
90114
endif()
91115

116+
# Try to find the RdKafka configuration file if present.
117+
# This will search default system locations as well as RdKafka_ROOT and RdKafka_Dir paths if specified.
118+
find_package(RdKafka ${FIND_PACKAGE_QUIET} CONFIG)
119+
set(RDKAFKA_TARGET_IMPORTS ${RdKafka_FOUND})
120+
if (NOT RdKafka_FOUND)
121+
message(STATUS "RdKafkaConfig.cmake not found. Please set RDKAFKA_ROOT or RDKAFKA_DIR if a config file is installed. Attempting to find module instead...")
122+
find_package(RdKafka REQUIRED ${FIND_PACKAGE_QUIET} MODULE)
123+
if (NOT RdKafka_FOUND)
124+
message(FATAL_ERROR "RdKafka module not found. Please set RDKAFKA_ROOT to the install path.")
125+
else()
126+
message(STATUS "RdKafka module found.")
127+
endif()
128+
else()
129+
set(RdKafka_DEPENDENCIES RdKafka::rdkafka)
130+
message(STATUS "RdKafka configuration file found: ${RdKafka_CONFIG}")
131+
endif()
132+
92133
add_subdirectory(src)
93-
add_subdirectory(include)
134+
add_subdirectory(include/cppkafka)
94135

95136
# Examples target
96137
if (NOT CPPKAFKA_DISABLE_EXAMPLES AND Boost_PROGRAM_OPTIONS_FOUND)

README.md

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ int main() {
5454
In order to compile _cppkafka_ you need:
5555

5656
* _librdkafka >= 0.9.4_
57-
* _CMake_
58-
* A compiler with good C++11 support (e.g. gcc >= 4.8). This was tested successfully on
59-
_g++ 4.8.3_.
60-
* The boost library.
57+
* _CMake >= 3.10_
58+
* A compiler with good C++11 support (e.g. gcc >= 4.8). This was tested successfully on _g++ 4.8.3_.
59+
* The boost library (for boost::optional)
6160

6261
Now, in order to build, just run:
6362

@@ -66,12 +65,14 @@ mkdir build
6665
cd build
6766
cmake <OPTIONS> ..
6867
make
68+
make install
6969
```
7070

7171
## CMake options
7272

7373
The following cmake options can be specified:
74-
* `RDKAFKA_ROOT_DIR` : Specify a different librdkafka install directory.
74+
* `RDKAFKA_ROOT` : Specify a different librdkafka install directory.
75+
* `RDKAFKA_DIR` : Specify a different directory where the RdKafkaConfig.cmake is installed.
7576
* `BOOST_ROOT` : Specify a different Boost install directory.
7677
* `CPPKAFKA_CMAKE_VERBOSE` : Generate verbose output. Default is `OFF`.
7778
* `CPPKAFKA_BUILD_SHARED` : Build cppkafka as a shared library. Default is `ON`.
@@ -80,25 +81,12 @@ The following cmake options can be specified:
8081
* `CPPKAFKA_BOOST_STATIC_LIBS` : Link with Boost static libraries. Default is `ON`.
8182
* `CPPKAFKA_BOOST_USE_MULTITHREADED` : Use Boost multi-threaded libraries. Default is `ON`.
8283
* `CPPKAFKA_RDKAFKA_STATIC_LIB` : Link to Rdkafka static library. Default is `OFF`.
84+
* `CPPKAFKA_CONFIG_DIR` : Install location of the cmake configuration files. Default is `lib/cmake/cppkafka`.
8385
* `CPPKAFKA_PKGCONFIG_DIR` : Install location of the .pc file. Default is `share/pkgconfig`.
8486

8587
Example:
8688
```Shell
87-
cmake -DRDKAFKA_ROOT_DIR=/some/other/dir -DCPPKAFKA_BUILD_SHARED=OFF ...
88-
```
89-
90-
The `RDKAFKA_ROOT_DIR` must contain the following structure. If the system
91-
architecture is 64-bit and both `lib` and `lib64` folders are available, the `lib64`
92-
folder location will be selected by cmake.
93-
94-
```Shell
95-
${RDKAFKA_ROOT_DIR}/
96-
|
97-
+ include/librdkafka/rdkafka.h
98-
|
99-
+ lib/librdkafka.a
100-
|
101-
+ lib64/librdkafka.a (optional)
89+
cmake -DRDKAFKA_ROOT=/some/other/dir -DCPPKAFKA_BUILD_SHARED=OFF ...
10290
```
10391

10492
# Using
@@ -108,6 +96,13 @@ If you want to use _cppkafka_, you'll need to link your application with:
10896
* _cppkafka_
10997
* _rdkafka_
11098

99+
If using CMake, this is simplified by doing:
100+
```cmake
101+
find_package(CppKafka REQUIRED)
102+
103+
target_link_libraries(<YourLibrary> CppKafka::cppkafka)
104+
```
105+
111106
# Documentation
112107

113108
You can generate the documentation by running `make docs` inside the build directory. This requires

cmake/FindRdKafka.cmake

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
# Override default CMAKE_FIND_LIBRARY_SUFFIXES
2-
# (Allows optional prioritization of static libraries during resolution)
1+
# This find module helps find the RdKafka module. It exports the following variables:
2+
# - RdKafka_INCLUDE_DIR : The directory where rdkafka.h is located.
3+
# - RdKafka_LIBNAME : The name of the library, i.e. librdkafka.a, librdkafka.so, etc.
4+
# - RdKafka_LIBRARY_DIR : The directory where the library is located.
5+
# - RdKafka_LIBRARY_PATH : The full library path i.e. ${RdKafka_LIBRARY_DIR}/${RdKafka_LIBNAME}
6+
# - RdKafka_DEPENDENCIES : Libs needed to link with RdKafka
7+
38
if (CPPKAFKA_RDKAFKA_STATIC_LIB)
49
set(RDKAFKA_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX})
510
set(RDKAFKA_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
@@ -8,48 +13,65 @@ else()
813
set(RDKAFKA_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
914
endif()
1015

11-
find_path(RDKAFKA_ROOT_DIR
12-
NAMES include/librdkafka/rdkafka.h
13-
)
16+
set(RdKafka_LIBNAME ${RDKAFKA_PREFIX}rdkafka${RDKAFKA_SUFFIX})
1417

15-
find_path(RDKAFKA_INCLUDE_DIR
18+
find_path(RdKafka_INCLUDE_DIR
1619
NAMES librdkafka/rdkafka.h
17-
HINTS ${RDKAFKA_ROOT_DIR}/include
20+
HINTS ${RdKafka_ROOT}/include
21+
)
22+
23+
find_path(RdKafka_LIBRARY_DIR
24+
NAMES ${RdKafka_LIBNAME} rdkafka
25+
HINTS ${RdKafka_ROOT}/lib ${RdKafka_ROOT}/lib64
26+
)
27+
28+
find_library(RdKafka_LIBRARY_PATH
29+
NAMES ${RdKafka_LIBNAME} rdkafka
30+
HINTS ${RdKafka_LIBRARY_DIR}
1831
)
1932

2033
# Check lib paths
2134
if (CPPKAFKA_CMAKE_VERBOSE)
2235
get_property(FIND_LIBRARY_32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
2336
get_property(FIND_LIBRARY_64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
24-
MESSAGE(STATUS "RDKAFKA search 32-bit library paths: ${FIND_LIBRARY_32}")
25-
MESSAGE(STATUS "RDKAFKA search 64-bit library paths: ${FIND_LIBRARY_64}")
37+
message(STATUS "RDKAFKA search 32-bit library paths: ${FIND_LIBRARY_32}")
38+
message(STATUS "RDKAFKA search 64-bit library paths: ${FIND_LIBRARY_64}")
39+
message(STATUS "RdKafka_ROOT = ${RdKafka_ROOT}")
40+
message(STATUS "RdKafka_INCLUDE_DIR = ${RdKafka_INCLUDE_DIR}")
41+
message(STATUS "RdKafka_LIBNAME = ${RdKafka_LIBNAME}")
42+
message(STATUS "RdKafka_LIBRARY_PATH = ${RdKafka_LIBRARY_PATH}")
43+
message(STATUS "RdKafka_LIBRARY_DIR = ${RdKafka_LIBRARY_DIR}")
2644
endif()
2745

28-
find_library(RDKAFKA_LIBRARY
29-
NAMES ${RDKAFKA_PREFIX}rdkafka${RDKAFKA_SUFFIX} rdkafka
30-
HINTS ${RDKAFKA_ROOT_DIR}/lib
31-
)
32-
3346
include(FindPackageHandleStandardArgs)
3447
find_package_handle_standard_args(RDKAFKA DEFAULT_MSG
35-
RDKAFKA_LIBRARY
36-
RDKAFKA_INCLUDE_DIR
48+
RdKafka_LIBNAME
49+
RdKafka_LIBRARY_DIR
50+
RdKafka_LIBRARY_PATH
51+
RdKafka_INCLUDE_DIR
3752
)
3853

39-
set(CONTENTS "#include <librdkafka/rdkafka.h>\n #if RD_KAFKA_VERSION >= ${RDKAFKA_MIN_VERSION}\n int main() { }\n #endif")
54+
set(CONTENTS "#include <librdkafka/rdkafka.h>\n #if RD_KAFKA_VERSION >= ${RDKAFKA_MIN_VERSION_HEX}\n int main() { }\n #endif")
4055
set(FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/rdkafka_version_test.c)
4156
file(WRITE ${FILE_NAME} ${CONTENTS})
4257

43-
try_compile(HAVE_VALID_KAFKA_VERSION ${CMAKE_CURRENT_BINARY_DIR}
58+
try_compile(RdKafka_FOUND ${CMAKE_CURRENT_BINARY_DIR}
4459
SOURCES ${FILE_NAME}
45-
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${RDKAFKA_INCLUDE_DIR}")
60+
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${RdKafka_INCLUDE_DIR}")
4661

47-
if (HAVE_VALID_KAFKA_VERSION)
62+
if (RdKafka_FOUND)
63+
if (CPPKAFKA_RDKAFKA_STATIC_LIB)
64+
set(RdKafka_DEPENDENCIES ${RdKafka_LIBNAME} pthread rt ssl crypto dl z)
65+
else()
66+
set(RdKafka_DEPENDENCIES ${RdKafka_LIBNAME} pthread)
67+
endif()
68+
include_directories(SYSTEM ${RdKafka_INCLUDE_DIR})
69+
link_directories(${RdKafka_LIBRARY_DIR})
4870
message(STATUS "Found valid rdkafka version")
4971
mark_as_advanced(
50-
RDKAFKA_ROOT_DIR
51-
RDKAFKA_INCLUDE_DIR
5272
RDKAFKA_LIBRARY
73+
RdKafka_LIBRARY_DIR
74+
RdKafka_INCLUDE_DIR
5375
)
5476
else()
5577
message(FATAL_ERROR "Failed to find valid rdkafka version")

cmake/config.cmake.in

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@PACKAGE_INIT@
2+
3+
include(CMakeFindDependencyMacro)
4+
5+
# Add FindRdKafka.cmake
6+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}")
7+
8+
# Find boost optional
9+
find_dependency(Boost REQUIRED)
10+
11+
# Try to find the RdKafka configuration file if present.
12+
# This will search default system locations as well as RdKafka_ROOT and RdKafka_Dir paths if specified.
13+
find_dependency(RdKafka QUIET CONFIG)
14+
set(RDKAFKA_TARGET_IMPORTS ${RdKafka_FOUND})
15+
if (NOT RdKafka_FOUND)
16+
message(STATUS "RdKafkaConfig.cmake not found. Please set RDKAFKA_ROOT or RDKAFKA_DIR if a config file is installed. Attempting to find module instead...")
17+
find_dependency(RdKafka REQUIRED QUIET MODULE)
18+
if (NOT RdKafka_FOUND)
19+
message(FATAL_ERROR "RdKafka module not found. Please set RDKAFKA_ROOT to the install path.")
20+
else()
21+
message(STATUS "RdKafka module found.")
22+
endif()
23+
else()
24+
set(RdKafka_DEPENDENCIES RdKafka::rdkafka)
25+
message(STATUS "RdKafka configuration file found: ${RdKafka_CONFIG}")
26+
endif()
27+
28+
include("${CMAKE_CURRENT_LIST_DIR}/@TARGET_EXPORT_NAME@.cmake")
29+
30+
# Export 'CppKafka_ROOT'
31+
set_and_check(@PROJECT_NAME@_ROOT "@PACKAGE_CMAKE_INSTALL_PREFIX@")
32+
33+
# Export 'CppKafka_INSTALL_INCLUDE_DIR'
34+
set_and_check(@PROJECT_NAME@_INSTALL_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@")
35+
36+
# Export 'CppKafka_INSTALL_LIB_DIR'
37+
set_and_check(@PROJECT_NAME@_INSTALL_LIB_DIR "@PACKAGE_CMAKE_INSTALL_LIBDIR@")
38+
39+
# Validate installed components
40+
check_required_components("@PROJECT_NAME@")
File renamed without changes.

cppkafka.pc.in renamed to cmake/cppkafka.pc.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
prefix=@CMAKE_INSTALL_PREFIX@
22
exec_prefix=${prefix}
3-
libdir=${prefix}/@LIBDIR@
4-
sharedlibdir=${prefix}/@LIBDIR@
3+
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
4+
sharedlibdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
55
includedir=${prefix}/include
66

77
Name: cppkafka

examples/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
link_libraries(cppkafka ${RDKAFKA_LIBRARY} ${Boost_LIBRARIES} pthread rt ssl crypto dl z)
21
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
3-
include_directories(SYSTEM ${RDKAFKA_INCLUDE_DIR})
42

53
add_custom_target(examples)
64
macro(create_example example_name)
75
string(REPLACE "_" "-" sanitized_name ${example_name})
86
add_executable(${sanitized_name} EXCLUDE_FROM_ALL "${example_name}_example.cpp")
7+
target_link_libraries(${sanitized_name} cppkafka ${RdKafka_DEPENDENCIES} Boost::boost Boost::program_options)
98
add_dependencies(examples ${sanitized_name})
109
endmacro()
1110

include/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

include/cppkafka/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function(make_cppkafka_header)
1010
endforeach()
1111

1212
#create file from template
13-
configure_file(${PROJECT_SOURCE_DIR}/cppkafka.h.in ${CPPKAFKA_HEADER} @ONLY)
13+
configure_file(${PROJECT_SOURCE_DIR}/cmake/cppkafka.h.in ${CPPKAFKA_HEADER} @ONLY)
1414
endfunction()
1515

1616
# Run file generation function

0 commit comments

Comments
 (0)