From 30c1597bf1df72c7769e68112388595482d056a4 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Wed, 22 Jan 2025 20:48:50 +0800 Subject: [PATCH 1/7] Export CMake package configuration file --- lang/c++/CMakeLists.txt | 57 +++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/lang/c++/CMakeLists.txt b/lang/c++/CMakeLists.txt index 8e7f12bb530..902bc0eba24 100644 --- a/lang/c++/CMakeLists.txt +++ b/lang/c++/CMakeLists.txt @@ -51,6 +51,7 @@ string(REPLACE "." ";" AVRO_VERSION ${AVRO_VERSION}) list(GET AVRO_VERSION 0 AVRO_VERSION_MAJOR) list(GET AVRO_VERSION 1 AVRO_VERSION_MINOR) list(GET AVRO_VERSION 2 AVRO_VERSION_PATCH) +set(AVRO_VERSION "${AVRO_VERSION_MAJOR}.${AVRO_VERSION_MINOR}.${AVRO_VERSION_PATCH}") project (Avro-cpp) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}) @@ -79,7 +80,8 @@ endif () endif () if (AVRO_BUILD_TESTS OR AVRO_USE_BOOST) - find_package (Boost 1.38 REQUIRED COMPONENTS system) + # Boost 1.70 and above provide a BoostConfig.cmake package configuration file. + find_package (Boost 1.70 REQUIRED CONFIG COMPONENTS system) endif () include(FetchContent) @@ -92,30 +94,26 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(fmt) -find_package(Snappy) -if (SNAPPY_FOUND) - set(SNAPPY_PKG libsnappy) +find_package(Snappy CONFIG) +if (TARGET Snappy::snappy) add_definitions(-DSNAPPY_CODEC_AVAILABLE) message("Enabled snappy codec") -else (SNAPPY_FOUND) - set(SNAPPY_PKG "") - set(SNAPPY_LIBRARIES "") - set(SNAPPY_INCLUDE_DIR "") - message("Disabled snappy codec. libsnappy not found.") -endif (SNAPPY_FOUND) +else () + message("Disabled snappy codec. Snappy::snappy not found.") +endif () find_package(ZLIB REQUIRED) -if (ZLIB_FOUND) +if (TARGET ZLIB::ZLIB) message("Enabled zlib codec") -else (ZLIB_FOUND) - message(FATAL_ERROR "ZLIB is not found") -endif (ZLIB_FOUND) +else () + message(FATAL_ERROR "ZLIB::ZLIB not found but required.") +endif () add_definitions (${Boost_LIB_DIAGNOSTIC_DEFINITIONS}) -add_definitions (-DAVRO_VERSION="${AVRO_VERSION_MAJOR}.${AVRO_VERSION_MINOR}.${AVRO_VERSION_PATCH}") +add_definitions (-DAVRO_VERSION="${AVRO_VERSION}") -include_directories (include/avro ${CMAKE_CURRENT_BINARY_DIR} ${Boost_INCLUDE_DIRS}) +include_directories (include/avro ${CMAKE_CURRENT_BINARY_DIR}) set (AVRO_SOURCE_FILES impl/Compiler.cc impl/Node.cc impl/LogicalType.cc @@ -135,26 +133,17 @@ set (AVRO_SOURCE_FILES impl/CustomAttributes.cc ) -add_library (avrocpp SHARED ${AVRO_SOURCE_FILES}) - -set_property (TARGET avrocpp - APPEND PROPERTY COMPILE_DEFINITIONS AVRO_DYN_LINK) - +add_library (avrocpp SHARED ${AVRO_SOURCE_FILES}) add_library (avrocpp_s STATIC ${AVRO_SOURCE_FILES}) -target_include_directories(avrocpp_s PRIVATE ${SNAPPY_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR}) -target_link_libraries(avrocpp_s ${Boost_LIBRARIES} ${SNAPPY_LIBRARIES} ${ZLIB_LIBRARIES} fmt::fmt-header-only) - -set_property (TARGET avrocpp avrocpp_s - APPEND PROPERTY COMPILE_DEFINITIONS AVRO_SOURCE) -set_target_properties (avrocpp PROPERTIES - VERSION ${AVRO_VERSION_MAJOR}.${AVRO_VERSION_MINOR}.${AVRO_VERSION_PATCH}) +target_compile_definitions(avrocpp PRIVATE AVRO_SOURCE AVRO_DYN_LINK AVRO_VERSION="${AVRO_VERSION}") +target_compile_definitions(avrocpp_s PRIVATE AVRO_SOURCE AVRO_VERSION="${AVRO_VERSION}") -set_target_properties (avrocpp_s PROPERTIES - VERSION ${AVRO_VERSION_MAJOR}.${AVRO_VERSION_MINOR}.${AVRO_VERSION_PATCH}) +set_target_properties (avrocpp PROPERTIES VERSION ${AVRO_VERSION}) +set_target_properties (avrocpp_s PROPERTIES VERSION ${AVRO_VERSION}) -target_link_libraries (avrocpp ${Boost_LIBRARIES} ${SNAPPY_LIBRARIES} ${ZLIB_LIBRARIES} fmt::fmt-header-only) -target_include_directories(avrocpp PRIVATE ${SNAPPY_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR}) +target_link_libraries (avrocpp $,Boost::system,> $,Snappy::snappy,> ZLIB::ZLIB fmt::fmt-header-only) +target_link_libraries (avrocpp_s $,Boost::system,> $,Snappy::snappy,> ZLIB::ZLIB fmt::fmt-header-only) target_include_directories(avrocpp PUBLIC $ @@ -202,7 +191,7 @@ if (AVRO_BUILD_EXECUTABLES) gen (union_redundant_types redundant_types) add_executable (avrogencpp impl/avrogencpp.cc) - target_link_libraries (avrogencpp avrocpp_s ${Boost_LIBRARIES}) + target_link_libraries (avrogencpp avrocpp_s) endif () if (AVRO_BUILD_TESTS) @@ -210,7 +199,7 @@ if (AVRO_BUILD_TESTS) macro (unittest name) add_executable (${name} test/${name}.cc) - target_link_libraries (${name} avrocpp_s ${Boost_LIBRARIES} ${SNAPPY_LIBRARIES} ${ZLIB_LIBRARIES}) + target_link_libraries (${name} avrocpp_s Boost::system ZLIB::ZLIB $,Snappy::snappy,>) add_test (NAME ${name} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${name}) endmacro (unittest) From 69b9bd83fee1274d8adcb7a418571a49eebeb389 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Wed, 22 Jan 2025 23:30:50 +0800 Subject: [PATCH 2/7] AVRO-3088: [C++] Export CMake package config file --- lang/c++/CMakeLists.txt | 78 ++++++++++++++++++++++-------- lang/c++/cmake/AvroConfig.cmake.in | 61 +++++++++++++++++++++++ 2 files changed, 120 insertions(+), 19 deletions(-) create mode 100644 lang/c++/cmake/AvroConfig.cmake.in diff --git a/lang/c++/CMakeLists.txt b/lang/c++/CMakeLists.txt index 902bc0eba24..254eb582e83 100644 --- a/lang/c++/CMakeLists.txt +++ b/lang/c++/CMakeLists.txt @@ -70,18 +70,16 @@ endif() if (CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wduplicated-cond -Wduplicated-branches -Wlogical-op -Wuseless-cast -Wconversion -pedantic -Werror") -if (AVRO_ADD_PROTECTOR_FLAGS) - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fstack-protector-all -D_GLIBCXX_DEBUG") - # Unset _GLIBCXX_DEBUG for avrogencpp.cc because using Boost Program Options - # leads to linking errors when compiling with _GLIBCXX_DEBUG as described on - # https://stackoverflow.com/questions/19729036/ - set_source_files_properties(impl/avrogencpp.cc PROPERTIES COMPILE_FLAGS "-U_GLIBCXX_DEBUG") -endif () endif () if (AVRO_BUILD_TESTS OR AVRO_USE_BOOST) # Boost 1.70 and above provide a BoostConfig.cmake package configuration file. find_package (Boost 1.70 REQUIRED CONFIG COMPONENTS system) + if (TARGET Boost::system) + message("Found Boost version: ${Boost_VERSION}") + else () + message(FATAL_ERROR "Boost::system not found") + endif () endif () include(FetchContent) @@ -97,22 +95,18 @@ FetchContent_MakeAvailable(fmt) find_package(Snappy CONFIG) if (TARGET Snappy::snappy) add_definitions(-DSNAPPY_CODEC_AVAILABLE) - message("Enabled snappy codec") + message("Enabled snappy codec, version: ${Snappy_VERSION}") else () message("Disabled snappy codec. Snappy::snappy not found.") endif () find_package(ZLIB REQUIRED) if (TARGET ZLIB::ZLIB) - message("Enabled zlib codec") + message("Enabled zlib codec, version: ${ZLIB_VERSION}") else () message(FATAL_ERROR "ZLIB::ZLIB not found but required.") endif () -add_definitions (${Boost_LIB_DIAGNOSTIC_DEFINITIONS}) - -add_definitions (-DAVRO_VERSION="${AVRO_VERSION}") - include_directories (include/avro ${CMAKE_CURRENT_BINARY_DIR}) set (AVRO_SOURCE_FILES @@ -136,14 +130,32 @@ set (AVRO_SOURCE_FILES add_library (avrocpp SHARED ${AVRO_SOURCE_FILES}) add_library (avrocpp_s STATIC ${AVRO_SOURCE_FILES}) -target_compile_definitions(avrocpp PRIVATE AVRO_SOURCE AVRO_DYN_LINK AVRO_VERSION="${AVRO_VERSION}") -target_compile_definitions(avrocpp_s PRIVATE AVRO_SOURCE AVRO_VERSION="${AVRO_VERSION}") +target_compile_definitions(avrocpp PRIVATE AVRO_SOURCE AVRO_DYN_LINK) +target_compile_definitions(avrocpp_s PRIVATE AVRO_SOURCE) set_target_properties (avrocpp PROPERTIES VERSION ${AVRO_VERSION}) set_target_properties (avrocpp_s PROPERTIES VERSION ${AVRO_VERSION}) -target_link_libraries (avrocpp $,Boost::system,> $,Snappy::snappy,> ZLIB::ZLIB fmt::fmt-header-only) -target_link_libraries (avrocpp_s $,Boost::system,> $,Snappy::snappy,> ZLIB::ZLIB fmt::fmt-header-only) +target_link_libraries(avrocpp PUBLIC + $ + $ + $> + $> + $ + $ + $> + $> +) +target_link_libraries(avrocpp_s PUBLIC + $ + $ + $> + $> + $ + $ + $> + $> +) target_include_directories(avrocpp PUBLIC $ @@ -192,6 +204,7 @@ if (AVRO_BUILD_EXECUTABLES) add_executable (avrogencpp impl/avrogencpp.cc) target_link_libraries (avrogencpp avrocpp_s) + target_compile_definitions(avrogencpp PRIVATE AVRO_VERSION="${AVRO_VERSION}") endif () if (AVRO_BUILD_TESTS) @@ -237,12 +250,14 @@ set (CPACK_PACKAGE_FILE_NAME "avrocpp-${AVRO_VERSION_MAJOR}") include (CPack) install (TARGETS avrocpp avrocpp_s + EXPORT avrocpp_targets LIBRARY DESTINATION lib ARCHIVE DESTINATION lib - RUNTIME DESTINATION lib) + RUNTIME DESTINATION lib + INCLUDES DESTINATION include) if (AVRO_BUILD_EXECUTABLES) - install (TARGETS avrogencpp RUNTIME DESTINATION bin) + install (TARGETS avrogencpp EXPORT avrocpp_targets RUNTIME DESTINATION bin) endif () install (DIRECTORY include/avro DESTINATION include @@ -253,3 +268,28 @@ if (NOT CMAKE_BUILD_TYPE) "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE) endif (NOT CMAKE_BUILD_TYPE) + +include(CMakePackageConfigHelpers) + +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/AvroConfigVersion.cmake" + VERSION ${AVRO_VERSION} + COMPATIBILITY SameMajorVersion) + +configure_package_config_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/AvroConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/AvroConfig.cmake" + INSTALL_DESTINATION lib/cmake/Avro +) + +install(EXPORT avrocpp_targets + NAMESPACE Avro:: + DESTINATION lib/cmake/Avro + FILE "AvroTargets.cmake" +) + +install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/AvroConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/AvroConfigVersion.cmake" + DESTINATION lib/cmake/Avro +) diff --git a/lang/c++/cmake/AvroConfig.cmake.in b/lang/c++/cmake/AvroConfig.cmake.in new file mode 100644 index 00000000000..9488fe35ba5 --- /dev/null +++ b/lang/c++/cmake/AvroConfig.cmake.in @@ -0,0 +1,61 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# This config sets the following variables in your project:: +# +# Avro_FOUND - true if Avro found on the system +# Avro_VERSION - version of the found Avro +# +# This config sets the following targets in your project:: +# +# Avro::avrocpp_shared +# Avro::avrocpp_static + +@PACKAGE_INIT@ + +include(CMakeFindDependencyMacro) + +if(DEFINED CMAKE_MODULE_PATH) + set(AVRO_CMAKE_MODULE_PATH_OLD ${CMAKE_MODULE_PATH}) +else() + unset(AVRO_CMAKE_MODULE_PATH_OLD) +endif() +set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") + +find_dependency(ZLIB REQUIRED) +find_dependency(fmt REQUIRED) +if(@Snappy_FOUND@) + find_dependency(Snappy) +endif() +if(@Boost_FOUND@) + find_dependency(Boost 1.70 REQUIRED COMPONENTS system) +endif() + +if(DEFINED AVRO_CMAKE_MODULE_PATH_OLD) + set(CMAKE_MODULE_PATH ${AVRO_CMAKE_MODULE_PATH_OLD}) + unset(AVRO_CMAKE_MODULE_PATH_OLD) +else() + unset(CMAKE_MODULE_PATH) +endif() + +include("${CMAKE_CURRENT_LIST_DIR}/AvroTargets.cmake") + +add_library(Avro::avrocpp_static ALIAS Avro::avrocpp_s) +add_library(Avro::avrocpp_shared ALIAS Avro::avrocpp) + +check_required_components(Avro) From 7c63dbc0c742c031cdd7511e68e455f4ff84299f Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Thu, 23 Jan 2025 00:38:08 +0800 Subject: [PATCH 3/7] simplify generator expression --- lang/c++/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lang/c++/CMakeLists.txt b/lang/c++/CMakeLists.txt index 254eb582e83..e99244937b9 100644 --- a/lang/c++/CMakeLists.txt +++ b/lang/c++/CMakeLists.txt @@ -127,13 +127,13 @@ set (AVRO_SOURCE_FILES impl/CustomAttributes.cc ) -add_library (avrocpp SHARED ${AVRO_SOURCE_FILES}) +add_library (avrocpp SHARED ${AVRO_SOURCE_FILES}) add_library (avrocpp_s STATIC ${AVRO_SOURCE_FILES}) -target_compile_definitions(avrocpp PRIVATE AVRO_SOURCE AVRO_DYN_LINK) +target_compile_definitions(avrocpp PRIVATE AVRO_SOURCE AVRO_DYN_LINK) target_compile_definitions(avrocpp_s PRIVATE AVRO_SOURCE) -set_target_properties (avrocpp PROPERTIES VERSION ${AVRO_VERSION}) +set_target_properties (avrocpp PROPERTIES VERSION ${AVRO_VERSION}) set_target_properties (avrocpp_s PROPERTIES VERSION ${AVRO_VERSION}) target_link_libraries(avrocpp PUBLIC @@ -212,7 +212,7 @@ if (AVRO_BUILD_TESTS) macro (unittest name) add_executable (${name} test/${name}.cc) - target_link_libraries (${name} avrocpp_s Boost::system ZLIB::ZLIB $,Snappy::snappy,>) + target_link_libraries (${name} avrocpp_s Boost::system ZLIB::ZLIB $<$:Snappy::snappy>) add_test (NAME ${name} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${name}) endmacro (unittest) From 39494f75e834373cd01981cf6c8a21842079ebf2 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Thu, 23 Jan 2025 00:40:26 +0800 Subject: [PATCH 4/7] fix AvroConfig.cmake.in --- lang/c++/cmake/AvroConfig.cmake.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/c++/cmake/AvroConfig.cmake.in b/lang/c++/cmake/AvroConfig.cmake.in index 9488fe35ba5..c7a89e29cd3 100644 --- a/lang/c++/cmake/AvroConfig.cmake.in +++ b/lang/c++/cmake/AvroConfig.cmake.in @@ -40,7 +40,7 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") find_dependency(ZLIB REQUIRED) find_dependency(fmt REQUIRED) if(@Snappy_FOUND@) - find_dependency(Snappy) + find_dependency(Snappy REQUIRED) endif() if(@Boost_FOUND@) find_dependency(Boost 1.70 REQUIRED COMPONENTS system) From 0aa7adf87a9af6d472a3e9d5966c5e7f1d6baa7d Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Thu, 23 Jan 2025 16:33:53 +0800 Subject: [PATCH 5/7] do not add fmt to install interface --- lang/c++/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/lang/c++/CMakeLists.txt b/lang/c++/CMakeLists.txt index e99244937b9..2d6dae3e11c 100644 --- a/lang/c++/CMakeLists.txt +++ b/lang/c++/CMakeLists.txt @@ -141,7 +141,6 @@ target_link_libraries(avrocpp PUBLIC $ $> $> - $ $ $> $> @@ -151,7 +150,6 @@ target_link_libraries(avrocpp_s PUBLIC $ $> $> - $ $ $> $> From ab3afcdf54c537b43aa229b3076658e327f27c37 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Thu, 23 Jan 2025 23:41:15 +0800 Subject: [PATCH 6/7] reword error message --- lang/c++/CMakeLists.txt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lang/c++/CMakeLists.txt b/lang/c++/CMakeLists.txt index 2d6dae3e11c..bfcea76dc71 100644 --- a/lang/c++/CMakeLists.txt +++ b/lang/c++/CMakeLists.txt @@ -74,11 +74,13 @@ endif () if (AVRO_BUILD_TESTS OR AVRO_USE_BOOST) # Boost 1.70 and above provide a BoostConfig.cmake package configuration file. + # It guarantees that Boost::system target exists if found. + # See https://cmake.org/cmake/help/latest/policy/CMP0167.html find_package (Boost 1.70 REQUIRED CONFIG COMPONENTS system) - if (TARGET Boost::system) + if (Boost_FOUND) message("Found Boost version: ${Boost_VERSION}") else () - message(FATAL_ERROR "Boost::system not found") + message(FATAL_ERROR "Boost not found but required") endif () endif () @@ -93,18 +95,21 @@ FetchContent_Declare( FetchContent_MakeAvailable(fmt) find_package(Snappy CONFIG) -if (TARGET Snappy::snappy) +if (Snappy_FOUND) + # Use CONFIG mode to guarantee that Snappy::snappy target exists if found. add_definitions(-DSNAPPY_CODEC_AVAILABLE) message("Enabled snappy codec, version: ${Snappy_VERSION}") else () - message("Disabled snappy codec. Snappy::snappy not found.") + message("Disabled snappy codec.") endif () find_package(ZLIB REQUIRED) -if (TARGET ZLIB::ZLIB) +if (ZLIB_FOUND) + # FindZLIB guarantees that ZLIB::ZLIB target exists if found + # See https://cmake.org/cmake/help/latest/module/FindZLIB.html#imported-targets message("Enabled zlib codec, version: ${ZLIB_VERSION}") else () - message(FATAL_ERROR "ZLIB::ZLIB not found but required.") + message(FATAL_ERROR "ZLIB not found but required") endif () include_directories (include/avro ${CMAKE_CURRENT_BINARY_DIR}) From 426cb6f09817421e7bbb242d8446dd5944655649 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Fri, 24 Jan 2025 00:17:48 +0800 Subject: [PATCH 7/7] remove if statement for required lib --- lang/c++/CMakeLists.txt | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/lang/c++/CMakeLists.txt b/lang/c++/CMakeLists.txt index bfcea76dc71..a9fbbc7615e 100644 --- a/lang/c++/CMakeLists.txt +++ b/lang/c++/CMakeLists.txt @@ -77,11 +77,6 @@ if (AVRO_BUILD_TESTS OR AVRO_USE_BOOST) # It guarantees that Boost::system target exists if found. # See https://cmake.org/cmake/help/latest/policy/CMP0167.html find_package (Boost 1.70 REQUIRED CONFIG COMPONENTS system) - if (Boost_FOUND) - message("Found Boost version: ${Boost_VERSION}") - else () - message(FATAL_ERROR "Boost not found but required") - endif () endif () include(FetchContent) @@ -103,14 +98,9 @@ else () message("Disabled snappy codec.") endif () +# FindZLIB guarantees that ZLIB::ZLIB target exists if found +# See https://cmake.org/cmake/help/latest/module/FindZLIB.html#imported-targets find_package(ZLIB REQUIRED) -if (ZLIB_FOUND) - # FindZLIB guarantees that ZLIB::ZLIB target exists if found - # See https://cmake.org/cmake/help/latest/module/FindZLIB.html#imported-targets - message("Enabled zlib codec, version: ${ZLIB_VERSION}") -else () - message(FATAL_ERROR "ZLIB not found but required") -endif () include_directories (include/avro ${CMAKE_CURRENT_BINARY_DIR})