Skip to content

Commit c0252b4

Browse files
committed
Modernize cmake files
1 parent c479f94 commit c0252b4

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

CMakeLists.txt

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.14)
1+
cmake_minimum_required(VERSION 3.25...3.30)
22
project(mylib
33
VERSION 1.0.0
44
DESCRIPTION "Template for C++ library built with CMake"
@@ -14,8 +14,8 @@ include(GNUInstallDirs)
1414
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level)
1515

1616
# MYLIB_SHARED_LIBS option (undefined by default) can be used to force shared/static build
17-
option(MYLIB_BUILD_TESTS "Build mylib tests" OFF)
18-
option(MYLIB_BUILD_EXAMPLES "Build mylib examples" OFF)
17+
option(MYLIB_BUILD_TESTS "Build mylib tests" ${is_top_level})
18+
option(MYLIB_BUILD_EXAMPLES "Build mylib examples" ${is_top_level})
1919
option(MYLIB_BUILD_DOCS "Build mylib documentation" OFF)
2020
option(MYLIB_INSTALL "Generate target for installing mylib" ${is_top_level})
2121
set_if_undefined(MYLIB_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/mylib" CACHE STRING
@@ -30,6 +30,10 @@ if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
3030
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
3131
endif()
3232

33+
# Neither of these two are technically needed, but they make the expectation clear
34+
set_if_undefined(CMAKE_CXX_STANDARD 20)
35+
set_if_undefined(CMAKE_CXX_EXTENSIONS FALSE)
36+
3337
set_if_undefined(CMAKE_CXX_VISIBILITY_PRESET hidden)
3438
set_if_undefined(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
3539

@@ -67,13 +71,22 @@ source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${sources})
6771

6872
include(CMakePackageConfigHelpers)
6973

70-
target_sources(mylib PRIVATE ${sources})
74+
75+
target_sources(mylib PRIVATE src/mylib.cpp)
76+
target_sources(mylib PUBLIC FILE_SET HEADERS
77+
BASE_DIRS
78+
${CMAKE_CURRENT_SOURCE_DIR}/include
79+
${CMAKE_CURRENT_BINARY_DIR}/include
80+
FILES
81+
${CMAKE_CURRENT_SOURCE_DIR}/include/mylib/mylib.h
82+
${CMAKE_CURRENT_SOURCE_DIR}/include/mylib/export.h
83+
${CMAKE_CURRENT_BINARY_DIR}/include/mylib/${export_file_name})
7184
target_compile_definitions(mylib PUBLIC "$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:MYLIB_STATIC_DEFINE>")
7285

73-
target_include_directories(mylib
74-
PUBLIC
75-
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
76-
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>")
86+
# target_include_directories(mylib
87+
# PUBLIC
88+
# "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
89+
# "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>")
7790

7891
set_target_properties(mylib PROPERTIES
7992
SOVERSION ${PROJECT_VERSION_MAJOR}
@@ -90,13 +103,15 @@ if(MYLIB_INSTALL AND NOT CMAKE_SKIP_INSTALL_RULES)
90103
RUNTIME COMPONENT mylib
91104
LIBRARY COMPONENT mylib NAMELINK_COMPONENT mylib-dev
92105
ARCHIVE COMPONENT mylib-dev
106+
FILE_SET HEADERS
93107
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
94-
install(DIRECTORY include/
95-
TYPE INCLUDE
96-
COMPONENT mylib-dev)
97-
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/include/mylib/${export_file_name}"
98-
COMPONENT mylib-dev
99-
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/mylib")
108+
109+
# install(DIRECTORY include/
110+
# TYPE INCLUDE
111+
# COMPONENT mylib-dev)
112+
# install(FILES "${CMAKE_CURRENT_BINARY_DIR}/include/mylib/${export_file_name}"
113+
# COMPONENT mylib-dev
114+
# DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/mylib")
100115

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

CMakePresets.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
"description": "Base preset for library developers",
1212
"binaryDir": "${sourceDir}/build",
1313
"hidden": true,
14+
"generator": "Ninja",
1415
"cacheVariables": {
15-
"MYLIB_BUILD_TESTS": "ON",
16-
"MYLIB_BUILD_EXAMPLES": "ON"
16+
"CMAKE_BUILD_TYPE": "Release",
17+
"CMAKE_EXPORT_COMPILE_COMMANDS": true,
18+
"MYLIB_SHARED_LIBS": true,
19+
"MYLIB_BUILD_TESTS": true,
20+
"MYLIB_BUILD_EXAMPLES": true
1721
}
1822
},
1923
{

tests/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.14)
1+
cmake_minimum_required(VERSION 3.25...3.30)
22
project(mylib-tests)
33

44
#----------------------------------------------------------------------------------------------------------------------
@@ -10,14 +10,19 @@ string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_
1010

1111
if(is_top_level)
1212
enable_testing()
13+
14+
# Neither of these two are technically needed, but they make the expectation clear
15+
set_if_undefined(CMAKE_CXX_STANDARD 20)
16+
set_if_undefined(CMAKE_CXX_EXTENSIONS FALSE)
1317
endif()
1418

1519
#----------------------------------------------------------------------------------------------------------------------
1620
# testing framework
1721
#----------------------------------------------------------------------------------------------------------------------
1822

1923
include(FetchContent)
20-
FetchContent_Declare(googletest URL https://github.com/google/googletest/archive/refs/tags/release-1.12.1.tar.gz)
24+
FetchContent_Declare(googletest URL https://github.com/google/googletest/archive/refs/tags/v1.15.2.tar.gz
25+
DOWNLOAD_EXTRACT_TIMESTAMP ON)
2126

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

0 commit comments

Comments
 (0)