From c6087d222ddf39329db4642d4ac0397153b3517a Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Sun, 20 Oct 2019 00:57:38 +0200 Subject: [PATCH 1/5] Add comments about library creation Why we create both static and shared type of library? Signed-off-by: Mikko Johannes Koivunalho --- src/CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 437ea2a6..7f99cab6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -45,6 +45,11 @@ set(HEADERS configure_file(check.h.in check.h @ONLY) +# To maintain compatibility with the Autotools installation +# we specifically create both shared and static libraries +# as that is what Autotools script has been doing. +# Normally CMake would create the system's native default library type. + add_library(check STATIC ${SOURCES} ${HEADERS}) # We would like to create an OBJECT library but currently they are From 86fe1ba22135f31cfd6cb6c94a48bb967fe35891 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Sun, 20 Oct 2019 00:59:21 +0200 Subject: [PATCH 2/5] Correct wrong Win dynamic library name Change from checkStatic to checkDynamic. Was erroneously "checkStatic". Signed-off-by: Mikko Johannes Koivunalho --- src/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7f99cab6..a2268239 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -153,7 +153,9 @@ if (MSVC) # "On Windows you should probably give each library a different name, # since there is a ".lib" file for both shared and static". # https://stackoverflow.com/a/2152157/4716395 - set(LIBRARY_OUTPUT_NAME "checkStatic") + # "Dynamic-Link Library" (DLL) is Microsoft terminology. + # So we call it this: + set(LIBRARY_OUTPUT_NAME "checkDynamic") endif (MSVC) set_target_properties(checkShared PROPERTIES OUTPUT_NAME ${LIBRARY_OUTPUT_NAME} From 8608dc08e25a9b4b1ec3da0e7bf6c627bf2acc07 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Sun, 20 Oct 2019 02:24:02 +0200 Subject: [PATCH 3/5] Add support for CMake User Package Registry The registries are especially useful to help projects find packages in non-standard install locations or directly in their own build trees. Signed-off-by: Mikko Johannes Koivunalho --- CMakeLists.txt | 26 ++++++++++++++++++++------ cmake/check-config.cmake.in | 2 +- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 12b01768..c5d1b18d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -487,29 +487,43 @@ set(EXPORT_NAME ${PROJECT_NAME}) include(CMakePackageConfigHelpers) configure_package_config_file( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${EXPORT_NAME}-config.cmake.in - ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/cmake/${EXPORT_NAME}-config.cmake INSTALL_DESTINATION ${LIB_INSTALL_DIR}/${EXPORT_NAME}/cmake ) write_basic_package_version_file( - ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config-version.cmake + ${CMAKE_CURRENT_BINARY_DIR}/cmake/${EXPORT_NAME}-config-version.cmake VERSION ${PROJECT_VERSION} COMPATIBILITY AnyNewerVersion ) export(EXPORT check-targets - FILE "${CMAKE_CURRENT_BINARY_DIR}/check-targets.cmake" + FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/${EXPORT_NAME}-targets.cmake" NAMESPACE Check:: ) install(EXPORT check-targets NAMESPACE Check:: - FILE check-targets.cmake + FILE "${EXPORT_NAME}-targets.cmake" DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${EXPORT_NAME} ) install( FILES - "${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config.cmake" - "${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config-version.cmake" + "${CMAKE_BINARY_DIR}/cmake/${EXPORT_NAME}-config.cmake" + "${CMAKE_BINARY_DIR}/cmake/${EXPORT_NAME}-config-version.cmake" DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${EXPORT_NAME} ) +# Store the current build directory in the CMake user package registry. +# This helps dependent projects use a package from the current +# project’s build tree, i.e. without installing it. + +# In CMake 3.14 and below the export(PACKAGE) command populated +# the user package registry by default and users needed to set +# the CMAKE_EXPORT_NO_PACKAGE_REGISTRY to disable it, +# e.g. in automated build and packaging environments. Since +# the user package registry is stored outside the build tree, +# this side effect should not be enabled by default. +# Therefore CMake 3.15 and above prefer that export(PACKAGE) does nothing +# unless an explicit CMAKE_EXPORT_PACKAGE_REGISTRY variable is set. +export(PACKAGE "${PROJECT_NAME}") + diff --git a/cmake/check-config.cmake.in b/cmake/check-config.cmake.in index 15aabf71..82620719 100644 --- a/cmake/check-config.cmake.in +++ b/cmake/check-config.cmake.in @@ -1,3 +1,3 @@ @PACKAGE_INIT@ -include("${CMAKE_CURRENT_LIST_DIR}/check-targets.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/@EXPORT_NAME@-targets.cmake") From fb49d7995ac6cb9fbe259d2d96a5bd43f5b115e0 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Sun, 20 Oct 2019 02:27:38 +0200 Subject: [PATCH 4/5] Add header check_stdint.h to public headers Signed-off-by: Mikko Johannes Koivunalho --- src/CMakeLists.txt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 437ea2a6..53ebf646 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -138,10 +138,12 @@ endif (MSVC) # More configuration for exporting set(LIBRARY_OUTPUT_NAME "check") +list(APPEND public_headers "${CMAKE_CURRENT_BINARY_DIR}/check.h") +list(APPEND public_headers "${CMAKE_BINARY_DIR}/check_stdint.h") set_target_properties(check PROPERTIES OUTPUT_NAME ${LIBRARY_OUTPUT_NAME} - PUBLIC_HEADER ${CMAKE_CURRENT_BINARY_DIR}/check.h + PUBLIC_HEADER "${public_headers}" ) if (MSVC) @@ -154,16 +156,18 @@ set_target_properties(checkShared PROPERTIES OUTPUT_NAME ${LIBRARY_OUTPUT_NAME} VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} - PUBLIC_HEADER ${CMAKE_CURRENT_BINARY_DIR}/check.h + PUBLIC_HEADER "${public_headers}" ) target_include_directories(check PUBLIC $ + $ $ ) target_include_directories(checkShared PUBLIC $ + $ $ ) From 401856dee667a8e3aa85c04280702e39c4281cc4 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Sun, 20 Oct 2019 01:20:21 +0200 Subject: [PATCH 5/5] Remove the forcing of the CMake build type * Do not force the build type to be Debug. * This actually didn't work because the variable was not declated CACHE. * Append /cmake dir to CMAKE_MODULE_PATH instead of replacing the previous content. Signed-off-by: Mikko Johannes Koivunalho --- CMakeLists.txt | 15 +++++++++------ cmake/BuildType.cmake | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 cmake/BuildType.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 12b01768..ed621251 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,12 +28,19 @@ project(check DESCRIPTION "Unit Testing Framework for C" LANGUAGES C) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") + +############################################################################### +# Set build features + +# Set CMAKE_BUILD_TYPE to Debug if source directory is a Git repository +# or user does not override on the command line +include(BuildType) + ############################################################################### # Configure a project for testing with CTest/CDash include(CTest) -set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") - macro(extract_version file setting_name) file(STRINGS ${file} VERSION_NUMBER REGEX "^${setting_name}") string(REPLACE "=" ";" VERSION_NUMBER_LIST ${VERSION_NUMBER}) @@ -49,10 +56,6 @@ set(PROJECT_VERSION_MINOR ${CHECK_MINOR_VERSION}) set(PROJECT_VERSION_PATCH ${CHECK_MICRO_VERSION}) set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") -############################################################################### -# Set build features -set(CMAKE_BUILD_TYPE Debug) - ############################################################################### # Provides install directory variables as defined by the GNU Coding Standards. include(GNUInstallDirs) diff --git a/cmake/BuildType.cmake b/cmake/BuildType.cmake new file mode 100644 index 00000000..4326c7c7 --- /dev/null +++ b/cmake/BuildType.cmake @@ -0,0 +1,15 @@ +# Set a default build type if none was specified +set(default_build_type "Release") +if(EXISTS "${CMAKE_SOURCE_DIR}/.git") + set(default_build_type "Debug") +endif() + +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "Setting build type to '${default_build_type}' as none was specified.") + set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE + STRING "Choose the type of build." FORCE) + # Set the possible values of build type for cmake-gui + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS + "Debug" "Release" "MinSizeRel" "RelWithDebInfo") +endif() +