This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
CMakeLists.txt
507 lines (430 loc) · 15.6 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#
# Copyright 2017, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cmake_minimum_required(VERSION 3.3)
project(pmemfile C CXX)
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
set(VERSION_MAJOR 0)
set(VERSION_MINOR 4)
set(VERSION_PATCH 0)
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
include(FindThreads)
include(CMakeDependentOption)
# XXX rename libpmemfile, since the whole repo is also just
# called pmemfile!
option(BUILD_LIBPMEMFILE
"build libpmemfile (requires libsyscall_intercept)" ON)
cmake_dependent_option(BUILD_LIBPMEMFILE_TESTS
"build libpmemfile tests" ON "BUILD_LIBPMEMFILE" OFF)
option(BUILD_LIBPMEMFILE_POSIX_TESTS "build libpmemfile-posix tests" ON)
option(TRACE_TESTS
"more verbose test outputs" OFF)
option(LONG_TESTS "build long running tests as well" OFF)
option(AUTO_GENERATE_SOURCES
"Use libclang to (re)generate certain source files -- this is required for the modification of some sources (e.g. libpmemfile-posix-wrappers.h must be regenerated each time libpmemfile-posix.h is modified" OFF)
option(BUILD_LIBPMEMFILE_POP "build libpmemfile_pop (libpmemfile_posix_over_posix) library" OFF)
option(BUILD_PMEMFILE_FUSE
"build pmemfile-fuse (requires libfuse)" ON)
option(LIBPMEMFILE_VALIDATE_POINTERS
"compiles in support for PMEMFILE_PRELOAD_VALIDATE_POINTERS environment variable" OFF)
set(TEST_DIR ${CMAKE_CURRENT_BINARY_DIR}/test
CACHE STRING "working directory for tests")
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RelWithDebInfo")
endif (NOT CMAKE_BUILD_TYPE)
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
execute_process(COMMAND git describe
OUTPUT_VARIABLE SRCVERSION
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
if(NOT SRCVERSION)
execute_process(COMMAND git log -1 --format=%h
OUTPUT_VARIABLE SRCVERSION
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
else()
execute_process(COMMAND cat .version
OUTPUT_VARIABLE SRCVERSION
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
if(WIN32)
message(FATAL_ERROR "Windows is not supported")
endif()
include(FindPerl)
if(NOT PERL_FOUND)
message(FATAL_ERROR "Perl not found")
endif()
if (PERL_VERSION_STRING VERSION_LESS 5.16)
message(FATAL_ERROR "Too old Perl (<5.16)")
endif()
add_definitions(-DSRCVERSION="${SRCVERSION}")
add_definitions(-DPMEMFILE_MAJOR_VERSION=${VERSION_MAJOR})
add_definitions(-DPMEMFILE_MINOR_VERSION=${VERSION_MINOR})
if(NOT WIN32)
add_definitions(-DUSE_VALGRIND)
endif()
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include(GNUInstallDirs)
find_package(PkgConfig QUIET)
include_directories(include)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
function(join SEP OUT VALUES)
string(REPLACE ";" "${SEP}" JOIN_TMP "${VALUES}")
set(${OUT} "${JOIN_TMP}" PARENT_SCOPE)
endfunction()
# Checks whether flag is supported by current C compiler and appends
# it to the relevant cmake variable.
# 1st argument is a flag
# 2nd (optional) argument is a build type (debug, release, relwithdebinfo)
macro(add_c_flag flag)
string(REPLACE - _ flag2 ${flag})
string(REPLACE " " _ flag2 ${flag2})
string(REPLACE = "_" flag2 ${flag2})
set(check_name "C_HAS_${flag2}")
check_c_compiler_flag("${flag}" "${check_name}")
if (${${check_name}})
if (${ARGC} EQUAL 1)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
else()
set(CMAKE_C_FLAGS_${ARGV1} "${CMAKE_C_FLAGS_${ARGV1}} ${flag}")
endif()
endif()
endmacro()
# Checks whether flag is supported by current C++ compiler and appends
# it to the relevant cmake variable.
# 1st argument is a flag
# 2nd (optional) argument is a build type (debug, release, relwithdebinfo)
macro(add_cxx_flag flag)
string(REPLACE - _ flag2 ${flag})
string(REPLACE " " _ flag2 ${flag2})
string(REPLACE = "_" flag2 ${flag2})
set(check_name "CXX_HAS_${flag2}")
check_cxx_compiler_flag(${flag} ${check_name})
if (${${check_name}})
if (${ARGC} EQUAL 1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
else()
set(CMAKE_CXX_FLAGS_${ARGV1} "${CMAKE_CXX_FLAGS_${ARGV1}} ${flag}")
endif()
endif()
endmacro()
# Checks whether flag is supported by current C & C++ compilers and appends
# it to the relevant cmake variables.
# 1st argument is a flag
# 2nd (optional) argument is a build type (debug, release, relwithdebinfo)
macro(add_flag flag)
add_c_flag(${flag} ${ARGN})
add_cxx_flag(${flag} ${ARGN})
endmacro()
add_flag(-Wall)
add_flag(-Wpointer-arith)
add_flag(-Wunused-macros)
add_flag(-Wsign-conversion)
add_flag(-Wsign-compare)
add_flag(-Wconversion)
add_flag(-Wunreachable-code-return)
add_flag(-Wmissing-variable-declarations)
add_flag(-fno-common)
# Conditionally using the -Wno-missing-field-initializers flag.
#
# As of 2017, the following clang erroneously issues a warning for
# the following code:
#
# struct x { int a; int b; int c; };
#
# struct x variable = {0};
#
# The warning is meant to warn about fields not being initialized while
# using this syntax in C++, but the clang warns while compiling C as well.
#
# See: https://llvm.org/bugs/show_bug.cgi?id=21689
# See also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36750
if("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
add_c_flag(-Wno-missing-field-initializers)
endif()
add_c_flag(-Wmissing-prototypes)
check_c_compiler_flag(-Wl,-z,relro LINKER_HAS_RELRO)
if(LINKER_HAS_RELRO)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,relro")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,relro")
endif()
check_c_compiler_flag(-Wl,--warn-common LINKER_HAS_WARN_COMMON)
if(LINKER_HAS_WARN_COMMON)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--warn-common")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--warn-common")
endif()
add_flag(-ggdb DEBUG)
add_flag(-DDEBUG DEBUG)
add_flag(-ggdb RELWITHDEBINFO)
add_flag(-fno-omit-frame-pointer RELWITHDEBINFO)
add_flag("-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2" RELEASE)
add_flag(-D_LARGEFILE64_SOURCE)
option(USE_ASAN "enable AddressSanitizer (debugging) (using with BUILD_LIBPMEMFILE=ON is experimental)" OFF)
option(USE_UBSAN "enable UndefinedBehaviorSanitizer (debugging) (using with BUILD_LIBPMEMFILE=ON is experimental)" OFF)
set(ASAN_RUNTIME "" CACHE STRING "(experimental) path to preloadable lib for sanitizers e.g.: /usr/lib/gcc/x86_64-linux-gnu/6/libasan.so")
macro(add_sanitizer_flag flag)
if(BUILD_LIBPMEMFILE)
message(WARNING "Sanitizers might be incompatible with LIBPMEMFILE. Running tests with sanitizers and BUILD_LIBPMEMFILE is experimental")
endif()
set(SAVED_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES} -fsanitize=${flag}")
check_c_compiler_flag("-fsanitize=${flag}" C_HAS_ASAN_UBSAN)
if(C_HAS_ASAN_UBSAN)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=${flag}")
endif()
check_cxx_compiler_flag("-fsanitize=${flag}" CXX_HAS_ASAN_UBSAN)
if(CXX_HAS_ASAN_UBSAN)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=${flag}")
endif()
if(C_HAS_ASAN_UBSAN OR CXX_HAS_ASAN_UBSAN)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=${flag}")
endif()
set(CMAKE_REQUIRED_LIBRARIES ${SAVED_CMAKE_REQUIRED_LIBRARIES})
endmacro()
if(USE_ASAN)
add_sanitizer_flag(address)
endif()
if(USE_UBSAN)
add_sanitizer_flag(undefined)
endif()
if(DEVELOPER_MODE)
option(FAULT_INJECTION "enable fault injection testing" ON)
else()
option(FAULT_INJECTION "enable fault injection testing" OFF)
endif()
if(DEVELOPER_MODE)
add_flag(-Werror)
check_c_compiler_flag(-Wl,--fatal-warnings LINKER_HAS_FATAL_WARNINGS)
if(LINKER_HAS_FATAL_WARNINGS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--fatal-warnings")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--fatal-warnings")
endif()
endif(DEVELOPER_MODE)
if(FAULT_INJECTION)
add_definitions(-DFAULT_INJECTION)
endif()
if(LIBPMEMFILE_VALIDATE_POINTERS)
add_definitions(-DVALIDATE_POINTERS)
endif()
add_executable(check_license EXCLUDE_FROM_ALL utils/check_license/check-license.c)
# Generates cstyle-$name target and attaches it as a dependency of global
# "cstyle" target. This target verifies C style of files in current source dir.
# If more arguments are used, then they are used as files to be checked
# instead.
# ${name} must be unique.
function(add_cstyle name)
if(${ARGC} EQUAL 1)
add_custom_target(cstyle-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/cstyle
${CMAKE_CURRENT_SOURCE_DIR}/*.c
${CMAKE_CURRENT_SOURCE_DIR}/*.h)
else()
add_custom_target(cstyle-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/cstyle ${ARGN})
endif()
add_dependencies(cstyle cstyle-${name})
endfunction()
# Generates cppstyle-$name and cppformat-$name targets and attaches them
# as dependencies of global "cstyle" and "cppformat" targets.
# cppstyle-$name target verifies C++ style of files in current source dir.
# cppformat-$name target reformats files in current source dir.
# If more arguments are used, then they are used as files to be checked
# instead.
# ${name} must be unique.
function(add_cppstyle name)
if(NOT CLANG_FORMAT)
return()
endif()
if(${ARGC} EQUAL 1)
add_custom_target(cppstyle-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/cppstyle
${CLANG_FORMAT}
check
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/*.hpp
)
add_custom_target(cppformat-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/cppstyle
${CLANG_FORMAT}
format
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/*.hpp
)
else()
add_custom_target(cppstyle-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/cppstyle
${CLANG_FORMAT}
check
${ARGN}
)
add_custom_target(cppformat-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/cppstyle
${CLANG_FORMAT}
format
${ARGN}
)
endif()
add_dependencies(cppstyle cppstyle-${name})
add_dependencies(cppformat cppformat-${name})
endfunction()
# Generates check-whitespace-$name target and attaches it as a dependency
# of global "check-whitespace" target. This target verifies C files in current
# source dir do not have any whitespace errors.
# If more arguments are used, then they are used as files to be checked
# instead.
# ${name} must be unique.
function(add_check_whitespace name)
if(${ARGC} EQUAL 1)
add_custom_target(check-whitespace-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/check_whitespace
${CMAKE_CURRENT_SOURCE_DIR}/*.c
${CMAKE_CURRENT_SOURCE_DIR}/*.h)
else()
add_custom_target(check-whitespace-${name}
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/check_whitespace ${ARGN})
endif()
add_dependencies(check-whitespace check-whitespace-${name})
endfunction()
add_custom_target(checkers ALL)
add_custom_target(cstyle)
add_custom_target(cppstyle)
add_custom_target(cppformat)
add_custom_target(check-whitespace)
add_custom_target(check-license
COMMAND ${CMAKE_SOURCE_DIR}/utils/check_license/check-headers.sh
${CMAKE_SOURCE_DIR}
${CMAKE_BINARY_DIR}/check_license
${CMAKE_SOURCE_DIR}/LICENSE
-a)
add_dependencies(check-license check_license)
add_cstyle(check_license ${CMAKE_SOURCE_DIR}/utils/check_license/*.c)
add_check_whitespace(check_license ${CMAKE_SOURCE_DIR}/utils/check_license/*.c)
add_check_whitespace(external_tests -r ${CMAKE_SOURCE_DIR}/utils/docker/external_tests)
add_custom_target(check-whitespace-main
COMMAND ${PERL_EXECUTABLE}
${CMAKE_SOURCE_DIR}/utils/check_whitespace
${CMAKE_SOURCE_DIR}/utils/check_license/*.sh
${CMAKE_SOURCE_DIR}/utils/*.sh
${CMAKE_SOURCE_DIR}/README.md
${CMAKE_SOURCE_DIR}/*.spec
${CMAKE_SOURCE_DIR}/debian/*
${CMAKE_SOURCE_DIR}/debian/*/*
${CMAKE_SOURCE_DIR}/doc/*.md)
add_dependencies(check-whitespace check-whitespace-main)
add_custom_target(tests)
option(DEVELOPER_MODE "enable developer checks" OFF)
if(DEVELOPER_MODE)
find_program(CLANG_FORMAT NAMES clang-format-3.9 clang-format-3.8)
if (NOT CLANG_FORMAT)
message(WARNING "clang-format not found - C++ sources will not be checked (needed version: 3.8 or 3.9)")
endif()
execute_process(COMMAND ${PERL_EXECUTABLE} -MText::Diff -e ""
ERROR_QUIET
RESULT_VARIABLE PERL_TEXT_DIFF_STATUS)
if (PERL_TEXT_DIFF_STATUS)
message(FATAL_ERROR "Text::Diff Perl module not found (install libtext-diff-perl or perl-Text-Diff)")
endif()
add_dependencies(checkers cstyle)
add_dependencies(checkers cppstyle)
add_dependencies(checkers check-whitespace)
add_dependencies(checkers check-license)
endif(DEVELOPER_MODE)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
if(PKG_CONFIG_FOUND)
pkg_check_modules(VALGRIND QUIET valgrind)
else()
find_package(VALGRIND QUIET)
endif()
if(VALGRIND_FOUND)
set(ENV{PATH} ${VALGRIND_PREFIX}/bin:$ENV{PATH})
execute_process(COMMAND valgrind --tool=pmemcheck --help
RESULT_VARIABLE VALGRIND_PMEMCHECK_NOT_FOUND
OUTPUT_QUIET
ERROR_QUIET)
if(VALGRIND_PMEMCHECK_NOT_FOUND)
message(WARNING "Valgrind pmemcheck NOT found. Pmemcheck tests will not be performed.")
endif()
else()
message(WARNING "Valgrind not found. Valgrind tests will not be performed.")
endif()
if(PKG_CONFIG_FOUND)
pkg_check_modules(LIBUNWIND QUIET libunwind)
else()
find_package(LIBUNWIND QUIET)
endif()
if(NOT LIBUNWIND_FOUND)
message(WARNING "libunwind not found. Stack traces from tests will not be reliable")
endif()
if(BUILD_LIBPMEMFILE_POSIX_TESTS OR BUILD_LIBPMEMFILE_TESTS OR NOT (ANTOOL_TESTS STREQUAL SKIP))
set(BUILD_ANY_TESTS ON)
endif()
if(BUILD_ANY_TESTS)
if(TEST_DIR)
enable_testing()
else()
message(WARNING "TEST_DIR is empty - 'make test' will not work")
endif()
endif()
if(AUTO_GENERATE_SOURCES)
add_subdirectory(utils/transform)
endif()
if (BUILD_LIBPMEMFILE_POP)
add_subdirectory(tests/libpmemfile-pop)
endif()
add_subdirectory(src/libpmemfile-posix)
if(BUILD_LIBPMEMFILE)
add_subdirectory(src/libpmemfile)
endif()
if(BUILD_PMEMFILE_FUSE)
add_subdirectory(src/pmemfile-fuse)
endif()
add_subdirectory(src/tools)
if(BUILD_ANY_TESTS)
add_subdirectory(tests)
endif()
add_subdirectory(doc)