Skip to content

Commit

Permalink
Add dual shared and static cmake targets and enable cpack package gen…
Browse files Browse the repository at this point in the history
…eration
  • Loading branch information
masariello committed Aug 21, 2020
1 parent 9e02bb6 commit d95cf31
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 212 deletions.
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
21 changes: 20 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ 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.")

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 @@ -159,7 +172,6 @@ if(ENABLE_LZ4_EXT)
endif()
# }

option(RDKAFKA_BUILD_STATIC "Build static rdkafka library" OFF)
option(RDKAFKA_BUILD_EXAMPLES "Build examples" ON)
option(RDKAFKA_BUILD_TESTS "Build tests" ON)
if(WIN32)
Expand Down Expand Up @@ -256,8 +268,15 @@ install(
DESTINATION "share/licenses/librdkafka"
)

INSTALL(FILES packaging/cmake/rdkafka.targets
DESTINATION "build/native"
RENAME ${CMAKE_PROJECT_NAME}.targets
)

# }

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

add_subdirectory(src)
add_subdirectory(src-cpp)

Expand Down
19 changes: 19 additions & 0 deletions packaging/cmake/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,22 @@ If you use librdkafka as submodule in cmake project and want static link of libr
set(RDKAFKA_BUILD_STATIC ON CACHE BOOL "")
add_subdirectory(librdkafka)
target_link_libraries(your_library_or_executable rdkafka)

# Build DEB/RPM packages on Linux

$ 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"?>
<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>
57 changes: 20 additions & 37 deletions src-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@ 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()
set(CMAKE_DEBUG_POSTFIX d)

add_library(rdkafka++ ${RDKAFKA_BUILD_MODE} ${sources})
if(NOT RDKAFKA_BUILD_STATIC)
set_property(TARGET rdkafka++ PROPERTY SOVERSION ${LIBVER})
endif()
add_library(rdkafka++ SHARED ${sources})
add_library(rdkafka++_static STATIC ${sources})

target_link_libraries(rdkafka++ PUBLIC rdkafka)
set_property(TARGET rdkafka++ PROPERTY SOVERSION ${LIBVER})
target_compile_definitions(rdkafka++ PRIVATE LIBRDKAFKACPP_EXPORTS)
target_compile_definitions(rdkafka++_static PUBLIC LIBRDKAFKACPP_STATICLIB)

# 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)
endif()
foreach(target rdkafka++ rdkafka++_static)

target_link_libraries(${target} PUBLIC rdkafka)

# Support '#include <rdkafcpp.h>'
target_include_directories(${target} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>")

endforeach()

# Generate pkg-config file
set(PKG_CONFIG_NAME
Expand All @@ -51,7 +49,7 @@ set(PKG_CONFIG_CFLAGS
"-I\${includedir}"
)
set(PKG_CONFIG_LIBS
"-L\${libdir} -lrdkafka++"
"-L\${libdir}"
)
set(PKG_CONFIG_LIBS_PRIVATE
"-lrdkafka"
Expand All @@ -64,35 +62,20 @@ configure_file(
install(FILES ${GENERATED_DIR}/rdkafka++.pc
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
)
if(RDKAFKA_BUILD_STATIC)
set(PKG_CONFIG_NAME
"librdkafka++-static"
)
set(PKG_CONFIG_DESCRIPTION
"The Apache Kafka C/C++ library (static)"
)
set(PKG_CONFIG_LIBS
"-L\${libdir} \${libdir}/librdkafka++.a"
)
configure_file(
"../packaging/cmake/rdkafka.pc.in"
"${GENERATED_DIR}/rdkafka++-static.pc"
@ONLY
)
install(FILES ${GENERATED_DIR}/rdkafka.pc
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
)
endif()

install(
TARGETS rdkafka++
TARGETS rdkafka++ rdkafka++_static
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 (WIN32)
install(FILES $<TARGET_PDB_FILE:rdkafka++> DESTINATION "${CMAKE_INSTALL_BINDIR}")
endif()

install(
FILES "rdkafkacpp.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/librdkafka"
Expand Down
Loading

0 comments on commit d95cf31

Please sign in to comment.