cmake_minimum_required(VERSION 3.10) set(CMAKE_CXX_STANDARD 20) 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) SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) OPTION(ENABLE_ASAN "Enable build with address sanitizer" ON) OPTION(WITH_UNIT_TESTS "Compile miniob with unit tests" ON) OPTION(CONCURRENCY "Support concurrency operations" OFF) MESSAGE(STATUS "HOME dir: $ENV{HOME}") #SET(ENV{变量名} 值) IF(WIN32) MESSAGE(STATUS "This is windows.") ADD_DEFINITIONS(-DWIN32) ELSEIF(WIN64) MESSAGE(STATUS "This is windows.") ADD_DEFINITIONS(-DWIN64) 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 SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -Wall -Werror") IF(DEBUG) MESSAGE(STATUS "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(STATUS "Disable debug") SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -O2 -g ") ELSE() MESSAGE(STATUS "Enable debug") SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -O0 -g -DDEBUG") ADD_DEFINITIONS(-DENABLE_DEBUG) ENDIF(DEBUG) IF (CONCURRENCY) MESSAGE(STATUS "CONCURRENCY is ON") SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -DCONCURRENCY") ADD_DEFINITIONS(-DCONCURRENCY) ENDIF (CONCURRENCY) MESSAGE(STATUS "CMAKE_CXX_COMPILER_ID is " ${CMAKE_CXX_COMPILER_ID}) IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") ADD_LINK_OPTIONS(-static-libgcc -static-libstdc++) ENDIF() IF (ENABLE_ASAN) SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fno-omit-frame-pointer -fsanitize=address") IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") ADD_LINK_OPTIONS(-static-libasan) ENDIF() ENDIF() IF (CMAKE_INSTALL_PREFIX) MESSAGE(STATUS "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(STATUS "Install target dir is " ${CMAKE_INSTALL_PREFIX}) 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) # ADD_SUBDIRECTORY(src bin) bin 为目标目录, 可以省略 ADD_SUBDIRECTORY(deps) ADD_SUBDIRECTORY(src/obclient) ADD_SUBDIRECTORY(src/observer) ADD_SUBDIRECTORY(test/perf) ADD_SUBDIRECTORY(benchmark) ADD_SUBDIRECTORY(tools) IF(WITH_UNIT_TESTS) SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fprofile-arcs -ftest-coverage") enable_testing() ADD_SUBDIRECTORY(unittest) 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}) INSTALL(DIRECTORY etc DESTINATION . FILE_PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ) #INSTALL([[SCRIPT ] [CODE ]] [...]) # script 表示安装时,调用cmake 脚步 # code 表示安装时,执行cmake 命令, 例如 INSTALL(CODE "MESSAGE(\"Sample install message.\")") # EXEC_PROGRAM make时执行命令 #EXEC_PROGRAM(Executable [directory in which to run] # [ARGS ] # [OUTPUT_VARIABLE ] #EXEC_PROGRAM(Executable [directory in which to run] # [ARGS ] # [OUTPUT_VARIABLE ] # [RETURN_VALUE ]) # 生产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)