1- cmake_minimum_required (VERSION 2.8.1)
2- project (cppkafka)
1+ cmake_minimum_required (VERSION 3.9.2)
2+ project (CppKafka)
3+ if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12.0" )
4+ # Use <package>_ROOT variable to find configuration files
5+ cmake_policy (SET CMP0074 NEW)
6+ endif ()
7+
8+ include (GNUInstallDirs)
9+ include (CMakePackageConfigHelpers)
310
411# Set the version number.
512set (CPPKAFKA_VERSION_MAJOR 0)
613set (CPPKAFKA_VERSION_MINOR 3)
714set (CPPKAFKA_VERSION_REVISION 1)
815set (CPPKAFKA_VERSION "${CPPKAFKA_VERSION_MAJOR} .${CPPKAFKA_VERSION_MINOR} .${CPPKAFKA_VERSION_REVISION} " )
9- set (RDKAFKA_MIN_VERSION 0x00090400)
16+ set (RDKAFKA_MIN_VERSION "0.9.4" )
17+ set (RDKAFKA_MIN_VERSION_HEX 0x00090400)
1018
1119if (NOT CMAKE_CXX_FLAGS )
1220 # Set default compile flags for the project
@@ -23,7 +31,6 @@ if (NOT CMAKE_CXX_FLAGS)
2331 set (CMAKE_CXX_FLAGS "-std=c++11 -Wall" )
2432 endif ()
2533endif ()
26- set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR} /cmake/" )
2734
2835# Set output directories
2936set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} /lib)
@@ -37,16 +44,35 @@ option(CPPKAFKA_DISABLE_EXAMPLES "Disable build of cppkafka examples." OFF)
3744option (CPPKAFKA_BOOST_STATIC_LIBS "Link with Boost static libraries." ON )
3845option (CPPKAFKA_BOOST_USE_MULTITHREADED "Use Boost multithreaded libraries." ON )
3946option (CPPKAFKA_RDKAFKA_STATIC_LIB "Link with Rdkafka static library." OFF )
47+ option (CPPKAFKA_EXPORT_PKGCONFIG "Generate 'cppkafka.pc' file" ON )
48+ option (CPPKAFKA_EXPORT_CMAKE_CONFIG "Generate CMake config, target and version files." ON )
49+
50+ # Add FindRdKafka.cmake
51+ set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR} /cmake/" )
4052
41- math (EXPR BITS "8*${CMAKE_SIZEOF_VOID_P} " )
53+ if (NOT CPPKAFKA_CONFIG_DIR)
54+ set (CPPKAFKA_CONFIG_DIR lib/cmake/${PROJECT_NAME} )
55+ endif ()
56+
57+ # Maintain previous compatibility
58+ if (RDKAFKA_ROOT_DIR)
59+ set (RdKafka_ROOT ${RDKAFKA_ROOT_DIR} )
60+ elseif (RDKAFKA_ROOT)
61+ set (RdKafka_ROOT ${RDKAFKA_ROOT} )
62+ endif ()
63+
64+ if (RdKafka_ROOT)
65+ if (NOT IS_ABSOLUTE ${RdKafka_ROOT} )
66+ set (RdKafka_ROOT "${CMAKE_SOURCE_DIR} /${RdKafka_ROOT} " )
67+ endif ()
68+ endif ()
4269
43- # Properly set the output directory
44- #if (${BITS} EQUAL 64)
45- # set(LIBDIR "lib64")
46- #else()
47- # set(LIBDIR "lib")
48- #endif()
49- set (LIBDIR "lib" )
70+ if (RDKAFKA_DIR)
71+ set (RdKafka_DIR ${RDKAFKA_DIR} ) # For older versions of find_package
72+ if (NOT IS_ABSOLUTE ${RdKafka_ROOT} )
73+ set (RdKafka_DIR "${CMAKE_SOURCE_DIR} /${RdKafka_DIR} " )
74+ endif ()
75+ endif ()
5076
5177# Disable output from find_package macro
5278if (NOT CPPKAFKA_CMAKE_VERBOSE)
@@ -61,19 +87,23 @@ else()
6187 message (STATUS "Build will generate a static library." )
6288 set (CPPKAFKA_LIBRARY_TYPE STATIC )
6389 add_definitions ("-DCPPKAFKA_STATIC=1" )
90+ set (CMAKE_POSITION_INDEPENDENT_CODE ON )
6491endif ()
6592
6693if (CPPKAFKA_RDKAFKA_STATIC_LIB)
6794 add_definitions ("-DLIBRDKAFKA_STATICLIB" )
6895endif ()
6996
97+ if (NOT CPPKAFKA_CONFIG_DIR)
98+ set (CPPKAFKA_CONFIG_DIR lib/cmake/${PROJECT_NAME} )
99+ endif ()
100+
70101if (NOT CPPKAFKA_PKGCONFIG_DIR)
71102 set (CPPKAFKA_PKGCONFIG_DIR share/pkgconfig)
72103endif ()
73104
74105# Look for Boost (just need boost.optional headers here)
75106find_package (Boost REQUIRED ${FIND_PACKAGE_QUIET} )
76- find_package (RdKafka REQUIRED ${FIND_PACKAGE_QUIET} )
77107
78108if (Boost_FOUND)
79109 find_package (Boost COMPONENTS program_options ${FIND_PACKAGE_QUIET} )
@@ -90,8 +120,24 @@ if (Boost_FOUND)
90120 endif ()
91121endif ()
92122
123+ # Try to find the RdKafka configuration file if present.
124+ # This will search default system locations as well as RdKafka_ROOT and RdKafka_Dir paths if specified.
125+ find_package (RdKafka ${FIND_PACKAGE_QUIET} CONFIG)
126+ set (RDKAFKA_TARGET_IMPORTS ${RdKafka_FOUND} )
127+ if (NOT RdKafka_FOUND)
128+ message (STATUS "RdKafkaConfig.cmake not found. Attempting to find module instead..." )
129+ find_package (RdKafka REQUIRED ${FIND_PACKAGE_QUIET} MODULE)
130+ if (NOT RdKafka_FOUND)
131+ 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." )
132+ else ()
133+ message (STATUS "RdKafka module found." )
134+ endif ()
135+ else ()
136+ message (STATUS "RdKafka configuration file found: ${RdKafka_CONFIG} " )
137+ endif ()
138+
93139add_subdirectory (src)
94- add_subdirectory (include )
140+ add_subdirectory (include /cppkafka )
95141
96142# Examples target
97143if (NOT CPPKAFKA_DISABLE_EXAMPLES AND Boost_PROGRAM_OPTIONS_FOUND)
0 commit comments