From 7576aa612c7b7b6b3035718155b6e83972ac2bd1 Mon Sep 17 00:00:00 2001 From: Thomas Devoogdt Date: Tue, 25 Jul 2023 09:10:41 +0200 Subject: [PATCH] CMakeLists.txt: allow compilation without CXX support #4364 e.g. fluent-bit is a c-only library, so allow compilation without cxx Signed-off-by: Thomas Devoogdt --- CMakeLists.txt | 16 +++++++- examples/CMakeLists.txt | 31 +++++++++------ tests/CMakeLists.txt | 85 +++++++++++++++++++++++------------------ tests/test.c | 60 +++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f3d05bad7a..5b535d05a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,16 @@ cmake_minimum_required(VERSION 3.5) include("packaging/cmake/parseversion.cmake") parseversion("src/rdkafka.h") -project(RdKafka VERSION ${RDKAFKA_VERSION}) +project(RdKafka VERSION ${RDKAFKA_VERSION} LANGUAGES C) + +# Check for CXX support +include(CheckLanguage) +check_language(CXX) +if(CMAKE_CXX_COMPILER) + enable_language(CXX) +else() + message(STATUS "C++ compiler not found, skipping C++ support") +endif() set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/packaging/cmake/Modules/") @@ -279,7 +288,10 @@ install( ) add_subdirectory(src) -add_subdirectory(src-cpp) + +if(CMAKE_CXX_COMPILER) + add_subdirectory(src-cpp) +endif() if(RDKAFKA_BUILD_EXAMPLES) add_subdirectory(examples) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 91851d2cbf..6874195ce2 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -5,8 +5,10 @@ endif(WIN32) add_executable(producer producer.c ${win32_sources}) target_link_libraries(producer PUBLIC rdkafka) -add_executable(producer_cpp producer.cpp ${win32_sources}) -target_link_libraries(producer_cpp PUBLIC rdkafka++) +if(CMAKE_CXX_COMPILER) + add_executable(producer_cpp producer.cpp ${win32_sources}) + target_link_libraries(producer_cpp PUBLIC rdkafka++) +endif() add_executable(consumer consumer.c ${win32_sources}) target_link_libraries(consumer PUBLIC rdkafka) @@ -14,14 +16,20 @@ target_link_libraries(consumer PUBLIC rdkafka) add_executable(rdkafka_performance rdkafka_performance.c ${win32_sources}) target_link_libraries(rdkafka_performance PUBLIC rdkafka) -add_executable(rdkafka_example_cpp rdkafka_example.cpp ${win32_sources}) -target_link_libraries(rdkafka_example_cpp PUBLIC rdkafka++) +if(CMAKE_CXX_COMPILER) + add_executable(rdkafka_example_cpp rdkafka_example.cpp ${win32_sources}) + target_link_libraries(rdkafka_example_cpp PUBLIC rdkafka++) +endif() -add_executable(rdkafka_complex_consumer_example_cpp rdkafka_complex_consumer_example.cpp ${win32_sources}) -target_link_libraries(rdkafka_complex_consumer_example_cpp PUBLIC rdkafka++) +if(CMAKE_CXX_COMPILER) + add_executable(rdkafka_complex_consumer_example_cpp rdkafka_complex_consumer_example.cpp ${win32_sources}) + target_link_libraries(rdkafka_complex_consumer_example_cpp PUBLIC rdkafka++) +endif() -add_executable(openssl_engine_example_cpp openssl_engine_example.cpp ${win32_sources}) -target_link_libraries(openssl_engine_example_cpp PUBLIC rdkafka++) +if(CMAKE_CXX_COMPILER) + add_executable(openssl_engine_example_cpp openssl_engine_example.cpp ${win32_sources}) + target_link_libraries(openssl_engine_example_cpp PUBLIC rdkafka++) +endif() add_executable(misc misc.c ${win32_sources}) target_link_libraries(misc PUBLIC rdkafka) @@ -73,7 +81,8 @@ if(NOT WIN32) add_executable(rdkafka_complex_consumer_example rdkafka_complex_consumer_example.c) target_link_libraries(rdkafka_complex_consumer_example PUBLIC rdkafka) - add_executable(kafkatest_verifiable_client kafkatest_verifiable_client.cpp) - target_link_libraries(kafkatest_verifiable_client PUBLIC rdkafka++) - + if(CMAKE_CXX_COMPILER) + add_executable(kafkatest_verifiable_client kafkatest_verifiable_client.cpp) + target_link_libraries(kafkatest_verifiable_client PUBLIC rdkafka++) + endif() endif(NOT WIN32) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 324281bd99..34239a79a8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -48,38 +48,22 @@ set( 0050-subscribe_adds.c 0051-assign_adds.c 0052-msg_timestamps.c - 0053-stats_cb.cpp - 0054-offset_time.cpp 0055-producer_latency.c 0056-balanced_group_mt.c - 0057-invalid_topic.cpp - 0058-log.cpp - 0059-bsearch.cpp - 0060-op_prio.cpp - 0061-consumer_lag.cpp - 0062-stats_event.c - 0063-clusterid.cpp 0064-interceptors.c - 0065-yield.cpp - 0066-plugins.cpp - 0067-empty_topic.cpp 0068-produce_timeout.c 0069-consumer_add_parts.c - 0070-null_empty.cpp 0072-headers_ut.c 0073-headers.c 0074-producev.c 0075-retry.c 0076-produce_retry.c 0077-compaction.c - 0078-c_from_cpp.cpp 0079-fork.c 0080-admin_ut.c 0081-admin.c - 0082-fetch_max_bytes.cpp 0083-cb_event.c 0084-destroy_flags.c - 0085-headers.cpp 0086-purge.c 0088-produce_metadata_timeout.c 0089-max_poll_interval.c @@ -88,29 +72,15 @@ set( 0092-mixed_msgver.c 0093-holb.c 0094-idempotence_msg_timeout.c - 0095-all_brokers_down.cpp - 0097-ssl_verify.cpp - 0098-consumer-txn.cpp - 0099-commit_metadata.c - 0100-thread_interceptors.cpp - 0101-fetch-from-follower.cpp 0102-static_group_rebalance.c 0103-transactions.c 0104-fetch_from_follower_mock.c 0105-transactions_mock.c 0106-cgrp_sess_timeout.c 0107-topic_recreate.c - 0109-auto_create_topics.cpp - 0110-batch_size.cpp - 0111-delay_create_topics.cpp 0112-assign_unknown_part.c - 0113-cooperative_rebalance.cpp - 0114-sticky_partitioning.cpp - 0115-producer_auth.cpp - 0116-kafkaconsumer_close.cpp 0117-mock_errors.c 0118-commit_rebalance.c - 0119-consumer_auth.cpp 0120-asymmetric_subscription.c 0121-clusterid.c 0122-buffer_cleaning_after_rebalance.c @@ -118,20 +88,16 @@ set( 0124-openssl_invalid_engine.c 0125-immediate_flush.c 0126-oauthbearer_oidc.c - 0127-fetch_queue_backoff.cpp - 0128-sasl_callback_queue.cpp 0129-fetch_aborted_msgs.c 0130-store_offsets.c 0131-connect_timeout.c 0132-strategy_ordering.c 0133-ssl_keys.c 0134-ssl_provider.c - 0135-sasl_credentials.cpp 0136-resolve_cb.c 0137-barrier_batch_consume.c 0138-admin_mock.c 0139-offset_validation_mock.c - 0140-commit_metadata.cpp 0142-reauthentication.c 0143-exponential_backoff_mock.c 0144-idempotence_mock.c @@ -144,13 +110,53 @@ set( 0151-purge-brokers.c 0152-rebootstrap.c 0153-memberid.c - 8000-idle.cpp 8001-fetch_from_follower_mock_manual.c test.c - testcpp.cpp rusage.c ) +if(CMAKE_CXX_COMPILER) + add_compile_definitions(_CXX=1) + list(APPEND sources + 0053-stats_cb.cpp + 0054-offset_time.cpp + 0057-invalid_topic.cpp + 0058-log.cpp + 0059-bsearch.cpp + 0060-op_prio.cpp + 0061-consumer_lag.cpp + 0062-stats_event.c + 0063-clusterid.cpp + 0065-yield.cpp + 0066-plugins.cpp + 0067-empty_topic.cpp + 0070-null_empty.cpp + 0078-c_from_cpp.cpp + 0082-fetch_max_bytes.cpp + 0085-headers.cpp + 0095-all_brokers_down.cpp + 0097-ssl_verify.cpp + 0098-consumer-txn.cpp + 0099-commit_metadata.c + 0100-thread_interceptors.cpp + 0101-fetch-from-follower.cpp + 0109-auto_create_topics.cpp + 0110-batch_size.cpp + 0111-delay_create_topics.cpp + 0113-cooperative_rebalance.cpp + 0114-sticky_partitioning.cpp + 0115-producer_auth.cpp + 0116-kafkaconsumer_close.cpp + 0119-consumer_auth.cpp + 0127-fetch_queue_backoff.cpp + 0128-sasl_callback_queue.cpp + 0135-sasl_credentials.cpp + 0140-commit_metadata.cpp + 8000-idle.cpp + testcpp.cpp + ) +endif() + if(NOT WIN32) list(APPEND sources sockem.c sockem_ctrl.c) else() @@ -158,7 +164,12 @@ else() endif() add_executable(test-runner ${sources}) -target_link_libraries(test-runner PUBLIC rdkafka++) + +if(CMAKE_CXX_COMPILER) + target_link_libraries(test-runner PUBLIC rdkafka++) +else() + target_link_libraries(test-runner PUBLIC rdkafka) +endif() add_test(NAME RdKafkaTestInParallel COMMAND test-runner -p5) add_test(NAME RdKafkaTestSequentially COMMAND test-runner -p1) diff --git a/tests/test.c b/tests/test.c index 70929f8551..ad01910df3 100644 --- a/tests/test.c +++ b/tests/test.c @@ -165,12 +165,15 @@ _TEST_DECL(0049_consume_conn_close); _TEST_DECL(0050_subscribe_adds); _TEST_DECL(0051_assign_adds); _TEST_DECL(0052_msg_timestamps); +#ifdef _CXX _TEST_DECL(0053_stats_timing); _TEST_DECL(0053_stats); _TEST_DECL(0054_offset_time); +#endif _TEST_DECL(0055_producer_latency); _TEST_DECL(0055_producer_latency_mock); _TEST_DECL(0056_balanced_group_mt); +#ifdef _CXX _TEST_DECL(0057_invalid_topic); _TEST_DECL(0058_log); _TEST_DECL(0059_bsearch); @@ -178,13 +181,18 @@ _TEST_DECL(0060_op_prio); _TEST_DECL(0061_consumer_lag); _TEST_DECL(0062_stats_event); _TEST_DECL(0063_clusterid); +#endif _TEST_DECL(0064_interceptors); +#ifdef _CXX _TEST_DECL(0065_yield); _TEST_DECL(0066_plugins); _TEST_DECL(0067_empty_topic); +#endif _TEST_DECL(0068_produce_timeout); _TEST_DECL(0069_consumer_add_parts); +#ifdef _CXX _TEST_DECL(0070_null_empty); +#endif _TEST_DECL(0072_headers_ut); _TEST_DECL(0073_headers); _TEST_DECL(0074_producev); @@ -192,15 +200,21 @@ _TEST_DECL(0075_retry); _TEST_DECL(0076_produce_retry); _TEST_DECL(0076_produce_retry_mock); _TEST_DECL(0077_compaction); +#ifdef _CXX _TEST_DECL(0078_c_from_cpp); +#endif _TEST_DECL(0079_fork); _TEST_DECL(0080_admin_ut); _TEST_DECL(0081_admin); +#ifdef _CXX _TEST_DECL(0082_fetch_max_bytes); +#endif _TEST_DECL(0083_cb_event); _TEST_DECL(0084_destroy_flags_local); _TEST_DECL(0084_destroy_flags); +#ifdef _CXX _TEST_DECL(0085_headers); +#endif _TEST_DECL(0086_purge_local); _TEST_DECL(0086_purge_remote); _TEST_DECL(0088_produce_metadata_timeout); @@ -210,6 +224,7 @@ _TEST_DECL(0091_max_poll_interval_timeout); _TEST_DECL(0092_mixed_msgver); _TEST_DECL(0093_holb_consumer); _TEST_DECL(0094_idempotence_msg_timeout); +#ifdef _CXX _TEST_DECL(0095_all_brokers_down); _TEST_DECL(0097_ssl_verify); _TEST_DECL(0097_ssl_verify_local); @@ -217,6 +232,7 @@ _TEST_DECL(0098_consumer_txn); _TEST_DECL(0099_commit_metadata); _TEST_DECL(0100_thread_interceptors); _TEST_DECL(0101_fetch_from_follower); +#endif _TEST_DECL(0102_static_group_rebalance); _TEST_DECL(0102_static_group_rebalance_mock); _TEST_DECL(0103_transactions_local); @@ -225,18 +241,24 @@ _TEST_DECL(0104_fetch_from_follower_mock); _TEST_DECL(0105_transactions_mock); _TEST_DECL(0106_cgrp_sess_timeout); _TEST_DECL(0107_topic_recreate); +#ifdef _CXX _TEST_DECL(0109_auto_create_topics); _TEST_DECL(0110_batch_size); _TEST_DECL(0111_delay_create_topics); +#endif _TEST_DECL(0112_assign_unknown_part); +#ifdef _CXX _TEST_DECL(0113_cooperative_rebalance_local); _TEST_DECL(0113_cooperative_rebalance); _TEST_DECL(0114_sticky_partitioning); _TEST_DECL(0115_producer_auth); _TEST_DECL(0116_kafkaconsumer_close); +#endif _TEST_DECL(0117_mock_errors); _TEST_DECL(0118_commit_rebalance); +#ifdef _CXX _TEST_DECL(0119_consumer_auth); +#endif _TEST_DECL(0120_asymmetric_subscription); _TEST_DECL(0121_clusterid); _TEST_DECL(0122_buffer_cleaning_after_rebalance); @@ -245,20 +267,26 @@ _TEST_DECL(0124_openssl_invalid_engine); _TEST_DECL(0125_immediate_flush); _TEST_DECL(0125_immediate_flush_mock); _TEST_DECL(0126_oauthbearer_oidc); +#ifdef _CXX _TEST_DECL(0127_fetch_queue_backoff); _TEST_DECL(0128_sasl_callback_queue); +#endif _TEST_DECL(0129_fetch_aborted_msgs); _TEST_DECL(0130_store_offsets); _TEST_DECL(0131_connect_timeout); _TEST_DECL(0132_strategy_ordering); _TEST_DECL(0133_ssl_keys); _TEST_DECL(0134_ssl_provider); +#ifdef _CXX _TEST_DECL(0135_sasl_credentials); +#endif _TEST_DECL(0136_resolve_cb); _TEST_DECL(0137_barrier_batch_consume); _TEST_DECL(0138_admin_mock); _TEST_DECL(0139_offset_validation_mock); +#ifdef _CXX _TEST_DECL(0140_commit_metadata); +#endif _TEST_DECL(0142_reauthentication); _TEST_DECL(0143_exponential_backoff_mock); _TEST_DECL(0144_idempotence_mock); @@ -273,7 +301,9 @@ _TEST_DECL(0152_rebootstrap_local); _TEST_DECL(0153_memberid); /* Manual tests */ +#ifdef _CXX _TEST_DECL(8000_idle); +#endif _TEST_DECL(8001_fetch_from_follower_mock_manual); @@ -400,12 +430,15 @@ struct test tests[] = { _TEST(0050_subscribe_adds, 0, TEST_BRKVER(0, 9, 0, 0)), _TEST(0051_assign_adds, 0, TEST_BRKVER(0, 9, 0, 0)), _TEST(0052_msg_timestamps, 0, TEST_BRKVER(0, 10, 0, 0)), +#ifdef _CXX _TEST(0053_stats_timing, TEST_F_LOCAL), _TEST(0053_stats, 0), _TEST(0054_offset_time, 0, TEST_BRKVER(0, 10, 1, 0)), +#endif _TEST(0055_producer_latency, TEST_F_KNOWN_ISSUE_WIN32), _TEST(0055_producer_latency_mock, TEST_F_LOCAL), _TEST(0056_balanced_group_mt, 0, TEST_BRKVER(0, 9, 0, 0)), +#ifdef _CXX _TEST(0057_invalid_topic, 0, TEST_BRKVER(0, 9, 0, 0)), _TEST(0058_log, TEST_F_LOCAL), _TEST(0059_bsearch, 0, TEST_BRKVER(0, 10, 0, 0)), @@ -413,20 +446,25 @@ struct test tests[] = { _TEST(0061_consumer_lag, 0), _TEST(0062_stats_event, TEST_F_LOCAL), _TEST(0063_clusterid, 0, TEST_BRKVER(0, 10, 1, 0)), +#endif _TEST(0064_interceptors, 0, TEST_BRKVER(0, 9, 0, 0)), +#ifdef _CXX _TEST(0065_yield, 0), _TEST(0066_plugins, TEST_F_LOCAL | TEST_F_KNOWN_ISSUE_WIN32 | TEST_F_KNOWN_ISSUE_OSX, .extra = "dynamic loading of tests might not be fixed for this platform"), _TEST(0067_empty_topic, 0), +#endif #if WITH_SOCKEM _TEST(0068_produce_timeout, TEST_F_SOCKEM), #endif _TEST(0069_consumer_add_parts, TEST_F_KNOWN_ISSUE_WIN32, TEST_BRKVER(1, 0, 0, 0)), +#ifdef _CXX _TEST(0070_null_empty, 0), +#endif _TEST(0072_headers_ut, TEST_F_LOCAL), _TEST(0073_headers, 0, TEST_BRKVER(0, 11, 0, 0)), _TEST(0074_producev, TEST_F_LOCAL), @@ -439,18 +477,24 @@ struct test tests[] = { 0, /* The test itself requires message headers */ TEST_BRKVER(0, 11, 0, 0)), +#ifdef _CXX _TEST(0078_c_from_cpp, TEST_F_LOCAL), +#endif _TEST(0079_fork, TEST_F_LOCAL | TEST_F_KNOWN_ISSUE, .extra = "using a fork():ed rd_kafka_t is not supported and will " "most likely hang"), _TEST(0080_admin_ut, TEST_F_LOCAL), _TEST(0081_admin, 0, TEST_BRKVER(0, 10, 2, 0)), +#ifdef _CXX _TEST(0082_fetch_max_bytes, 0, TEST_BRKVER(0, 10, 1, 0)), +#endif _TEST(0083_cb_event, 0, TEST_BRKVER(0, 9, 0, 0)), _TEST(0084_destroy_flags_local, TEST_F_LOCAL), _TEST(0084_destroy_flags, 0), +#ifdef _CXX _TEST(0085_headers, 0, TEST_BRKVER(0, 11, 0, 0)), +#endif _TEST(0086_purge_local, TEST_F_LOCAL), _TEST(0086_purge_remote, 0), #if WITH_SOCKEM @@ -466,6 +510,7 @@ struct test tests[] = { TEST_F_SOCKEM, TEST_BRKVER(0, 11, 0, 0)), #endif +#ifdef _CXX _TEST(0095_all_brokers_down, TEST_F_LOCAL), _TEST(0097_ssl_verify, 0), _TEST(0097_ssl_verify_local, TEST_F_LOCAL), @@ -473,6 +518,7 @@ struct test tests[] = { _TEST(0099_commit_metadata, 0), _TEST(0100_thread_interceptors, TEST_F_LOCAL), _TEST(0101_fetch_from_follower, 0, TEST_BRKVER(2, 4, 0, 0)), +#endif _TEST(0102_static_group_rebalance, 0, TEST_BRKVER(2, 3, 0, 0)), _TEST(0102_static_group_rebalance_mock, TEST_F_LOCAL), _TEST(0103_transactions_local, TEST_F_LOCAL), @@ -487,13 +533,16 @@ struct test tests[] = { 0, TEST_BRKVER_TOPIC_ADMINAPI, .scenario = "noautocreate"), +#ifdef _CXX _TEST(0109_auto_create_topics, 0), _TEST(0110_batch_size, 0), _TEST(0111_delay_create_topics, 0, TEST_BRKVER_TOPIC_ADMINAPI, .scenario = "noautocreate"), +#endif _TEST(0112_assign_unknown_part, 0), +#ifdef _CXX _TEST(0113_cooperative_rebalance_local, TEST_F_LOCAL, TEST_BRKVER(2, 4, 0, 0)), @@ -501,9 +550,12 @@ struct test tests[] = { _TEST(0114_sticky_partitioning, 0), _TEST(0115_producer_auth, 0, TEST_BRKVER(2, 1, 0, 0)), _TEST(0116_kafkaconsumer_close, TEST_F_LOCAL), +#endif _TEST(0117_mock_errors, TEST_F_LOCAL), _TEST(0118_commit_rebalance, 0), +#ifdef _CXX _TEST(0119_consumer_auth, 0, TEST_BRKVER(2, 1, 0, 0)), +#endif _TEST(0120_asymmetric_subscription, TEST_F_LOCAL), _TEST(0121_clusterid, TEST_F_LOCAL), _TEST(0122_buffer_cleaning_after_rebalance, 0, TEST_BRKVER(2, 4, 0, 0)), @@ -512,20 +564,26 @@ struct test tests[] = { _TEST(0125_immediate_flush, 0), _TEST(0125_immediate_flush_mock, TEST_F_LOCAL), _TEST(0126_oauthbearer_oidc, 0, TEST_BRKVER(3, 1, 0, 0)), +#ifdef _CXX _TEST(0127_fetch_queue_backoff, 0), _TEST(0128_sasl_callback_queue, TEST_F_LOCAL, TEST_BRKVER(2, 0, 0, 0)), +#endif _TEST(0129_fetch_aborted_msgs, 0, TEST_BRKVER(0, 11, 0, 0)), _TEST(0130_store_offsets, 0), _TEST(0131_connect_timeout, TEST_F_LOCAL), _TEST(0132_strategy_ordering, 0, TEST_BRKVER(2, 4, 0, 0)), _TEST(0133_ssl_keys, TEST_F_LOCAL), _TEST(0134_ssl_provider, TEST_F_LOCAL), +#ifdef _CXX _TEST(0135_sasl_credentials, 0), +#endif _TEST(0136_resolve_cb, TEST_F_LOCAL), _TEST(0137_barrier_batch_consume, 0), _TEST(0138_admin_mock, TEST_F_LOCAL, TEST_BRKVER(2, 4, 0, 0)), _TEST(0139_offset_validation_mock, 0), +#ifdef _CXX _TEST(0140_commit_metadata, 0), +#endif _TEST(0142_reauthentication, 0, TEST_BRKVER(2, 2, 0, 0)), _TEST(0143_exponential_backoff_mock, TEST_F_LOCAL), _TEST(0144_idempotence_mock, TEST_F_LOCAL, TEST_BRKVER(0, 11, 0, 0)), @@ -540,7 +598,9 @@ struct test tests[] = { _TEST(0153_memberid, 0, TEST_BRKVER(0, 4, 0, 0)), /* Manual tests */ +#ifdef _CXX _TEST(8000_idle, TEST_F_MANUAL), +#endif _TEST(8001_fetch_from_follower_mock_manual, TEST_F_MANUAL), {NULL}};