CMakeLists.txt 4.4 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4
# include 另外一个cmake 配置
#INCLUDE(file1 [OPTIONAL])

cmake_minimum_required(VERSION 3.10)
5
set(CMAKE_CXX_STANDARD 20)
羽飞's avatar
羽飞 已提交
6 7 8 9 10 11 12 13 14 15 16 17
#SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

project(minidb)

MESSAGE(STATUS "This is SOURCE dir " ${test_SOURCE_DIR})
MESSAGE(STATUS "This is BINARY dir " ${test_BINARY_DIR})
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)
#SET(LIBRARY_OUTPUT_PATH <路径>)

18
OPTION(ENABLE_ASAN "Enable build with address sanitizer" OFF)
19 20 21
OPTION(WITH_UNIT_TESTS "Compile miniob with unit tests" ON)
OPTION(CONCURRENCY "Support concurrency operations" OFF)

羽飞's avatar
羽飞 已提交
22 23 24 25 26 27
MESSAGE(STATUS "HOME dir: $ENV{HOME}")
#SET(ENV{变量名} 值)
IF(WIN32)
    MESSAGE(STATUS "This is windows.")
    ADD_DEFINITIONS(-DWIN32)
ELSEIF(WIN64)
28 29
    MESSAGE(STATUS "This is windows.")
    ADD_DEFINITIONS(-DWIN64)
羽飞's avatar
羽飞 已提交
30 31 32 33 34 35 36 37 38 39 40 41
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
羽飞 已提交
42
SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -Wall -Werror")
羽飞's avatar
羽飞 已提交
43 44 45 46 47 48 49 50 51 52 53
IF(DEBUG)
    MESSAGE("DEBUG has been set as TRUE ${DEBUG}")
    SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS}  -O0 -g -DDEBUG ")
    ADD_DEFINITIONS(-DENABLE_DEBUG)
ELSEIF(NOT DEFINED ENV{DEBUG})
    MESSAGE("Disable debug")
    SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS}  -O2 -g ")
ELSE()
    MESSAGE("Enable debug")
    SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS}  -O0 -g -DDEBUG")
    ADD_DEFINITIONS(-DENABLE_DEBUG)
54 55 56 57 58 59 60
ENDIF(DEBUG)

IF (CONCURRENCY)
    MESSAGE("CONCURRENCY is ON")
    SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -DCONCURRENCY")
    ADD_DEFINITIONS(-DCONCURRENCY)
ENDIF (CONCURRENCY)
羽飞's avatar
羽飞 已提交
61 62 63 64
SET(CMAKE_CXX_FLAGS ${CMAKE_COMMON_FLAGS})
SET(CMAKE_C_FLAGS ${CMAKE_COMMON_FLAGS})
MESSAGE("CMAKE_CXX_FLAGS is " ${CMAKE_CXX_FLAGS})

65 66 67 68 69
IF (ENABLE_ASAN)
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
    SET(CMAKE_LINKER_FLAGS_DEBUG  "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
ENDIF()
羽飞's avatar
羽飞 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82

IF (CMAKE_INSTALL_PREFIX)
    MESSAGE("CMAKE_INSTALL_PREFIX has been set as " ${CMAKE_INSTALL_PREFIX} )
ELSEIF(DEFINED ENV{CMAKE_INSTALL_PREFIX})
    SET(CMAKE_INSTALL_PREFIX $ENV{CMAKE_INSTALL_PREFIX})
ELSE()
    SET(CMAKE_INSTALL_PREFIX /tmp/${PROJECT_NAME})
ENDIF()
MESSAGE("Install target dir is " ${CMAKE_INSTALL_PREFIX})


# ADD_SUBDIRECTORY(src bin)  bin 为目标目录, 可以省略
ADD_SUBDIRECTORY(deps)
83 84
ADD_SUBDIRECTORY(src/obclient)
ADD_SUBDIRECTORY(src/observer)
羽飞's avatar
羽飞 已提交
85
ADD_SUBDIRECTORY(test/perf)
86
ADD_SUBDIRECTORY(benchmark)
87
ADD_SUBDIRECTORY(tools)
Q
Qi Wang 已提交
88 89

IF(WITH_UNIT_TESTS)
羽飞's avatar
羽飞 已提交
90
    enable_testing()
91
    ADD_SUBDIRECTORY(unittest)
Q
Qi Wang 已提交
92
ENDIF()
羽飞's avatar
羽飞 已提交
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

# install 准备安装的目录是cmakefile 的当前目录, 不是build 后生成的目录
# Files 默认权限OWNER_WRITE, OWNER_READ, GROUP_READ,和WORLD_READ,即644权限
# INSTALL(FILES docs/README DESTINATION ./  )
# INSTALL(DIRECTORY docs DESTINATION ./
#        PATTERN "README" EXCLUDE)
#  PERMISSIONS 可以直接替换
#INSTALL(DIRECTORY bin DESTINATION ./
#        FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_READ
#        DIRECTORY_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE)

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>]




# ADD_TEST与ENABLE_TESTING 参考书籍

#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)