Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dual shared and static cmake targets and enable cpack package generation #3042

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ test_report*.json
cov-int
gdbrun*.gdb
TAGS
build
.vs
out
25 changes: 25 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ parseversion("src/rdkafka.h")

project(RdKafka VERSION ${RDKAFKA_VERSION})

set(CPACK_PACKAGE_VENDOR "Magnus Edenhill")
set(CPACK_PACKAGE_CONTACT "info@edenhill.se")
set(CPACK_PACKAGE_HOMEPAGE_URL "https://github.com/edenhill/librdkafka")
set(CPACK_PACKAGE_DESCRIPTION "\
librdkafka is a C library implementation of the Apache Kafka protocol, \
providing Producer, Consumer and Admin clients. It was designed with message \
delivery reliability and high performance in mind, current figures exceed 1 \
million msgs/second for the producer and 3 million msgs/second for the consumer. \
\
librdkafka is licensed under the 2-clause BSD license.")

# All this block is the machinery required to install the .targets file only for the NuGet generator
# This is really something that the cpack generator ought to support directly
set(CPACK_PROJECT_CONFIG_FILE "${CMAKE_CURRENT_LIST_DIR}/CPackProjectConfig.cmake")
set(CPACK_NUGET_TARGETS_FILE "packaging/cmake/rdkafka.targets")
set(CPACK_STAGING_DIR "${CMAKE_BINARY_DIR}")
# This install statement is from the binary dir, but the above CPackProjectConfig.cmake will copy it
# there only when building a NuGet package
install(FILES "${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}.targets"
DESTINATION "build/native" OPTIONAL)

include(CPack)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/packaging/cmake/Modules/")

# Options. No 'RDKAFKA_' prefix to match old C++ code. {
Expand Down Expand Up @@ -258,6 +281,8 @@ install(

# }

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

add_subdirectory(src)
add_subdirectory(src-cpp)

Expand Down
6 changes: 6 additions & 0 deletions CPackProjectConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if (CPACK_GENERATOR STREQUAL NuGet)
message("Copying ${CMAKE_CURRENT_LIST_DIR}/${CPACK_NUGET_TARGETS_FILE} to ${CPACK_STAGING_DIR}/${CPACK_PACKAGE_NAME}.targets for inclusion into NuGet package")
file(INSTALL DESTINATION "${CPACK_STAGING_DIR}" TYPE FILE
RENAME "${CPACK_PACKAGE_NAME}.targets"
FILES "${CMAKE_CURRENT_LIST_DIR}/${CPACK_NUGET_TARGETS_FILE}")
endif()
22 changes: 20 additions & 2 deletions packaging/cmake/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ Install library:

If you use librdkafka as submodule in cmake project and want static link of librdkafka:

set(RDKAFKA_BUILD_STATIC ON CACHE BOOL "")
add_subdirectory(librdkafka)
target_link_libraries(your_library_or_executable rdkafka)
target_link_libraries(your_library_or_executable rdkafka_static)

# Build DEB/RPM packages on Linux
masariello marked this conversation as resolved.
Show resolved Hide resolved

$ cd _cmake_build
$ cpack -G DEB # or RPM

# Build a Nuget package with Visual Studio

Build both Debug and RelWithDebInfo

$ cmake -G "Visual Studio 16 2019" -A x64 -B _cmake_build
$ cmake --build _cmake_build --config Debug -j
$ cmake --build _cmake_build --config RelWithDebInfo -j

Then run cpack as follows

$ cd _cmake_build
$ set PATH=%PATH%;c:\some\dir\that\contains\nuget.exe
$ cpack -G NuGet -C Debug;RelWithDebInfo
24 changes: 24 additions & 0 deletions packaging/cmake/rdkafka.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
masariello marked this conversation as resolved.
Show resolved Hide resolved
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)\..\..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(MSBuildThisFileDirectory)\..\..\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>

<Target Name="RdKafka_CopyReleaseDllsToOutDir" BeforeTargets="PrepareForRun">
<ItemGroup>
<ReleaseFilesToCopy Include="$(MSBuildThisFileDirectory)\..\..\bin\*.dll" Exclude="$(MSBuildThisFileDirectory)\..\..\bin\*d.dll"/>
<ReleaseFilesToCopy Include="$(MSBuildThisFileDirectory)\..\..\bin\*.pdb" Exclude="$(MSBuildThisFileDirectory)\..\..\bin\*d.pdb"/>
<DebugFilesToCopy Include="$(MSBuildThisFileDirectory)\..\..\bin\*d.dll"/>
<DebugFilesToCopy Include="$(MSBuildThisFileDirectory)\..\..\bin\*d.pdb"/>
</ItemGroup>
<Copy SourceFiles="@(DebugFilesToCopy)" SkipUnchangedFiles="true" DestinationFolder="$(OutDir)" Condition="'$(Configuration)'=='Debug'" />
<Copy SourceFiles="@(ReleaseFilesToCopy)" SkipUnchangedFiles="true" DestinationFolder="$(OutDir)" Condition="'$(Configuration)'!='Debug'"/>
</Target>

</Project>
1 change: 0 additions & 1 deletion packaging/mingw-w64/configure-build-msys2-mingw.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ cmake \
-D WITH_SASL=ON \
-D WITH_SSL=ON \
-D WITH_ZLIB=OFF \
-D RDKAFKA_BUILD_STATIC=OFF \
-D CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=TRUE .

$mingw64 mingw32-make
Expand Down
65 changes: 47 additions & 18 deletions src-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,32 @@ set(
TopicPartitionImpl.cpp
)

if(RDKAFKA_BUILD_STATIC)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(RDKAFKA_BUILD_MODE STATIC)
else()
set(RDKAFKA_BUILD_MODE SHARED)
endif()

add_library(rdkafka++ ${RDKAFKA_BUILD_MODE} ${sources})
if(NOT RDKAFKA_BUILD_STATIC)
set_property(TARGET rdkafka++ PROPERTY SOVERSION ${LIBVER})
endif()
set(CMAKE_DEBUG_POSTFIX d)

add_library(rdkafka++ SHARED ${sources})
set_property(TARGET rdkafka++ PROPERTY SOVERSION ${LIBVER})
target_compile_definitions(rdkafka++ PRIVATE LIBRDKAFKACPP_EXPORTS)
target_link_libraries(rdkafka++ PUBLIC rdkafka)

# Support '#include <rdkafcpp.h>'
target_include_directories(rdkafka++ PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>")
if(NOT RDKAFKA_BUILD_STATIC)
target_compile_definitions(rdkafka++ PRIVATE LIBRDKAFKACPP_EXPORTS)
if(RDKAFKA_BUILD_STATIC)

add_library(rdkafka++_static STATIC ${sources})
set_target_properties(rdkafka++_static
PROPERTIES COMPILE_PDB_NAME rdkafka++_static)
set_target_properties(rdkafka++_static
PROPERTIES COMPILE_PDB_NAME_DEBUG rdkafka++_static${CMAKE_DEBUG_POSTFIX})
target_compile_definitions(rdkafka++_static PUBLIC LIBRDKAFKACPP_STATICLIB)
target_link_libraries(rdkafka++_static PUBLIC rdkafka_static)

endif()

foreach(target rdkafka++ rdkafka++_static)
if (TARGET ${target})
# Support '#include <rdkafcpp.h>'
target_include_directories(${target} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>")
endif()
endforeach()

# Generate pkg-config file
set(PKG_CONFIG_NAME
"librdkafka++"
Expand Down Expand Up @@ -71,29 +77,52 @@ if(RDKAFKA_BUILD_STATIC)
set(PKG_CONFIG_DESCRIPTION
"The Apache Kafka C/C++ library (static)"
)
set(PKG_CONFIG_REQUIRES "rdkafka_static")
set(PKG_CONFIG_LIBS
"-L\${libdir} \${libdir}/librdkafka++.a"
"-L\${libdir} \${libdir}/librdkafka++_static.a"
)
configure_file(
"../packaging/cmake/rdkafka.pc.in"
"${GENERATED_DIR}/rdkafka++-static.pc"
@ONLY
)
install(FILES ${GENERATED_DIR}/rdkafka.pc
install(FILES ${GENERATED_DIR}/rdkafka++-static.pc
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
)
set(static_target_for_install rdkafka++_static)
endif()

install(
TARGETS rdkafka++
TARGETS rdkafka++ ${static_target_for_install}
EXPORT "${targets_export_name}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

if(TARGET rdkafka++_static)
if (MSVC)
set_target_properties(rdkafka++_static
PROPERTIES
COMPILE_PDB_NAME_RELWITHDEBINFO rdkafka++_static
COMPILE_PDB_NAME_DEBUG rdkafka++_static${CMAKE_DEBUG_POSTFIX})
endif()
endif()

install(
FILES "rdkafkacpp.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/librdkafka"
)

if (MSVC)
install(FILES $<TARGET_PDB_FILE:rdkafka++>
DESTINATION ${CMAKE_INSTALL_BINDIR}
CONFIGURATIONS Debug RelWithDebInfo)
if(TARGET rdkafka++_static)
install(FILES $<TARGET_FILE_DIR:rdkafka++_static>/$<TARGET_FILE_BASE_NAME:rdkafka++_static>.pdb
DESTINATION ${CMAKE_INSTALL_LIBDIR}
CONFIGURATIONS Debug RelWithDebInfo)
endif()
endif()

Loading