-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
168 lines (136 loc) · 5.58 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
cmake_minimum_required(VERSION 2.8)
# Имя проекта
project("primitive")
set(CODENAME "primitive")
set(CMAKE_EXPORT_COMPILE_COMMANDS On)
string(TOUPPER ${CODENAME} CODENAMEUC)
# Запрещаем сборку не на линукс
if (NOT UNIX)
message(FATAL_ERROR "Not Unix!")
endif ()
get_directory_property(hasParent PARENT_DIRECTORY)
# Подключаем внешние модули cmake
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/)
#message(STATUS "Module paths:")
#foreach(F ${CMAKE_MODULE_PATH})
# message(STATUS " ${F}")
#endforeach()
# Включаем использование ccache, если установлен
find_program(CCACHE_FOUND ccache)
if (CCACHE_FOUND)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
else ()
message(STATUS "Try to install `ccache` for faster compilation")
endif ()
# Выбираем стандарт С++
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -rdynamic -O0 -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -rdynamic -O0 -g")
if (NOT PROJECT_NAME)
set(PROJECT_NAME ${CODENAME})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPROJECT_NAME=${PROJECT_NAME}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPROJECT_NAME=${PROJECT_NAME}")
endif ()
#set(SANITIZERS "-fsanitize=leak")
#set(SANITIZERS "-fsanitize=address")
#set(SANITIZERS "-fsanitize=thread")
#set(SANITIZERS "-fsanitize=undefined")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZERS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZERS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SANITIZERS}")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(${CODENAME}_INCLUDES)
set(${CODENAME}_LIBRARIES)
# Подключаем динамическую линковку
list(APPEND "${CODENAME}_LIBRARIES" dl)
# Подключаем библиотеку сжатия
list(APPEND "${CODENAME}_LIBRARIES" z)
# Подключаем многопоточность
find_package(Threads REQUIRED)
if (NOT THREADS_FOUND)
message(FATAL_ERROR "library for multithreading not found")
endif ()
list(APPEND "${CODENAME}_LIBRARIES" ${CMAKE_THREAD_LIBS_INIT})
# Подключаем cxxopts
list(APPEND "${CODENAME}_INCLUDES" ${CMAKE_CURRENT_SOURCE_DIR}/lib/cxxopts/include)
# Подключаем libconfig++
find_package(Libconfig++)
if (NOT LIBCONFIG++_FOUND)
message(FATAL_ERROR "libconfig++ not found")
endif ()
list(APPEND "${CODENAME}_INCLUDES" ${LIBCONFIG++_INCLUDE_DIR})
list(APPEND "${CODENAME}_LIBRARIES" ${LIBCONFIG++_LIBRARIES})
# Подключаем openssl
find_package(OpenSSL)
if (NOT OPENSSL_FOUND)
message(FATAL_ERROR "ssl not found")
endif ()
list(APPEND "${CODENAME}_INCLUDES" ${OPENSSL_INCLUDE_DIR})
list(APPEND "${CODENAME}_LIBRARIES" ${OPENSSL_LIBRARIES})
# Подключаем mysql
find_package(MariaDB)
if (NOT MARIADB_FOUND)
find_package(MySQL)
if (NOT MYSQL_FOUND)
message(FATAL_ERROR "libmysqlclient and libmariadbclient not found")
else()
list(APPEND "${CODENAME}_INCLUDES" ${MYSQL_INCLUDE_DIR})
list(APPEND "${CODENAME}_LIBRARIES" ${MYSQL_LIBRARIES})
endif ()
else()
list(APPEND "${CODENAME}_INCLUDES" ${MARIADB_INCLUDE_DIR})
list(APPEND "${CODENAME}_LIBRARIES" ${MARIADB_LIBRARIES})
endif ()
# Подключаем protobuf++
find_package(Protobuf)
if (NOT PROTOBUF_FOUND)
message(WARNING "Protobuf not found")
else()
list(APPEND "${CODENAME}_INCLUDES" ${PROTOBUF_INCLUDE_DIRS})
list(APPEND "${CODENAME}_LIBRARIES" ${PROTOBUF_LIBRARIES})
endif ()
#message(STATUS "Project includes:")
foreach(F ${${CODENAME}_INCLUDES})
# message(STATUS " ${F}")
include_directories(${F})
endforeach()
#message(STATUS "Project libraries:")
#foreach(F ${${CODENAME}_LIBRARIES})
# message(STATUS " ${F}")
#endforeach()
add_subdirectory(src)
if(hasParent)
file(GLOB_RECURSE src_files impl/status/*.cpp)
list(APPEND SOURCE_FILES ${src_files})
else()
add_subdirectory(impl)
endif()
add_library(${CODENAME}_object OBJECT ${SOURCE_FILES})
set_target_properties(${CODENAME}_object PROPERTIES COMPILE_FLAGS "-fPIC")
add_library(${CODENAME}_shared SHARED $<TARGET_OBJECTS:${CODENAME}_object>)
target_link_libraries(${CODENAME}_shared ${${CODENAME}_LIBRARIES})
set_target_properties(${CODENAME}_shared PROPERTIES OUTPUT_NAME ${CODENAME})
add_dependencies(${CODENAME}_shared ${CODENAME}_object)
add_library(${CODENAME}_static STATIC $<TARGET_OBJECTS:${CODENAME}_object>)
target_link_libraries(${CODENAME}_static ${${CODENAME}_LIBRARIES})
set_target_properties(${CODENAME}_static PROPERTIES OUTPUT_NAME ${CODENAME})
add_dependencies(${CODENAME}_static ${CODENAME}_object)
if(hasParent)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} PARENT_SCOPE)
set(${CODENAMEUC}_INCLUDES ${${CODENAME}_INCLUDES} PARENT_SCOPE)
set(${CODENAMEUC}_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src" PARENT_SCOPE)
set(${CODENAMEUC}_LIBRARIES "${${CODENAME}_LIBRARIES}" PARENT_SCOPE)
set(${CODENAMEUC}_LIBRARY ${CODENAME} PARENT_SCOPE)
set(${CODENAMEUC}_OBJECTS $<TARGET_OBJECTS:${CODENAME}_object> PARENT_SCOPE)
set(${CODENAMEUC}_FOUND TRUE PARENT_SCOPE)
else()
add_executable(${CODENAME}_executable $<TARGET_OBJECTS:${CODENAME}_object>)
target_link_libraries(${CODENAME}_executable ${${CODENAME}_LIBRARIES})
set_target_properties(${CODENAME}_executable PROPERTIES OUTPUT_NAME ${CODENAME})
add_dependencies(${CODENAME}_executable ${CODENAME}_object)
endif()