1
1
cmake_minimum_required (VERSION 3.25...3.30)
2
- project (mylib
3
- VERSION 1.0.0
4
- DESCRIPTION "Template for C++ library built with CMake"
5
- LANGUAGES CXX)
2
+ project (mylib VERSION 1.0.0 DESCRIPTION "Template for C++ library built with CMake" LANGUAGES CXX)
6
3
7
4
#----------------------------------------------------------------------------------------------------------------------
8
5
# general settings and options
@@ -18,16 +15,28 @@ option(MYLIB_BUILD_TESTS "Build mylib tests" ${is_top_level})
18
15
option (MYLIB_BUILD_EXAMPLES "Build mylib examples" ${is_top_level} )
19
16
option (MYLIB_BUILD_DOCS "Build mylib documentation" OFF )
20
17
option (MYLIB_INSTALL "Generate target for installing mylib" ${is_top_level} )
21
- set_if_undefined(MYLIB_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR} /cmake/mylib" CACHE STRING
22
- "Install path for mylib package-related CMake files" )
18
+ set_if_undefined(
19
+ MYLIB_INSTALL_CMAKEDIR
20
+ "${CMAKE_INSTALL_LIBDIR} /cmake/mylib"
21
+ CACHE
22
+ STRING
23
+ "Install path for mylib package-related CMake files"
24
+ )
23
25
24
26
if (DEFINED MYLIB_SHARED_LIBS)
25
27
set (BUILD_SHARED_LIBS ${MYLIB_SHARED_LIBS} )
26
28
endif ()
27
29
28
30
if (NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES )
29
31
set (CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
30
- set_property (CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo" )
32
+ set_property (
33
+ CACHE CMAKE_BUILD_TYPE
34
+ PROPERTY STRINGS
35
+ "Debug"
36
+ "Release"
37
+ "MinSizeRel"
38
+ "RelWithDebInfo"
39
+ )
31
40
endif ()
32
41
33
42
# Neither of these two are technically needed, but they make the expectation clear
@@ -59,10 +68,7 @@ endif()
59
68
60
69
generate_export_header(mylib EXPORT_FILE_NAME include /mylib/${export_file_name} )
61
70
62
- set (sources
63
- include /mylib/export.h
64
- include /mylib/mylib.h
65
- src/mylib.cpp)
71
+ set (sources include /mylib/export.h include /mylib/mylib.h src/mylib.cpp)
66
72
source_group (TREE "${CMAKE_CURRENT_SOURCE_DIR} " FILES ${sources} )
67
73
68
74
#----------------------------------------------------------------------------------------------------------------------
@@ -71,40 +77,44 @@ source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${sources})
71
77
72
78
include (CMakePackageConfigHelpers)
73
79
74
-
75
80
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} )
81
+ target_sources (
82
+ mylib
83
+ PUBLIC FILE_SET
84
+ HEADERS
85
+ BASE_DIRS
86
+ ${CMAKE_CURRENT_SOURCE_DIR} /include
87
+ ${CMAKE_CURRENT_BINARY_DIR} /include
88
+ FILES
89
+ ${CMAKE_CURRENT_SOURCE_DIR} /include /mylib/mylib.h
90
+ ${CMAKE_CURRENT_SOURCE_DIR} /include /mylib/export.h
91
+ ${CMAKE_CURRENT_BINARY_DIR} /include /mylib/${export_file_name}
92
+ )
84
93
target_compile_definitions (mylib PUBLIC "$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS} >>:MYLIB_STATIC_DEFINE>" )
85
94
86
95
# target_include_directories(mylib
87
96
# PUBLIC
88
97
# "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
89
98
# "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>")
90
99
91
- set_target_properties (mylib PROPERTIES
92
- SOVERSION ${PROJECT_VERSION_MAJOR}
93
- VERSION ${PROJECT_VERSION} )
100
+ set_target_properties (mylib PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR} VERSION ${PROJECT_VERSION} )
94
101
95
102
if (MYLIB_INSTALL AND NOT CMAKE_SKIP_INSTALL_RULES)
96
- configure_package_config_file(cmake/mylib-config.cmake.in mylib-config.cmake
97
- INSTALL_DESTINATION "${MYLIB_INSTALL_CMAKEDIR} " )
98
-
99
- write_basic_package_version_file(mylib-config-version .cmake
100
- COMPATIBILITY SameMajorVersion)
101
-
102
- install (TARGETS mylib EXPORT mylib_export
103
- RUNTIME COMPONENT mylib
104
- LIBRARY COMPONENT mylib NAMELINK_COMPONENT mylib-dev
105
- ARCHIVE COMPONENT mylib-dev
106
- FILE_SET HEADERS
107
- INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR} " )
103
+ configure_package_config_file(
104
+ cmake/mylib-config.cmake.in mylib-config.cmake INSTALL_DESTINATION "${MYLIB_INSTALL_CMAKEDIR} "
105
+ )
106
+
107
+ write_basic_package_version_file(mylib-config-version .cmake COMPATIBILITY SameMajorVersion)
108
+
109
+ install (TARGETS mylib
110
+ EXPORT mylib_export
111
+ RUNTIME COMPONENT mylib
112
+ LIBRARY COMPONENT mylib NAMELINK_COMPONENT mylib-dev
113
+ ARCHIVE COMPONENT mylib-dev FILE_SET HEADERS COMPONENT mylib-dev
114
+ INCLUDES
115
+ COMPONENT mylib-dev
116
+ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR} "
117
+ )
108
118
109
119
# install(DIRECTORY include/
110
120
# TYPE INCLUDE
@@ -120,16 +130,15 @@ if(MYLIB_INSTALL AND NOT CMAKE_SKIP_INSTALL_RULES)
120
130
endif ()
121
131
122
132
install (EXPORT mylib_export
123
- COMPONENT mylib-dev
124
- FILE "${targets_file} "
125
- DESTINATION "${MYLIB_INSTALL_CMAKEDIR} "
126
- NAMESPACE mylib::)
133
+ COMPONENT mylib-dev
134
+ FILE "${targets_file} "
135
+ DESTINATION "${MYLIB_INSTALL_CMAKEDIR} "
136
+ NAMESPACE mylib::
137
+ )
127
138
128
- install (FILES
129
- "${CMAKE_CURRENT_BINARY_DIR} /mylib-config.cmake"
130
- "${CMAKE_CURRENT_BINARY_DIR} /mylib-config-version.cmake"
131
- COMPONENT mylib-dev
132
- DESTINATION "${MYLIB_INSTALL_CMAKEDIR} " )
139
+ install (FILES "${CMAKE_CURRENT_BINARY_DIR} /mylib-config.cmake" "${CMAKE_CURRENT_BINARY_DIR} /mylib-config-version.cmake"
140
+ COMPONENT mylib-dev DESTINATION "${MYLIB_INSTALL_CMAKEDIR} "
141
+ )
133
142
134
143
if (MSVC )
135
144
set (pdb_file "" )
@@ -145,10 +154,11 @@ if(MYLIB_INSTALL AND NOT CMAKE_SKIP_INSTALL_RULES)
145
154
endif ()
146
155
147
156
install (FILES "${pdb_file} "
148
- COMPONENT mylib-dev
149
- CONFIGURATIONS Debug RelWithDebInfo
150
- DESTINATION "${pdb_file_destination} "
151
- OPTIONAL )
157
+ COMPONENT mylib-dev
158
+ CONFIGURATIONS Debug RelWithDebInfo
159
+ DESTINATION "${pdb_file_destination} "
160
+ OPTIONAL
161
+ )
152
162
endif ()
153
163
endif ()
154
164
0 commit comments