1
0
mirror of https://github.com/eclipse/paho.mqtt.cpp.git synced 2025-05-09 11:21:24 +08:00

cmake: modernize the build system

- Write a CMake module for finding and importing the Paho MQTT C library.
- Prepare the build system for exporting targets

Signed-off-by: David Wagner <david.wagner@easymile.com>
This commit is contained in:
David Wagner 2018-03-30 16:54:24 +02:00
parent 62c2595c1e
commit d16b94c646
6 changed files with 65 additions and 123 deletions

View File

@ -61,9 +61,6 @@ endif()
option(PAHO_BUILD_SAMPLES "Build sample programs" FALSE)
option(PAHO_BUILD_DOCUMENTATION "Create and install the API documentation (requires Doxygen)" FALSE)
set(PAHO_MQTT_C_PATH "" CACHE PATH "Add a path to paho.mqtt.c library and headers")
set(PAHO_MQTT_C paho-mqtt3a)
## --- C++11 build flags ---
@ -73,12 +70,9 @@ set(CMAKE_CXX_EXTENSIONS OFF)
## --- Build directories ---
# For the paho_mqtt_c module
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
add_subdirectory(src)
add_subdirectory(src/mqtt)
if(PAHO_BUILD_SAMPLES)
add_subdirectory(src/samples)
endif()
if(PAHO_BUILD_DOCUMENTATION)
add_subdirectory(doc)

View File

@ -52,13 +52,15 @@ Before compiling, determine the value of some variables in order to configure fe
Variable | Default Value | Description
------------ | ------------- | -------------
PAHO_MQTT_C_PATH | "" | Add a path paho.mqtt.c library and headers
PAHO_BUILD_SHARED | TRUE (Linux), FALSE (Win32) | Whether to build the shared library
PAHO_BUILD_STATIC | FALSE (Linux), TRUE (Win32) | Whether to build the static library
PAHO_BUILD_DOCUMENTATION | FALSE | Create and install the HTML based API documentation (requires Doxygen)
PAHO_BUILD_SAMPLES | FALSE | Build sample programs
PAHO_WITH_SSL | TRUE (Linux), FALSE (Win32) | Flag that defines whether to build ssl-enabled binaries too
If you installed the C library on a non-standard path, you might want to pass it as value to the
`CMAKE_PREFIX_PATH` option.
Using these variables CMake can be used to generate your Makefiles. The out-of-source build is the default on CMake. Therefore it is recommended to invoke all build commands inside your chosen build directory.
An example build session targeting the build platform could look like this:
@ -68,7 +70,7 @@ $ git clone https://github.com/eclipse/paho.mqtt.cpp
$ cd paho.mqtt.cpp
$ mkdir build
$ cd build
$ cmake -DPAHO_BUILD_DOCUMENTATION=TRUE -DPAHO_BUILD_SAMPLES=TRUE -DPAHO_MQTT_C_PATH=../../paho.mqtt.c ..
$ cmake -DPAHO_BUILD_DOCUMENTATION=TRUE -DPAHO_BUILD_SAMPLES=TRUE -DCMAKE_PREFIX_PATH=../../paho.mqtt.c ..
$ make
$ sudo make install
```
@ -76,7 +78,7 @@ $ sudo make install
or
```
$ cmake -DCMAKE_INSTALL_PREFIX=/tmp/paho-cpp -DPAHO_MQTT_C_PATH=/tmp/paho-c \
$ cmake -DCMAKE_INSTALL_PREFIX=/tmp/paho-cpp -DCMAKE_PREFIX_PATH=/tmp/paho-c \
-DPAHO_BUILD_SAMPLES:BOOL=ON -DPAHO_BUILD_STATIC:BOOL=ON \
-DPAHO_BUILD_DOCUMENTATION:BOOL=ON
$ make
@ -113,7 +115,7 @@ The build process currently supports a number Windows versions. The build proces
First install and open the cmake-gui application. This tutorial is based on cmake-gui 3.5.2.
Second, select the path to the Paho MQTT C library (PAHO_MQTT_C_PATH). Remember that the Paho MQTT C must be installed on the system. Next, choose if it is supposed to build the documentation (PAHO_BUILD_DOCUMENTATION) and/or the sample applications (PAHO_BUILD_SAMPLES).
Second, select the path to the Paho MQTT C library (CMAKE_PREFIX_PATH) if not installed in a standard path. Remember that the Paho MQTT C must be installed on the system. Next, choose if it is supposed to build the documentation (PAHO_BUILD_DOCUMENTATION) and/or the sample applications (PAHO_BUILD_SAMPLES).
Once the configuration is done, click on the Configure button, select the version of the Visual Studio, and then click on Generate button.

View File

@ -27,7 +27,7 @@ build_script:
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
cmake -G "NMake Makefiles" -DCMAKE_INSTALL_PREFIX="C:\Temp\paho-cpp" -DPAHO_MQTT_C_PATH="C:\Temp\paho-c" -DPAHO_WITH_SSL=TRUE -DPAHO_BUILD_DOCUMENTATION=FALSE -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=TRUE ..
cmake -G "NMake Makefiles" -DCMAKE_INSTALL_PREFIX="C:\Temp\paho-cpp" -DCMAKE_PREFIX_PATH="C:\Temp\paho-c" -DPAHO_WITH_SSL=TRUE -DPAHO_BUILD_DOCUMENTATION=FALSE -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=TRUE ..
nmake

23
cmake/FindPahoMqttC.cmake Normal file
View File

@ -0,0 +1,23 @@
# find the Paho MQTT C library
if(PAHO_WITH_SSL)
set(_PAHO_MQTT_C_LIB_NAME paho-mqtt3as)
find_package(OpenSSL REQUIRED)
else()
set(_PAHO_MQTT_C_LIB_NAME paho-mqtt3a)
endif()
find_library(PAHO_MQTT_C_LIBRARIES NAMES ${_PAHO_MQTT_C_LIB_NAME})
unset(_PAHO_MQTT_C_LIB_NAME)
find_path(PAHO_MQTT_C_INCLUDE_DIRS NAMES MQTTAsync.h)
add_library(PahoMqttC::PahoMqttC UNKNOWN IMPORTED)
set_target_properties(PahoMqttC::PahoMqttC PROPERTIES
IMPORTED_LOCATION "${PAHO_MQTT_C_LIBRARIES}"
INTERFACE_INCLUDE_DIRECTORIES "${PAHO_MQTT_C_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${PAHO_MQTT_C_LIBRARIES}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C")
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(PahoMqttC
REQUIRED_VARS PAHO_MQTT_C_LIBRARIES PAHO_MQTT_C_INCLUDE_DIRS)

View File

@ -25,18 +25,20 @@
## Note: on OS X you should install XCode and the associated command-line tools
## include directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
find_package(PahoMqttC REQUIRED)
add_subdirectory(mqtt)
if(PAHO_BUILD_SAMPLES)
add_subdirectory(samples)
endif()
## --- Library dependencies ---
find_package(Threads)
set(LIBS_SYSTEM Threads::Threads)
if(WIN32)
set(LIBS_SYSTEM ws2_32)
elseif(UNIX)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
set(LIB_DL dl)
endif()
set(LIBS_SYSTEM ${LIB_DL} c stdc++ pthread)
string(APPEND LIBS_SYSTEM " ws2_32")
endif()
## --- use Object Library to optimize compilation ---
@ -59,7 +61,14 @@ if(PAHO_WITH_SSL)
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})
## --- Build the shared library, if requested ---
@ -70,7 +79,11 @@ if(PAHO_BUILD_SHARED)
add_library(${PAHO_MQTT_CPP} SHARED $<TARGET_OBJECTS:OBJS>)
## add dependencies to the shared library
target_link_libraries(${PAHO_MQTT_CPP} ${LIBS_SYSTEM})
target_link_libraries(${PAHO_MQTT_CPP}
PRIVATE ${LIBS_SYSTEM}
PUBLIC PahoMqttC::PahoMqttC)
target_include_directories(${PAHO_MQTT_CPP}
PUBLIC $<INSTALL_INTERFACE:include>)
## set the shared library soname
set_target_properties(${PAHO_MQTT_CPP} PROPERTIES
@ -78,7 +91,7 @@ if(PAHO_BUILD_SHARED)
SOVERSION ${PAHO_VERSION_MAJOR})
## install the shared library
install(TARGETS ${PAHO_MQTT_CPP}
install(TARGETS ${PAHO_MQTT_CPP} EXPORT PahoMqttCpp
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
@ -90,73 +103,18 @@ if(PAHO_BUILD_STATIC)
## create the static library
add_library(${PAHO_MQTT_CPP_STATIC} STATIC $<TARGET_OBJECTS:OBJS>)
## add dependencies to the static library
target_link_libraries(${PAHO_MQTT_CPP_STATIC} ${LIBS_SYSTEM})
## 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>)
## install the static library
install(TARGETS ${PAHO_MQTT_CPP_STATIC}
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})
endif()
## extract Paho MQTT C include directory
get_filename_component(PAHO_MQTT_C_DEV_INC_DIR ${PAHO_MQTT_C_PATH}/src ABSOLUTE)
get_filename_component(PAHO_MQTT_C_STD_INC_DIR ${PAHO_MQTT_C_PATH}/include ABSOLUTE)
set(PAHO_MQTT_C_INC_DIR
${PAHO_MQTT_C_DEV_INC_DIR}
${PAHO_MQTT_C_STD_INC_DIR})
## extract Paho MQTT C library directory
get_filename_component(PAHO_MQTT_C_DEV_LIB_DIR ${PAHO_MQTT_C_PATH}/build/output ABSOLUTE)
get_filename_component(PAHO_MQTT_C_STD_LIB_DIR ${PAHO_MQTT_C_PATH}/lib ABSOLUTE)
get_filename_component(PAHO_MQTT_C_STD64_LIB_DIR ${PAHO_MQTT_C_PATH}/lib64 ABSOLUTE)
set(PAHO_MQTT_C_LIB_DIR
${PAHO_MQTT_C_DEV_LIB_DIR}
${PAHO_MQTT_C_STD_LIB_DIR}
${PAHO_MQTT_C_STD64_LIB_DIR})
## extract Paho MQTT C binary directory (Windows may place libraries there)
get_filename_component(PAHO_MQTT_C_BIN_DIR ${PAHO_MQTT_C_PATH}/bin ABSOLUTE)
## add library suffixes so Windows can find Paho DLLs
set(CMAKE_FIND_LIBRARY_PREFIXES ${CMAKE_FIND_LIBRARY_PREFIXES} "")
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES} ".dll" ".lib")
if(PAHO_WITH_SSL)
## find the Paho MQTT C SSL library
find_library(PAHO_MQTT_C_LIB
NAMES paho-mqtt3as
mqtt3as
PATHS ${PAHO_MQTT_C_LIB_DIR}
${PAHO_MQTT_C_BIN_DIR})
find_package(OpenSSL REQUIRED)
else()
## find the Paho MQTT C library
find_library(PAHO_MQTT_C_LIB
NAMES paho-mqtt3a
mqtt
paho-mqtt
mqtt3
paho-mqtt3
mqtt3a
PATHS ${PAHO_MQTT_C_LIB_DIR}
${PAHO_MQTT_C_BIN_DIR})
endif()
## use the Paho MQTT C library if found. Otherwise terminate the compilation
if(${PAHO_MQTT_C_LIB} STREQUAL "PAHO_MQTT_C_LIB-NOTFOUND")
message(FATAL_ERROR "Could not find Paho MQTT C library")
else()
include_directories(${PAHO_MQTT_C_INC_DIR})
link_directories(${PAHO_MQTT_C_LIB_DIR})
if(PAHO_BUILD_SHARED)
target_link_libraries(${PAHO_MQTT_CPP} ${PAHO_MQTT_C_LIB})
endif()
if(PAHO_BUILD_STATIC)
target_link_libraries(${PAHO_MQTT_CPP_STATIC} ${PAHO_MQTT_C_LIB})
endif()
endif()

View File

@ -24,35 +24,8 @@
## --- Library dependencies ---
if(NOT WIN32)
set(LIBS_THREADING pthread)
endif()
## Paho MQTT C include directory
get_filename_component(PAHO_MQTT_C_DEV_INC_DIR ${PAHO_MQTT_C_PATH}/src ABSOLUTE)
get_filename_component(PAHO_MQTT_C_STD_INC_DIR ${PAHO_MQTT_C_PATH}/include ABSOLUTE)
set(PAHO_MQTT_C_INC_DIR
${PAHO_MQTT_C_DEV_INC_DIR}
${PAHO_MQTT_C_STD_INC_DIR})
## Paho MQTT C++ include directory
get_filename_component(PAHO_MQTT_CPP_INC_DIR ${CMAKE_SOURCE_DIR}/src ABSOLUTE)
## include directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${PAHO_MQTT_C_INC_DIR})
include_directories(${PAHO_MQTT_CPP_INC_DIR})
## Paho MQTT C library directory
get_filename_component(PAHO_MQTT_C_LIB_DIR ${PAHO_MQTT_C_LIB} DIRECTORY)
## Paho MQTT C++ library directory
get_filename_component(PAHO_MQTT_CPP_LIB_DIR ${CMAKE_BINARY_DIR}/src ABSOLUTE)
## link directories
link_directories(${PAHO_MQTT_C_LIB_DIR})
link_directories(${PAHO_MQTT_CPP_LIB_DIR})
## binary files
add_executable(async_publish async_publish.cpp)
@ -65,27 +38,21 @@ add_executable(pub_speed_test pub_speed_test.cpp)
## link binaries
target_link_libraries(async_publish
${PAHO_MQTT_C}
${PAHO_MQTT_CPP})
target_link_libraries(async_subscribe
${PAHO_MQTT_C}
${PAHO_MQTT_CPP})
target_link_libraries(async_consume
${PAHO_MQTT_C}
${PAHO_MQTT_CPP})
target_link_libraries(sync_publish
${PAHO_MQTT_C}
${PAHO_MQTT_CPP})
target_link_libraries(sync_consume
${PAHO_MQTT_C}
${PAHO_MQTT_CPP})
target_link_libraries(data_publish
${PAHO_MQTT_C}
${PAHO_MQTT_CPP})
find_package(Threads)
target_link_libraries(pub_speed_test
${PAHO_MQTT_C}
${PAHO_MQTT_CPP}
${LIBS_THREADING})
Threads::Threads)
set(INSTALL_TARGETS
async_publish
@ -102,7 +69,6 @@ if(PAHO_WITH_SSL)
## link SSL binaries
target_link_libraries(ssl_publish
${PAHO_MQTT_C}
${PAHO_MQTT_CPP})
set(INSTALL_TARGETS
@ -113,8 +79,7 @@ if(PAHO_WITH_SSL)
endif()
## install binaries
install(TARGETS ${INSTALL_TARGETS}
install(TARGETS ${INSTALL_TARGETS} EXPORT PahoMqttCppSamples
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
)