Skip to content

Commit 6abe93f

Browse files
friendlyanonsiladic
authored andcommitted
Add CMake build and client support
1 parent bf81ab5 commit 6abe93f

File tree

11 files changed

+263
-5
lines changed

11 files changed

+263
-5
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.cache/
2+
build/
3+
prefix/
4+
CMakeUserPresets.json

CMakeLists.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
project(async-mqtt5 VERSION 0.0.1 LANGUAGES NONE)
4+
5+
include(cmake/project-is-top-level.cmake)
6+
include(cmake/variables.cmake)
7+
8+
add_library(async_mqtt5 INTERFACE)
9+
add_library(Async::MQTT5 ALIAS async_mqtt5)
10+
11+
set_property(
12+
TARGET async_mqtt5 PROPERTY
13+
EXPORT_NAME MQTT5
14+
)
15+
16+
target_include_directories(
17+
async_mqtt5 ${warning_guard}
18+
INTERFACE
19+
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
20+
)
21+
22+
target_compile_features(async_mqtt5 INTERFACE cxx_std_20)
23+
24+
find_package(Boost 1.82 REQUIRED)
25+
target_link_libraries(async_mqtt5 INTERFACE Boost::headers)
26+
27+
if(NOT CMAKE_SKIP_INSTALL_RULES)
28+
include(cmake/install-rules.cmake)
29+
endif()
30+
31+
if(PROJECT_IS_TOP_LEVEL)
32+
option(BUILD_EXAMPLES "Build examples tree." "${async-mqtt5_DEVELOPER_MODE}")
33+
if(BUILD_EXAMPLES)
34+
add_subdirectory(example)
35+
endif()
36+
endif()
37+
38+
if(NOT async-mqtt5_DEVELOPER_MODE)
39+
return()
40+
elseif(NOT PROJECT_IS_TOP_LEVEL)
41+
message(
42+
AUTHOR_WARNING
43+
"Developer mode is intended for developers of async-mqtt5"
44+
)
45+
endif()
46+
47+
include(cmake/dev-mode.cmake)

cmake/dev-mode.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include(CTest)
2+
if(BUILD_TESTING)
3+
add_subdirectory(test/unit)
4+
endif()

cmake/install-config.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include(CMakeFindDependencyMacro)
2+
find_dependency(Boost 1.82)
3+
4+
include("${CMAKE_CURRENT_LIST_DIR}/async-mqtt5Targets.cmake")

cmake/install-rules.cmake

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
if(PROJECT_IS_TOP_LEVEL)
2+
set(
3+
CMAKE_INSTALL_INCLUDEDIR "include/async-mqtt5-${PROJECT_VERSION}"
4+
CACHE STRING ""
5+
)
6+
set_property(CACHE CMAKE_INSTALL_INCLUDEDIR PROPERTY TYPE PATH)
7+
endif()
8+
9+
# Project is configured with no languages, so tell GNUInstallDirs the lib dir
10+
set(CMAKE_INSTALL_LIBDIR lib CACHE PATH "")
11+
12+
include(CMakePackageConfigHelpers)
13+
include(GNUInstallDirs)
14+
15+
# find_package(<package>) call for consumers to find this project
16+
set(package async-mqtt5)
17+
18+
install(
19+
DIRECTORY include/
20+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
21+
COMPONENT async-mqtt5_Development
22+
)
23+
24+
install(
25+
TARGETS async_mqtt5
26+
EXPORT async-mqtt5Targets
27+
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
28+
)
29+
30+
write_basic_package_version_file(
31+
"${package}ConfigVersion.cmake"
32+
COMPATIBILITY SameMajorVersion
33+
ARCH_INDEPENDENT
34+
)
35+
36+
# Allow package maintainers to freely override the path for the configs
37+
set(
38+
async-mqtt5_INSTALL_CMAKEDIR "${CMAKE_INSTALL_DATADIR}/${package}"
39+
CACHE STRING "CMake package config location relative to the install prefix"
40+
)
41+
set_property(CACHE async-mqtt5_INSTALL_CMAKEDIR PROPERTY TYPE PATH)
42+
mark_as_advanced(async-mqtt5_INSTALL_CMAKEDIR)
43+
44+
install(
45+
FILES cmake/install-config.cmake
46+
DESTINATION "${async-mqtt5_INSTALL_CMAKEDIR}"
47+
RENAME "${package}Config.cmake"
48+
COMPONENT async-mqtt5_Development
49+
)
50+
51+
install(
52+
FILES "${PROJECT_BINARY_DIR}/${package}ConfigVersion.cmake"
53+
DESTINATION "${async-mqtt5_INSTALL_CMAKEDIR}"
54+
COMPONENT async-mqtt5_Development
55+
)
56+
57+
install(
58+
EXPORT async-mqtt5Targets
59+
NAMESPACE Async::
60+
DESTINATION "${async-mqtt5_INSTALL_CMAKEDIR}"
61+
COMPONENT async-mqtt5_Development
62+
)
63+
64+
if(PROJECT_IS_TOP_LEVEL)
65+
include(CPack)
66+
endif()

cmake/project-is-top-level.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This variable is set by project() in CMake 3.21+
2+
string(
3+
COMPARE EQUAL
4+
"${CMAKE_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}"
5+
PROJECT_IS_TOP_LEVEL
6+
)

cmake/variables.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# ---- Developer mode ----
2+
3+
# Developer mode enables targets and code paths in the CMake scripts that are
4+
# only relevant for the developer(s) of async-mqtt5
5+
# Targets necessary to build the project must be provided unconditionally, so
6+
# consumers can trivially build and package the project
7+
if(PROJECT_IS_TOP_LEVEL)
8+
option(async-mqtt5_DEVELOPER_MODE "Enable developer mode" OFF)
9+
endif()
10+
11+
# ---- Warning guard ----
12+
13+
# target_include_directories with the SYSTEM modifier will request the compiler
14+
# to omit warnings from the provided paths, if the compiler supports that
15+
# This is to provide a user experience similar to find_package when
16+
# add_subdirectory or FetchContent is used to consume this project
17+
set(warning_guard "")
18+
if(NOT PROJECT_IS_TOP_LEVEL)
19+
option(
20+
async-mqtt5_INCLUDES_WITH_SYSTEM
21+
"Use SYSTEM modifier for async-mqtt5's includes, disabling warnings"
22+
ON
23+
)
24+
mark_as_advanced(async-mqtt5_INCLUDES_WITH_SYSTEM)
25+
if(async-mqtt5_INCLUDES_WITH_SYSTEM)
26+
set(warning_guard SYSTEM)
27+
endif()
28+
endif()

example/CMakeLists.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
project(async-mqtt5-examples CXX)
4+
5+
include(../cmake/project-is-top-level.cmake)
6+
7+
if(PROJECT_IS_TOP_LEVEL)
8+
find_package(async_mqtt5 REQUIRED)
9+
endif()
10+
11+
function(add_example name)
12+
add_executable("${name}" ${ARGN})
13+
target_compile_features("${name}" PRIVATE cxx_std_20)
14+
target_link_libraries("${name}" PRIVATE Async::MQTT5)
15+
endfunction()
16+
17+
foreach(f callbacks cpp20_coroutines futures publisher receiver)
18+
add_example("${f}" "${f}.cpp")
19+
endforeach()
20+
21+
#[[
22+
add_example(
23+
misc
24+
src/run_examples.cpp
25+
network_connection.cpp
26+
openssl-tls.cpp
27+
tcp.cpp
28+
websocket-tcp.cpp
29+
websocket-tls.cpp
30+
)
31+
32+
find_package(OpenSSL REQUIRED)
33+
target_link_libraries(misc PRIVATE OpenSSL::SSL)
34+
]]

test/unit/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
project(async-mqtt5-tests CXX)
4+
5+
include(../../cmake/project-is-top-level.cmake)
6+
7+
if(PROJECT_IS_TOP_LEVEL)
8+
find_package(async_mqtt5 REQUIRED)
9+
enable_testing()
10+
endif()
11+
12+
add_executable(
13+
mqtt-test
14+
src/run_tests.cpp
15+
test/cancellation.cpp
16+
test/client_broker.cpp
17+
test/coroutine.cpp
18+
test/publish_send_op.cpp
19+
test/serialization.cpp
20+
test/session.cpp
21+
)
22+
23+
target_include_directories(mqtt-test PRIVATE include)
24+
target_compile_features(mqtt-test PRIVATE cxx_std_20)
25+
26+
find_package(Boost 1.82 REQUIRED unit_test_framework)
27+
target_link_libraries(mqtt-test PRIVATE Async::MQTT5 Boost::unit_test_framework)
28+
29+
add_test(NAME mqtt-test COMMAND mqtt-test)

test/unit/src/run_tests.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
1-
#include <boost/test/included/unit_test.hpp>
1+
#include <boost/test/unit_test.hpp>
22

33
#include <test_common/protocol_logging.hpp>
44

5-
boost::unit_test::test_suite* init_tests(
6-
int /*argc*/, char* /*argv*/[]
7-
) {
5+
namespace {
6+
7+
void setup_mqtt() {
88
async_mqtt5::test::logging_enabled() = true;
9-
return nullptr;
9+
}
10+
11+
}
12+
13+
#ifdef BOOST_TEST_DYN_LINK
14+
static bool init_tests() {
15+
setup_mqtt();
16+
return true;
1017
}
1118

1219
int main(int argc, char* argv[]) {
1320
return boost::unit_test::unit_test_main(&init_tests, argc, argv);
1421
}
22+
#else
23+
boost::unit_test::test_suite* init_unit_test_suite(int, char** const) {
24+
setup_mqtt();
25+
return nullptr;
26+
}
27+
#endif
1528

1629
/*
1730
* usage: ./mqtt-test [boost test --arg=val]*

0 commit comments

Comments
 (0)