Skip to content

Commit

Permalink
Use ctest --build-and-test (#1)
Browse files Browse the repository at this point in the history
* Use ctest --build-and-test

* Move sub presets to cmake/presets

* Final cleanup
  • Loading branch information
ClausKlein authored Oct 13, 2024
1 parent 20e6a1e commit c45f20e
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .cmake-format.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
format:
line_width: 123
line_width: 120
tab_size: 4
max_subgroups_hwrap: 4
max_pargs_hwrap: 4
Expand Down
24 changes: 12 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ project(mylib VERSION 1.0.0 DESCRIPTION "Template for C++ library built with CMa
include(cmake/utils.cmake)
include(GNUInstallDirs)

string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level)

# MYLIB_SHARED_LIBS option (undefined by default) can be used to force shared/static build
option(MYLIB_BUILD_TESTS "Build mylib tests" ${is_top_level})
option(MYLIB_BUILD_EXAMPLES "Build mylib examples" ${is_top_level})
option(MYLIB_BUILD_TESTS "Build mylib tests" ${PROJECT_IS_TOP_LEVEL})
option(MYLIB_BUILD_EXAMPLES "Build mylib examples" ${PROJECT_IS_TOP_LEVEL})
option(MYLIB_BUILD_DOCS "Build mylib documentation" OFF)
option(MYLIB_INSTALL "Generate target for installing mylib" ${is_top_level})
option(MYLIB_INSTALL "Generate target for installing mylib" ${PROJECT_IS_TOP_LEVEL})
set_if_undefined(
MYLIB_INSTALL_CMAKEDIR
"${CMAKE_INSTALL_LIBDIR}/cmake/mylib"
Expand All @@ -40,7 +38,7 @@ if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
endif()

# Neither of these two are technically needed, but they make the expectation clear
set_if_undefined(CMAKE_CXX_STANDARD 20)
set_if_undefined(CMAKE_CXX_STANDARD 17)
set_if_undefined(CMAKE_CXX_EXTENSIONS FALSE)

set_if_undefined(CMAKE_CXX_VISIBILITY_PRESET hidden)
Expand Down Expand Up @@ -101,15 +99,16 @@ if(MYLIB_INSTALL AND NOT CMAKE_SKIP_INSTALL_RULES)

write_basic_package_version_file(mylib-config-version.cmake COMPATIBILITY SameMajorVersion)

# cmake-format: off
install(TARGETS mylib
EXPORT mylib_export
RUNTIME COMPONENT mylib
LIBRARY COMPONENT mylib NAMELINK_COMPONENT mylib-dev
ARCHIVE COMPONENT mylib-dev FILE_SET HEADERS COMPONENT mylib-dev
INCLUDES
COMPONENT mylib-dev
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
ARCHIVE COMPONENT mylib-dev
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
FILE_SET HEADERS COMPONENT mylib-dev
)
# cmake-format: on

set(targets_file "mylib-shared-targets.cmake")

Expand All @@ -124,8 +123,9 @@ if(MYLIB_INSTALL AND NOT CMAKE_SKIP_INSTALL_RULES)
NAMESPACE mylib::
)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/mylib-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/mylib-config-version.cmake"
COMPONENT mylib-dev DESTINATION "${MYLIB_INSTALL_CMAKEDIR}"
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/mylib-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/mylib-config-version.cmake" COMPONENT mylib-dev
DESTINATION "${MYLIB_INSTALL_CMAKEDIR}"
)

if(MSVC)
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"patch": 0
},
"include": [
"CMake${hostSystemName}Presets.json"
"cmake/presets/CMake${hostSystemName}Presets.json"
],
"buildPresets": [
{
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"name": "Debug",
"description": "Possix preset for library developers",
"inherits": [
"Release",
"dev-mode",
"Release",
"clang-tidy"
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"name": "Debug",
"description": "Windows preset for library developers",
"inherits": [
"Release",
"dev-mode"
"dev-mode",
"Release"
],
"generator": "Visual Studio 17 2022"
}
Expand Down
9 changes: 4 additions & 5 deletions examples/add/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
cmake_minimum_required(VERSION 3.14)
cmake_minimum_required(VERSION 3.25...3.30)
project(mylib-add LANGUAGES CXX)

include("../../cmake/utils.cmake")
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level)

if(is_top_level)
find_package(mylib REQUIRED)
if(PROJECT_IS_TOP_LEVEL)
find_package(mylib 1.0.0 REQUIRED)
endif()

set(sources main.cpp)
Expand All @@ -15,6 +14,6 @@ add_executable(mylib-add)
target_sources(mylib-add PRIVATE ${sources})
target_link_libraries(mylib-add PRIVATE mylib::mylib)

if(NOT is_top_level)
if(NOT PROJECT_IS_TOP_LEVEL)
win_copy_deps_to_target_dir(mylib-add mylib::mylib)
endif()
44 changes: 31 additions & 13 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
cmake_minimum_required(VERSION 3.25...3.30)
project(mylib-tests)
project(mylib-tests LANGUAGES CXX)

#----------------------------------------------------------------------------------------------------------------------
# general settings and options
#----------------------------------------------------------------------------------------------------------------------

include("../cmake/utils.cmake")
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level)

if(is_top_level)
if(PROJECT_IS_TOP_LEVEL)
enable_testing()

# Neither of these two are technically needed, but they make the expectation clear
set_if_undefined(CMAKE_CXX_STANDARD 20)
set_if_undefined(CMAKE_CXX_STANDARD 17)
set_if_undefined(CMAKE_CXX_EXTENSIONS FALSE)
endif()

Expand All @@ -23,24 +22,26 @@ endif()
include(FetchContent)
FetchContent_Declare(
googletest URL https://github.com/google/googletest/archive/refs/tags/v1.15.2.tar.gz DOWNLOAD_EXTRACT_TIMESTAMP ON
DOWNLOAD_NO_PROGRESS ON
DOWNLOAD_NO_PROGRESS ON FIND_PACKAGE_ARGS NAMES GTest
)

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # do not override parent project's runtime settings on Windows
set(INSTALL_GTEST OFF)

# For simplicity, always build googletest as static library. This prevents mylib-tests executable from
# complaining about missing googletest DLLs on Windows.
set(BUILD_SHARED_LIBS OFF)
set(BUILD_GMOCK OFF)

FetchContent_MakeAvailable(googletest)

#----------------------------------------------------------------------------------------------------------------------
# tests dependencies
#----------------------------------------------------------------------------------------------------------------------

if(is_top_level)
find_package(mylib REQUIRED)
if(PROJECT_IS_TOP_LEVEL)
find_package(mylib 1.0.0 QUIET)
if(NOT mylib_FOUND)
message(STATUS "find_package(mylib) was NOT found, use as subproject ...")
# test if the targets are usable if used as subproject
add_subdirectory(.. mylib EXCLUDE_FROM_ALL)
endif()
endif()

#----------------------------------------------------------------------------------------------------------------------
Expand All @@ -57,9 +58,26 @@ source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${sources})
add_executable(mylib-tests)
target_sources(mylib-tests PRIVATE ${sources})

target_link_libraries(mylib-tests PRIVATE mylib::mylib gtest_main)
target_link_libraries(mylib-tests PRIVATE mylib::mylib GTest::gtest_main)

if(NOT PROJECT_IS_TOP_LEVEL)
# test if the targets are findable from the build directory
# cmake-format: off
add_test(find-package-test
${CMAKE_CTEST_COMMAND}
-C ${CMAKE_BUILD_TYPE}
--build-and-test
"${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_BINARY_DIR}/find-package-test"
--build-generator ${CMAKE_GENERATOR}
--build-makeprogram ${CMAKE_MAKE_PROGRAM}
--build-options
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}"
)
# cmake-format: on

if(NOT is_top_level)
win_copy_deps_to_target_dir(mylib-tests mylib::mylib)
endif()

Expand Down

0 comments on commit c45f20e

Please sign in to comment.