Skip to content

Commit

Permalink
Made an explicit 'shared' target in CMake with 'paho-mqttpp3' a defau…
Browse files Browse the repository at this point in the history
…lt for whichever target was built.
  • Loading branch information
fpagliughi committed Jul 3, 2024
1 parent a4bb565 commit 134e40d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 32 deletions.
14 changes: 4 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ endif()

if(PAHO_WITH_SSL)
find_package(OpenSSL REQUIRED)
set(PAHO_MQTT_C_LIB paho-mqtt3as)
set(PAHO_MQTT_C_LIB eclipse-paho-mqtt-c::paho-mqtt3as)
else()
set(PAHO_MQTT_C_LIB paho-mqtt3a)
set(PAHO_MQTT_C_LIB eclipse-paho-mqtt-c::paho-mqtt3a)
endif()

if(PAHO_WITH_MQTT_C)
Expand Down Expand Up @@ -124,14 +124,6 @@ if(PAHO_BUILD_DOCUMENTATION)
add_subdirectory(doc)
endif()

# --- Default library for samples and unit tests ---

if(PAHO_BUILD_SHARED)
set(PAHO_CPP_LIB paho-mqttpp3)
else()
set(PAHO_CPP_LIB paho-mqttpp3-static)
endif()

# --- Example Apps ---

if(PAHO_BUILD_SAMPLES OR PAHO_BUILD_EXAMPLES)
Expand Down Expand Up @@ -160,5 +152,7 @@ endif()

include(CPack)

# --- Export CMake TARGETS ---

add_subdirectory(cmake)

4 changes: 2 additions & 2 deletions cmake/CPackDebConfig.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if(CPACK_GENERATOR MATCHES "DEB")
set(CPACK_PACKAGE_NAME "libpaho-mqtt.cpp")
set(CPACK_PACKAGE_NAME "libpaho-mqtt.cpp")
set(CPACK_DEBIAN_PACKAGE_NAME ${CPACK_PACKAGE_NAME})
set(CPACK_PACKAGE_CONTACT "Eclipse")
set(CPACK_PACKAGE_CONTACT "Eclipse")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Eclipse Paho MQTT C++ client")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER " <>")
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
Expand Down
10 changes: 9 additions & 1 deletion cmake/PahoMqttCppConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ if (PAHO_WITH_SSL)
find_dependency(OpenSSL REQUIRED)
endif()

include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
if(NOT TARGET PahoMqttCpp::paho-mqttpp3-shared AND NOT TARGET PahoMqttCpp::paho-mqttpp3-static)
include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")

if(TARGET PahoMqttCpp::paho-mqttpp3-shared)
add_library(PahoMqttCpp::paho-mqttpp3 ALIAS PahoMqttCpp::paho-mqttpp3-shared)
else()
add_library(PahoMqttCpp::paho-mqttpp3 ALIAS PahoMqttCpp::paho-mqttpp3-static)
endif()
endif()
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ endif()
## Build the example apps
foreach(EXECUTABLE ${EXECUTABLES} ${SSL_EXECUTABLES})
add_executable(${EXECUTABLE} ${EXECUTABLE}.cpp)
target_link_libraries(${EXECUTABLE} ${PAHO_CPP_LIB})
target_link_libraries(${EXECUTABLE} PahoMqttCpp::paho-mqttpp3)

target_compile_features(${EXECUTABLE} PRIVATE cxx_std_17)

Expand Down
36 changes: 27 additions & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,28 @@ set(COMMON_SRC
## --- Build the shared library, if requested ---

if(PAHO_BUILD_SHARED)
message(STATUS "Creating shared library")

## Create the shared library
add_library(paho-mqttpp3 SHARED ${COMMON_SRC})
list(APPEND PAHO_CPP_TARGETS paho-mqttpp3)
add_library(paho-mqttpp3-shared SHARED ${COMMON_SRC})
list(APPEND PAHO_CPP_TARGETS paho-mqttpp3-shared)

## Alias for subdirectory builds
add_library(PahoMqttCpp::paho-mqttpp3-shared ALIAS paho-mqttpp3-shared)
add_library(PahoMqttCpp::paho-mqttpp3 ALIAS paho-mqttpp3-shared)

target_compile_definitions(paho-mqttpp3 PRIVATE PAHO_MQTTPP_EXPORTS)
target_compile_definitions(paho-mqttpp3-shared PRIVATE PAHO_MQTTPP_EXPORTS)

## Add dependencies to the shared library
target_link_libraries(paho-mqttpp3 PUBLIC
eclipse-paho-mqtt-c::${PAHO_MQTT_C_LIB}
target_link_libraries(paho-mqttpp3-shared PUBLIC
${PAHO_MQTT_C_LIB}
Threads::Threads
${LIBS_SYSTEM}
)

## set the shared library soname
set_target_properties(paho-mqttpp3 PROPERTIES
set_target_properties(paho-mqttpp3-shared PROPERTIES
OUTPUT_NAME paho-mqttpp3
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
Expand All @@ -74,17 +81,28 @@ endif()
## --- Build static version of the library, if requested ---

if(PAHO_BUILD_STATIC)
## create the static library
## Create the static library
add_library(paho-mqttpp3-static STATIC ${COMMON_SRC})
list(APPEND PAHO_CPP_TARGETS paho-mqttpp3-static)

## add dependencies to the shared library
## Alias for subdirectory builds
add_library(PahoMqttCpp::paho-mqttpp3-static ALIAS paho-mqttpp3-static)

## Add dependencies to the static library
target_link_libraries(paho-mqttpp3-static PUBLIC
eclipse-paho-mqtt-c::${PAHO_MQTT_C_LIB}-static
${PAHO_MQTT_C_LIB}-static
Threads::Threads
${LIBS_SYSTEM}
)

if(${PAHO_BUILD_SHARED})
# This lib should configure for static exports
target_compile_definitions(paho-mqttpp3-static PRIVATE PAHO_MQTTPP_STATIC)
else()
# If no shared lib, make this one the default target
add_library(PahoMqttCpp::paho-mqttpp3 ALIAS paho-mqttpp3-static)
endif()

## Let the archive use the same base name as the shared lib on *nix systems
if(UNIX)
set_target_properties(paho-mqttpp3-static PROPERTIES OUTPUT_NAME paho-mqttpp3)
Expand Down
19 changes: 10 additions & 9 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,22 @@ if(PAHO_WITH_SSL)
)
endif()

target_compile_features(unit_tests PRIVATE cxx_std_17)

set_target_properties(unit_tests PROPERTIES
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)

if (Catch2_VERSION VERSION_LESS "3.0")
target_compile_definitions(unit_tests PUBLIC CATCH2_V2)
endif()

# --- Link for executables ---

message(STATUS "Using library for unit tests: ${PAHO_CPP_LIB}")

target_link_libraries(unit_tests ${PAHO_CPP_LIB} Catch2::Catch2)

target_compile_features(unit_tests PRIVATE cxx_std_17)

set_target_properties(unit_tests PROPERTIES
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
target_link_libraries(unit_tests
PahoMqttCpp::paho-mqttpp3
Catch2::Catch2
)

if(PAHO_BUILD_SHARED)
Expand Down

0 comments on commit 134e40d

Please sign in to comment.