diff options
author | Manuel Bentele | 2020-10-16 17:15:49 +0200 |
---|---|---|
committer | Manuel Bentele | 2020-10-16 17:15:49 +0200 |
commit | 969496f15e1e0359e26c2c6e995ad4ef82720f86 (patch) | |
tree | 358466216630eb10e8ee91b9ede490424f6db848 /CMakeLists.txt | |
parent | [FUSE] turn on compiler optimization to fix warning (diff) | |
download | dnbd3-969496f15e1e0359e26c2c6e995ad4ef82720f86.tar.gz dnbd3-969496f15e1e0359e26c2c6e995ad4ef82720f86.tar.xz dnbd3-969496f15e1e0359e26c2c6e995ad4ef82720f86.zip |
[BUILD] rewrite CMake build system to track changes of source files
This change restructures the source code directories, separates shared
form non-shared application code and adds CMake dependencies. These
dependencies allow the tracking of changes and trigger a rebuild of
those build targets where changed files are involved.
WARNING: Note that the support of the DNBD3_SERVER_AFL build option is
not supported yet. Thus, the option should be never turned on.
Diffstat (limited to 'CMakeLists.txt')
-rw-r--r-- | CMakeLists.txt | 378 |
1 files changed, 101 insertions, 277 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 079fd9b..a1a91aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,279 +1,103 @@ -################################################################################ -# GENERAL # -################################################################################ - -PROJECT(dnbd3 C) -CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12) -IF (CMAKE_BUILD_TYPE STREQUAL "") - SET(CMAKE_BUILD_TYPE Debug) -ENDIF() - -SET(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Path prefix for system installation") -OPTION(BUILD_FUSE_CLIENT "Build dnbd3 fuse client" ON) -OPTION(BUILD_SERVER "Build dnbd3 server" ON) -OPTION(BUILD_SERVER_FUSE "Enable FUSE-Integration for dnbd3 server" OFF) -OPTION(BUILD_STRESSTEST "Build dnbd3 stress testing tool" OFF) -SET(EXTRA_C_FLAGS "" CACHE STRING "Additional options to pass to compiler") -OPTION(DEBUG_LOCKS "Add lock debugging code to dnbd3-server" OFF) -OPTION(DEBUG_THREADS "Add thread debugging code to dnbd3-server" OFF) - -OPTION(SERVER_FOR_AFL "Build dnbd3-server for usage with afl-fuzz" OFF) - -# Is there a non-retarded way to check if build type is debug or release? -# When specifying, it is case insensitive, so DeBuG would also enable debug builds, -# but in cmake, we can only do case sensitive matches... :/ -string( TOLOWER "${CMAKE_BUILD_TYPE}" bt_lower ) -if (NOT bt_lower MATCHES "^(debug|release)$") - message( FATAL_ERROR "Build type needs to be either Debug or Release" ) -endif() - -message( "Build Type selected: ${CMAKE_BUILD_TYPE}" ) - -IF(CMAKE_SYSTEM_NAME MATCHES "BSD") - message("Detected *BSD System: disable build of Kernel Module.") - SET(BUILD_KERNEL_MODULE False) -ELSE() - OPTION(BUILD_KERNEL_MODULE "Build the dnbd3 Linux kernel module" ON) -ENDIF() - -INCLUDE(CheckCCompilerFlag) -macro (TRY_ADD_FLAG _FLAG) - UNSET(TMP_TEST CACHE) - CHECK_C_COMPILER_FLAG("${_FLAG}" TMP_TEST) - if (TMP_TEST) - message(":-) Compiler supports ${_FLAG}") - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FLAG}") - else() - message(":-( Compiler does not support ${_FLAG}") - endif() - -endmacro() - -# Common for gcc and clang -#SET(CMAKE_EXE_LINKER_FLAGS "-Wl,-z,relro,-z,now,-z,defs -pie") -SET(CMAKE_C_FLAGS_RELEASE "-DNDEBUG") -SET(CMAKE_C_FLAGS "-O2 -std=c11 -Wno-multichar -fno-strict-aliasing -D_GNU_SOURCE -D_FORTIFY_SOURCE=2 ${EXTRA_C_FLAGS}") -# Hardening. Try as much as is possible. -#TRY_ADD_FLAG("-mmitigate-rop") -#TRY_ADD_FLAG("-fstack-protector-strong") -#TRY_ADD_FLAG("-fstack-clash-protection") -#TRY_ADD_FLAG("-Wformat") -#TRY_ADD_FLAG("-Wformat-security") -#TRY_ADD_FLAG("-Werror=format-security") -if(CMAKE_C_COMPILER MATCHES "clang") - message( "Using clang flags." ) - SET(CMAKE_C_FLAGS_DEBUG " -Og -fno-omit-frame-pointer -g -Wall -Wextra -Wpedantic -Wno-unused-result -D_DEBUG") -elseif (CMAKE_C_COMPILER MATCHES "(cc-)|(cc$)") - message( "Using (g)cc flags." ) - SET(CMAKE_C_FLAGS_DEBUG " -Og -g -Wall -Wextra -Wpedantic -Wconversion -Wno-sign-conversion -D_DEBUG") -else() - message( FATAL_ERROR "Could not determine compiler type." ) -endif() - -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") - -ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64) -ADD_DEFINITIONS(-DWITH_IPV6) -ADD_DEFINITIONS(-DBUILD_TYPE=${CMAKE_BUILD_TYPE}) -if (DEBUG_LOCKS) - ADD_DEFINITIONS(-DDEBUG_LOCKS) -ENDIF() -if (DEBUG_THREADS) - ADD_DEFINITIONS(-DDEBUG_THREADS) -ENDIF() - -FIND_PACKAGE(Threads) -if(BUILD_SERVER_FUSE OR BUILD_FUSE_CLIENT) - FIND_PACKAGE(Fuse) - message( " FUSE: ${FUSE_VERSION} " ) -endif() - -SET(DO_ABORT False) - -message( " *************************************************" ) -if(BUILD_FUSE_CLIENT) - if(NOT FUSE_FOUND) - message( " *** No fuse dev libs found, can't build dnbd3-fuse" ) - SET(DO_ABORT True) - endif() - if(NOT THREADS_FOUND) - message( " *** No threads found, can't build dnbd3-fuse" ) - SET(DO_ABORT True) - endif() -endif() -if(BUILD_SERVER) - FIND_PACKAGE(Jansson) - if(NOT THREADS_FOUND) - message( " *** No threads found, can't build dnbd3-server" ) - SET(DO_ABORT True) - endif() - if(NOT JANSSON_FOUND) - message( " *** No jansson lib found, can't build dnbd3-server" ) - SET(DO_ABORT True) - endif() - if(BUILD_SERVER_FUSE AND NOT FUSE_FOUND) - message( " *** No fuse dev libs found, can't build dnbd3-server with fuse support" ) - SET(DO_ABORT True) - endif() -endif() -if(BUILD_STRESSTEST) - if(NOT THREADS_FOUND) - message( " *** No threads found, can't build dnbd3-bench" ) - SET(DO_ABORT True) - endif() -endif() -message( " *************************************************" ) -if(DO_ABORT) - message( FATAL_ERROR "Aborting." ) -endif() - -#SET(FUSE_INCLUDE_DIR "") -#SET(JANSSON_INCLUDE_DIR "") - -################################################################################ -# VERSION HEADER # -################################################################################ - -FILE(WRITE ${CMAKE_BINARY_DIR}/version.cmake -"EXECUTE_PROCESS( - COMMAND \${CMD} - OUTPUT_VARIABLE VERSION - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - CONFIGURE_FILE(\${SRC} \${DST} @ONLY) -") -ADD_CUSTOM_COMMAND(OUTPUT fake-file.c ${CMAKE_BINARY_DIR}/generated/version.c - COMMAND - ${CMAKE_COMMAND} -D SRC=${CMAKE_SOURCE_DIR}/src/version.c.in - -D DST=${CMAKE_BINARY_DIR}/generated/version.c - -D CMD=${CMAKE_SOURCE_DIR}/get-version.sh - -P ${CMAKE_BINARY_DIR}/version.cmake -) -ADD_CUSTOM_TARGET(version ALL DEPENDS fake-file.c) - -################################################################################ -# CLIENT # -################################################################################ - -if(BUILD_KERNEL_MODULE) - FILE(GLOB_RECURSE CLIENT_SRCS src/client/*.c) - ADD_EXECUTABLE(dnbd3-client ${CMAKE_BINARY_DIR}/generated/version.c ${CLIENT_SRCS}) - TARGET_LINK_LIBRARIES(dnbd3-client) - ADD_DEPENDENCIES(dnbd3-client version) - INSTALL(TARGETS dnbd3-client RUNTIME DESTINATION sbin) -ENDIF() - - -################################################################################ -# SERVER # -################################################################################ - -if(BUILD_SERVER) - IF(SERVER_FOR_AFL) - message(" ######################## Building server for AFL mode - will be useless otherwise!") - ADD_DEFINITIONS(-DAFL_MODE) - ENDIF() - FILE(GLOB SERVER_SRCS src/server/*.c src/shared/*.c src/server/picohttpparser/*.c) - ADD_EXECUTABLE(dnbd3-server ${CMAKE_BINARY_DIR}/generated/version.c ${SERVER_SRCS}) - TARGET_INCLUDE_DIRECTORIES(dnbd3-server PRIVATE ${JANSSON_INCLUDE_DIR}) - TARGET_LINK_LIBRARIES(dnbd3-server ${CMAKE_THREAD_LIBS_INIT} ${JANSSON_LIBRARIES}) - if(BUILD_SERVER_FUSE) - TARGET_LINK_LIBRARIES(dnbd3-server ${FUSE_LIBRARIES}) - ADD_DEFINITIONS(-DBUILD_SERVER_FUSE) - TARGET_INCLUDE_DIRECTORIES(dnbd3-server PRIVATE ${FUSE_INCLUDE_DIRS}) - endif() - if(UNIX AND NOT APPLE) - target_link_libraries(dnbd3-server rt) - endif() - ADD_DEPENDENCIES(dnbd3-server version) - INSTALL(TARGETS dnbd3-server RUNTIME DESTINATION sbin) +cmake_minimum_required(VERSION 3.10) + +# include CMake macros +set(PROJECT_MODULES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} + ${PROJECT_MODULES_DIR}) + +# define root CMake project +project(dnbd3 + DESCRIPTION "dnbd3 Linux kernel module, server, clients and utilities" + LANGUAGES C) + +# define project options to define build configuration +OPTION(DNBD3_KERNEL_MODULE "Build the dnbd3 Linux kernel module" ON) +OPTION(DNBD3_SERVER_FUSE "Enable FUSE-Integration for dnbd3-server" OFF) +OPTION(DNBD3_SERVER_AFL "Build dnbd3-server for usage with afl-fuzz" OFF) +OPTION(DNBD3_SERVER_DEBUG_LOCKS "Add lock debugging code to dnbd3-server" OFF) +OPTION(DNBD3_SERVER_DEBUG_THREADS "Add thread debugging code to dnbd3-server" OFF) +OPTION(DNBD3_RELEASE_HARDENING "Harden dnbd3 programs in Release build configuration" OFF) + +# set supported build configurations +set(CMAKE_CONFIGURATION_TYPES Debug Release) + +# set compilation in debug mode as default configuration +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Debug) + message(STATUS "Build type is not set. Defaulting to ${CMAKE_BUILD_TYPE} build!") endif() - - -################################################################################ -# FUSE # -################################################################################ - -if(BUILD_FUSE_CLIENT) - FILE(GLOB FUSE_SRCS src/fuse/*.c src/shared/*.c) - ADD_EXECUTABLE(dnbd3-fuse ${FUSE_SRCS}) - TARGET_INCLUDE_DIRECTORIES(dnbd3-fuse PRIVATE ${FUSE_INCLUDE_DIRS}) - TARGET_LINK_LIBRARIES(dnbd3-fuse ${CMAKE_THREAD_LIBS_INIT} ${FUSE_LIBRARIES}) - ADD_DEPENDENCIES(dnbd3-fuse version) - INSTALL(TARGETS dnbd3-fuse RUNTIME DESTINATION bin) -endif() - -################################################################################ -# STRESSTEST # -################################################################################ - -if(BUILD_STRESSTEST) - FILE(GLOB BENCH_SRCS src/bench/*.c src/shared/*.c) - ADD_EXECUTABLE(dnbd3-bench ${BENCH_SRCS}) - TARGET_LINK_LIBRARIES(dnbd3-bench ${CMAKE_THREAD_LIBS_INIT}) - ADD_DEPENDENCIES(dnbd3-bench version) - INSTALL(TARGETS dnbd3-bench RUNTIME DESTINATION bin) -endif() - -################################################################################ -# MODULE # -################################################################################ - -IF(BUILD_KERNEL_MODULE) - SET(MODULE_NAME dnbd3) - SET(MODULE_FILE ${MODULE_NAME}.ko) - FILE(GLOB MODULE_SOURCE_FILES src/kernel/*.c src/serialize.c) - FILE(GLOB MODULE_HEADER_FILES src/kernel/*.h src/*.h) - - SET(KERNEL_DIR "" CACHE PATH "Path to kernel sources to compile against") - IF(KERNEL_DIR STREQUAL "") - SET(KERNEL_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/build") - ENDIF() - - SET(KERNEL_C_FLAGS "" CACHE STRING "Additional C flags to be used for building the kernel module") - IF(CMAKE_BUILD_TYPE MATCHES Debug) - SET(KERNEL_C_FLAGS "-g -DDEBUG") - ENDIF() - - SET(KBUILD_COMMAND ${CMAKE_MAKE_PROGRAM} -C ${KERNEL_DIR} - M=${CMAKE_BINARY_DIR} modules - EXTRA_CFLAGS=${KERNEL_C_FLAGS} - ) - - CONFIGURE_FILE(Kbuild.in ${CMAKE_BINARY_DIR}/Kbuild) - - FOREACH(MODULE_SOURCE_FILE ${MODULE_SOURCE_FILES}) - CONFIGURE_FILE(${MODULE_SOURCE_FILE} ${CMAKE_BINARY_DIR} COPYONLY) - ENDFOREACH( MODULE_SOURCE_FILE ) - - FOREACH(MODULE_HEADER_FILE ${MODULE_HEADER_FILES}) - CONFIGURE_FILE(${MODULE_HEADER_FILE} ${CMAKE_BINARY_DIR} COPYONLY) - ENDFOREACH( MODULE_HEADER_FILE ) - - ADD_CUSTOM_COMMAND( - OUTPUT ${CMAKE_BINARY_DIR}/${MODULE_FILE} - COMMAND ${KBUILD_COMMAND} - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} - DEPENDS ${MODULE_SOURCE_FILES} Kbuild.in - VERBATIM - ) - - ADD_CUSTOM_TARGET(${MODULE_NAME} ALL DEPENDS ${CMAKE_BINARY_DIR}/${MODULE_FILE}) - - INSTALL(FILES ${CMAKE_BINARY_DIR}/${MODULE_NAME}.ko - DESTINATION /lib/modules/${CMAKE_SYSTEM_VERSION}/kernel/drivers/block - PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ - ) - - INSTALL(CODE "EXECUTE_PROCESS(COMMAND depmod -a)") -ENDIF() - - -# -# Other install files -# - -FILE(GLOB conf_files "${CMAKE_CURRENT_SOURCE_DIR}/conf/*") -INSTALL(FILES ${conf_files} DESTINATION /etc/dnbd3-server/sample/) - +# disable build of the dnbd3 Linux kernel module on a BSD system +if(CMAKE_SYSTEM_NAME MATCHES "BSD") + message(STATUS "Detected BSD System: Disable build of the dnbd3 Linux kernel module") + set(DNBD3_KERNEL_MODULE OFF) +endif(CMAKE_SYSTEM_NAME MATCHES "BSD") + +# set Linux kernel directories +set(KERNEL_BUILD_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/build" + CACHE PATH "Path to Linux kernel modules to compile against") +set(KERNEL_INSTALL_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/extra" + CACHE PATH "Path to install Linux kernel modules") + +# search for required packages +find_package(Git) +find_package(Threads) +find_package(Fuse) + +# abort if a required package is not available +if(NOT GIT_FOUND) + message(FATAL_ERROR "No Git found, can't determine dnbd3 project version number!") +endif(NOT GIT_FOUND) +if(NOT THREADS_FOUND) + message(FATAL_ERROR "No threads found, can't build dnbd3 project!") +endif(NOT THREADS_FOUND) +if(NOT FUSE_FOUND) + message(FATAL_ERROR "No Fuse found, can't build dnbd3 project!") +endif(NOT FUSE_FOUND) + +# include project version related macros +include(KernelVersion) +# include project version related macros +include(ProjectVersion) + +# get Linux kernel version from path +get_kernel_version(LINUX_KERNEL_VERSION ${KERNEL_BUILD_DIR}) + +# set include directories +set(PROJECT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/inc) +set(PROJECT_INCLUDE_TMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/inc) +include_directories(${PROJECT_INCLUDE_DIR}) + +# generate project version C header file from template +# exposes dnbd3-generate-version and dnbd3-version target +gen_project_version(${PROJECT_INCLUDE_DIR}/dnbd3/version.h.in ${PROJECT_INCLUDE_TMP_DIR}/dnbd3/version.h) + +# print configured settings +message(STATUS "Path to Linux kernel modules to compile against is " ${KERNEL_BUILD_DIR}) +message(STATUS "Path to install Linux kernel modules is " ${KERNEL_INSTALL_DIR}) + +# add compile option to handle files greater than 2GB on a 32bit system +add_definitions(-D_FILE_OFFSET_BITS=64) + +# define global C flags for compilation +set(CMAKE_C_FLAGS "-std=c11") + +# enable all error warnings in Debug build configuration +set(CMAKE_C_FLAGS_DEBUG "-Wall -Wextra -Wpedantic -Wconversion -Wformat -Wformat-security -Werror=format-security") +set(CMAKE_C_FLAGS_RELEASE "-Wno-error") + +# set compilation optimization +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -Og -DDEBUG") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -DNDEBUG") + +if(DNBD3_RELEASE_HARDENING) + # harden Release builds with specific C flags + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -D_FORTIFY_SOURCE=2 -fstack-protector-strong -fstack-clash-protection -mfunction-return=thunk -mindirect-branch=thunk") +endif(DNBD3_RELEASE_HARDENING) + +# add all dnbd3 related projects from the source code directory +add_subdirectory(src) + +# install config files into sample directory +file(GLOB DNBD3_CONF_FILES "${CMAKE_CURRENT_SOURCE_DIR}/conf/*") +install(FILES ${DNBD3_CONF_FILES} DESTINATION /etc/dnbd3-server/sample) |