leveldb cmakelist注解

最近学习使用CMake,看了一些文章感觉还是不会,索性就拿一个开源代码的CMakeList.txt来看,直接学习其写法。

CMakeList.txt示例

以下是leveldb的CMakeList.txt,其中##部分是我加入的注释,这个CMakeList.txt基本涵盖了CMake的基本功能,看懂以后在自己的项目中也基本会用 了。

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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# Copyright 2017 The LevelDB Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. See the AUTHORS file for names of contributors.

cmake_minimum_required(VERSION 3.9)
# Keep the version below in sync with the one in db.h
project(leveldb VERSION 1.21.0 LANGUAGES C CXX)

# This project can use C11, but will gracefully decay down to C89.
## Similar to CXX_STANDARD
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED OFF)
set(CMAKE_C_EXTENSIONS OFF)

# This project requires C++11.
## The C++ standard whose features are requested to build this target.
##
## This property specifies the C++ standard whose features are requested to build this target. For
## some compilers, this results in adding a flag such as -std=gnu++11 to the compile line. For
## compilers that have no notion of a standard level, such as Microsoft Visual C++ before 2015
## Update 3, this has no effect.
##
## Supported values are 98, 11, 14, 17, and 20.
set(CMAKE_CXX_STANDARD 11)
## Boolean describing whether the value of CXX_STANDARD is a requirement.
##
## If this property is set to ON, then the value of the CXX_STANDARD target property is treated
## as a requirement. If this property is OFF or unset, the CXX_STANDARD target property is treated
## as optional and may “decay” to a previous standard if the requested is not available. For
## compilers that have no notion of a standard level, such as MSVC, this has no effect.
set(CMAKE_CXX_STANDARD_REQUIRED ON)
## Boolean specifying whether compiler specific extensions are requested.
##
## This property specifies whether compiler specific extensions should be used. For some compilers,
## this results in adding a flag such as -std=gnu++11 instead of -std=c++11 to the compile line.
## This property is ON by default. The basic C++ standard level is controlled by the CXX_STANDARD
## target property.
set(CMAKE_CXX_EXTENSIONS OFF)

## WIN32为运行cmake时指定的宏
if (WIN32)
set(LEVELDB_PLATFORM_NAME LEVELDB_PLATFORM_WINDOWS)
# TODO(cmumford): Make UNICODE configurable for Windows.
## Add -D define flags to the compilation of source files.
##
## add_definitions(-DFOO -DBAR ...)
## Adds definitions to the compiler command line for targets in the current directory and below
## (whether added before or after this command is invoked). This command can be used to add any
## flags, but it is intended to add preprocessor definitions.
add_definitions(-D_UNICODE -DUNICODE)
else (WIN32)
set(LEVELDB_PLATFORM_NAME LEVELDB_PLATFORM_POSIX)
endif (WIN32)

## Provide an option that the user can optionally select.
##
## option(<variable> "<help_text>" [value])
## Provides an option for the user to select as ON or OFF. If no initial <value> is provided, OFF
## is used. If <variable> is already set as a normal variable then the command does nothing.
## 可通过命令行运行时更改默认值
## 比如 -DLEVELDB_BUILD_TESTS=OFF
option(LEVELDB_BUILD_TESTS "Build LevelDB's unit tests" ON)
option(LEVELDB_BUILD_BENCHMARKS "Build LevelDB's benchmarks" ON)
option(LEVELDB_INSTALL "Install LevelDB's header and library" ON)

## Load and run CMake code from a file or module.
##
## include(<file|module> [OPTIONAL] [RESULT_VARIABLE <var>]
## [NO_POLICY_SCOPE])
## Loads and runs CMake code from the file given. Variable reads and writes access the scope of the
## caller (dynamic scoping). If OPTIONAL is present, then no error is raised if the file does not
## exist. If RESULT_VARIABLE is given the variable <var> will be set to the full filename which has
## been included or NOTFOUND if it failed.
##
## If a module is specified instead of a file, the file with name <modulename>.cmake is searched
## first in CMAKE_MODULE_PATH, then in the CMake module directory. There is one exception to this:
## if the file which calls include() is located itself in the CMake builtin module directory, then
## first the CMake builtin module directory is searched and CMAKE_MODULE_PATH afterwards. See also
## policy CMP0017.
## 引入TestBigEndian模块(cmake内部模块)
include(TestBigEndian)
## 判断系统是大端还是小端,结果存在 LEVELDB_IS_BIG_ENDIAN 宏
test_big_endian(LEVELDB_IS_BIG_ENDIAN)

include(CheckIncludeFile)
## 判断头文件是否存在,结果存在 HAVE_UNISTD_H
check_include_file("unistd.h" HAVE_UNISTD_H)

## 同上面的原理
include(CheckLibraryExists)
check_library_exists(crc32c crc32c_value "" HAVE_CRC32C)
check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)
check_library_exists(tcmalloc malloc "" HAVE_TCMALLOC)

include(CheckCXXSymbolExists)
# Using check_cxx_symbol_exists() instead of check_c_symbol_exists() because
# we're including the header from C++, and feature detection should use the same
# compiler language that the project will use later. Principles aside, some
# versions of do not expose fdatasync() in <unistd.h> in standard C mode
# (-std=c11), but do expose the function in standard C++ mode (-std=c++11).
check_cxx_symbol_exists(fdatasync "unistd.h" HAVE_FDATASYNC)
check_cxx_symbol_exists(F_FULLFSYNC "fcntl.h" HAVE_FULLFSYNC)

include(CheckCXXSourceCompiles)

# Test whether -Wthread-safety is available. See
# https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
# -Werror is necessary because unknown attributes only generate warnings.
set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
## 把后两个编译选项添加到CMAKE_REQUIRED_FLAGS
list(APPEND CMAKE_REQUIRED_FLAGS -Werror -Wthread-safety)
## 判断第一个参数的代码是否能编译链接成可行性文件
check_cxx_source_compiles("
struct __attribute__((lockable)) Lock {
void Acquire() __attribute__((exclusive_lock_function()));
void Release() __attribute__((unlock_function()));
};
struct ThreadSafeType {
Lock lock_;
int data_ __attribute__((guarded_by(lock_)));
};
int main() { return 0; }
" HAVE_CLANG_THREAD_SAFETY)
set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})

# Test whether C++17 __has_include is available.
check_cxx_source_compiles("
#if defined(__has_include) && __has_include(<string>)
#include <string>
#endif
int main() { std::string str; return 0; }
" HAVE_CXX17_HAS_INCLUDE)

set(LEVELDB_PUBLIC_INCLUDE_DIR "include/leveldb")
set(LEVELDB_PORT_CONFIG_DIR "include/port")

## 复制源文件到目的地址,并替换其中的宏定义
configure_file(
"${PROJECT_SOURCE_DIR}/port/port_config.h.in"
"${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
)

## Add the given directories to those the compiler uses to search for include files. Relative
## paths are interpreted as relative to the current source directory.
include_directories(
"${PROJECT_BINARY_DIR}/include"
"${PROJECT_SOURCE_DIR}"
)

## 编译动态库,需要再命令行运行时指定宏
## 默认是静态?
if(BUILD_SHARED_LIBS)
# Only export LEVELDB_EXPORT symbols from the shared library.
## 编译时全部隐藏接口,只有在代码中显示暴露的才暴露
add_compile_options(-fvisibility=hidden)
endif(BUILD_SHARED_LIBS)

## 指定要生成的库为leveldb
add_library(leveldb "")
## 指定生成leveldb需要的源文件
## PRIVATE 表明生成leveldb的编译选项不传递(引用了leveldb的target,不确定就写private),可选值有private\public\interface
target_sources(leveldb
PRIVATE
## PROJECT_BINARY_DIR -- cmake构建时的根目录(通常就是build路径)
## PROJECT_SOURCE_DIR -- 源码目录,即cmake构建时指定的cmakelist.txt所在路径(通常是src目录)
"${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
"${PROJECT_SOURCE_DIR}/db/builder.cc"
"${PROJECT_SOURCE_DIR}/db/builder.h"
"${PROJECT_SOURCE_DIR}/db/c.cc"
"${PROJECT_SOURCE_DIR}/db/db_impl.cc"
"${PROJECT_SOURCE_DIR}/db/db_impl.h"
"${PROJECT_SOURCE_DIR}/db/db_iter.cc"
"${PROJECT_SOURCE_DIR}/db/db_iter.h"
"${PROJECT_SOURCE_DIR}/db/dbformat.cc"
"${PROJECT_SOURCE_DIR}/db/dbformat.h"
"${PROJECT_SOURCE_DIR}/db/dumpfile.cc"
"${PROJECT_SOURCE_DIR}/db/filename.cc"
"${PROJECT_SOURCE_DIR}/db/filename.h"
"${PROJECT_SOURCE_DIR}/db/log_format.h"
"${PROJECT_SOURCE_DIR}/db/log_reader.cc"
"${PROJECT_SOURCE_DIR}/db/log_reader.h"
"${PROJECT_SOURCE_DIR}/db/log_writer.cc"
"${PROJECT_SOURCE_DIR}/db/log_writer.h"
"${PROJECT_SOURCE_DIR}/db/memtable.cc"
"${PROJECT_SOURCE_DIR}/db/memtable.h"
"${PROJECT_SOURCE_DIR}/db/repair.cc"
"${PROJECT_SOURCE_DIR}/db/skiplist.h"
"${PROJECT_SOURCE_DIR}/db/snapshot.h"
"${PROJECT_SOURCE_DIR}/db/table_cache.cc"
"${PROJECT_SOURCE_DIR}/db/table_cache.h"
"${PROJECT_SOURCE_DIR}/db/version_edit.cc"
"${PROJECT_SOURCE_DIR}/db/version_edit.h"
"${PROJECT_SOURCE_DIR}/db/version_set.cc"
"${PROJECT_SOURCE_DIR}/db/version_set.h"
"${PROJECT_SOURCE_DIR}/db/write_batch_internal.h"
"${PROJECT_SOURCE_DIR}/db/write_batch.cc"
"${PROJECT_SOURCE_DIR}/port/port_stdcxx.h"
"${PROJECT_SOURCE_DIR}/port/port.h"
"${PROJECT_SOURCE_DIR}/port/thread_annotations.h"
"${PROJECT_SOURCE_DIR}/table/block_builder.cc"
"${PROJECT_SOURCE_DIR}/table/block_builder.h"
"${PROJECT_SOURCE_DIR}/table/block.cc"
"${PROJECT_SOURCE_DIR}/table/block.h"
"${PROJECT_SOURCE_DIR}/table/filter_block.cc"
"${PROJECT_SOURCE_DIR}/table/filter_block.h"
"${PROJECT_SOURCE_DIR}/table/format.cc"
"${PROJECT_SOURCE_DIR}/table/format.h"
"${PROJECT_SOURCE_DIR}/table/iterator_wrapper.h"
"${PROJECT_SOURCE_DIR}/table/iterator.cc"
"${PROJECT_SOURCE_DIR}/table/merger.cc"
"${PROJECT_SOURCE_DIR}/table/merger.h"
"${PROJECT_SOURCE_DIR}/table/table_builder.cc"
"${PROJECT_SOURCE_DIR}/table/table.cc"
"${PROJECT_SOURCE_DIR}/table/two_level_iterator.cc"
"${PROJECT_SOURCE_DIR}/table/two_level_iterator.h"
"${PROJECT_SOURCE_DIR}/util/arena.cc"
"${PROJECT_SOURCE_DIR}/util/arena.h"
"${PROJECT_SOURCE_DIR}/util/bloom.cc"
"${PROJECT_SOURCE_DIR}/util/cache.cc"
"${PROJECT_SOURCE_DIR}/util/coding.cc"
"${PROJECT_SOURCE_DIR}/util/coding.h"
"${PROJECT_SOURCE_DIR}/util/comparator.cc"
"${PROJECT_SOURCE_DIR}/util/crc32c.cc"
"${PROJECT_SOURCE_DIR}/util/crc32c.h"
"${PROJECT_SOURCE_DIR}/util/env.cc"
"${PROJECT_SOURCE_DIR}/util/filter_policy.cc"
"${PROJECT_SOURCE_DIR}/util/hash.cc"
"${PROJECT_SOURCE_DIR}/util/hash.h"
"${PROJECT_SOURCE_DIR}/util/logging.cc"
"${PROJECT_SOURCE_DIR}/util/logging.h"
"${PROJECT_SOURCE_DIR}/util/mutexlock.h"
"${PROJECT_SOURCE_DIR}/util/no_destructor.h"
"${PROJECT_SOURCE_DIR}/util/options.cc"
"${PROJECT_SOURCE_DIR}/util/random.h"
"${PROJECT_SOURCE_DIR}/util/status.cc"

# Only CMake 3.3+ supports PUBLIC sources in targets exported by "install".
## 看不懂
$<$<VERSION_GREATER:CMAKE_VERSION,3.2>:PUBLIC>
"${LEVELDB_PUBLIC_INCLUDE_DIR}/c.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/cache.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/comparator.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/db.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/dumpfile.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/env.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/export.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/filter_policy.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/iterator.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/options.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/slice.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/status.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/table_builder.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/table.h"
"${LEVELDB_PUBLIC_INCLUDE_DIR}/write_batch.h"
)

if (WIN32)
target_sources(leveldb
PRIVATE
"${PROJECT_SOURCE_DIR}/util/env_windows.cc"
"${PROJECT_SOURCE_DIR}/util/windows_logger.h"
)
else (WIN32)
target_sources(leveldb
PRIVATE
"${PROJECT_SOURCE_DIR}/util/env_posix.cc"
"${PROJECT_SOURCE_DIR}/util/posix_logger.h"
)
endif (WIN32)

# MemEnv is not part of the interface and could be pulled to a separate library.
target_sources(leveldb
PRIVATE
"${PROJECT_SOURCE_DIR}/helpers/memenv/memenv.cc"
"${PROJECT_SOURCE_DIR}/helpers/memenv/memenv.h"
)

## build和install时候的包含路径是不同的
## 不同的情形下运用不同的变量
## CMAKE_INSTALL_INCLUDEDIR的默认值是 include
target_include_directories(leveldb
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_compile_definitions(leveldb
PRIVATE
# Used by include/export.h when building shared libraries.
LEVELDB_COMPILE_LIBRARY
# Used by port/port.h.
${LEVELDB_PLATFORM_NAME}=1
)
if (NOT HAVE_CXX17_HAS_INCLUDE)
target_compile_definitions(leveldb
PRIVATE
LEVELDB_HAS_PORT_CONFIG_H=1
)
endif(NOT HAVE_CXX17_HAS_INCLUDE)

if(BUILD_SHARED_LIBS)
target_compile_definitions(leveldb
PUBLIC
# Used by include/export.h.
LEVELDB_SHARED_LIBRARY
)
endif(BUILD_SHARED_LIBS)

if(HAVE_CLANG_THREAD_SAFETY)
target_compile_options(leveldb
PUBLIC
-Werror -Wthread-safety)
endif(HAVE_CLANG_THREAD_SAFETY)

if(HAVE_CRC32C)
target_link_libraries(leveldb crc32c)
endif(HAVE_CRC32C)
if(HAVE_SNAPPY)
target_link_libraries(leveldb snappy)
endif(HAVE_SNAPPY)
if(HAVE_TCMALLOC)
target_link_libraries(leveldb tcmalloc)
endif(HAVE_TCMALLOC)

# Needed by port_stdcxx.h
## 查找相关的库的依赖
find_package(Threads REQUIRED)
target_link_libraries(leveldb Threads::Threads)

add_executable(leveldbutil
"${PROJECT_SOURCE_DIR}/db/leveldbutil.cc"
)
target_link_libraries(leveldbutil leveldb)

if(LEVELDB_BUILD_TESTS)
## test必须要有这个
enable_testing()

function(leveldb_test test_file)
## 提取 test_file 中的文件名,把目录和后缀都去掉
## 例如:your_path/my_test.cc -> my_test
## 结果存在 test_target_name 中
get_filename_component(test_target_name "${test_file}" NAME_WE)

add_executable("${test_target_name}" "")
target_sources("${test_target_name}"
PRIVATE
"${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
"${PROJECT_SOURCE_DIR}/util/testharness.cc"
"${PROJECT_SOURCE_DIR}/util/testharness.h"
"${PROJECT_SOURCE_DIR}/util/testutil.cc"
"${PROJECT_SOURCE_DIR}/util/testutil.h"

"${test_file}"
)
target_link_libraries("${test_target_name}" leveldb)
target_compile_definitions("${test_target_name}"
PRIVATE
${LEVELDB_PLATFORM_NAME}=1
)
if (NOT HAVE_CXX17_HAS_INCLUDE)
target_compile_definitions("${test_target_name}"
PRIVATE
LEVELDB_HAS_PORT_CONFIG_H=1
)
endif(NOT HAVE_CXX17_HAS_INCLUDE)

## 向项目添加一个测试,让ctest能够运行
## 测试名称NAME--test_target_name,测试启动命令COMMAND--test_target_name
add_test(NAME "${test_target_name}" COMMAND "${test_target_name}")
endfunction(leveldb_test)

leveldb_test("${PROJECT_SOURCE_DIR}/db/c_test.c")
leveldb_test("${PROJECT_SOURCE_DIR}/db/fault_injection_test.cc")

leveldb_test("${PROJECT_SOURCE_DIR}/issues/issue178_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/issues/issue200_test.cc")

leveldb_test("${PROJECT_SOURCE_DIR}/util/env_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/util/status_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/util/no_destructor_test.cc")

if(NOT BUILD_SHARED_LIBS)
leveldb_test("${PROJECT_SOURCE_DIR}/db/autocompact_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/corruption_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/db_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/dbformat_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/filename_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/log_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/recovery_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/skiplist_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/version_edit_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/version_set_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/write_batch_test.cc")

leveldb_test("${PROJECT_SOURCE_DIR}/helpers/memenv/memenv_test.cc")

leveldb_test("${PROJECT_SOURCE_DIR}/table/filter_block_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/table/table_test.cc")

leveldb_test("${PROJECT_SOURCE_DIR}/util/arena_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/util/bloom_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/util/cache_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/util/coding_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/util/crc32c_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/util/hash_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/util/logging_test.cc")

# TODO(costan): This test also uses
# "${PROJECT_SOURCE_DIR}/util/env_{posix|windows}_test_helper.h"
if (WIN32)
leveldb_test("${PROJECT_SOURCE_DIR}/util/env_windows_test.cc")
else (WIN32)
leveldb_test("${PROJECT_SOURCE_DIR}/util/env_posix_test.cc")
endif (WIN32)
endif(NOT BUILD_SHARED_LIBS)
endif(LEVELDB_BUILD_TESTS)

## 与test部分类似
if(LEVELDB_BUILD_BENCHMARKS)
function(leveldb_benchmark bench_file)
get_filename_component(bench_target_name "${bench_file}" NAME_WE)

add_executable("${bench_target_name}" "")
target_sources("${bench_target_name}"
PRIVATE
"${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
"${PROJECT_SOURCE_DIR}/util/histogram.cc"
"${PROJECT_SOURCE_DIR}/util/histogram.h"
"${PROJECT_SOURCE_DIR}/util/testharness.cc"
"${PROJECT_SOURCE_DIR}/util/testharness.h"
"${PROJECT_SOURCE_DIR}/util/testutil.cc"
"${PROJECT_SOURCE_DIR}/util/testutil.h"

"${bench_file}"
)
target_link_libraries("${bench_target_name}" leveldb)
target_compile_definitions("${bench_target_name}"
PRIVATE
${LEVELDB_PLATFORM_NAME}=1
)
if (NOT HAVE_CXX17_HAS_INCLUDE)
target_compile_definitions("${bench_target_name}"
PRIVATE
LEVELDB_HAS_PORT_CONFIG_H=1
)
endif(NOT HAVE_CXX17_HAS_INCLUDE)
endfunction(leveldb_benchmark)

if(NOT BUILD_SHARED_LIBS)
leveldb_benchmark("${PROJECT_SOURCE_DIR}/db/db_bench.cc")
endif(NOT BUILD_SHARED_LIBS)

check_library_exists(sqlite3 sqlite3_open "" HAVE_SQLITE3)
if(HAVE_SQLITE3)
leveldb_benchmark("${PROJECT_SOURCE_DIR}/doc/bench/db_bench_sqlite3.cc")
target_link_libraries(db_bench_sqlite3 sqlite3)
endif(HAVE_SQLITE3)

# check_library_exists is insufficient here because the library names have
# different manglings when compiled with clang or gcc, at least when installed
# with Homebrew on Mac.
set(OLD_CMAKE_REQURED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
list(APPEND CMAKE_REQUIRED_LIBRARIES kyotocabinet)
check_cxx_source_compiles("
#include <kcpolydb.h>

int main() {
kyotocabinet::TreeDB* db = new kyotocabinet::TreeDB();
delete db;
return 0;
}
" HAVE_KYOTOCABINET)
## 临时替换一个变量
set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQURED_LIBRARIES})
if(HAVE_KYOTOCABINET)
leveldb_benchmark("${PROJECT_SOURCE_DIR}/doc/bench/db_bench_tree_db.cc")
target_link_libraries(db_bench_tree_db kyotocabinet)
endif(HAVE_KYOTOCABINET)
endif(LEVELDB_BUILD_BENCHMARKS)

## 安装这部分没看懂
if(LEVELDB_INSTALL)
## 引入安装模块
include(GNUInstallDirs)
## TARGETS -- 指定安装的模块名称
install(TARGETS leveldb
## EXPORT -- This option associates the installed target files with an
## export called <export-name>.
## It must appear before any target options.
EXPORT leveldbTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(
FILES
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/c.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/cache.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/comparator.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/db.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/dumpfile.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/env.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/export.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/filter_policy.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/iterator.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/options.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/slice.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/status.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/table_builder.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/table.h"
"${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/write_batch.h"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/leveldb
)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/leveldbConfigVersion.cmake"
COMPATIBILITY SameMajorVersion
)
install(
EXPORT leveldbTargets
NAMESPACE leveldb::
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/leveldb"
)
install(
FILES
"${PROJECT_SOURCE_DIR}/cmake/leveldbConfig.cmake"
"${PROJECT_BINARY_DIR}/leveldbConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/leveldb"
)
endif(LEVELDB_INSTALL)

cmake命令

构建

cmake {your_source_path}

cmake 会去your_source_path找CMakeList.txt,根据这个文件生成相应的文件(主要是平台相关的makefile)。生成文件的目录就是当前目录。

编译

make 或者 cmake –build

后面也可以指定生成路径。

测试

make test

生成的 makefile 中有 test target,这里用的是ctest测试框架。

安装

安装还没测过,不过应该同理,在makefile里面生成 install target。