-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCMakeLists.txt
154 lines (130 loc) · 4.6 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
cmake_minimum_required(VERSION 3.19)
project(mqtt-server VERSION 3.1.1 LANGUAGES CXX)
include(cmake/settings.cmake)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
endif()
if(MSVC)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Od /Zi")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2 /GL /DNDEBUG")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 -ggdb -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -Wno-unused -DNDEBUG")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 -ggdb -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -Wno-unused -DNDEBUG")
endif()
add_subdirectory(third-party/spdlog)
include(FetchContent)
FetchContent_Declare(
yaml-cpp
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
GIT_TAG 0.8.0
)
FetchContent_GetProperties(yaml-cpp)
if(NOT yaml-cpp_POPULATED)
message(STATUS "Fetching yaml-cpp...")
FetchContent_Populate(yaml-cpp)
add_subdirectory(${yaml-cpp_SOURCE_DIR} ${yaml-cpp_BINARY_DIR})
endif()
message(STATUS "Build ${PROJECT_NAME}: ${PROJECT_VERSION}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
if (NOT LOG_LEVEL)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG)
else()
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_INFO)
endif()
else()
if (LOG_LEVEL STREQUAL "Trace")
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE)
elseif(LOG_LEVEL STREQUAL "Debug")
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG)
elseif(LOG_LEVEL STREQUAL "Info")
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_INFO)
elseif(LOG_LEVEL STREQUAL "Warn")
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_WARN)
elseif(LOG_LEVEL STREQUAL "Error")
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_ERROR)
elseif(LOG_LEVEL STREQUAL "Critical")
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_CRITICAL)
elseif(LOG_LEVEL STREQUAL "Off")
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_OFF)
else()
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG)
endif()
endif()
file(GLOB_RECURSE srcs ${PROJECT_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE hrds ${PROJECT_SOURCE_DIR}/src/*.h)
file(GLOB_RECURSE ipps ${PROJECT_SOURCE_DIR}/src/*.ipp)
include_directories(
third-party/asio/include
third-party/sha1
third-party/picohttpparser
src/
)
add_executable(${PROJECT_NAME} main.cpp ${srcs})
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(${PROJECT_NAME} PUBLIC
pthread
spdlog::spdlog
yaml-cpp::yaml-cpp)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
target_link_libraries(${PROJECT_NAME} PUBLIC ws2_32 wsock32 spdlog::spdlog yaml-cpp::yaml-cpp)
else()
message(STATUS "This operating system is not supported")
endif()
#
# SSL/TLS
#
if (MQ_WITH_TLS)
message(STATUS "Enable SSL/TLS for mqtt server")
add_definitions(-DMQ_WITH_TLS)
find_package(OpenSSL REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC OpenSSL::SSL)
endif()
#
# Valgrind
#
find_program(VALGRIND_EXECUTABLE
NAMES "valgrind"
PATHS "/usr/bin" "/usr/local/bin"
)
if (VALGRIND_EXECUTABLE)
message(STATUS "Successfully find program `valgrind`")
message(STATUS "You can use the `make valgrind` command to perform memory leak detection")
add_custom_target(valgrind
COMMAND
${VALGRIND_EXECUTABLE} --log-file=memcheck.log --leak-check=full --track-origins=yes $<TARGET_FILE:${PROJECT_NAME}>
COMMENT
"Perform memory leak detection, end with `Ctrl + C`"
)
endif()
#
# Clang-Format
#
find_program(CLANG_FORMAT_EXECUTABLE
NAME "clang-format-12"
PATHS "/usr/bin" "/usr/local/bin"
)
if (CLANG_FORMAT_EXECUTABLE)
message(STATUS "Successfully find program `clang-format-12`")
message(STATUS "You can use the `make clang-format` command to automatically format the code style")
add_custom_target(clang-format ALL
COMMAND
${CLANG_FORMAT_EXECUTABLE} --style=file -i ${hdrs};${srcs};${ipps};${PROJECT_SOURCE_DIR}/main.cpp
COMMENT
"Automatically format the code style"
)
endif()
#
# Unit testing setup
#
if (ENABLE_UNIT_TESTING)
enable_testing()
message(STATUS "Build unit tests for the project. Tests should always be found in the test folder")
add_subdirectory(test)
endif()