CMakeLists.txt 4.3 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
cmake_minimum_required(VERSION 3.10)
2
set(CMAKE_CXX_STANDARD 20)
羽飞's avatar
羽飞 已提交
3 4 5 6 7 8 9 10

project(minidb)

MESSAGE(STATUS "This is Project source dir " ${PROJECT_SOURCE_DIR})
MESSAGE(STATUS "This is PROJECT_BINARY_DIR dir " ${PROJECT_BINARY_DIR})

SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

11 12 13
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)

OPTION(ENABLE_ASAN "Enable build with address sanitizer" ON)
14 15 16
OPTION(WITH_UNIT_TESTS "Compile miniob with unit tests" ON)
OPTION(CONCURRENCY "Support concurrency operations" OFF)

羽飞's avatar
羽飞 已提交
17 18 19 20 21 22
MESSAGE(STATUS "HOME dir: $ENV{HOME}")
#SET(ENV{变量名} 值)
IF(WIN32)
    MESSAGE(STATUS "This is windows.")
    ADD_DEFINITIONS(-DWIN32)
ELSEIF(WIN64)
23 24
    MESSAGE(STATUS "This is windows.")
    ADD_DEFINITIONS(-DWIN64)
羽飞's avatar
羽飞 已提交
25 26 27 28 29 30 31 32 33 34 35 36
ELSEIF(APPLE)
    MESSAGE(STATUS "This is apple")
    # normally __MACH__ has already been defined
    ADD_DEFINITIONS(-D__MACH__ )
ELSEIF(UNIX)
    MESSAGE(STATUS "This is UNIX")
    ADD_DEFINITIONS(-DUNIX -DLINUX)
ELSE()
    MESSAGE(STATUS "This is UNKNOW OS")
ENDIF(WIN32)

# This is for clangd plugin for vscode
羽飞's avatar
羽飞 已提交
37
SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -Wall -Werror")
羽飞's avatar
羽飞 已提交
38
IF(DEBUG)
羽飞's avatar
羽飞 已提交
39
    MESSAGE(STATUS "DEBUG has been set as TRUE ${DEBUG}")
羽飞's avatar
羽飞 已提交
40 41 42
    SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS}  -O0 -g -DDEBUG ")
    ADD_DEFINITIONS(-DENABLE_DEBUG)
ELSEIF(NOT DEFINED ENV{DEBUG})
羽飞's avatar
羽飞 已提交
43
    MESSAGE(STATUS "Disable debug")
羽飞's avatar
羽飞 已提交
44 45
    SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS}  -O2 -g ")
ELSE()
羽飞's avatar
羽飞 已提交
46
    MESSAGE(STATUS "Enable debug")
羽飞's avatar
羽飞 已提交
47 48
    SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS}  -O0 -g -DDEBUG")
    ADD_DEFINITIONS(-DENABLE_DEBUG)
49 50 51
ENDIF(DEBUG)

IF (CONCURRENCY)
羽飞's avatar
羽飞 已提交
52
    MESSAGE(STATUS "CONCURRENCY is ON")
53 54 55
    SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -DCONCURRENCY")
    ADD_DEFINITIONS(-DCONCURRENCY)
ENDIF (CONCURRENCY)
羽飞's avatar
羽飞 已提交
56 57 58

MESSAGE(STATUS "CMAKE_CXX_COMPILER_ID is " ${CMAKE_CXX_COMPILER_ID})
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
羽飞's avatar
羽飞 已提交
59
    ADD_LINK_OPTIONS(-static-libgcc -static-libstdc++)
羽飞's avatar
羽飞 已提交
60
ENDIF()
羽飞's avatar
羽飞 已提交
61

62
IF (ENABLE_ASAN)
羽飞's avatar
羽飞 已提交
63
    SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
羽飞's avatar
羽飞 已提交
64
    IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
羽飞's avatar
羽飞 已提交
65
        ADD_LINK_OPTIONS(-static-libasan)
羽飞's avatar
羽飞 已提交
66
    ENDIF()
67
ENDIF()
羽飞's avatar
羽飞 已提交
68 69

IF (CMAKE_INSTALL_PREFIX)
羽飞's avatar
羽飞 已提交
70
    MESSAGE(STATUS "CMAKE_INSTALL_PREFIX has been set as " ${CMAKE_INSTALL_PREFIX} )
羽飞's avatar
羽飞 已提交
71 72 73 74 75
ELSEIF(DEFINED ENV{CMAKE_INSTALL_PREFIX})
    SET(CMAKE_INSTALL_PREFIX $ENV{CMAKE_INSTALL_PREFIX})
ELSE()
    SET(CMAKE_INSTALL_PREFIX /tmp/${PROJECT_NAME})
ENDIF()
羽飞's avatar
羽飞 已提交
76
MESSAGE(STATUS "Install target dir is " ${CMAKE_INSTALL_PREFIX})
羽飞's avatar
羽飞 已提交
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
IF (DEFINED ENV{LD_LIBRARY_PATH})
    SET(LD_LIBRARY_PATH_STR $ENV{LD_LIBRARY_PATH})
    string(REPLACE ":" ";" LD_LIBRARY_PATH_LIST ${LD_LIBRARY_PATH_STR})
    MESSAGE(" Add LD_LIBRARY_PATH to -L flags " ${LD_LIBRARY_PATH_LIST})
    LINK_DIRECTORIES(${LD_LIBRARY_PATH_LIST})
ENDIF ()

IF (EXISTS /usr/local/lib)
    LINK_DIRECTORIES (/usr/local/lib)
ENDIF ()
IF (EXISTS /usr/local/lib64)
    LINK_DIRECTORIES (/usr/local/lib64)
ENDIF ()

INCLUDE_DIRECTORIES(. ${PROJECT_SOURCE_DIR}/deps /usr/local/include)
羽飞's avatar
羽飞 已提交
93 94 95

# ADD_SUBDIRECTORY(src bin)  bin 为目标目录, 可以省略
ADD_SUBDIRECTORY(deps)
96 97
ADD_SUBDIRECTORY(src/obclient)
ADD_SUBDIRECTORY(src/observer)
羽飞's avatar
羽飞 已提交
98
ADD_SUBDIRECTORY(test/perf)
99
ADD_SUBDIRECTORY(benchmark)
100
ADD_SUBDIRECTORY(tools)
Q
Qi Wang 已提交
101 102

IF(WITH_UNIT_TESTS)
羽飞's avatar
羽飞 已提交
103
    SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fprofile-arcs -ftest-coverage")
羽飞's avatar
羽飞 已提交
104
    enable_testing()
105
    ADD_SUBDIRECTORY(unittest)
羽飞's avatar
羽飞 已提交
106 107 108 109 110
ENDIF(WITH_UNIT_TESTS)

SET(CMAKE_CXX_FLAGS ${CMAKE_COMMON_FLAGS})
SET(CMAKE_C_FLAGS ${CMAKE_COMMON_FLAGS})
MESSAGE(STATUS "CMAKE_CXX_FLAGS is " ${CMAKE_CXX_FLAGS})
羽飞's avatar
羽飞 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

INSTALL(DIRECTORY etc DESTINATION .
		FILE_PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)

#INSTALL([[SCRIPT <file>] [CODE <code>]] [...])
# script 表示安装时,调用cmake 脚步
# code 表示安装时,执行cmake 命令, 例如
INSTALL(CODE "MESSAGE(\"Sample install message.\")")
# EXEC_PROGRAM make时执行命令
#EXEC_PROGRAM(Executable [directory in which to run]
#        [ARGS <arguments to executable>]
#        [OUTPUT_VARIABLE <var>]

#EXEC_PROGRAM(Executable [directory in which to run]
#        [ARGS <arguments to executable>]
#        [OUTPUT_VARIABLE <var>]
#        [RETURN_VALUE <var>])
# 生产make时,执行
#EXEC_PROGRAM(ls ARGS "*.c" OUTPUT_VARIABLE LS_OUTPUT RETURN_VALUE
#        LS_RVALUE)
#IF(not LS_RVALUE)
#    MESSAGE(STATUS "ls result: " ${LS_OUTPUT})
#ENDIF(not LS_RVALUE)