Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ script:
- ./configure --prefix=./install && make libs && make install
- cd ..
- mkdir build && cd build
- cmake .. -DRDKAFKA_ROOT_DIR=../librdkafka/install/ -DKAFKA_TEST_INSTANCE=localhost:9092
- cmake .. -DCPPKAFKA_CMAKE_VERBOSE=ON -DRDKAFKA_ROOT=./librdkafka/install -DKAFKA_TEST_INSTANCE=localhost:9092
- make examples
- make tests
- ./tests/cppkafka_tests
74 changes: 60 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
cmake_minimum_required(VERSION 2.8.1)
project(cppkafka)
cmake_minimum_required(VERSION 3.9.2)
project(CppKafka)
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12.0")
# Use <package>_ROOT variable to find configuration files
cmake_policy(SET CMP0074 NEW)
endif()

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

# Set the version number.
set(CPPKAFKA_VERSION_MAJOR 0)
set(CPPKAFKA_VERSION_MINOR 3)
set(CPPKAFKA_VERSION_REVISION 1)
set(CPPKAFKA_VERSION "${CPPKAFKA_VERSION_MAJOR}.${CPPKAFKA_VERSION_MINOR}.${CPPKAFKA_VERSION_REVISION}")
set(RDKAFKA_MIN_VERSION 0x00090400)
set(RDKAFKA_MIN_VERSION "0.9.4")
set(RDKAFKA_MIN_VERSION_HEX 0x00090400)

if (NOT CMAKE_CXX_FLAGS)
# Set default compile flags for the project
Expand All @@ -23,7 +31,6 @@ if (NOT CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall")
endif()
endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")

# Set output directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
Expand All @@ -37,16 +44,35 @@ option(CPPKAFKA_DISABLE_EXAMPLES "Disable build of cppkafka examples." OFF)
option(CPPKAFKA_BOOST_STATIC_LIBS "Link with Boost static libraries." ON)
option(CPPKAFKA_BOOST_USE_MULTITHREADED "Use Boost multithreaded libraries." ON)
option(CPPKAFKA_RDKAFKA_STATIC_LIB "Link with Rdkafka static library." OFF)
option(CPPKAFKA_EXPORT_PKGCONFIG "Generate 'cppkafka.pc' file" ON)
option(CPPKAFKA_EXPORT_CMAKE_CONFIG "Generate CMake config, target and version files." ON)

# Add FindRdKafka.cmake
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")

math(EXPR BITS "8*${CMAKE_SIZEOF_VOID_P}")
if (NOT CPPKAFKA_CONFIG_DIR)
set(CPPKAFKA_CONFIG_DIR lib/cmake/${PROJECT_NAME})
endif()

# Maintain previous compatibility
if (RDKAFKA_ROOT_DIR)
set(RdKafka_ROOT ${RDKAFKA_ROOT_DIR})
elseif (RDKAFKA_ROOT)
set(RdKafka_ROOT ${RDKAFKA_ROOT})
endif()

if (RdKafka_ROOT)
if (NOT IS_ABSOLUTE ${RdKafka_ROOT})
set(RdKafka_ROOT "${CMAKE_SOURCE_DIR}/${RdKafka_ROOT}")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? I think I've used -Dsomething=../../x and it works IIRC.

Copy link
Contributor Author

@accelerated accelerated Jun 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't for me. Seems that find_package() expects a proper relative path from the point where it's being evaluated. In this case it was evaluated from cppkafka/cmake which made Travis fail. The second thing is that when using the CppkafkaConfig.cmake, which in turn calls FindRdKafka.cmake , you have no control of where it will be accessed from. This is meant to be used by 3rd party libs linking CppKafka. So FindRdKafka.cmake should have a proper path to the RdKafka root.

endif()
endif()

# Properly set the output directory
#if (${BITS} EQUAL 64)
# set(LIBDIR "lib64")
#else()
# set(LIBDIR "lib")
#endif()
set(LIBDIR "lib")
if (RDKAFKA_DIR)
set(RdKafka_DIR ${RDKAFKA_DIR}) # For older versions of find_package
if (NOT IS_ABSOLUTE ${RdKafka_ROOT})
set(RdKafka_DIR "${CMAKE_SOURCE_DIR}/${RdKafka_DIR}")
endif()
endif()

# Disable output from find_package macro
if (NOT CPPKAFKA_CMAKE_VERBOSE)
Expand All @@ -61,19 +87,23 @@ else()
message(STATUS "Build will generate a static library.")
set(CPPKAFKA_LIBRARY_TYPE STATIC)
add_definitions("-DCPPKAFKA_STATIC=1")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()

if (CPPKAFKA_RDKAFKA_STATIC_LIB)
add_definitions("-DLIBRDKAFKA_STATICLIB")
endif()

if (NOT CPPKAFKA_CONFIG_DIR)
set(CPPKAFKA_CONFIG_DIR lib/cmake/${PROJECT_NAME})
endif()

if (NOT CPPKAFKA_PKGCONFIG_DIR)
set(CPPKAFKA_PKGCONFIG_DIR share/pkgconfig)
endif()

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

if (Boost_FOUND)
find_package(Boost COMPONENTS program_options ${FIND_PACKAGE_QUIET})
Expand All @@ -90,8 +120,24 @@ if (Boost_FOUND)
endif()
endif()

# Try to find the RdKafka configuration file if present.
# This will search default system locations as well as RdKafka_ROOT and RdKafka_Dir paths if specified.
find_package(RdKafka ${FIND_PACKAGE_QUIET} CONFIG)
set(RDKAFKA_TARGET_IMPORTS ${RdKafka_FOUND})
if (NOT RdKafka_FOUND)
message(STATUS "RdKafkaConfig.cmake not found. Attempting to find module instead...")
find_package(RdKafka REQUIRED ${FIND_PACKAGE_QUIET} MODULE)
if (NOT RdKafka_FOUND)
message(FATAL_ERROR "RdKafka module not found. Please set RDKAFKA_ROOT to the install path or RDKAFKA_DIR pointing to the RdKafka configuration file location.")
else()
message(STATUS "RdKafka module found.")
endif()
else()
message(STATUS "RdKafka configuration file found: ${RdKafka_CONFIG}")
endif()

add_subdirectory(src)
add_subdirectory(include)
add_subdirectory(include/cppkafka)

# Examples target
if (NOT CPPKAFKA_DISABLE_EXAMPLES AND Boost_PROGRAM_OPTIONS_FOUND)
Expand Down
37 changes: 17 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ int main() {
In order to compile _cppkafka_ you need:

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

Now, in order to build, just run:

Expand All @@ -66,12 +65,14 @@ mkdir build
cd build
cmake <OPTIONS> ..
make
make install
```

## CMake options

The following cmake options can be specified:
* `RDKAFKA_ROOT_DIR` : Specify a different librdkafka install directory.
* `RDKAFKA_ROOT` : Specify a different librdkafka install directory.
* `RDKAFKA_DIR` : Specify a different directory where the RdKafkaConfig.cmake is installed.
* `BOOST_ROOT` : Specify a different Boost install directory.
* `CPPKAFKA_CMAKE_VERBOSE` : Generate verbose output. Default is `OFF`.
* `CPPKAFKA_BUILD_SHARED` : Build cppkafka as a shared library. Default is `ON`.
Expand All @@ -80,25 +81,14 @@ The following cmake options can be specified:
* `CPPKAFKA_BOOST_STATIC_LIBS` : Link with Boost static libraries. Default is `ON`.
* `CPPKAFKA_BOOST_USE_MULTITHREADED` : Use Boost multi-threaded libraries. Default is `ON`.
* `CPPKAFKA_RDKAFKA_STATIC_LIB` : Link to Rdkafka static library. Default is `OFF`.
* `CPPKAFKA_CONFIG_DIR` : Install location of the cmake configuration files. Default is `lib/cmake/cppkafka`.
* `CPPKAFKA_PKGCONFIG_DIR` : Install location of the .pc file. Default is `share/pkgconfig`.
* `CPPKAFKA_EXPORT_PKGCONFIG` : Generate `cppkafka.pc` file. Default is `ON`.
* `CPPKAFKA_EXPORT_CMAKE_CONFIG` : Generate CMake config, target and version files. Default is `ON`.

Example:
```Shell
cmake -DRDKAFKA_ROOT_DIR=/some/other/dir -DCPPKAFKA_BUILD_SHARED=OFF ...
```

The `RDKAFKA_ROOT_DIR` must contain the following structure. If the system
architecture is 64-bit and both `lib` and `lib64` folders are available, the `lib64`
folder location will be selected by cmake.

```Shell
${RDKAFKA_ROOT_DIR}/
|
+ include/librdkafka/rdkafka.h
|
+ lib/librdkafka.a
|
+ lib64/librdkafka.a (optional)
cmake -DRDKAFKA_ROOT=/some/other/dir -DCPPKAFKA_BUILD_SHARED=OFF ...
```

# Using
Expand All @@ -108,6 +98,13 @@ If you want to use _cppkafka_, you'll need to link your application with:
* _cppkafka_
* _rdkafka_

If using CMake, this is simplified by doing:
```cmake
find_package(CppKafka REQUIRED)

target_link_libraries(<YourLibrary> CppKafka::cppkafka)
```

# Documentation

You can generate the documentation by running `make docs` inside the build directory. This requires
Expand Down
71 changes: 48 additions & 23 deletions cmake/FindRdKafka.cmake
Original file line number Diff line number Diff line change
@@ -1,55 +1,80 @@
# Override default CMAKE_FIND_LIBRARY_SUFFIXES
# (Allows optional prioritization of static libraries during resolution)
# This find module helps find the RdKafka module. It exports the following variables:
# - RdKafka_INCLUDE_DIR : The directory where rdkafka.h is located.
# - RdKafka_LIBNAME : The name of the library, i.e. librdkafka.a, librdkafka.so, etc.
# - RdKafka_LIBRARY_DIR : The directory where the library is located.
# - RdKafka_LIBRARY_PATH : The full library path i.e. ${RdKafka_LIBRARY_DIR}/${RdKafka_LIBNAME}
# - RdKafka::rdkafka : Imported library containing all above properties set.

if (CPPKAFKA_RDKAFKA_STATIC_LIB)
set(RDKAFKA_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX})
set(RDKAFKA_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
set(RDKAFKA_LIBRARY_TYPE STATIC)
else()
set(RDKAFKA_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX})
set(RDKAFKA_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
set(RDKAFKA_LIBRARY_TYPE SHARED)
endif()

find_path(RDKAFKA_ROOT_DIR
NAMES include/librdkafka/rdkafka.h
)
set(RdKafka_LIBNAME ${RDKAFKA_PREFIX}rdkafka${RDKAFKA_SUFFIX})

find_path(RDKAFKA_INCLUDE_DIR
find_path(RdKafka_INCLUDE_DIR
NAMES librdkafka/rdkafka.h
HINTS ${RDKAFKA_ROOT_DIR}/include
HINTS ${RdKafka_ROOT}/include
)

find_path(RdKafka_LIBRARY_DIR
NAMES ${RdKafka_LIBNAME} rdkafka
HINTS ${RdKafka_ROOT}/lib ${RdKafka_ROOT}/lib64
)

find_library(RdKafka_LIBRARY_PATH
NAMES ${RdKafka_LIBNAME} rdkafka
HINTS ${RdKafka_LIBRARY_DIR}
)

# Check lib paths
if (CPPKAFKA_CMAKE_VERBOSE)
get_property(FIND_LIBRARY_32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
get_property(FIND_LIBRARY_64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
MESSAGE(STATUS "RDKAFKA search 32-bit library paths: ${FIND_LIBRARY_32}")
MESSAGE(STATUS "RDKAFKA search 64-bit library paths: ${FIND_LIBRARY_64}")
message(STATUS "RDKAFKA search 32-bit library paths: ${FIND_LIBRARY_32}")
message(STATUS "RDKAFKA search 64-bit library paths: ${FIND_LIBRARY_64}")
message(STATUS "RdKafka_ROOT = ${RdKafka_ROOT}")
message(STATUS "RdKafka_INCLUDE_DIR = ${RdKafka_INCLUDE_DIR}")
message(STATUS "RdKafka_LIBNAME = ${RdKafka_LIBNAME}")
message(STATUS "RdKafka_LIBRARY_PATH = ${RdKafka_LIBRARY_PATH}")
message(STATUS "RdKafka_LIBRARY_DIR = ${RdKafka_LIBRARY_DIR}")
endif()

find_library(RDKAFKA_LIBRARY
NAMES ${RDKAFKA_PREFIX}rdkafka${RDKAFKA_SUFFIX} rdkafka
HINTS ${RDKAFKA_ROOT_DIR}/lib
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(RDKAFKA DEFAULT_MSG
RDKAFKA_LIBRARY
RDKAFKA_INCLUDE_DIR
RdKafka_LIBNAME
RdKafka_LIBRARY_DIR
RdKafka_LIBRARY_PATH
RdKafka_INCLUDE_DIR
)

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

try_compile(HAVE_VALID_KAFKA_VERSION ${CMAKE_CURRENT_BINARY_DIR}
try_compile(RdKafka_FOUND ${CMAKE_CURRENT_BINARY_DIR}
SOURCES ${FILE_NAME}
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${RDKAFKA_INCLUDE_DIR}")
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${RdKafka_INCLUDE_DIR}")

if (HAVE_VALID_KAFKA_VERSION)
if (RdKafka_FOUND)
add_library(RdKafka::rdkafka ${RDKAFKA_LIBRARY_TYPE} IMPORTED GLOBAL)
set(RDKAFKA_DEPENDENCIES pthread rt ssl crypto dl z)
set_target_properties(RdKafka::rdkafka PROPERTIES
IMPORTED_NAME RdKafka
IMPORTED_LOCATION "${RdKafka_LIBRARY_PATH}"
INTERFACE_INCLUDE_DIRECTORIES "${RdKafka_INCLUDE_DIR}"
INTERFACE_LINK_DIRECTORIES "${RdKafka_LIBRARY_DIR}"
INTERFACE_LINK_LIBRARIES "${RDKAFKA_DEPENDENCIES}")
message(STATUS "Found valid rdkafka version")
mark_as_advanced(
RDKAFKA_ROOT_DIR
RDKAFKA_INCLUDE_DIR
RDKAFKA_LIBRARY
RdKafka_LIBRARY_DIR
RdKafka_INCLUDE_DIR
)
else()
message(FATAL_ERROR "Failed to find valid rdkafka version")
Expand Down
33 changes: 33 additions & 0 deletions cmake/config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)

# Add FindRdKafka.cmake
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}")

set(RDKAFKA_MIN_VERSION_HEX "@RDKAFKA_MIN_VERSION_HEX@")

# Find boost optional
find_dependency(Boost REQUIRED)

# Try to find the RdKafka configuration file if present.
# This will search default system locations as well as RdKafka_ROOT and RdKafka_DIR paths if specified.
find_package(RdKafka QUIET CONFIG)
set(RDKAFKA_TARGET_IMPORTS ${RdKafka_FOUND})
if (NOT RdKafka_FOUND)
find_dependency(RdKafka REQUIRED MODULE)
endif()

include("${CMAKE_CURRENT_LIST_DIR}/@TARGET_EXPORT_NAME@.cmake")

# Export 'CppKafka_ROOT'
set_and_check(@PROJECT_NAME@_ROOT "@PACKAGE_CMAKE_INSTALL_PREFIX@")

# Export 'CppKafka_INSTALL_INCLUDE_DIR'
set_and_check(@PROJECT_NAME@_INSTALL_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@")

# Export 'CppKafka_INSTALL_LIB_DIR'
set_and_check(@PROJECT_NAME@_INSTALL_LIB_DIR "@PACKAGE_CMAKE_INSTALL_LIBDIR@")

# Validate installed components
check_required_components("@PROJECT_NAME@")
File renamed without changes.
4 changes: 2 additions & 2 deletions cppkafka.pc.in → cmake/cppkafka.pc.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
libdir=${prefix}/@LIBDIR@
sharedlibdir=${prefix}/@LIBDIR@
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
sharedlibdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/include

Name: cppkafka
Expand Down
3 changes: 1 addition & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
link_libraries(cppkafka ${RDKAFKA_LIBRARY} ${Boost_LIBRARIES} pthread rt ssl crypto dl z)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
include_directories(SYSTEM ${RDKAFKA_INCLUDE_DIR})

add_custom_target(examples)
macro(create_example example_name)
string(REPLACE "_" "-" sanitized_name ${example_name})
add_executable(${sanitized_name} EXCLUDE_FROM_ALL "${example_name}_example.cpp")
target_link_libraries(${sanitized_name} cppkafka RdKafka::rdkafka Boost::boost Boost::program_options)
add_dependencies(examples ${sanitized_name})
endmacro()

Expand Down
1 change: 0 additions & 1 deletion include/CMakeLists.txt

This file was deleted.

2 changes: 1 addition & 1 deletion include/cppkafka/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function(make_cppkafka_header)
endforeach()

#create file from template
configure_file(${PROJECT_SOURCE_DIR}/cppkafka.h.in ${CPPKAFKA_HEADER} @ONLY)
configure_file(${PROJECT_SOURCE_DIR}/cmake/cppkafka.h.in ${CPPKAFKA_HEADER} @ONLY)
endfunction()

# Run file generation function
Expand Down
Loading