Skip to content

Commit

Permalink
Updates to CMake build - removing vars, targets exporting include paths
Browse files Browse the repository at this point in the history
  • Loading branch information
fpagliughi committed Dec 8, 2018
1 parent 1b31e7d commit 2cd0109
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 100 deletions.
35 changes: 23 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,15 @@
cmake_minimum_required(VERSION 3.1)

## project name
project("paho-mqtt-cpp" LANGUAGES CXX)
project("paho-mqtt-cpp"
VERSION "1.0.1"
LANGUAGES CXX
)

## library name
set(PAHO_MQTT_CPP paho-mqttpp3)
set(PAHO_MQTT_CPP_STATIC ${PAHO_MQTT_CPP}-static)

## build settings
set(PAHO_VERSION_MAJOR 1)
set(PAHO_VERSION_MINOR 0)
set(PAHO_VERSION_PATCH 1)

set(CLIENT_VERSION ${PAHO_VERSION_MAJOR}.${PAHO_VERSION_MINOR}.${PAHO_VERSION_PATCH})

set(CPACK_PACKAGE_VERSION_MAJOR ${PAHO_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PAHO_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PAHO_VERSION_PATCH})

INCLUDE(GNUInstallDirs)

## --- Build options ---
Expand All @@ -60,6 +52,7 @@ else()
endif()

option(PAHO_BUILD_SAMPLES "Build sample programs" FALSE)
option(PAHO_BUILD_TESTS "Build tests" FALSE)
option(PAHO_BUILD_DOCUMENTATION "Create and install the API documentation (requires Doxygen)" FALSE)

## --- C++11 build flags ---
Expand All @@ -74,10 +67,26 @@ set(CMAKE_CXX_EXTENSIONS OFF)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
add_subdirectory(src)

# --- Documentation ---

if(PAHO_BUILD_DOCUMENTATION)
add_subdirectory(doc)
endif()

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

if(PAHO_BUILD_SHARED)
set(PAHO_CPP_LIB ${PAHO_MQTT_CPP})
else()
set(PAHO_CPP_LIB ${PAHO_MQTT_CPP_STATIC})
endif()

# --- Unit Tests ---

if(PAHO_BUILD_TESTS)
add_subdirectory(test/unit)
endif()

## --- Packaging settings ---

if(WIN32)
Expand All @@ -88,3 +97,5 @@ endif()

include(CPack)
add_subdirectory(cmake)


21 changes: 6 additions & 15 deletions buildtst.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ for COMPILER in $COMPILERS; do
rm -rf buildtst-build/
mkdir buildtst-build ; pushd buildtst-build &> /dev/null

if ! cmake -DCMAKE_CXX_COMPILER=${COMPILER} -DPAHO_WITH_SSL=ON -DPAHO_BUILD_SAMPLES=ON ${PAHO_C_SWITCH} .. ; then
if ! cmake -DCMAKE_CXX_COMPILER=${COMPILER} -DPAHO_WITH_SSL=ON -DPAHO_BUILD_SAMPLES=ON -DPAHO_BUILD_TESTS=ON ${PAHO_C_SWITCH} .. ; then
printf "\nCMake configuration failed for %s\n" "${COMPILER}"
exit 1
fi
Expand All @@ -37,20 +37,11 @@ for COMPILER in $COMPILERS; do
exit 1
fi

#pushd test/unit &> /dev/null
#make clean
#if ! make CXX=${COMPILER} SSL=1 ; then
# printf "\nUnit test compilation failed for %s\n" "${COMPILER}"
# exit 2
#fi
#rm -rf tmp/*
#printf "Running unit tests:\n"
#if ! ./mqttpp-unittest ; then
# printf "\nUnit test failed for %s\n" "${COMPILER}"
# exit 3
#fi
#make clean
#popd &> /dev/null
printf "Running unit tests:\n"
if ! ./test/unit/test ; then
printf "\nUnit test failed for %s\n" "${COMPILER}"
exit 3
fi

popd &> /dev/null
fi
Expand Down
3 changes: 2 additions & 1 deletion cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ configure_file(${package_name}Config.cmake.in ${package_name}Config.cmake @ONLY)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${package_name}ConfigVersion.cmake"
VERSION ${CLIENT_VERSION}
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion) # TODO

export(EXPORT ${package_name}
Expand All @@ -14,6 +14,7 @@ export(EXPORT ${package_name}
install(EXPORT ${package_name} DESTINATION lib/cmake/${package_name}
FILE ${package_name}Targets.cmake
NAMESPACE ${package_name}::)

install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${package_name}Config.cmake"
FindPahoMqttC.cmake
Expand Down
3 changes: 1 addition & 2 deletions doc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
# Guilherme Maciel Ferreira - initial version
#*******************************************************************************/

## Note: on OS X you should install XCode and the associated command-line tools

## documentation settings
find_package(Doxygen)
if(NOT DOXYGEN_FOUND)
Expand All @@ -42,3 +40,4 @@ set(DOXYTARGETS ${DOXYTARGETS} ${DOXYFILE_SRC}.target)
add_custom_target(doc ALL DEPENDS ${DOXYTARGETS})

install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doc DESTINATION share)

98 changes: 63 additions & 35 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#*******************************************************************************
# Copyright (c) 2016-2017, Guilherme Maciel Ferreira
# Copyright (c) 2017, Frank Pagliughi
# Copyright (c) 2017-2018, Frank Pagliughi
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -23,8 +23,6 @@
# Frank Pagliughi - made the shared library optional
#*******************************************************************************/

## Note: on OS X you should install XCode and the associated command-line tools

find_package(PahoMqttC REQUIRED)

add_subdirectory(mqtt)
Expand All @@ -41,54 +39,79 @@ if(WIN32)
string(APPEND LIBS_SYSTEM " ws2_32")
endif()

## --- use Object Library to optimize compilation ---
## --- Use object library to optimize compilation ---

set(SOURCES
add_library(paho-cpp-objs OBJECT
async_client.cpp
client.cpp
disconnect_options.cpp
iclient_persistence.cpp
message.cpp
response_options.cpp
ssl_options.cpp
string_collection.cpp
token.cpp
topic.cpp
connect_options.cpp
will_options.cpp)
client.cpp
disconnect_options.cpp
iclient_persistence.cpp
message.cpp
response_options.cpp
ssl_options.cpp
string_collection.cpp
token.cpp
topic.cpp
connect_options.cpp
will_options.cpp
)

## install the shared library
#install(TARGETS paho-cpp-objs EXPORT PahoMqttCpp
# OBJECTS DESTINATION ${CMAKE_INSTALL_LIBDIR}
#)

if(PAHO_WITH_SSL)
add_definitions(-DOPENSSL)
add_definitions(-DOPENSSL)
endif()

# Automatically add the current source- and build directories to the include path
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE TRUE)

add_library(OBJS OBJECT ${SOURCES})
# Object libraries can't use target_link_libraries in order to take advantage
# of transitive usage requirements until CMake 3.12. This is a workaround:
target_include_directories(OBJS PRIVATE ${PAHO_MQTT_C_INCLUDE_DIRS})
#target_include_directories(OBJS PRIVATE ${PAHO_MQTT_C_INCLUDE_DIRS})

target_include_directories(paho-cpp-objs
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
PRIVATE
${PAHO_MQTT_C_INCLUDE_DIRS}
src
)


## --- Build the shared library, if requested ---

if(PAHO_BUILD_SHARED)
## set position independent flag (-fPIC on Unix)
set_property(TARGET OBJS PROPERTY POSITION_INDEPENDENT_CODE ON)
# this may only be necessary for old compiler, but doesn't hurt
set_property(TARGET paho-cpp-objs PROPERTY POSITION_INDEPENDENT_CODE ON)

add_library(${PAHO_MQTT_CPP} SHARED $<TARGET_OBJECTS:OBJS>)
add_library(${PAHO_MQTT_CPP} SHARED $<TARGET_OBJECTS:paho-cpp-objs>)

## add dependencies to the shared library
target_link_libraries(${PAHO_MQTT_CPP}
PRIVATE ${LIBS_SYSTEM}
PUBLIC PahoMqttC::PahoMqttC)
target_include_directories(${PAHO_MQTT_CPP}
PUBLIC $<INSTALL_INTERFACE:include>)

# It would be nice to exort the include paths from the obj lib, but we
# get an export error. Perhaps in a future version?
#
# CMake Error: install(EXPORT "PahoMqttCpp" ...) includes target "paho-mqttpp3"
# which requires target "paho-cpp-objs" that is not in the export set.

#target_include_directories(${PAHO_MQTT_CPP} PUBLIC
# $<TARGET_PROPERTY:paho-cpp-objs,INCLUDE_DIRECTORIES>
#)

target_include_directories(${PAHO_MQTT_CPP} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)

## set the shared library soname
set_target_properties(${PAHO_MQTT_CPP} PROPERTIES
VERSION ${CLIENT_VERSION}
SOVERSION ${PAHO_VERSION_MAJOR})
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR})

## install the shared library
install(TARGETS ${PAHO_MQTT_CPP} EXPORT PahoMqttCpp
Expand All @@ -101,20 +124,25 @@ endif()

if(PAHO_BUILD_STATIC)
## create the static library
add_library(${PAHO_MQTT_CPP_STATIC} STATIC $<TARGET_OBJECTS:OBJS>)
add_library(${PAHO_MQTT_CPP_STATIC} STATIC $<TARGET_OBJECTS:paho-cpp-objs>)

## add dependencies to the shared library
target_link_libraries(${PAHO_MQTT_CPP}
target_link_libraries(${PAHO_MQTT_CPP_STATIC}
PRIVATE ${LIBS_SYSTEM}
PUBLIC PahoMqttC::PahoMqttC)
target_include_directories(${PAHO_MQTT_CPP}
PUBLIC $<INSTALL_INTERFACE:include>)

target_include_directories(${PAHO_MQTT_CPP_STATIC} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)

## install the static library
install(TARGETS ${PAHO_MQTT_CPP_STATIC} EXPORT PahoMqttCpp
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

## Static lib has same name as shared lib
set_target_properties(${PAHO_MQTT_CPP_STATIC} PROPERTIES OUTPUT_NAME ${PAHO_MQTT_CPP})
## Let the archive use the same name as the shared lib on *nix systems
if(UNIX)
set_target_properties(${PAHO_MQTT_CPP_STATIC} PROPERTIES OUTPUT_NAME ${PAHO_MQTT_CPP})
endif()
endif()
62 changes: 27 additions & 35 deletions src/mqtt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#*******************************************************************************
# Copyright (c) 2016
# Copyright (c) 2016-2018
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -12,41 +12,33 @@
#
# Contributors:
# Guilherme Maciel Ferreira - initial version
# Frank Pagliughi
#*******************************************************************************/

## Note: on OS X you should install XCode and the associated command-line tools

## install headers
set(COMMON_HDR
async_client.h
buffer_ref.h
buffer_view.h
callback.h
client.h
connect_options.h
delivery_token.h
disconnect_options.h
exception.h
iaction_listener.h
iasync_client.h
iclient_persistence.h
message.h
response_options.h
ssl_options.h
string_collection.h
thread_queue.h
token.h
topic.h
types.h
will_options.h)

#if(PAHO_WITH_SSL)
# set(COMMON_HDR
# ${COMMON_HDR}
# ssl_options.h)
#endif()

install(
FILES ${COMMON_HDR}
DESTINATION include/mqtt
FILES
async_client.h
buffer_ref.h
buffer_view.h
callback.h
client.h
connect_options.h
delivery_token.h
disconnect_options.h
exception.h
iaction_listener.h
iasync_client.h
iclient_persistence.h
message.h
response_options.h
ssl_options.h
string_collection.h
thread_queue.h
token.h
topic.h
types.h
will_options.h
DESTINATION
include/mqtt
)

0 comments on commit 2cd0109

Please sign in to comment.