xref: /5.5.2/forestdb/CMakeLists.txt (revision cf87722c)
1cmake_minimum_required(VERSION 2.8.8)
2project (ForestDB)
3
4IF (${CMAKE_MAJOR_VERSION} GREATER 2)
5    CMAKE_POLICY(SET CMP0042 NEW)
6ENDIF (${CMAKE_MAJOR_VERSION} GREATER 2)
7
8INCLUDE(CheckCXXCompilerFlag)
9CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
10CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
11IF (COMPILER_SUPPORTS_CXX11)
12    SET(STD_VERSION "-std=c++11")
13ELSEIF(COMPILER_SUPPORTS_CXX0X)
14    SET(STD_VERSION "-std=c++0x")
15ELSE()
16    IF (NOT WIN32)
17        MESSAGE(FATAL_ERROR "Compiler ${CMAKE_CXX_COMPILER} has no C++11 support.")
18    ENDIF()
19ENDIF()
20
21INCLUDE(CheckFunctionExists)
22INCLUDE(CheckIncludeFileCXX)
23INCLUDE(CheckIncludeFiles)
24INCLUDE(CheckLibraryExists)
25INCLUDE(CheckSymbolExists)
26INCLUDE(CheckTypeSize)
27INCLUDE(CTest)
28
29IF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
30    EXECUTE_PROCESS(COMMAND git describe --abbrev=7 --always --tags
31                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
32                    OUTPUT_VARIABLE FORESTDB_VERSION
33                    ERROR_QUIET
34                    OUTPUT_STRIP_TRAILING_WHITESPACE)
35ELSE (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
36    SET(FORESTDB_VERSION "unknown")
37ENDIF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
38
39include_directories(BEFORE ${PROJECT_SOURCE_DIR}/option)
40include_directories(BEFORE ${PROJECT_SOURCE_DIR}/utils)
41include_directories(BEFORE ${PROJECT_SOURCE_DIR}/src)
42include_directories(BEFORE ${PROJECT_SOURCE_DIR}/include)
43include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}/src)
44
45CHECK_INCLUDE_FILES("sched.h" HAVE_SCHED_H)
46
47CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/src/config.cmake.h
48                ${CMAKE_CURRENT_BINARY_DIR}/src/config.h)
49
50LIST(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/Modules")
51
52if (COUCHBASE_SERVER_BUILD)
53    ADD_DEFINITIONS(-D_DOC_COMP=1)
54    include_directories(AFTER ${SNAPPY_INCLUDE_DIR})
55
56    if (WIN32)
57       # The rest of Couchbase server use tcmalloc as the memory
58       # allocator for WIN32. Override the allocator to jemalloc
59       ADD_DEFINITIONS(-UHAVE_TCMALLOC)
60       SET(COUCHBASE_MEMORY_ALLOCATOR "jemalloc")
61       # Make sure that we define all of the variables
62       include(CouchbaseMemoryAllocator)
63    endif (WIN32)
64
65    if (MEMORY_ALLOCATOR)
66        include_directories(AFTER ${MALLOC_INCLUDE_DIR})
67    endif (MEMORY_ALLOCATOR)
68    set(DIRUTILS_LIBRARY dirutils)
69    set(GTEST_LIBRARY gtest)
70
71else(COUCHBASE_SERVER_BUILD)
72    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GNU_SOURCE -D__DEBUG")
73    if (NOT WIN32)
74        if (NOT(SNAPPY_OPTION STREQUAL "Disable"))
75            INCLUDE(FindSnappy)
76            ADD_DEFINITIONS(-D_DOC_COMP=1)
77        endif (NOT(SNAPPY_OPTION STREQUAL "Disable"))
78    endif(NOT WIN32)
79    if (_JEMALLOC EQUAL 1)
80        INCLUDE(FindJemalloc)
81    endif(_JEMALLOC EQUAL 1)
82endif(COUCHBASE_SERVER_BUILD)
83
84INCLUDE(FindAsyncIOLib)
85
86if (NOT CMAKE_BUILD_TYPE)
87    set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
88endif(NOT CMAKE_BUILD_TYPE)
89
90if(NOT LIBRT)
91    unset(LIBRT CACHE)
92endif(NOT LIBRT)
93if (NOT WIN32)
94    set ( CMAKE_CXX_FLAGS_DEBUG "-O0 -Wunused-variable -Wall -pedantic ${STD_VERSION} -Wredundant-decls")
95    set(PTHREAD_LIB pthread)
96    set(LIBM m)
97    set(FORESTDB_FILE_OPS "src/filemgr_ops_linux.cc")
98    if (NOT APPLE)
99        set(LIBRT rt)
100    endif (NOT APPLE)
101    FIND_LIBRARY(DL_LIBRARY NAMES dl)
102    if (DL_LIBRARY)
103        set(DL_LIBRARIES "${DL_LIBRARY}")
104    endif (DL_LIBRARY)
105else (NOT WIN32)
106    set(FORESTDB_FILE_OPS "src/filemgr_ops_windows.cc")
107    set(GETTIMEOFDAY_VS "${PROJECT_SOURCE_DIR}/utils/gettimeofday_vs.cc")
108endif(NOT WIN32)
109
110IF (_MVCC_WAL EQUAL 1)
111    ADD_DEFINITIONS(-D_MVCC_WAL_ENABLE=1)
112ENDIF()
113
114IF (_MEMLEAK EQUAL 1)
115    ADD_DEFINITIONS(-D_MEMLEAK_ENABLE=1)
116ELSEIF (_MEMLEAK EQUAL 2)
117    ADD_DEFINITIONS(-D_MEMLEAK_ENABLE=1)
118    ADD_DEFINITIONS(-D_CHK_MODIFY_AFTER_FREE=1)
119ENDIF()
120
121IF ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
122    ADD_DEFINITIONS(-D_LATENCY_STATS)
123ENDIF ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
124
125IF ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
126    ADD_DEFINITIONS(-D_LATENCY_STATS)
127    ADD_DEFINITIONS(-D_LATENCY_STATS_DUMP_TO_FILE)
128ENDIF ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
129
130# Comment the line below to disable compiling Copy-On-Write (COW) optimizations
131ADD_DEFINITIONS (-D_COW_COMPACTION=1)
132
133if (COUCHBASE_SERVER_BUILD)
134    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
135       # using /W0 cause msvc to print out a warning that it overrides
136       # previous settings (/W3 is added by default).
137       # Let's just disable the ones we're "violating"
138       # (we should probably fix them at some point?)
139       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4716 /wd4244 /wd4267 /wd4101 /wd4800 /wd4700 /wd4018 /wd4102")
140   endif()
141else()
142  # If building outside CB server build, set required flags for the selected compiler.
143    if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
144        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${STD_VERSION} -Qunused-arguments -g -fomit-frame-pointer -pthread")
145    elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
146        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${STD_VERSION} -g -fomit-frame-pointer -pthread")
147    elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
148       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4716 /W0")
149    endif()
150endif()
151
152# Breakpad support with Couchbase Server Build only when on Windows/Linux.
153if (COUCHBASE_SERVER_BUILD)
154    if (BREAKPAD_FOUND)
155        INCLUDE_DIRECTORIES(AFTER ${BREAKPAD_INCLUDE_DIR})
156        if (WIN32)
157            SET(BREAKPAD_SRC src/breakpad_win32.cc)
158        elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
159            SET(BREAKPAD_SRC src/breakpad_linux.cc)
160            # Breakpad has headers (which are dependencies for our breakpad
161            # integration code) that don't compile cleanly as C++11, so the
162            # GNU standard is used instead.
163            SET_SOURCE_FILES_PROPERTIES(src/breakpad_linux.cc
164                                        PROPERTIES
165                                        COMPILE_FLAGS ${CB_GNU_CXX11_OPTION})
166        else()
167            SET(BREAKPAD_SRC src/breakpad_dummy.cc)
168        endif()
169    else (BREAKPAD_FOUND)
170        SET(BREAKPAD_SRC src/breakpad_dummy.cc)
171    endif(BREAKPAD_FOUND)
172else()
173    SET(BREAKPAD_SRC src/breakpad_dummy.cc)
174endif()
175
176if (NOT COUCHBASE_SERVER_BUILD)
177   INCLUDE(ThreadSanitizer)
178endif()
179
180IF (_ENCRYPTION)
181    INCLUDE(FindEncryptionLib)
182ENDIF (_ENCRYPTION)
183
184SET(FORESTDB_CORE_SRC
185    ${PROJECT_SOURCE_DIR}/src/api_wrapper.cc
186    ${PROJECT_SOURCE_DIR}/src/avltree.cc
187    ${PROJECT_SOURCE_DIR}/src/bgflusher.cc
188    ${PROJECT_SOURCE_DIR}/src/blockcache.cc
189    ${PROJECT_SOURCE_DIR}/${BREAKPAD_SRC}
190    ${PROJECT_SOURCE_DIR}/src/btree.cc
191    ${PROJECT_SOURCE_DIR}/src/btree_kv.cc
192    ${PROJECT_SOURCE_DIR}/src/btree_str_kv.cc
193    ${PROJECT_SOURCE_DIR}/src/btree_fast_str_kv.cc
194    ${PROJECT_SOURCE_DIR}/src/btreeblock.cc
195    ${PROJECT_SOURCE_DIR}/src/checksum.cc
196    ${PROJECT_SOURCE_DIR}/src/compactor.cc
197    ${PROJECT_SOURCE_DIR}/src/configuration.cc
198    ${PROJECT_SOURCE_DIR}/src/docio.cc
199    ${PROJECT_SOURCE_DIR}/src/encryption.cc
200    ${PROJECT_SOURCE_DIR}/src/encryption_aes.cc
201    ${PROJECT_SOURCE_DIR}/src/encryption_bogus.cc
202    ${PROJECT_SOURCE_DIR}/src/fdb_errors.cc
203    ${PROJECT_SOURCE_DIR}/src/filemgr.cc
204    ${PROJECT_SOURCE_DIR}/src/filemgr_ops.cc
205    ${PROJECT_SOURCE_DIR}/src/forestdb.cc
206    ${PROJECT_SOURCE_DIR}/src/hash.cc
207    ${PROJECT_SOURCE_DIR}/src/hash_functions.cc
208    ${PROJECT_SOURCE_DIR}/src/hbtrie.cc
209    ${PROJECT_SOURCE_DIR}/src/iterator.cc
210    ${PROJECT_SOURCE_DIR}/src/kv_instance.cc
211    ${PROJECT_SOURCE_DIR}/src/list.cc
212    ${PROJECT_SOURCE_DIR}/src/staleblock.cc
213    ${PROJECT_SOURCE_DIR}/src/superblock.cc
214    ${PROJECT_SOURCE_DIR}/src/transaction.cc
215    ${PROJECT_SOURCE_DIR}/src/version.cc
216    ${PROJECT_SOURCE_DIR}/src/wal.cc)
217
218SET(FORESTDB_UTILS_SRC
219    ${PROJECT_SOURCE_DIR}/utils/crc32.cc
220    ${PROJECT_SOURCE_DIR}/utils/debug.cc
221    ${PROJECT_SOURCE_DIR}/utils/memleak.cc
222    ${PROJECT_SOURCE_DIR}/utils/partiallock.cc
223    ${PROJECT_SOURCE_DIR}/utils/system_resource_stats.cc
224    ${PROJECT_SOURCE_DIR}/utils/time_utils.cc
225    ${PROJECT_SOURCE_DIR}/utils/timing.cc)
226
227add_library(forestdb SHARED
228            ${FORESTDB_FILE_OPS}
229            ${GETTIMEOFDAY_VS}
230            ${FORESTDB_CORE_SRC}
231            ${FORESTDB_UTILS_SRC})
232target_link_libraries(forestdb ${PTHREAD_LIB} ${LIBM} ${SNAPPY_LIBRARIES}
233                      ${ASYNC_IO_LIB} ${MALLOC_LIBRARIES} ${LIBRT}
234                      ${CRYPTO_LIB}
235                      ${DL_LIBRARIES} ${BREAKPAD_LIBRARIES})
236
237# Create objects libraries for the different "tools" parts forestdb, which can be reused
238# by various targets.
239add_library(FDB_TOOLS_CORE OBJECT ${FORESTDB_CORE_SRC})
240set_target_properties(FDB_TOOLS_CORE PROPERTIES COMPILE_FLAGS "-D_FDB_TOOLS")
241
242add_library(FDB_TOOLS_UTILS OBJECT ${FORESTDB_UTILS_SRC})
243set_target_properties(FDB_TOOLS_UTILS PROPERTIES COMPILE_FLAGS "-D_FDB_TOOLS")
244
245add_executable(forestdb_dump
246               tools/dump_common.cc
247               tools/forestdb_dump.cc
248               ${FORESTDB_FILE_OPS}
249               ${GETTIMEOFDAY_VS}
250               $<TARGET_OBJECTS:FDB_TOOLS_CORE>
251               $<TARGET_OBJECTS:FDB_TOOLS_UTILS>)
252target_link_libraries(forestdb_dump ${PTHREAD_LIB} ${LIBM} ${SNAPPY_LIBRARIES}
253                      ${ASYNC_IO_LIB} ${MALLOC_LIBRARIES} ${PLATFORM_LIBRARY}
254                      ${LIBRT} ${CRYPTO_LIB}
255                      ${DL_LIBRARIES} ${BREAKPAD_LIBRARIES})
256set_target_properties(forestdb_dump PROPERTIES COMPILE_FLAGS "-D_FDB_TOOLS")
257
258add_executable(forestdb_hexamine
259               tools/dump_common.cc
260               tools/forestdb_hexamine.cc
261               ${FORESTDB_FILE_OPS}
262               ${GETTIMEOFDAY_VS}
263               $<TARGET_OBJECTS:FDB_TOOLS_CORE>
264               $<TARGET_OBJECTS:FDB_TOOLS_UTILS>)
265target_link_libraries(forestdb_hexamine ${PTHREAD_LIB} ${LIBM} ${SNAPPY_LIBRARIES}
266                      ${ASYNC_IO_LIB} ${MALLOC_LIBRARIES} ${PLATFORM_LIBRARY}
267                      ${LIBRT} ${CRYPTO_LIB}
268                      ${DL_LIBRARIES} ${BREAKPAD_LIBRARIES})
269set_target_properties(forestdb_hexamine PROPERTIES COMPILE_FLAGS "-D_FDB_TOOLS")
270
271if ((NOT COUCHBASE_SERVER_BUILD) OR (INSTALL_HEADER_FILES))
272    INSTALL(FILES
273            include/libforestdb/forestdb.h
274            include/libforestdb/fdb_types.h
275            include/libforestdb/fdb_errors.h
276            DESTINATION include/libforestdb)
277endif((NOT COUCHBASE_SERVER_BUILD) OR (INSTALL_HEADER_FILES))
278
279INSTALL(TARGETS forestdb
280        RUNTIME DESTINATION bin
281        LIBRARY DESTINATION lib
282        ARCHIVE DESTINATION lib)
283
284INSTALL(TARGETS forestdb_dump
285        RUNTIME DESTINATION bin)
286
287
288if ("${CMAKE_BUILD_TYPE}" STREQUAL "Coverage")
289    if ((NOT COUCHBASE_SERVER_BUILD) AND (NOT WIN32))
290        INCLUDE(CodeCoverage)
291        SETUP_TARGET_FOR_COVERAGE(test_coverage ctest ${PROJECT_SOURCE_DIR}/coverage)
292    endif((NOT COUCHBASE_SERVER_BUILD) AND (NOT WIN32))
293endif()
294
295if (("${CMAKE_BUILD_TYPE}" STREQUAL "Valgrind") AND (NOT WIN32))
296    INCLUDE(MemoryCheck)
297    SETUP_TARGET_FOR_MEMCHECK(test_valgrind ${PROJECT_BINARY_DIR}/fdb_functional_test)
298endif()
299
300# add top-level testing dir
301enable_testing()
302add_subdirectory("${PROJECT_SOURCE_DIR}/tests")
303
304if (COUCHBASE_SERVER_BUILD)
305    ENABLE_CODE_COVERAGE_REPORT()
306endif()
307