forked from fast-project/fast-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
260 lines (221 loc) · 9.89 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#
# This file is part of fast-lib.
# Copyright (C) 2015 RWTH Aachen University - ACS
#
# This file is licensed under the GNU Lesser General Public License Version 3
# Version 3, 29 June 2007. For details see 'LICENSE.md' in the root directory.
#
cmake_minimum_required(VERSION 2.8)
########
# If the user specifies -DCMAKE_BUILD_TYPE on the command line, take their definition
# and dump it in the cache along with proper documentation, otherwise set CMAKE_BUILD_TYPE
# to Debug prior to calling PROJECT()
IF(DEFINED CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.")
ELSE()
SET(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.")
ENDIF()
string( TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE )
########
project(fast-lib)
if(POLICY CMP0026)
cmake_policy(SET CMP0026 OLD) # Used to by MergeStaticLibraries
endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
# Cache
set(BUILD_SHARED_LIBS OFF CACHE BOOL "If ON build shared, else static libraries.")
if(BUILD_SHARED_LIBS)
message(FATAL_ERROR "BUILD_SHARED_LIBS=ON is not supported yet. Use static libs instead.")
endif()
set(ENABLE_LOGGING ON CACHE BOOL "Enable logging.")
if(ENABLE_LOGGING)
add_definitions(-DFASTLIB_ENABLE_LOGGING)
endif()
set(BUILD_TESTS ON CACHE BOOL "Enable build of tests.")
# Library names
set(FASTLIB "fastlib")
# Define version number
set(FASTLIB_VERSION_MAJOR "0")
set(FASTLIB_VERSION_MINOR "4")
set(FASTLIB_VERSION_PATCH "0")
set(FASTLIB_VERSION "${FASTLIB_VERSION_MAJOR}.${FASTLIB_VERSION_MINOR}.${FASTLIB_VERSION_PATCH}")
# Check compiler version
set(MIN_GCC_VERSION "4.9.0")
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
if(CMAKE_C_COMPILER_VERSION LESS "${MIN_GCC_VERSION}")
message(FATAL_ERROR "gcc must be of version ${MIN_GCC_VERSION} or higher.")
endif()
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION LESS "${MIN_GCC_VERSION}")
message(FATAL_ERROR "g++ must be of version ${MIN_GCC_VERSION} or higher.")
endif()
endif()
# Set compiler flags
########
# Compiler/linker options based on the different compilers
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# Using Clang
if ("${CMAKE_BUILD_TYPE}" STREQUAL "release")
ADD_DEFINITIONS(-g -Ofast -march=native)
endif()
if ("${CMAKE_BUILD_TYPE}" STREQUAL "debug")
ADD_DEFINITIONS(-g3 -O0 -Weverything)
ADD_DEFINITIONS(-Wno-c++98-compat -Wno-shadow -Wno-c++98-compat-pedantic -Wno-padded)
ADD_DEFINITIONS(-Wno-exit-time-destructors -Wno-global-constructors)
endif()
# Options required to reduce the noise of spdlog
ADD_DEFINITIONS(-Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-sign-conversion -Wno-padded -Wno-switch-enum)
ADD_DEFINITIONS(-Wno-old-style-cast -Wno-undef -Wno-documentation-unknown-command)
ADD_DEFINITIONS(-Wno-weak-vtables -Wno-global-constructors -Wno-exit-time-destructors -Wno-newline-eof)
ADD_DEFINITIONS(-Wno-missing-variable-declarations -Wno-double-promotion -Wno-extra-semi)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
# Using GCC
if ("${CMAKE_BUILD_TYPE}" STREQUAL "release")
ADD_DEFINITIONS(-g -O2 -march=native)
endif()
if ("${CMAKE_BUILD_TYPE}" STREQUAL "debug")
ADD_DEFINITIONS(-g3 -O0 -Wall -Wextra -Wunused)
endif()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# Using Intel C++
if ("${CMAKE_BUILD_TYPE}" STREQUAL "release")
ADD_DEFINITIONS(-g -fast)
endif()
if ("${CMAKE_BUILD_TYPE}" STREQUAL "debug")
ADD_DEFINITIONS(-g3 -O0 -Wall -Wextra)
endif()
endif()
########
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" CXX11_SUPPORTED)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" CXX0X_SUPPORTED)
if(CXX11_SUPPORTED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(CXX0X_SUPPORTED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support.")
endif()
### Define header, source and lib files
# Headers
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
set(HEADERS
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/communicator.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/mqtt_communicator.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/serializable.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/log.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/optional.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/agent/init.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/agent/init_agent.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/agent/stop_monitor.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/agent/mmbwmon/ack.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/agent/mmbwmon/request.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/agent/mmbwmon/reply.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/agent/mmbwmon/stop.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/agent/mmbwmon/restart.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/migfra/pci_id.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/migfra/ivshmem.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/migfra/time_measurement.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/migfra/result.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fast-lib/message/migfra/task.hpp"
)
# Source
set(SRC
"${CMAKE_CURRENT_SOURCE_DIR}/src/mqtt_communicator.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/serializable.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/log.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/agent/init.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/agent/init_agent.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/agent/stop_monitor.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/agent/mmbwmon/ack.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/agent/mmbwmon/request.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/agent/mmbwmon/reply.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/agent/mmbwmon/stop.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/agent/mmbwmon/restart.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/migfra/pci_id.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/migfra/ivshmem.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/migfra/time_measurement.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/migfra/result.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/message/migfra/task.cpp"
)
# Add library
add_library(${FASTLIB} ${SRC} ${HEADERS})
# Enable support for external projects
include(ExternalProject)
set(EXTERNAL_INCLUDES ${CMAKE_CURRENT_BINARY_DIR}/external_includes)
file(MAKE_DIRECTORY ${EXTERNAL_INCLUDES}/fast-lib/external)
include_directories(SYSTEM ${EXTERNAL_INCLUDES}/fast-lib/external)
########
# mosquitto
ExternalProject_Add(mosquitto
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vendor/mosquitto-1.3.5
CMAKE_COMMAND CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER} cmake
CMAKE_ARGS -DWITH_SRV=OFF -DWITH_TLS=OFF -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR> -DCMAKE_C_FLAGS=-fpic -DCMAKE_CXX_FLAGS=-fpic
)
add_dependencies(${FASTLIB} mosquitto)
# Store mosquitto variables
ExternalProject_Get_Property(mosquitto install_dir)
set(MOSQUITTO_LIBRARIES ${install_dir}/lib/libmosquittopp.a ${install_dir}/lib/libmosquitto.a)
set(MOSQUITTO_INCLUDE_DIR "${install_dir}/include")
set(LIBS "${LIBS}" "${MOSQUITTO_LIBRARIES}")
#include_directories(SYSTEM "${MOSQUITTO_INCLUDE_DIR}")
add_custom_command(
TARGET mosquitto
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink "${MOSQUITTO_INCLUDE_DIR}/mosquitto.h" "${EXTERNAL_INCLUDES}/fast-lib/external/mosquitto.h"
COMMAND ${CMAKE_COMMAND} -E create_symlink "${MOSQUITTO_INCLUDE_DIR}/mosquittopp.h" "${EXTERNAL_INCLUDES}/fast-lib/external/mosquittopp.h"
COMMAND ${CMAKE_COMMAND} -E create_symlink "${MOSQUITTO_INCLUDE_DIR}/mosquitto_plugin.h" "${EXTERNAL_INCLUDES}/fast-lib/external/mosquitto_plugin.h")
# Install headers
install(DIRECTORY "${MOSQUITTO_INCLUDE_DIR}/" DESTINATION include/fast-lib/external)
########
########
# yaml-cpp
ExternalProject_Add(yaml-cpp
SOURCE_DIR ${PROJECT_SOURCE_DIR}/vendor/yaml-cpp
CMAKE_COMMAND CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER} cmake
CMAKE_ARGS -DCMAKE_CXX_FLAGS=-fpic -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR> -DYAML_CPP_BUILD_TOOLS=OFF)
add_dependencies(${FASTLIB} yaml-cpp)
# Store yaml-cpp variables
ExternalProject_Get_Property(yaml-cpp install_dir)
set(YAMLCPP_LIBRARY "${install_dir}/lib/libyaml-cpp.a")
set(YAMLCPP_INCLUDE_DIR "${install_dir}/include")
set(LIBS "${LIBS}" "${YAMLCPP_LIBRARY}")
#include_directories(SYSTEM "${YAMLCPP_INCLUDE_DIR}")
add_custom_command(
TARGET yaml-cpp
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink "${YAMLCPP_INCLUDE_DIR}/yaml-cpp" "${EXTERNAL_INCLUDES}/fast-lib/external/yaml-cpp")
# Install headers
install(DIRECTORY "${YAMLCPP_INCLUDE_DIR}/" DESTINATION include/fast-lib/external)
########
########
# spdlog
add_custom_target(spdlog)
add_dependencies(${FASTLIB} spdlog)
set(SPDLOG_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/vendor/spdlog/include")
#include_directories(SYSTEM "${SPDLOG_INCLUDE_DIR}")
add_custom_command(
TARGET spdlog
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink "${SPDLOG_INCLUDE_DIR}/spdlog" "${EXTERNAL_INCLUDES}/fast-lib/external/spdlog")
# Install headers
install(DIRECTORY "${SPDLOG_INCLUDE_DIR}/" DESTINATION include/fast-lib/external)
########
# Merge with dependent libraries
include(MergeStaticLibraries)
MERGE_STATIC_LIBRARIES(${FASTLIB} ALL "${LIBS}")
# As librt should always be available it is linked dynamically and therefore has to be linked in applications
# that use fast-lib as well.
target_link_libraries(${FASTLIB} -lrt)
# Install
install(TARGETS "${FASTLIB}"
LIBRARY DESTINATION "lib"
ARCHIVE DESTINATION "lib")
install(DIRECTORY "include/"
DESTINATION "include")
# Testing
if(BUILD_TESTS)
enable_testing()
add_subdirectory(test)
endif()