Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit b2a520c

Browse files
authored
Merge pull request #905 from lukaszstolarczuk/cleanup-cmake
cleanups in CMake files
2 parents a1798df + 205c536 commit b2a520c

File tree

8 files changed

+238
-193
lines changed

8 files changed

+238
-193
lines changed

CMakeLists.txt

Lines changed: 99 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
cmake_minimum_required(VERSION 3.3)
55
project(libpmemobj-cpp C CXX)
66

7+
## Set required and useful variables
78
set(VERSION_MAJOR 1)
89
set(VERSION_MINOR 11)
910
set(VERSION_PATCH 0)
1011
#set(VERSION_PRERELEASE rc3)
1112

1213
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR})
13-
if (VERSION_PATCH GREATER 0)
14+
if(VERSION_PATCH GREATER 0)
1415
set(VERSION ${VERSION}.${VERSION_PATCH})
1516
endif()
16-
if (VERSION_PRERELEASE)
17+
if(VERSION_PRERELEASE)
1718
set(VERSION ${VERSION}-${VERSION_PRERELEASE})
1819
endif()
1920

@@ -30,15 +31,37 @@ set(CXX_STANDARD_REQUIRED ON)
3031
set(CXX_STANDARD 14 CACHE STRING "C++ language standard")
3132
set(CMAKE_CXX_STANDARD ${CXX_STANDARD})
3233

33-
include(FindPerl)
34-
include(FindThreads)
35-
include(CMakeDependentOption)
36-
include(CMakePackageConfigHelpers)
37-
include(CheckCXXSourceCompiles)
38-
include(CheckCXXCompilerFlag)
39-
include(GNUInstallDirs)
40-
include(${CMAKE_SOURCE_DIR}/cmake/functions.cmake)
34+
# Do not treat include directories from the interfaces
35+
# of consumed Imported Targets as SYSTEM by default.
36+
set(CMAKE_NO_SYSTEM_FROM_IMPORTED 1)
37+
38+
set(TEST_DIR ${CMAKE_CURRENT_BINARY_DIR}/test CACHE STRING "working directory for tests")
39+
40+
if(NOT CMAKE_BUILD_TYPE)
41+
set(CMAKE_BUILD_TYPE "Debug")
42+
endif()
4143

44+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
45+
46+
# XXX: use it in version.hpp and in Doxygen.in
47+
# Set ${SRCVERSION}
48+
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
49+
execute_process(COMMAND git describe
50+
OUTPUT_VARIABLE SRCVERSION
51+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
52+
OUTPUT_STRIP_TRAILING_WHITESPACE
53+
ERROR_QUIET)
54+
if(NOT SRCVERSION)
55+
execute_process(COMMAND git log -1 --format=%h
56+
OUTPUT_VARIABLE SRCVERSION
57+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
58+
OUTPUT_STRIP_TRAILING_WHITESPACE)
59+
endif()
60+
else()
61+
file(STRINGS ${CMAKE_SOURCE_DIR}/.version SRCVERSION)
62+
endif()
63+
64+
## CMake build options
4265
option(BUILD_EXAMPLES "build examples" ON)
4366
option(BUILD_TESTS "build tests" ON)
4467
option(BUILD_DOC "build documentation" ON)
@@ -52,11 +75,11 @@ option(USE_ASAN "enable AddressSanitizer (debugging)" OFF)
5275
option(USE_UBSAN "enable UndefinedBehaviorSanitizer (debugging)" OFF)
5376
option(USE_CCACHE "Use ccache if it is available in the system" ON)
5477

55-
option(TESTS_USE_FORCED_PMEM "run tests with PMEM_IS_PMEM_FORCE=1" OFF)
78+
option(TESTS_USE_FORCED_PMEM "run tests with PMEM_IS_PMEM_FORCE=1 - it speeds up tests execution on emulated pmem" OFF)
5679
option(TESTS_USE_VALGRIND "enable tests with valgrind (if found)" ON)
5780
option(TESTS_PMREORDER "enable tests with pmreorder (if pmreorder found; it requires PMDK ver. >= 1.9)" OFF)
58-
option(TESTS_CONCURRENT_HASH_MAP_DRD_HELGRIND "enable concurrent_hash_map tests with drd and helgrind (can only be run on PMEM)." OFF)
59-
option(TESTS_CONCURRENT_GDB "enable concurrent gdb tests - require 'set scheduler-locking on' support (OS dependent) " OFF)
81+
option(TESTS_CONCURRENT_HASH_MAP_DRD_HELGRIND "enable concurrent_hash_map tests with drd and helgrind (can only be run on PMEM)" OFF)
82+
option(TESTS_CONCURRENT_GDB "enable concurrent gdb tests - require 'set scheduler-locking on' support (OS dependent)" OFF)
6083
option(TESTS_LONG "enable long running tests" OFF)
6184
option(TESTS_TBB "enable tests which require TBB" OFF)
6285
option(TESTS_COMPATIBILITY "enable compatibility tests (requires internet connection)" OFF)
@@ -68,7 +91,7 @@ option(TEST_CONCURRENT_HASHMAP "enable testing of pmem::obj::concurrent_hash_map
6891
option(TEST_SEGMENT_VECTOR_ARRAY_EXPSIZE "enable testing of pmem::obj::segment_vector with array as segment_vector_type and exponential_size_policy" ON)
6992
option(TEST_SEGMENT_VECTOR_VECTOR_EXPSIZE "enable testing of pmem::obj::segment_vector with vector as segment_vector_type and exponential_size_policy" ON)
7093
option(TEST_SEGMENT_VECTOR_VECTOR_FIXEDSIZE "enable testing of pmem::obj::segment_vector with vector as segment_vector_type and fixed_size_policy" ON)
71-
option(TEST_ENUMERABLE_THREAD_SPECIFIC "enable testing of pmem::obj::experimental::enumerable_thread_specific" ON)
94+
option(TEST_ENUMERABLE_THREAD_SPECIFIC "enable testing of pmem::obj::enumerable_thread_specific" ON)
7295
option(TEST_CONCURRENT_MAP "enable testing of pmem::obj::experimental::concurrent_map (depends on TEST_STRING)" ON)
7396
option(TEST_SELF_RELATIVE_POINTER "enable testing of pmem::obj::experimental::self_relative_ptr" ON)
7497
option(TEST_RADIX_TREE "enable testing of pmem::obj::experimental::radix_tree" ON)
@@ -80,60 +103,52 @@ option(INSTALL_CONCURRENT_HASHMAP "enable installation of pmem::obj::concurrent_
80103
option(INSTALL_SEGMENT_VECTOR "enable installation of pmem::obj::segment_vector" ON)
81104
option(INSTALL_CONCURRENT_MAP "enable installation of pmem::obj::experimental::concurrent_map (depends on INSTALL_STRING)" ON)
82105
option(INSTALL_SELF_RELATIVE_POINTER "enable installation of pmem::obj::experimental::self_relative_ptr" ON)
83-
option(INSTALL_RADIX_TREE "enable testing of pmem::obj::experimental::radix_tree" ON)
84-
85-
# Configure the ccache as compiler launcher
86-
find_program(CCACHE_FOUND ccache)
87-
if(USE_CCACHE AND CCACHE_FOUND)
88-
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
89-
endif()
106+
option(INSTALL_RADIX_TREE "enable installation of pmem::obj::experimental::radix_tree" ON)
90107

91-
# Do not treat include directories from the interfaces
92-
# of consumed Imported Targets as SYSTEM by default.
93-
set(CMAKE_NO_SYSTEM_FROM_IMPORTED 1)
108+
## Setup environment, find packages, set compiler's flags, add additional custom targets
109+
include(FindPerl)
110+
include(FindThreads)
111+
include(CMakeDependentOption)
112+
include(CMakePackageConfigHelpers)
113+
include(CheckCXXSourceCompiles)
114+
include(CheckCXXCompilerFlag)
115+
include(GNUInstallDirs)
116+
include(${CMAKE_SOURCE_DIR}/cmake/functions.cmake)
94117

95118
# Required for MSVC to correctly define __cplusplus
96119
add_flag("/Zc:__cplusplus")
97120

98-
set(TEST_DIR ${CMAKE_CURRENT_BINARY_DIR}/test
99-
CACHE STRING "working directory for tests")
121+
if(NOT WIN32)
122+
find_package(PkgConfig QUIET)
123+
endif()
100124

101-
if(NOT CMAKE_BUILD_TYPE)
102-
set(CMAKE_BUILD_TYPE "Debug")
103-
endif (NOT CMAKE_BUILD_TYPE)
125+
# Configure the ccache as compiler launcher
126+
find_program(CCACHE_FOUND ccache)
127+
if(USE_CCACHE AND CCACHE_FOUND)
128+
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
129+
endif()
104130

105-
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
106-
execute_process(COMMAND git describe
107-
OUTPUT_VARIABLE SRCVERSION
108-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
109-
OUTPUT_STRIP_TRAILING_WHITESPACE
110-
ERROR_QUIET)
111-
if(NOT SRCVERSION)
112-
execute_process(COMMAND git log -1 --format=%h
113-
OUTPUT_VARIABLE SRCVERSION
114-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
115-
OUTPUT_STRIP_TRAILING_WHITESPACE)
116-
endif()
131+
# Find Valgrind
132+
if(PKG_CONFIG_FOUND)
133+
pkg_check_modules(VALGRIND QUIET valgrind)
117134
else()
118-
execute_process(COMMAND cat .version
119-
OUTPUT_VARIABLE SRCVERSION
120-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
121-
OUTPUT_STRIP_TRAILING_WHITESPACE)
135+
find_package(VALGRIND QUIET)
122136
endif()
123137

124-
if(NOT WIN32)
125-
find_package(PkgConfig QUIET)
126-
endif()
138+
if(VALGRIND_FOUND)
139+
add_flag(-DLIBPMEMOBJ_CPP_VG_MEMCHECK_ENABLED=1)
140+
add_flag(-DLIBPMEMOBJ_CPP_VG_DRD_ENABLED=1)
141+
add_flag(-DLIBPMEMOBJ_CPP_VG_HELGRIND_ENABLED=1)
127142

128-
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
143+
include_directories(${VALGRIND_INCLUDE_DIRS})
144+
find_pmemcheck()
129145

130-
if(NOT PERL_FOUND)
131-
message(FATAL_ERROR "Perl not found")
132-
endif()
133-
if (PERL_VERSION_STRING VERSION_LESS 5.16)
134-
message(FATAL_ERROR "Too old Perl (<5.16)")
146+
if(VALGRIND_PMEMCHECK_FOUND)
147+
add_flag(-DLIBPMEMOBJ_CPP_VG_PMEMCHECK_ENABLED=1)
148+
endif()
135149
endif()
136150

151+
# XXX: move under if(BUILD_TESTS)
137152
# Some tests and examples require clang >= 8.0
138153
# because of the following bug:
139154
# https://bugs.llvm.org/show_bug.cgi?id=28280
@@ -159,27 +174,7 @@ if(BUILD_TESTS OR BUILD_EXAMPLES OR BUILD_BENCHMARKS)
159174
find_package(LIBPMEM REQUIRED ${LIBPMEM_REQUIRED_VERSION})
160175
endif()
161176

162-
if (LIBPMEMOBJ_VERSION AND LIBPMEMOBJ_VERSION MATCHES "[0-9]+[.][0-9]+.*")
163-
string(REGEX REPLACE "\\+git.*" "" LIBPMEMOBJ_VERSION_SHORT ${LIBPMEMOBJ_VERSION})
164-
string(REGEX REPLACE "-rc.*" "" LIBPMEMOBJ_VERSION_SHORT ${LIBPMEMOBJ_VERSION_SHORT})
165-
string(REPLACE "." ";" VERSION_LIST ${LIBPMEMOBJ_VERSION_SHORT})
166-
list(GET VERSION_LIST 0 LIBPMEMOBJ_VERSION_MAJOR)
167-
list(GET VERSION_LIST 1 LIBPMEMOBJ_VERSION_MINOR)
168-
list(LENGTH VERSION_LIST OBJ_VER_COMPS)
169-
if (${OBJ_VER_COMPS} LESS 3)
170-
list(APPEND VERSION_LIST "0")
171-
endif()
172-
list(GET VERSION_LIST 2 LIBPMEMOBJ_VERSION_PATCH)
173-
else()
174-
message(WARNING "cannot detect libpmemobj version, some tests will be skipped")
175-
# assume 0.0.0
176-
set(LIBPMEMOBJ_VERSION_MAJOR 0)
177-
set(LIBPMEMOBJ_VERSION_MINOR 0)
178-
set(LIBPMEMOBJ_VERSION_PATCH 0)
179-
endif()
180-
181-
math(EXPR LIBPMEMOBJ_VERSION_NUM "${LIBPMEMOBJ_VERSION_PATCH} + ${LIBPMEMOBJ_VERSION_MINOR} * 100 + ${LIBPMEMOBJ_VERSION_MAJOR} * 10000")
182-
177+
# XXX: required by tests only?
183178
include(tbb)
184179
endif()
185180

@@ -189,18 +184,8 @@ add_custom_target(cppformat)
189184
add_custom_target(check-whitespace)
190185
add_custom_target(check-license
191186
COMMAND ${CMAKE_SOURCE_DIR}/utils/check_license/check-headers.sh
192-
${CMAKE_SOURCE_DIR}
193-
BSD-3-Clause)
194-
195-
add_custom_target(check-whitespace-main
196-
COMMAND ${PERL_EXECUTABLE}
197-
${CMAKE_SOURCE_DIR}/utils/check_whitespace
198-
${CMAKE_SOURCE_DIR}/utils/check_license/*.sh
199-
${CMAKE_SOURCE_DIR}/README.md)
200-
201-
add_dependencies(check-whitespace check-whitespace-main)
202-
203-
add_custom_target(tests)
187+
${CMAKE_SOURCE_DIR}
188+
BSD-3-Clause)
204189

205190
if(CHECK_CPP_STYLE)
206191
find_program(CLANG_FORMAT NAMES clang-format clang-format-9 clang-format-9.0)
@@ -214,31 +199,44 @@ if(CHECK_CPP_STYLE)
214199
else()
215200
message(FATAL_ERROR "CHECK_CPP_STYLE=ON, but clang-format not found (required version: ${CLANG_FORMAT_REQUIRED})")
216201
endif()
202+
203+
add_dependencies(checkers cppstyle)
217204
endif()
218205

219206
if(DEVELOPER_MODE)
207+
# treat compiler warnings as errors
220208
if(WIN32)
221209
add_flag(-WX)
222210
else()
223211
add_flag(-Werror)
212+
213+
# check for required programs for whitespace and license checks and add dependencies to ALL
214+
if(NOT PERL_FOUND)
215+
message(FATAL_ERROR "Perl not found")
216+
endif()
217+
if(PERL_VERSION_STRING VERSION_LESS 5.16)
218+
message(FATAL_ERROR "Too old Perl (<5.16)")
219+
endif()
224220
execute_process(COMMAND ${PERL_EXECUTABLE} -MText::Diff -e ""
225-
ERROR_QUIET
226-
RESULT_VARIABLE PERL_TEXT_DIFF_STATUS)
227-
if (PERL_TEXT_DIFF_STATUS)
221+
ERROR_QUIET
222+
RESULT_VARIABLE PERL_TEXT_DIFF_STATUS)
223+
if(PERL_TEXT_DIFF_STATUS)
228224
message(FATAL_ERROR "Text::Diff Perl module not found (install libtext-diff-perl or perl-Text-Diff)")
229225
endif()
230226

231-
add_dependencies(checkers cppstyle)
232227
add_dependencies(checkers check-whitespace)
233228
add_dependencies(checkers check-license)
234229
endif()
235-
endif(DEVELOPER_MODE)
230+
endif()
236231

237232
add_cppstyle(include ${CMAKE_CURRENT_SOURCE_DIR}/include/libpmemobj++/*.hpp)
238233
add_cppstyle(include-container ${CMAKE_CURRENT_SOURCE_DIR}/include/libpmemobj++/container/*.hpp)
239234
add_cppstyle(include-container-detail ${CMAKE_CURRENT_SOURCE_DIR}/include/libpmemobj++/container/detail/*.hpp)
240235
add_cppstyle(include-detail ${CMAKE_CURRENT_SOURCE_DIR}/include/libpmemobj++/detail/*.hpp)
241236
add_cppstyle(include-experimental ${CMAKE_CURRENT_SOURCE_DIR}/include/libpmemobj++/experimental/*.hpp)
237+
238+
add_check_whitespace(main ${CMAKE_SOURCE_DIR}/utils/check_license/*.sh
239+
${CMAKE_SOURCE_DIR}/README.md)
242240
add_check_whitespace(include ${CMAKE_CURRENT_SOURCE_DIR}/include/libpmemobj++/*.hpp)
243241
add_check_whitespace(include-container ${CMAKE_CURRENT_SOURCE_DIR}/include/libpmemobj++/container/*.hpp)
244242
add_check_whitespace(include-container-detail ${CMAKE_CURRENT_SOURCE_DIR}/include/libpmemobj++/container/detail/*.hpp)
@@ -247,6 +245,7 @@ add_check_whitespace(include-experimental ${CMAKE_CURRENT_SOURCE_DIR}/include/li
247245
add_check_whitespace(cmake-main ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt)
248246
add_check_whitespace(cmake-helpers ${CMAKE_CURRENT_SOURCE_DIR}/cmake/*.cmake)
249247

248+
## Configure make install/uninstall and packages
250249
configure_file(${CMAKE_SOURCE_DIR}/cmake/version.hpp.in
251250
${CMAKE_SOURCE_DIR}/include/libpmemobj++/version.hpp @ONLY)
252251

@@ -316,7 +315,6 @@ if(INSTALL_RADIX_TREE)
316315
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} FILES_MATCHING PATTERN "radix_tree.hpp")
317316
endif()
318317

319-
320318
install(DIRECTORY examples/ DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples
321319
FILES_MATCHING PATTERN "*.*pp")
322320

@@ -346,29 +344,17 @@ write_basic_package_version_file(libpmemobj++-config-version.cmake
346344
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpmemobj++-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/libpmemobj++-config-version.cmake
347345
DESTINATION ${CMAKE_INSTALL_LIBDIR}/libpmemobj++/cmake)
348346

347+
348+
# set up project's include dir (our source 'include' dir)
349349
include_directories(include)
350350

351+
# Run checks for known issues, it's required to enable/disable some tests and examples.
352+
# It has to be executed (included) here, when environment is fully set up,
353+
# all packages are found and all paths/variables are set.
351354
include(${CMAKE_SOURCE_DIR}/cmake/check_compiling_issues.cmake)
352355

353-
if(PKG_CONFIG_FOUND)
354-
pkg_check_modules(VALGRIND QUIET valgrind)
355-
else()
356-
find_package(VALGRIND QUIET)
357-
endif()
358-
359-
if(VALGRIND_FOUND)
360-
add_flag(-DLIBPMEMOBJ_CPP_VG_MEMCHECK_ENABLED=1)
361-
add_flag(-DLIBPMEMOBJ_CPP_VG_DRD_ENABLED=1)
362-
add_flag(-DLIBPMEMOBJ_CPP_VG_HELGRIND_ENABLED=1)
363-
364-
include_directories(${VALGRIND_INCLUDE_DIRS})
365-
find_pmemcheck()
366-
367-
if(VALGRIND_PMEMCHECK_FOUND)
368-
add_flag(-DLIBPMEMOBJ_CPP_VG_PMEMCHECK_ENABLED=1)
369-
endif()
370-
endif()
371356

357+
## Add/include sub-directories if build options enabled them
372358
if(BUILD_TESTS)
373359
if(TEST_DIR)
374360
enable_testing()
@@ -383,14 +369,14 @@ if(BUILD_DOC)
383369
add_subdirectory(doc)
384370
endif()
385371

386-
if (BUILD_BENCHMARKS)
372+
if(BUILD_BENCHMARKS)
387373
add_subdirectory(benchmarks)
388374
endif()
389375

390376
if(BUILD_EXAMPLES AND NO_GCC_VARIADIC_TEMPLATE_BUG)
391377
add_subdirectory(examples)
392378
elseif(BUILD_EXAMPLES)
393-
message(WARNING "Skipping build of examples because of compiler issue")
379+
message(WARNING "Skipping building of examples because of compiler issue")
394380
endif()
395381

396382
if(NOT "${CPACK_GENERATOR}" STREQUAL "")

0 commit comments

Comments
 (0)