From 74df0a0ceb5132c7f3fb9035b3b45cb430c40da6 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Mon, 26 Oct 2020 16:57:06 +0100 Subject: Add CMake build requirements check to find libraries and Linux kernel headers --- cmake/FindKernelHeaders.cmake | 45 ++++++++++++++++++++++++++++++++++++ cmake/FindLibcap.cmake | 49 +++++++++++++++++++++++++++++++++++++++ cmake/FindLibncurses.cmake | 54 +++++++++++++++++++++++++++++++++++++++++++ cmake/Version.cmake | 8 ------- 4 files changed, 148 insertions(+), 8 deletions(-) create mode 100644 cmake/FindKernelHeaders.cmake create mode 100644 cmake/FindLibcap.cmake create mode 100644 cmake/FindLibncurses.cmake (limited to 'cmake') diff --git a/cmake/FindKernelHeaders.cmake b/cmake/FindKernelHeaders.cmake new file mode 100644 index 0000000..60d2380 --- /dev/null +++ b/cmake/FindKernelHeaders.cmake @@ -0,0 +1,45 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020 Manuel Bentele +# + +# check if custom Linux kernel build directory was specified +if(NOT KERNEL_BUILD_DIR) + set(KERNEL_BUILD_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/build" + CACHE PATH "Path to Linux kernel modules to compile against") +endif(NOT KERNEL_BUILD_DIR) + +# check if custom Linux kernel output directory was specified +if(NOT KERNEL_INSTALL_DIR) + set(KERNEL_INSTALL_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/extra" + CACHE PATH "Path to install Linux kernel modules") +endif(NOT KERNEL_INSTALL_DIR) + +# find the Linux kernel headers from given KERNEL_BUILD_DIR +find_path(KernelHeaders_INCLUDE_DIR + NAMES linux/user.h + PATHS ${KERNEL_BUILD_DIR}/include) + +# get Linux kernel headers version +string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" KernelHeaders_VERSION ${KernelHeaders_INCLUDE_DIR}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(KernelHeaders + FOUND_VAR KernelHeaders_FOUND + REQUIRED_VARS KernelHeaders_INCLUDE_DIR + VERSION_VAR KernelHeaders_VERSION + FAIL_MESSAGE "Linux kernel headers are not available! Please install them to build kernel modules!") + +if(KernelHeaders_FOUND) + set(KernelHeaders_INCLUDE_DIRS ${KernelHeaders_INCLUDE_DIR}/include + CACHE PATH "Path to Linux kernel header includes") +endif(KernelHeaders_FOUND) + +mark_as_advanced(KernelHeaders_INCLUDE_DIR KernelHeaders_MODULE_INSTALL_DIR) + +# print found information +message(VERBOSE "KERNEL_BUILD_DIR: ${KERNEL_BUILD_DIR}") +message(VERBOSE "KERNEL_INSTALL_DIR: ${KERNEL_INSTALL_DIR}") +message(VERBOSE "KernelHeaders_FOUND: ${KernelHeaders_FOUND}") +message(VERBOSE "KernelHeaders_VERSION: ${KernelHeaders_VERSION}") +message(VERBOSE "KernelHeaders_INCLUDE_DIRS: ${KernelHeaders_INCLUDE_DIRS}") diff --git a/cmake/FindLibcap.cmake b/cmake/FindLibcap.cmake new file mode 100644 index 0000000..8529426 --- /dev/null +++ b/cmake/FindLibcap.cmake @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020 Manuel Bentele +# + +# Use pkg-config to get the directories and then use these values +# in the FIND_PATH() and FIND_LIBRARY() calls +find_package(PkgConfig QUIET) +pkg_check_modules(PKG_Libcap QUIET libcap) + +set(Libcap_COMPILE_OPTIONS ${PKG_Libcap_CFLAGS_OTHER}) +set(Libcap_VERSION ${PKG_Libcap_VERSION}) + +find_path(Libcap_INCLUDE_DIR + NAMES sys/capability.h + HINTS ${PKG_Libcap_INCLUDE_DIRS}) +find_library(Libcap_LIBRARY + NAMES cap + HINTS ${PKG_Libcap_LIBRARY_DIRS}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Libcap + FOUND_VAR Libcap_FOUND + REQUIRED_VARS Libcap_LIBRARY + Libcap_INCLUDE_DIR + VERSION_VAR Libcap_VERSION + FAIL_MESSAGE "Library 'cap' is not available! Please install this required library!") + +if(Libcap_FOUND AND NOT TARGET Libcap::Libcap) + add_library(Libcap::Libcap UNKNOWN IMPORTED) + set_target_properties(Libcap::Libcap PROPERTIES + IMPORTED_LOCATION "${Libcap_LIBRARY}" + INTERFACE_COMPILE_OPTIONS "${Libcap_COMPILE_OPTIONS}" + INTERFACE_INCLUDE_DIRECTORIES "${Libcap_INCLUDE_DIR}") +endif(Libcap_FOUND AND NOT TARGET Libcap::Libcap) + +mark_as_advanced(Libcap_LIBRARY Libcap_INCLUDE_DIR) + +if(Libcap_FOUND) + set(Libcap_LIBRARIES ${Libcap_LIBRARY}) + set(Libcap_INCLUDE_DIRS ${Libcap_INCLUDE_DIR}) +endif(Libcap_FOUND) + +# print found information +message(VERBOSE "Libcap_FOUND: ${Libcap_FOUND}") +message(VERBOSE "Libcap_VERSION: ${Libcap_VERSION}") +message(VERBOSE "Libcap_INCLUDE_DIRS: ${Libcap_INCLUDE_DIRS}") +message(VERBOSE "Libcap_COMPILE_OPTIONS: ${Libcap_COMPILE_OPTIONS}") +message(VERBOSE "Libcap_LIBRARIES: ${Libcap_LIBRARIES}") diff --git a/cmake/FindLibncurses.cmake b/cmake/FindLibncurses.cmake new file mode 100644 index 0000000..2589ff0 --- /dev/null +++ b/cmake/FindLibncurses.cmake @@ -0,0 +1,54 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020 Manuel Bentele +# + +# Use pkg-config to get the directories and then use these values +# in the FIND_PATH() and FIND_LIBRARY() calls +find_package(PkgConfig QUIET) +pkg_check_modules(PKG_Libncurses QUIET libncurses) + +set(Libncurses_COMPILE_OPTIONS ${PKG_Libncurses_CFLAGS_OTHER}) +set(Libncurses_VERSION ${PKG_Libncurses_VERSION}) + +find_path(Libncurses_INCLUDE_DIR + NAMES ncursesw/ncurses.h + ncurses/ncurses.h + ncurses.h + ncursesw/term.h + ncurses/term.h + term.h + HINTS ${PKG_Libncurses_INCLUDE_DIRS}) +find_library(Libncurses_LIBRARY + NAMES ncurses + HINTS ${PKG_Libncurses_LIBRARY_DIRS}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Libncurses + FOUND_VAR Libncurses_FOUND + REQUIRED_VARS Libncurses_LIBRARY + Libncurses_INCLUDE_DIR + VERSION_VAR Libncurses_VERSION + FAIL_MESSAGE "Library 'ncurses' is not available! Please install this required library!") + +if(Libncurses_FOUND AND NOT TARGET Libncurses::Libncurses) + add_library(Libncurses::Libncurses UNKNOWN IMPORTED) + set_target_properties(Libncurses::Libncurses PROPERTIES + IMPORTED_LOCATION "${Libncurses_LIBRARY}" + INTERFACE_COMPILE_OPTIONS "${Libncurses_COMPILE_OPTIONS}" + INTERFACE_INCLUDE_DIRECTORIES "${Libncurses_INCLUDE_DIR}") +endif(Libncurses_FOUND AND NOT TARGET Libncurses::Libncurses) + +mark_as_advanced(Libncurses_LIBRARY Libncurses_INCLUDE_DIR) + +if(Libncurses_FOUND) + set(Libncurses_LIBRARIES ${Libncurses_LIBRARY}) + set(Libncurses_INCLUDE_DIRS ${Libncurses_INCLUDE_DIR}) +endif(Libncurses_FOUND) + +# print found information +message(VERBOSE "Libncurses_FOUND: ${Libncurses_FOUND}") +message(VERBOSE "Libncurses_VERSION: ${Libncurses_VERSION}") +message(VERBOSE "Libncurses_INCLUDE_DIRS: ${Libncurses_INCLUDE_DIRS}") +message(VERBOSE "Libncurses_COMPILE_OPTIONS: ${Libncurses_COMPILE_OPTIONS}") +message(VERBOSE "Libncurses_LIBRARIES: ${Libncurses_LIBRARIES}") diff --git a/cmake/Version.cmake b/cmake/Version.cmake index a061e48..5c72c35 100644 --- a/cmake/Version.cmake +++ b/cmake/Version.cmake @@ -28,14 +28,6 @@ macro(gen_project_version VERSION_INPUT_FILE VERSION_INPUT_FILE_TEMPLATE VERSION 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 -- cgit v1.2.3-55-g7522