-
Notifications
You must be signed in to change notification settings - Fork 13
/
CMakeLists.txt
282 lines (233 loc) · 8.98 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Set the minimum version of CMake that can be used
#
# To find the cmake version run
# $ cmake --version
# In order to get aws-sdk-cpp 1.9.179 build successful, need to set cmake version to 3.22
# cmake_minimum_required(VERSION 3.22)
cmake_minimum_required(VERSION 3.5.1)
# Project settings
project(starcache)
set(PACKAGE_VERSION "0.0.1")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Turn on -fPIC by default for both static and shared library
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if (DEFINED ENV{GCC_INSTALL_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --gcc-toolchain=$ENV{GCC_INSTALL_DIR}")
else()
message(WARNING "GCC_INSTALL_DIR evnironment variable is not set!")
endif()
endif()
option(STARCACHE_WITH_TESTS "Build the starcache tests" OFF)
option(STARCACHE_WITH_TOOLS "Build the starcache tools" OFF)
option(WITH_COVERAGE "Enable code coverage build" OFF)
option(STARCACHE_FIND_DEFAULT_PATH "Whether to find the library in default system library path" OFF)
option(STARCACHE_SKIP_INSTALL "Skip installing the output files after building the starcache" OFF)
# set CMAKE_BUILD_TYPE
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RELEASE)
endif()
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
message(STATUS "Build type is ${CMAKE_BUILD_TYPE}")
# support to pass cxx flags from environment.
set(CXX_COMMON_FLAGS "${CXX_COMMON_FLAGS} $ENV{STARCACHE_CXX_COMMON_FLAGS}")
# For CMAKE_BUILD_TYPE=Debug
# -ggdb: Enable gdb debugging
# Debug information is stored as dwarf2 to be as compatible as possible
# -Werror: compile warnings should be errors when using the toolchain compiler.
# Only enable for debug builds because this is what we test in pre-commit tests.
set(CXX_FLAGS_DEBUG "-ggdb -O0 -gdwarf-4 -DDEBUG")
# For CMAKE_BUILD_TYPE=Release
# -O3: Enable all compiler optimizations
# -DNDEBUG: Turn off dchecks/asserts/debug only code.
# -gdwarf-4: Debug information is stored as dwarf2 to be as compatible as possible
set(CXX_FLAGS_RELEASE "-O3 -gdwarf-4 -DNDEBUG")
# For CMAKE_BUILD_TYPE=RelWithDebInfo
set(CXX_FLAGS_RELWITHDEBINFO "-O2 -ggdb -gdwarf-4 -DNDEBUG")
# For CMAKE_BUILD_TYPE=Asan
SET(CXX_FLAGS_ASAN "-ggdb3 -O0 -gdwarf-4 -fsanitize=address -DADDRESS_SANITIZER")
# Set compile flags based on the build type.
if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS_DEBUG}")
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RELEASE")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS_RELEASE}")
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "ASAN")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS_ASAN}")
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RELWITHDEBINFO")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS_RELWITHDEBINFO}")
else()
message(FATAL_ERROR "Unknown build type: ${CMAKE_BUILD_TYPE}")
endif()
# Add flags that are common across build types
SET(CMAKE_CXX_FLAGS "${CXX_COMMON_FLAGS} ${CMAKE_CXX_FLAGS}")
message(STATUS "Compiler Flags: ${CMAKE_CXX_FLAGS}")
if ("${STARCACHE_INSTALL_DIR}" STREQUAL "")
set(STARCACHE_INSTALL_DIR ${CMAKE_CURRENT_LIST_DIR}/installed)
endif()
set(CMAKE_INSTALL_PREFIX ${STARCACHE_INSTALL_DIR})
set(CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/lib)
set(CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/include)
#### External Dependencies ####
FUNCTION(FIND_LIBRARY_IN_PATH RESULT LIB_FILE LIB_NAME LIB_PATH)
if(STARCACHE_FIND_DEFAULT_PATH)
find_library(${RESULT} NAMES ${LIB_FILE} ${LIB_NAME} PATHS ${LIB_PATH}/lib)
else()
find_library(${RESULT} NAMES ${LIB_FILE} ${LIB_NAME} PATHS ${LIB_PATH}/lib NO_DEFAULT_PATH)
endif()
if (NOT ${RESULT})
if(STARCACHE_FIND_DEFAULT_PATH)
find_library(${RESULT} NAMES ${LIB_FILE} ${LIB_NAME} PATHS ${LIB_PATH}/lib64)
else()
find_library(${RESULT} NAMES ${LIB_FILE} ${LIB_NAME} PATHS ${LIB_PATH}/lib64 NO_DEFAULT_PATH)
endif()
endif()
ENDFUNCTION()
FUNCTION(SEARCH_LIBRARY RESULT LIB_FILE LIB_NAME LIB_ROOT)
if ("${LIB_ROOT}" STREQUAL "")
set(SEARCH_PATH ${STARCACHE_THIRDPARTY_DIR})
else()
set(SEARCH_PATH ${LIB_ROOT})
endif()
if ("${SEARCH_PATH}" STREQUAL "")
message(ERROR " The 'STARCACHE_THIRDPARTY_DIR' or the ${LIB_NAME} root path need to be set to find the library!")
endif()
FIND_LIBRARY_IN_PATH(${RESULT} ${LIB_FILE} ${LIB_NAME} "${SEARCH_PATH}")
if (${RESULT})
message(STATUS "find library ${LIB_NAME}")
if ((NOT "${LIB_ROOT}" STREQUAL "") AND (EXISTS "${LIB_ROOT}/include"))
include_directories(${LIB_ROOT}/include)
endif()
else()
message(ERROR "cannot find library ${LIB_NAME}")
endif()
ENDFUNCTION()
## PROTOBUF
SEARCH_LIBRARY(PROTOBUF_LIBPROTOBUF libprotobuf.a protobuf "${PROTOBUF_ROOT}")
## GFLAGS
SEARCH_LIBRARY(GFLAGS_LIB libgflags.a gflags "${GFLAGS_ROOT}")
## GLOG
SEARCH_LIBRARY(GLOG_LIB libglog.a glog "${GLOG_ROOT}")
## BRPC
SEARCH_LIBRARY(BRPC_LIB libbrpc.a brpc "${BRPC_ROOT}")
## SSL
SEARCH_LIBRARY(SSL_LIB libssl.a ssl "${SSL_ROOT}")
SEARCH_LIBRARY(CRYPTO_LIB libcrypto.a crypto "${SSL_ROOT}")
## FMT
SEARCH_LIBRARY(FMT_LIB libfmt.a fmt "${FMT_ROOT}")
## BOOST
if ("${BOOST_ROOT}" STREQUAL "")
set(BOOST_ROOT ${STARCACHE_THIRDPARTY_DIR})
endif()
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)
set(Boost_DEBUG FALSE)
set(Boost_USE_MULTITHREADED ON)
set(Boost_NO_BOOST_CMAKE ON)
# boost suppress warning is supported on cmake 3.20
# https://cmake.org/cmake/help/latest/module/FindBoost.html
set(Boost_NO_WARN_NEW_VERSIONS ON)
find_package(Boost 1.75.0 REQUIRED COMPONENTS thread regex program_options filesystem context)
message(STATUS " include: ${Boost_INCLUDE_DIRS}")
message(STATUS " libs: ${Boost_LIBRARIES}")
include_directories(${Boost_INCLUDE_DIRS})
set(BOOST_LIB ${Boost_LIBRARIES})
## include third_party/include
message(STATUS "STARCACHE_THIRDPARTY_DIR: ${STARCACHE_THIRDPARTY_DIR}")
include_directories(${STARCACHE_THIRDPARTY_DIR}/include)
#### STARCACHE PROJECT ####
## GLOBAL VARAIBLES
set(STARCACHE_SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/src)
set(STARCACHE_TEST_DIR ${CMAKE_CURRENT_LIST_DIR}/test)
include_directories(BEFORE ${STARCACHE_SRC_DIR})
## Common
set(STARCACHE_COMMON_SRCS
${STARCACHE_SRC_DIR}/common/config.cpp
)
add_library(starcache_common_objs OBJECT ${STARCACHE_COMMON_SRCS})
## Utils
set(STARCACHE_UTILS_SRCS
${STARCACHE_SRC_DIR}/utils/murmur_hash3.cpp
${STARCACHE_SRC_DIR}/utils/lru_container.cpp
)
add_library(starcache_utils_objs OBJECT ${STARCACHE_UTILS_SRCS})
## StarCache core source
set(STARCACHE_CORE_SRCS
${STARCACHE_SRC_DIR}/aux_funcs.cpp
${STARCACHE_SRC_DIR}/block_file.cpp
${STARCACHE_SRC_DIR}/disk_cache.cpp
${STARCACHE_SRC_DIR}/disk_space_manager.cpp
${STARCACHE_SRC_DIR}/hashtable_access_index.cpp
${STARCACHE_SRC_DIR}/mem_cache.cpp
${STARCACHE_SRC_DIR}/mem_space_manager.cpp
${STARCACHE_SRC_DIR}/size_based_admission_policy.cpp
${STARCACHE_SRC_DIR}/capacity_based_promotion_policy.cpp
${STARCACHE_SRC_DIR}/star_cache.cpp
${STARCACHE_SRC_DIR}/star_cache_impl.cpp
)
add_library(starcache_core_objs OBJECT ${STARCACHE_CORE_SRCS})
### Build target libraries
## libstarcache.a
add_library(starcache STATIC
$<TARGET_OBJECTS:starcache_common_objs>
$<TARGET_OBJECTS:starcache_utils_objs>
$<TARGET_OBJECTS:starcache_core_objs>
)
set(STARCACHE_BASIC_LIBS
-lc -lm -ldl -rdynamic -pthread -static-libstdc++ -static-libgcc
)
if ("${CMAKE_BUILD_TYPE}" STREQUAL "ASAN")
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(STARCACHE_BASIC_LIBS "${STARCACHE_BASIC_LIBS} -static-libsan")
else()
set(STARCACHE_BASIC_LIBS "${STARCACHE_BASIC_LIBS} -static-libasan")
endif()
endif()
target_link_libraries(starcache
-Wl,--start-group
${STARCACHE_BASIC_LIBS}
${PROTOBUF_LIBPROTOBUF}
${SSL_LIB}
${CRYPTO_LIB}
${BOOST_LIB}
${GLOG_LIB}
${GFLAGS_LIB}
${BRPC_LIB}
${FMT_LIB}
-Wl,--end-group
)
### BINARY EXECUTABLE
# tools
if(STARCACHE_WITH_TOOLS)
set(STARCACHE_TOOLS_SRCS
${STARCACHE_SRC_DIR}/tools/starcache_tester.cpp
)
add_executable(starcache_tester ${STARCACHE_TOOLS_SRCS})
target_link_libraries(starcache_tester starcache)
endif()
### TEST SECTION
if(STARCACHE_WITH_TESTS)
if(WITH_COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage")
endif()
unset(CXX_INCLUDES)
SEARCH_LIBRARY(GTEST_LIB libgtest.a gtest "${GTEST_ROOT}")
# TEST LIBRARY
set(STARCACHE_TEST_SRCS
${STARCACHE_TEST_DIR}/star_cache_test.cpp
)
add_definitions(-DUNIT_TEST)
add_executable(starcache_test ${STARCACHE_TEST_SRCS})
# Allow includes from source file location for testing
target_include_directories(starcache_test PRIVATE ${STARCACHE_SRC_DIR})
target_link_libraries(starcache_test
starcache
${GTEST_LIB}
)
endif()
### INSTALLATION SECTION
if(STARCACHE_SKIP_INSTALL)
message(STATUS "Skip installing starcache build output.")
else()
include(cmake/install.cmake)
endif()