diff --git a/CMakeLists.txt b/CMakeLists.txt index 06c6e278..2c70039e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,8 @@ set(CPPKAFKA_VERSION "${CPPKAFKA_VERSION_MAJOR}.${CPPKAFKA_VERSION_MINOR}.${CPPK set(RDKAFKA_MIN_VERSION "0.9.4") set(RDKAFKA_MIN_VERSION_HEX 0x00090400) +option(USE_CPP17 "Use C++17 features instead of Boost (still, if you don't have boost you can't build examples" OFF) + if (NOT CMAKE_CXX_FLAGS) # Set default compile flags for the project if(MSVC) @@ -31,8 +33,13 @@ if (NOT CMAKE_CXX_FLAGS) set(CMAKE_CXX_FLAGS "-Wall") endif() endif() -# Use C++17 -set(CMAKE_CXX_STANDARD 17) + +if(USE_CPP17) + set(CMAKE_CXX_STANDARD 17) + add_definitions("-D_USE_CPP17") +else() + set(CMAKE_CXX_STANDARD 11) +endif() # Set output directories set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib) @@ -104,32 +111,11 @@ if (NOT CPPKAFKA_PKGCONFIG_DIR) set(CPPKAFKA_PKGCONFIG_DIR share/pkgconfig) 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/cppkafka) - -# Examples target -if (NOT CPPKAFKA_DISABLE_EXAMPLES) +if(NOT USE_CPP17 OR NOT CPPKAFKA_DISABLE_EXAMPLES OR NOT CPPKAFKA_DISABLE_TESTS) # Look for Boost (just need boost.optional headers here) - find_package(Boost ${FIND_PACKAGE_QUIET} CONFIG) + find_package(Boost REQUIRED ${FIND_PACKAGE_QUIET}) + if (Boost_FOUND) - option(CPPKAFKA_BOOST_STATIC_LIBS "Link with Boost static libraries." ON) - option(CPPKAFKA_BOOST_USE_MULTITHREADED "Use Boost multithreaded libraries." ON) find_package(Boost COMPONENTS program_options ${FIND_PACKAGE_QUIET}) set(Boost_USE_STATIC_LIBS ${CPPKAFKA_BOOST_STATIC_LIBS}) set(Boost_USE_MULTITHREADED ${CPPKAFKA_BOOST_USE_MULTITHREADED}) @@ -142,12 +128,37 @@ if (NOT CPPKAFKA_DISABLE_EXAMPLES) message(STATUS "Boost is multi-threaded: ${CPPKAFKA_BOOST_USE_MULTITHREADED}") message(STATUS "Boost libraries: ${Boost_LIBRARIES}") endif() + endif() + + # Examples target + if (Boost_PROGRAM_OPTIONS_FOUND) add_subdirectory(examples) else() message(STATUS "Disabling examples") 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/cppkafka) + + + # Add a target to generate API documentation using Doxygen find_package(Doxygen ${FIND_PACKAGE_QUIET}) if(DOXYGEN_FOUND) @@ -188,4 +199,4 @@ if(NOT TARGET uninstall) # Add uninstall target add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) -endif() +endif() \ No newline at end of file diff --git a/include/cppkafka/cppkafka.h b/include/cppkafka/cppkafka.h index 86ac366d..450b4a70 100644 --- a/include/cppkafka/cppkafka.h +++ b/include/cppkafka/cppkafka.h @@ -57,11 +57,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include diff --git a/include/cppkafka/message.h b/include/cppkafka/message.h index ace85622..8168f436 100644 --- a/include/cppkafka/message.h +++ b/include/cppkafka/message.h @@ -34,7 +34,7 @@ #include #include #include -#include +#include "utils/optional.h" #include #include "buffer.h" #include "macros.h" @@ -189,7 +189,7 @@ class CPPKAFKA_API Message { * * If calling rd_kafka_message_timestamp returns -1, then boost::none_t will be returned. */ - std::optional get_timestamp() const; + optional get_timestamp() const; #if RD_KAFKA_VERSION >= RD_KAFKA_MESSAGE_LATENCY_SUPPORT_VERSION /** diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b9861a3b..49d5bc1c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -49,7 +49,7 @@ set_target_properties(${TARGET_NAME} PROPERTIES # In CMake >= 3.15 Boost::boost == Boost::headers target_link_libraries(${TARGET_NAME} PUBLIC RdKafka::rdkafka) -if(NOT CPPKAFKA_DISABLE_EXAMPLES) +if(NOT USE_CPP17 OR NOT CPPKAFKA_DISABLE_EXAMPLES OR NOT CPPKAFKA_DISABLE_TESTS) target_link_libraries(${TARGET_NAME} PUBLIC Boost::boost) endif() diff --git a/src/message.cpp b/src/message.cpp index ca83040c..a49f61ae 100644 --- a/src/message.cpp +++ b/src/message.cpp @@ -84,7 +84,7 @@ Message& Message::load_internal() { return *this; } -std::optional Message::get_timestamp() const { +optional Message::get_timestamp() const { rd_kafka_timestamp_type_t type = RD_KAFKA_TIMESTAMP_NOT_AVAILABLE; int64_t timestamp = rd_kafka_message_timestamp(handle_.get(), &type); if (timestamp == -1 || type == RD_KAFKA_TIMESTAMP_NOT_AVAILABLE) {