diff options
author | Manuel Bentele | 2020-10-23 18:13:15 +0200 |
---|---|---|
committer | Manuel Bentele | 2020-10-23 18:13:15 +0200 |
commit | 588368dd2a46ddec70741c6cc87a79a0b26ee383 (patch) | |
tree | 69265caba9b1fb0ee126c142538b75fd49bbe8ee /cmake | |
parent | Move the source code of all xloop components to the common 'src' directory (diff) | |
download | xloop-588368dd2a46ddec70741c6cc87a79a0b26ee383.tar.gz xloop-588368dd2a46ddec70741c6cc87a79a0b26ee383.tar.xz xloop-588368dd2a46ddec70741c6cc87a79a0b26ee383.zip |
Add automatic generation of version and build type headers for compilation
This change replaces the static version and build type header file generation
by CMake with dynamic CMake targets to generate the version file whenever a Make
target is executed. Thus, there is no need anymore to reconfigure and rerun
CMake after the repository version or build configuration has changed.
Diffstat (limited to 'cmake')
-rw-r--r-- | cmake/Build.cmake | 27 | ||||
-rw-r--r-- | cmake/GenerateBuild.cmake | 10 | ||||
-rw-r--r-- | cmake/GenerateVersion.cmake | 20 | ||||
-rw-r--r-- | cmake/InstallVersionFile.cmake.in | 8 | ||||
-rw-r--r-- | cmake/Kernel.cmake | 57 | ||||
-rw-r--r-- | cmake/Version.cmake | 80 | ||||
-rw-r--r-- | cmake/version.cmake | 21 |
7 files changed, 202 insertions, 21 deletions
diff --git a/cmake/Build.cmake b/cmake/Build.cmake new file mode 100644 index 0000000..bad9755 --- /dev/null +++ b/cmake/Build.cmake @@ -0,0 +1,27 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020 Manuel Bentele <development@manuel-bentele.de> +# + +macro(gen_build_type BUILD_INPUT_FILE_TEMPLATE BUILD_OUTPUT_FILE) + get_filename_component(BUILD_OUTPUT_FILENAME ${BUILD_OUTPUT_FILE} NAME) + # command that will trigger a rebuild of build.h every time + add_custom_command(OUTPUT regenerate-build-file + COMMAND ${CMAKE_COMMAND} -E sleep 0 + COMMENT "Trigger generating ${BUILD_OUTPUT_FILENAME}") + + # call the GenerateBuild.cmake file to generate the build.h file + add_custom_command(OUTPUT ${BUILD_OUTPUT_FILE} + COMMAND ${CMAKE_COMMAND} -D BUILD_INPUT_FILE_TEMPLATE=${BUILD_INPUT_FILE_TEMPLATE} + -D BUILD_OUTPUT_FILE=${BUILD_OUTPUT_FILE} + -D BUILD_TYPE=${CMAKE_BUILD_TYPE} + -P ${PROJECT_MODULES_DIR}/GenerateBuild.cmake + COMMENT "Generating ${BUILD_OUTPUT_FILENAME}" + DEPENDS regenerate-build-file) + add_custom_target(xloop-generate-build DEPENDS ${BUILD_OUTPUT_FILE}) + + # create target to expose project build type + add_library(xloop-build INTERFACE) + target_include_directories(xloop-build INTERFACE ${PROJECT_INCLUDE_GEN_DIR}) + add_dependencies(xloop-build xloop-generate-build) +endmacro(gen_build_type BUILD_INPUT_FILE_TEMPLATE BUILD_OUTPUT_FILE) diff --git a/cmake/GenerateBuild.cmake b/cmake/GenerateBuild.cmake new file mode 100644 index 0000000..5af35d2 --- /dev/null +++ b/cmake/GenerateBuild.cmake @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020 Manuel Bentele <development@manuel-bentele.de> +# + +# set current build type of the project +set(XLOOP_BUILD ${BUILD_TYPE}) + +# write dnbd3 build type into a new C source file based on the specified build file template +configure_file(${BUILD_INPUT_FILE_TEMPLATE} ${BUILD_OUTPUT_FILE}) diff --git a/cmake/GenerateVersion.cmake b/cmake/GenerateVersion.cmake new file mode 100644 index 0000000..73b0771 --- /dev/null +++ b/cmake/GenerateVersion.cmake @@ -0,0 +1,20 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020 Manuel Bentele <development@manuel-bentele.de> +# + +# set CMake module path to include version macros +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} + ${VERSION_MODULE_PATH}) + +# include version macros +include(Version) + +# get Git version of Git repository +get_repository_version(XLOOP_VERSION ${VERSION_INPUT_FILE} ${VERSION_BUILD_TYPE}) + +# generate version header if header does not exists +if(NOT EXISTS ${VERSION_INPUT_FILE}) + # write dnbd3 version into a new C source file based on the specified version template + configure_file(${VERSION_INPUT_FILE_TEMPLATE} ${VERSION_OUTPUT_FILE}) +endif(NOT EXISTS ${VERSION_INPUT_FILE}) diff --git a/cmake/InstallVersionFile.cmake.in b/cmake/InstallVersionFile.cmake.in new file mode 100644 index 0000000..8121c25 --- /dev/null +++ b/cmake/InstallVersionFile.cmake.in @@ -0,0 +1,8 @@ +# +# AUTOGENERATED: DO NOT EDIT THIS FILE +# + +if(CPACK_SOURCE_INSTALLED_DIRECTORIES AND EXISTS "@INCLUDE_VERSION_HEADER_GENERATE@") + file(INSTALL "@INCLUDE_VERSION_HEADER_GENERATE@" + DESTINATION "@INCLUDE_VERSION_HEADER_GENERATE_PREFIX@") +endif(CPACK_SOURCE_INSTALLED_DIRECTORIES AND EXISTS "@INCLUDE_VERSION_HEADER_GENERATE@") diff --git a/cmake/Kernel.cmake b/cmake/Kernel.cmake new file mode 100644 index 0000000..232c298 --- /dev/null +++ b/cmake/Kernel.cmake @@ -0,0 +1,57 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# CMake macros to build and install Linux kernel modules +# Copyright (C) 2020 Manuel Bentele <development@manuel-bentele.de> +# + +# macro to define kernel module targets +macro(add_kernel_module MODULE_NAME KERNEL_BUILD_DIR KERNEL_INSTALL_DIR MODULE_MACRO MODULE_SOURCE_FILES MODULE_HEADER_FILES BUILD_SOURCE_FILE) + # create directory for kernel module + file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}) + # copy build source file + get_filename_component(BUILD_SOURCE_FILENAME ${BUILD_SOURCE_FILE} NAME) + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${BUILD_SOURCE_FILENAME} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${BUILD_SOURCE_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME} + DEPENDS ${BUILD_SOURCE_FILE}) + set(BUILD_SOURCE_FILE_PREPARED ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${BUILD_SOURCE_FILENAME}) + # copy source files + foreach(MODULE_SOURCE_FILE ${MODULE_SOURCE_FILES}) + get_filename_component(MODULE_SOURCE_FILENAME ${MODULE_SOURCE_FILE} NAME) + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_SOURCE_FILENAME} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${MODULE_SOURCE_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME} + DEPENDS ${MODULE_SOURCE_FILE}) + set(MODULE_SOURCE_FILES_PREPARED ${MODULE_SOURCE_FILES_PREPARED} + ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_SOURCE_FILENAME}) + endforeach() + # copy header files + foreach(MODULE_HEADER_FILE ${MODULE_HEADER_FILES}) + get_filename_component(MODULE_HEADER_FILENAME ${MODULE_HEADER_FILE} NAME) + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_HEADER_FILENAME} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${MODULE_HEADER_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME} + DEPENDS ${MODULE_HEADER_FILE}) + set(MODULE_HEADER_FILES_PREPARED ${MODULE_HEADER_FILES_PREPARED} + ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_HEADER_FILENAME}) + endforeach() + # check if module depends on another module + if(NOT ${ARGV7} STREQUAL "") + set(MODULE_EXTRA_SYMBOLS ${CMAKE_CURRENT_BINARY_DIR}/${ARGV7}/Module.symvers) + endif() + # define build command + set(MODULE_BUILD_COMMAND ${CMAKE_MAKE_PROGRAM} ${MODULE_MACRO} + -C ${KERNEL_BUILD_DIR} + M=${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME} modules + EXTRA_CFLAGS=${KERNEL_C_FLAGS} + KBUILD_EXTRA_SYMBOLS=${MODULE_EXTRA_SYMBOLS}) + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_NAME}.ko + COMMAND ${MODULE_BUILD_COMMAND} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME} + COMMENT "Build kernel module ${MODULE_NAME}" + DEPENDS ${BUILD_SOURCE_FILE_PREPARED} ${MODULE_HEADER_FILES_PREPARED} ${MODULE_SOURCE_FILES_PREPARED} + VERBATIM) + add_custom_target(${MODULE_NAME} ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_NAME}.ko ${ARGV7}) + # install kernel module + install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_NAME}.ko + DESTINATION ${KERNEL_INSTALL_DIR} + PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ + COMPONENT kernel) +endmacro(add_kernel_module) diff --git a/cmake/Version.cmake b/cmake/Version.cmake new file mode 100644 index 0000000..a061e48 --- /dev/null +++ b/cmake/Version.cmake @@ -0,0 +1,80 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020 Manuel Bentele <development@manuel-bentele.de> +# + +macro(gen_project_version VERSION_INPUT_FILE VERSION_INPUT_FILE_TEMPLATE VERSION_OUTPUT_FILE) + get_filename_component(VERSION_OUTPUT_FILENAME ${VERSION_OUTPUT_FILE} NAME) + # command that will trigger a rebuild of version.h every time + add_custom_command(OUTPUT regenerate-version-file + COMMAND ${CMAKE_COMMAND} -E sleep 0 + COMMENT "Trigger generating ${VERSION_OUTPUT_FILENAME}") + + # call the GenerateVersion.cmake file to generate the version.c file + add_custom_command(OUTPUT ${VERSION_OUTPUT_FILE} + COMMAND ${CMAKE_COMMAND} -D VERSION_MODULE_PATH=${PROJECT_MODULES_DIR} + -D VERSION_INPUT_FILE=${VERSION_INPUT_FILE} + -D VERSION_INPUT_FILE_TEMPLATE=${VERSION_INPUT_FILE_TEMPLATE} + -D VERSION_OUTPUT_FILE=${VERSION_OUTPUT_FILE} + -D VERSION_BUILD_TYPE=${CMAKE_BUILD_TYPE} + -P ${PROJECT_MODULES_DIR}/GenerateVersion.cmake + COMMENT "Generating ${VERSION_OUTPUT_FILENAME}" + DEPENDS regenerate-version-file) + add_custom_target(xloop-generate-version DEPENDS ${VERSION_OUTPUT_FILE}) + + # create target to expose project version + add_library(xloop-version INTERFACE) + target_include_directories(xloop-version INTERFACE ${PROJECT_INCLUDE_GEN_DIR}) + add_dependencies(xloop-version xloop-generate-version) +endmacro(gen_project_version VERSION_INPUT_FILE VERSION_INPUT_FILE_TEMPLATE VERSION_OUTPUT_FILE) + +# macro to get Linux kernel version from KERNEL_BUILD_DIR string +macro(get_kernel_version LINUX_KERNEL_VERSION KERNEL_BUILD_DIR) + string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" LINUX_KERNEL_VERSION ${KERNEL_BUILD_DIR}) + if(LINUX_KERNEL_VERSION STREQUAL "") + set(LINUX_KERNEL_VERSION "unknown") + endif(LINUX_KERNEL_VERSION STREQUAL "") +endmacro(get_kernel_version LINUX_KERNEL_VERSION KERNEL_BUILD_DIR) + +# macro to get Git version information +macro(get_repository_version REPOSITORY_VERSION VERSION_HEADER_FILE VERSION_BUILD_TYPE) + # check if generated version header from source package is available + if(EXISTS ${VERSION_HEADER_FILE}) + # get version information from the generated version header of the source package + file(READ ${VERSION_HEADER_FILE} GIT_VERSION) + string(REGEX MATCH "\"(([0-9]+:)?[0-9][A-Za-z0-9.+~-]*)\"" GIT_VERSION ${GIT_VERSION}) + set(GIT_VERSION "${CMAKE_MATCH_1}") + else(EXISTS ${VERSION_HEADER_FILE}) + # get detailed Git version information from Git repository + execute_process(COMMAND git describe HEAD + OUTPUT_VARIABLE GIT_VERSION + OUTPUT_STRIP_TRAILING_WHITESPACE) + + # remove the first letter of the version to satisfy packaging rules + string(REGEX MATCH "([0-9]+:)?[0-9][A-Za-z0-9.+~-]*" GIT_VERSION ${GIT_VERSION}) + + # overwrite version from Git if version is unknown + if(GIT_VERSION STREQUAL "") + set(GIT_VERSION "unknown") + endif(GIT_VERSION STREQUAL "") + + # get status of Git repository + execute_process(COMMAND git status --porcelain + OUTPUT_VARIABLE GIT_STATUS + OUTPUT_STRIP_TRAILING_WHITESPACE) + + # check if Git repository is dirty + if(NOT GIT_STATUS STREQUAL "") + # the Git repository is dirty, thus extend the version information + set(GIT_VERSION "${GIT_VERSION}-modified") + + # print a message in Release build configuration to warn about the dirty repository + if(${VERSION_BUILD_TYPE} MATCHES "Release") + message(WARNING "This xloop Git repository is dirty! Please commit or revert all changes for a ${VERSION_BUILD_TYPE} build!") + endif(${VERSION_BUILD_TYPE} MATCHES "Release") + endif(NOT GIT_STATUS STREQUAL "") + endif(EXISTS ${VERSION_HEADER_FILE}) + + # return version to caller + set(${REPOSITORY_VERSION} ${GIT_VERSION}) +endmacro(get_repository_version) diff --git a/cmake/version.cmake b/cmake/version.cmake deleted file mode 100644 index 07483af..0000000 --- a/cmake/version.cmake +++ /dev/null @@ -1,21 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0 -# -# CMake macros to get version numbers -# Copyright (C) 2020 Manuel Bentele <development@manuel-bentele.de> -# - -# macro to get Linux kernel version from KERNEL_BUILD_DIR -macro(get_kernel_version LINUX_KERNEL_VERSION KERNEL_BUILD_DIR) - string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" LINUX_KERNEL_VERSION ${KERNEL_BUILD_DIR}) - if(LINUX_KERNEL_VERSION STREQUAL "") - set(LINUX_KERNEL_VERSION "unknown") - endif() -endmacro(get_kernel_version) - -# macro to get short hash of latest commit -macro(get_repository_version REPOSITORY_VERSION) - execute_process(COMMAND git rev-parse --short HEAD - OUTPUT_VARIABLE SHORT_HASH_RESULT - OUTPUT_STRIP_TRAILING_WHITESPACE) - string(REGEX MATCH "[0-9a-fA-F]+" REPOSITORY_VERSION ${SHORT_HASH_RESULT}) -endmacro(get_repository_version) |