Merge pull request #8 from guilhermeferreira/build-cmake

Add support for the CMake build system
This commit is contained in:
Frank Pagliughi 2017-04-03 23:14:43 -04:00 committed by GitHub
commit 9d0a6a64a5
7 changed files with 2219 additions and 2 deletions

65
CMakeLists.txt Normal file
View File

@ -0,0 +1,65 @@
#*******************************************************************************
# Copyright (c) 2016
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v1.0 which accompany this distribution.
#
# The Eclipse Public License is available at
# http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
# Guilherme Maciel Ferreira - initial version
#*******************************************************************************/
## Note: on OS X you should install XCode and the associated command-line tools
## cmake flags
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
## project name
project("paho-mqtt-cpp" LANGUAGES CXX)
## library name
set(PAHO_MQTT_CPP paho-mqttpp3)
## build settings
set(PAHO_VERSION_MAJOR 0)
set(PAHO_VERSION_MINOR 2)
set(CLIENT_VERSION ${PAHO_VERSION_MAJOR}.${PAHO_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_MAJOR ${PAHO_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PAHO_VERSION_MINOR})
## build options
set(PAHO_BUILD_STATIC FALSE CACHE BOOL "Build static library")
set(PAHO_BUILD_SAMPLES FALSE CACHE BOOL "Build sample programs")
set(PAHO_BUILD_DOCUMENTATION FALSE CACHE BOOL "Create and install the HTML based API documentation (requires Doxygen)")
set(PAHO_MQTT_C_PATH "" CACHE PATH "Add a path to paho.mqtt.c library and headers")
set(PAHO_MQTT_C paho-mqtt3a)
## build flags
set(CMAKE_CXX_STANDARD 11)
## build directories
add_subdirectory(src)
add_subdirectory(src/mqtt)
if(PAHO_BUILD_SAMPLES)
add_subdirectory(src/samples)
endif()
if(PAHO_BUILD_DOCUMENTATION)
add_subdirectory(doc)
endif()
## packaging settings
if(WIN32)
set(CPACK_GENERATOR "ZIP")
elseif(UNIX)
set(CPACK_GENERATOR "TGZ")
endif()
include(CPack)

View File

@ -135,9 +135,100 @@ $ $PAHO_DIR/configure --host=arm-linux-gnueabi
```
### Windows
### CMake
The CMake is a cross-platform building system suitable for Unix and non-Unix platforms, like Microsoft Windows.
#### Unix and GNU/Linux
On Unix systems CMake creates Makefiles.
##### Build requirements
The build process currently supports a number of Unix and Linux flavors. The build process requires the following tools:
* CMake (http://cmake.org)
* GNU Make (https://www.gnu.org/software/make/), and
* GCC (https://gcc.gnu.org/).
On Debian based systems this would mean that the following packages have to be installed:
```
apt-get install build-essential gcc make cmake cmake-gui cmake-curses-gui
```
The documentation requires doxygen and optionally graphviz:
```
apt-get install doxygen graphviz
```
##### Build instructions
Before compiling, determine the value of some variables in order to configure features, library locations, and other options:
Variable | Default Value | Description
------------ | ------------- | -------------
PAHO_MQTT_C_PATH | "" | Add a path paho.mqtt.c library and headers
PAHO_BUILD_DOCUMENTATION | FALSE | Create and install the HTML based API documentation (requires Doxygen)
PAHO_BUILD_SAMPLES | FALSE | Build sample programs
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:
```
$ 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 ..
$ make
```
or
```
$ cmake -DCMAKE_INSTALL_PREFIX=/tmp/paho-cpp -DPAHO_MQTT_C_PATH=/tmp/paho-c \
-DPAHO_BUILD_SAMPLES:BOOL=ON -DPAHO_BUILD_STATIC:BOOL=ON \
-DPAHO_BUILD_DOCUMENTATION:BOOL=ON
$ make
```
Invoking cmake and specifying build options can also be performed using cmake-gui or ccmake (see https://cmake.org/runningcmake/). For example:
```
$ ccmake ..
$ make
```
To use another compiler:
```
$ cmake -DCMAKE_CXX_COMPILER=clang++
$ make
```
#### Windows
On Windows systems CMake creates Visual Studio project files.
##### Build requirements
The build process currently supports a number Windows versions. The build process requires the following tools:
* CMake GUI (http://cmake.org), and
* Visual Studio (https://www.gnu.org/software/make/).
##### Build instructions
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).
Once the configuration is done, click on the Configure button, select the version of the Visual Studio, and then click on Generate button.
At the end of this process you have a Visual Studio solution.
TBD
## Example

44
doc/CMakeLists.txt Normal file
View File

@ -0,0 +1,44 @@
#*******************************************************************************
# Copyright (c) 2016
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v1.0 which accompany this distribution.
#
# The Eclipse Public License is available at
# http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
# 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)
message(FATAL_ERROR "Doxygen is needed to build the documentation.")
endif()
set(DOXYTARGETS)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doc)
set(DOXYFILE_SRC Doxyfile.cmake)
set(DOXYFILE ${CMAKE_CURRENT_BINARY_DIR}/${DOXYFILE_SRC})
configure_file(${DOXYFILE_SRC} ${DOXYFILE} @ONLY)
add_custom_target(
${DOXYFILE_SRC}.target
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYFILE}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen"
VERBATIM
)
set(DOXYTARGETS ${DOXYTARGETS} ${DOXYFILE_SRC}.target)
add_custom_target(doc ALL DEPENDS ${DOXYTARGETS})
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doc DESTINATION share)

1795
doc/Doxyfile.cmake Normal file

File diff suppressed because it is too large Load Diff

121
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,121 @@
#*******************************************************************************
# Copyright (c) 2016
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v1.0 which accompany this distribution.
#
# The Eclipse Public License is available at
# http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
# Guilherme Maciel Ferreira - initial version
#*******************************************************************************/
## Note: on OS X you should install XCode and the associated command-line tools
## include directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
## libraries
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)
endif()
## use Object Library to optimize compilation
add_library(common_obj OBJECT
async_client.cpp
client.cpp
disconnect_options.cpp
iclient_persistence.cpp
message.cpp
response_options.cpp
token.cpp
topic.cpp
connect_options.cpp
ssl_options.cpp
will_options.cpp)
## set position independent flag (-fPIC on Unix)
set_property(TARGET common_obj
PROPERTY POSITION_INDEPENDENT_CODE ON)
## create the shared library
add_library(${PAHO_MQTT_CPP} SHARED
$<TARGET_OBJECTS:common_obj>)
## add dependencies to the shared library
target_link_libraries(${PAHO_MQTT_CPP}
${LIBS_SYSTEM})
## set the shared library soname
set_target_properties(${PAHO_MQTT_CPP} PROPERTIES
VERSION ${CLIENT_VERSION}
SOVERSION ${PAHO_VERSION_MAJOR})
## install the shared library
install(TARGETS ${PAHO_MQTT_CPP}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib)
## build static version of the Paho MQTT C++ library
if(PAHO_BUILD_STATIC)
## create the static library
add_library(${PAHO_MQTT_CPP}-static STATIC
$<TARGET_OBJECTS:common_obj>)
## add dependencies to the static library
target_link_libraries(${PAHO_MQTT_CPP}-static
${LIBS_SYSTEM})
## install the static library
install(TARGETS ${PAHO_MQTT_CPP}-static
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib)
endif()
## extract Paho MQTT C include directory
get_filename_component(PAHO_MQTT_C_INC_DIR ${PAHO_MQTT_C_PATH}/include ABSOLUTE)
## extract Paho MQTT C library directory
get_filename_component(PAHO_MQTT_C_LIB_DIR ${PAHO_MQTT_C_PATH}/lib ABSOLUTE)
## 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")
## 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})
## 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})
target_link_libraries(${PAHO_MQTT_CPP}
${PAHO_MQTT_C_LIB})
endif()

39
src/mqtt/CMakeLists.txt Normal file
View File

@ -0,0 +1,39 @@
#*******************************************************************************
# Copyright (c) 2016
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v1.0 which accompany this distribution.
#
# The Eclipse Public License is available at
# http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
# Guilherme Maciel Ferreira - initial version
#*******************************************************************************/
## Note: on OS X you should install XCode and the associated command-line tools
## install headers
install(
FILES async_client.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
ipersistable.h
message.h
response_options.h
token.h
topic.h
will_options.h
ssl_options.h
DESTINATION include
)

View File

@ -0,0 +1,62 @@
#*******************************************************************************
# Copyright (c) 2016
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v1.0 which accompany this distribution.
#
# The Eclipse Public License is available at
# http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
# Guilherme Maciel Ferreira - initial version
#*******************************************************************************/
## Note: on OS X you should install XCode and the associated command-line tools
## Paho MQTT C include directory
get_filename_component(PAHO_MQTT_C_INC_DIR ${PAHO_MQTT_C_PATH}/include ABSOLUTE)
## 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)
add_executable(async_subscribe async_subscribe.cpp)
add_executable(sync_publish sync_publish.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(sync_publish
${PAHO_MQTT_C}
${PAHO_MQTT_CPP})
## install binaries
install(TARGETS async_publish
async_subscribe
sync_publish
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
)