diff --git a/CMakeLists.txt b/CMakeLists.txt index 524cc70d..fe746766 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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) @@ -160,5 +152,7 @@ endif() include(CPack) +# --- Export CMake TARGETS --- + add_subdirectory(cmake) diff --git a/cmake/CPackDebConfig.cmake b/cmake/CPackDebConfig.cmake index a0b93184..2c140f82 100644 --- a/cmake/CPackDebConfig.cmake +++ b/cmake/CPackDebConfig.cmake @@ -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) diff --git a/cmake/PahoMqttCppConfig.cmake.in b/cmake/PahoMqttCppConfig.cmake.in index 73816766..b7cc7f4b 100644 --- a/cmake/PahoMqttCppConfig.cmake.in +++ b/cmake/PahoMqttCppConfig.cmake.in @@ -16,4 +16,12 @@ if (PAHO_WITH_SSL) find_dependency(OpenSSL REQUIRED) endif() -include("${CMAKE_CURRENT_LIST_DIR}/@package_name@Targets.cmake") +if(NOT TARGET PahoMqttCpp::paho-mqttpp3-shared AND NOT TARGET PahoMqttCpp::paho-mqttpp3-static) + include("${CMAKE_CURRENT_LIST_DIR}/@package_name@Targets.cmake") + + 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() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 92279942..efd6eedc 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a62bea0b..35075b9a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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} ) @@ -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) diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 7c20bb4d..186bc723 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -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)