作者: 韩晨旭 10225101440 李畅 10225102463
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
15 KiB

  1. # Copyright 2017 The LevelDB Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. cmake_minimum_required(VERSION 3.9)
  5. project(Leveldb VERSION 0.1.0 LANGUAGES C CXX)
  6. # This project can take advantage of C++11.
  7. set(CMAKE_CXX_STANDARD 11)
  8. set(CMAKE_CXX_STANDARD_REQUIRED OFF)
  9. set(CMAKE_CXX_EXTENSIONS OFF)
  10. option(LEVELDB_BUILD_TESTS "Build LevelDB's unit tests" ON)
  11. option(LEVELDB_BUILD_BENCHMARKS "Build LevelDB's benchmarks" ON)
  12. option(LEVELDB_INSTALL "Install LevelDB's header and library" ON)
  13. include(TestBigEndian)
  14. test_big_endian(LEVELDB_IS_BIG_ENDIAN)
  15. include(CheckIncludeFile)
  16. check_include_file("unistd.h" HAVE_UNISTD_H)
  17. include(CheckIncludeFileCXX)
  18. check_include_file_cxx("atomic" LEVELDB_ATOMIC_PRESENT)
  19. include(CheckLibraryExists)
  20. check_library_exists(crc32c crc32c_value "" HAVE_CRC32C)
  21. check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)
  22. check_library_exists(tcmalloc malloc "" HAVE_TCMALLOC)
  23. include(CheckSymbolExists)
  24. check_symbol_exists(fdatasync "unistd.h" HAVE_FDATASYNC)
  25. include(CheckCXXSourceCompiles)
  26. # Test whether -Wthread-safety is available. See
  27. # https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
  28. # -Werror is necessary because unknown attributes only generate warnings.
  29. set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  30. list(APPEND CMAKE_REQUIRED_FLAGS -Werror -Wthread-safety)
  31. check_cxx_source_compiles("
  32. struct __attribute__((lockable)) Lock {
  33. void Acquire() __attribute__((exclusive_lock_function()));
  34. void Release() __attribute__((unlock_function()));
  35. };
  36. struct ThreadSafeType {
  37. Lock lock_;
  38. int data_ __attribute__((guarded_by(lock_)));
  39. };
  40. int main() { return 0; }
  41. " HAVE_CLANG_THREAD_SAFETY)
  42. set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  43. set(LEVELDB_PUBLIC_INCLUDE_DIR "include/leveldb")
  44. set(LEVELDB_PORT_CONFIG_DIR "include/port")
  45. configure_file(
  46. "${PROJECT_SOURCE_DIR}/port/port_config.h.in"
  47. "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
  48. )
  49. include_directories(
  50. "${PROJECT_BINARY_DIR}/include"
  51. "${PROJECT_SOURCE_DIR}"
  52. )
  53. if(BUILD_SHARED_LIBS)
  54. # Only export LEVELDB_EXPORT symbols from the shared library.
  55. add_compile_options(-fvisibility=hidden)
  56. endif(BUILD_SHARED_LIBS)
  57. # POSIX code is specified separately so we can leave it out in the future.
  58. add_library(leveldb_port_posix OBJECT "")
  59. target_sources(leveldb_port_posix
  60. PRIVATE
  61. "${PROJECT_SOURCE_DIR}/port/port_posix.cc"
  62. PUBLIC
  63. # The headers below are dependencies for leveldb, but aren't needed by users
  64. # that link to the installed version of leveldb and rely on its public API.
  65. $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h>
  66. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/port/atomic_pointer.h>
  67. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/port/port_posix.h>
  68. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/port/port.h>
  69. )
  70. if(BUILD_SHARED_LIBS)
  71. set_property(TARGET leveldb_port_posix PROPERTY POSITION_INDEPENDENT_CODE ON)
  72. endif(BUILD_SHARED_LIBS)
  73. add_library(leveldb "")
  74. target_sources(leveldb
  75. PRIVATE
  76. "${PROJECT_SOURCE_DIR}/db/builder.cc"
  77. "${PROJECT_SOURCE_DIR}/db/builder.h"
  78. "${PROJECT_SOURCE_DIR}/db/c.cc"
  79. "${PROJECT_SOURCE_DIR}/db/db_impl.cc"
  80. "${PROJECT_SOURCE_DIR}/db/db_impl.h"
  81. "${PROJECT_SOURCE_DIR}/db/db_iter.cc"
  82. "${PROJECT_SOURCE_DIR}/db/db_iter.h"
  83. "${PROJECT_SOURCE_DIR}/db/dbformat.cc"
  84. "${PROJECT_SOURCE_DIR}/db/dbformat.h"
  85. "${PROJECT_SOURCE_DIR}/db/dumpfile.cc"
  86. "${PROJECT_SOURCE_DIR}/db/filename.cc"
  87. "${PROJECT_SOURCE_DIR}/db/filename.h"
  88. "${PROJECT_SOURCE_DIR}/db/log_format.h"
  89. "${PROJECT_SOURCE_DIR}/db/log_reader.cc"
  90. "${PROJECT_SOURCE_DIR}/db/log_reader.h"
  91. "${PROJECT_SOURCE_DIR}/db/log_writer.cc"
  92. "${PROJECT_SOURCE_DIR}/db/log_writer.h"
  93. "${PROJECT_SOURCE_DIR}/db/memtable.cc"
  94. "${PROJECT_SOURCE_DIR}/db/memtable.h"
  95. "${PROJECT_SOURCE_DIR}/db/repair.cc"
  96. "${PROJECT_SOURCE_DIR}/db/skiplist.h"
  97. "${PROJECT_SOURCE_DIR}/db/snapshot.h"
  98. "${PROJECT_SOURCE_DIR}/db/table_cache.cc"
  99. "${PROJECT_SOURCE_DIR}/db/table_cache.h"
  100. "${PROJECT_SOURCE_DIR}/db/version_edit.cc"
  101. "${PROJECT_SOURCE_DIR}/db/version_edit.h"
  102. "${PROJECT_SOURCE_DIR}/db/version_set.cc"
  103. "${PROJECT_SOURCE_DIR}/db/version_set.h"
  104. "${PROJECT_SOURCE_DIR}/db/write_batch_internal.h"
  105. "${PROJECT_SOURCE_DIR}/db/write_batch.cc"
  106. "${PROJECT_SOURCE_DIR}/port/port.h"
  107. "${PROJECT_SOURCE_DIR}/port/thread_annotations.h"
  108. "${PROJECT_SOURCE_DIR}/table/block_builder.cc"
  109. "${PROJECT_SOURCE_DIR}/table/block_builder.h"
  110. "${PROJECT_SOURCE_DIR}/table/block.cc"
  111. "${PROJECT_SOURCE_DIR}/table/block.h"
  112. "${PROJECT_SOURCE_DIR}/table/filter_block.cc"
  113. "${PROJECT_SOURCE_DIR}/table/filter_block.h"
  114. "${PROJECT_SOURCE_DIR}/table/format.cc"
  115. "${PROJECT_SOURCE_DIR}/table/format.h"
  116. "${PROJECT_SOURCE_DIR}/table/iterator_wrapper.h"
  117. "${PROJECT_SOURCE_DIR}/table/iterator.cc"
  118. "${PROJECT_SOURCE_DIR}/table/merger.cc"
  119. "${PROJECT_SOURCE_DIR}/table/merger.h"
  120. "${PROJECT_SOURCE_DIR}/table/table_builder.cc"
  121. "${PROJECT_SOURCE_DIR}/table/table.cc"
  122. "${PROJECT_SOURCE_DIR}/table/two_level_iterator.cc"
  123. "${PROJECT_SOURCE_DIR}/table/two_level_iterator.h"
  124. "${PROJECT_SOURCE_DIR}/util/arena.cc"
  125. "${PROJECT_SOURCE_DIR}/util/arena.h"
  126. "${PROJECT_SOURCE_DIR}/util/bloom.cc"
  127. "${PROJECT_SOURCE_DIR}/util/cache.cc"
  128. "${PROJECT_SOURCE_DIR}/util/coding.cc"
  129. "${PROJECT_SOURCE_DIR}/util/coding.h"
  130. "${PROJECT_SOURCE_DIR}/util/comparator.cc"
  131. "${PROJECT_SOURCE_DIR}/util/crc32c.cc"
  132. "${PROJECT_SOURCE_DIR}/util/crc32c.h"
  133. "${PROJECT_SOURCE_DIR}/util/env_posix.cc"
  134. "${PROJECT_SOURCE_DIR}/util/env.cc"
  135. "${PROJECT_SOURCE_DIR}/util/filter_policy.cc"
  136. "${PROJECT_SOURCE_DIR}/util/hash.cc"
  137. "${PROJECT_SOURCE_DIR}/util/hash.h"
  138. "${PROJECT_SOURCE_DIR}/util/logging.cc"
  139. "${PROJECT_SOURCE_DIR}/util/logging.h"
  140. "${PROJECT_SOURCE_DIR}/util/mutexlock.h"
  141. "${PROJECT_SOURCE_DIR}/util/options.cc"
  142. "${PROJECT_SOURCE_DIR}/util/posix_logger.h"
  143. "${PROJECT_SOURCE_DIR}/util/random.h"
  144. "${PROJECT_SOURCE_DIR}/util/status.cc"
  145. $<TARGET_OBJECTS:leveldb_port_posix>
  146. # Only CMake 3.3+ supports PUBLIC sources in targets exported by "install".
  147. $<$<VERSION_GREATER:CMAKE_VERSION,3.2>:PUBLIC>
  148. "${LEVELDB_PUBLIC_INCLUDE_DIR}/c.h"
  149. "${LEVELDB_PUBLIC_INCLUDE_DIR}/cache.h"
  150. "${LEVELDB_PUBLIC_INCLUDE_DIR}/comparator.h"
  151. "${LEVELDB_PUBLIC_INCLUDE_DIR}/db.h"
  152. "${LEVELDB_PUBLIC_INCLUDE_DIR}/dumpfile.h"
  153. "${LEVELDB_PUBLIC_INCLUDE_DIR}/env.h"
  154. "${LEVELDB_PUBLIC_INCLUDE_DIR}/filter_policy.h"
  155. "${LEVELDB_PUBLIC_INCLUDE_DIR}/iterator.h"
  156. "${LEVELDB_PUBLIC_INCLUDE_DIR}/options.h"
  157. "${LEVELDB_PUBLIC_INCLUDE_DIR}/slice.h"
  158. "${LEVELDB_PUBLIC_INCLUDE_DIR}/status.h"
  159. "${LEVELDB_PUBLIC_INCLUDE_DIR}/table_builder.h"
  160. "${LEVELDB_PUBLIC_INCLUDE_DIR}/table.h"
  161. "${LEVELDB_PUBLIC_INCLUDE_DIR}/write_batch.h"
  162. )
  163. # MemEnv is not part of the interface and could be pulled to a separate library.
  164. target_sources(leveldb
  165. PRIVATE
  166. "${PROJECT_SOURCE_DIR}/helpers/memenv/memenv.cc"
  167. "${PROJECT_SOURCE_DIR}/helpers/memenv/memenv.h"
  168. )
  169. target_include_directories(leveldb
  170. PUBLIC
  171. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
  172. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  173. )
  174. target_compile_definitions(leveldb
  175. PRIVATE
  176. # Used by include/export.h when building shared libraries.
  177. LEVELDB_COMPILE_LIBRARY
  178. # Used by port/port.h.
  179. LEVELDB_PLATFORM_POSIX=1
  180. )
  181. if(BUILD_SHARED_LIBS)
  182. target_compile_definitions(leveldb
  183. PUBLIC
  184. # Used by include/export.h.
  185. LEVELDB_SHARED_LIBRARY
  186. )
  187. endif(BUILD_SHARED_LIBS)
  188. if(HAVE_CLANG_THREAD_SAFETY)
  189. target_compile_options(leveldb
  190. PUBLIC
  191. -Werror -Wthread-safety)
  192. endif(HAVE_CLANG_THREAD_SAFETY)
  193. # TODO(costan): This is only needed for port_posix.
  194. set(THREADS_PREFER_PTHREAD_FLAG ON)
  195. find_package(Threads REQUIRED)
  196. target_link_libraries(leveldb Threads::Threads)
  197. if(HAVE_CRC32C)
  198. target_link_libraries(leveldb crc32c)
  199. endif(HAVE_CRC32C)
  200. if(HAVE_SNAPPY)
  201. target_link_libraries(leveldb snappy)
  202. endif(HAVE_SNAPPY)
  203. if(HAVE_TCMALLOC)
  204. target_link_libraries(leveldb tcmalloc)
  205. endif(HAVE_TCMALLOC)
  206. add_executable(leveldbutil
  207. "${PROJECT_SOURCE_DIR}/db/leveldbutil.cc"
  208. )
  209. target_link_libraries(leveldbutil leveldb)
  210. if(LEVELDB_BUILD_TESTS)
  211. enable_testing()
  212. function(leveldb_test test_file)
  213. get_filename_component(test_target_name "${test_file}" NAME_WE)
  214. add_executable("${test_target_name}" "")
  215. target_sources("${test_target_name}"
  216. PRIVATE
  217. "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
  218. "${PROJECT_SOURCE_DIR}/util/testharness.cc"
  219. "${PROJECT_SOURCE_DIR}/util/testharness.h"
  220. "${PROJECT_SOURCE_DIR}/util/testutil.cc"
  221. "${PROJECT_SOURCE_DIR}/util/testutil.h"
  222. "${test_file}"
  223. )
  224. if(BUILD_SHARED_LIBS)
  225. # Port functions aren't exposed in the shared library build.
  226. target_sources("${test_target_name}"
  227. PRIVATE
  228. $<TARGET_OBJECTS:leveldb_port_posix>
  229. )
  230. endif(BUILD_SHARED_LIBS)
  231. target_link_libraries("${test_target_name}" leveldb)
  232. target_compile_definitions("${test_target_name}"
  233. PRIVATE
  234. LEVELDB_PLATFORM_POSIX=1
  235. )
  236. add_test(NAME "${test_target_name}" COMMAND "${test_target_name}")
  237. endfunction(leveldb_test)
  238. leveldb_test("${PROJECT_SOURCE_DIR}/db/c_test.c")
  239. leveldb_test("${PROJECT_SOURCE_DIR}/db/fault_injection_test.cc")
  240. leveldb_test("${PROJECT_SOURCE_DIR}/issues/issue178_test.cc")
  241. leveldb_test("${PROJECT_SOURCE_DIR}/issues/issue200_test.cc")
  242. leveldb_test("${PROJECT_SOURCE_DIR}/util/env_test.cc")
  243. if(NOT BUILD_SHARED_LIBS)
  244. leveldb_test("${PROJECT_SOURCE_DIR}/db/autocompact_test.cc")
  245. leveldb_test("${PROJECT_SOURCE_DIR}/db/corruption_test.cc")
  246. leveldb_test("${PROJECT_SOURCE_DIR}/db/db_test.cc")
  247. leveldb_test("${PROJECT_SOURCE_DIR}/db/dbformat_test.cc")
  248. leveldb_test("${PROJECT_SOURCE_DIR}/db/filename_test.cc")
  249. leveldb_test("${PROJECT_SOURCE_DIR}/db/log_test.cc")
  250. leveldb_test("${PROJECT_SOURCE_DIR}/db/recovery_test.cc")
  251. leveldb_test("${PROJECT_SOURCE_DIR}/db/skiplist_test.cc")
  252. leveldb_test("${PROJECT_SOURCE_DIR}/db/version_edit_test.cc")
  253. leveldb_test("${PROJECT_SOURCE_DIR}/db/version_set_test.cc")
  254. leveldb_test("${PROJECT_SOURCE_DIR}/db/write_batch_test.cc")
  255. leveldb_test("${PROJECT_SOURCE_DIR}/helpers/memenv/memenv_test.cc")
  256. leveldb_test("${PROJECT_SOURCE_DIR}/table/filter_block_test.cc")
  257. leveldb_test("${PROJECT_SOURCE_DIR}/table/table_test.cc")
  258. leveldb_test("${PROJECT_SOURCE_DIR}/util/arena_test.cc")
  259. leveldb_test("${PROJECT_SOURCE_DIR}/util/bloom_test.cc")
  260. leveldb_test("${PROJECT_SOURCE_DIR}/util/cache_test.cc")
  261. leveldb_test("${PROJECT_SOURCE_DIR}/util/coding_test.cc")
  262. leveldb_test("${PROJECT_SOURCE_DIR}/util/crc32c_test.cc")
  263. leveldb_test("${PROJECT_SOURCE_DIR}/util/hash_test.cc")
  264. # TODO(costan): This test also uses
  265. # "${PROJECT_SOURCE_DIR}/util/env_posix_test_helper.h"
  266. leveldb_test("${PROJECT_SOURCE_DIR}/util/env_posix_test.cc")
  267. endif(NOT BUILD_SHARED_LIBS)
  268. endif(LEVELDB_BUILD_TESTS)
  269. if(LEVELDB_BUILD_BENCHMARKS)
  270. function(leveldb_benchmark bench_file)
  271. get_filename_component(bench_target_name "${bench_file}" NAME_WE)
  272. add_executable("${bench_target_name}" "")
  273. target_sources("${bench_target_name}"
  274. PRIVATE
  275. "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
  276. "${PROJECT_SOURCE_DIR}/util/histogram.cc"
  277. "${PROJECT_SOURCE_DIR}/util/histogram.h"
  278. "${PROJECT_SOURCE_DIR}/util/testharness.cc"
  279. "${PROJECT_SOURCE_DIR}/util/testharness.h"
  280. "${PROJECT_SOURCE_DIR}/util/testutil.cc"
  281. "${PROJECT_SOURCE_DIR}/util/testutil.h"
  282. "${bench_file}"
  283. )
  284. if(BUILD_SHARED_LIBS)
  285. # Port functions aren't exposed in the shared library build.
  286. target_sources("${bench_target_name}"
  287. PRIVATE
  288. $<TARGET_OBJECTS:leveldb_port_posix>
  289. )
  290. endif(BUILD_SHARED_LIBS)
  291. target_link_libraries("${bench_target_name}" leveldb)
  292. target_compile_definitions("${bench_target_name}"
  293. PRIVATE
  294. LEVELDB_PLATFORM_POSIX=1
  295. )
  296. endfunction(leveldb_benchmark)
  297. if(NOT BUILD_SHARED_LIBS)
  298. leveldb_benchmark("${PROJECT_SOURCE_DIR}/db/db_bench.cc")
  299. endif(NOT BUILD_SHARED_LIBS)
  300. check_library_exists(sqlite3 sqlite3_open "" HAVE_SQLITE3)
  301. if(HAVE_SQLITE3)
  302. leveldb_benchmark("${PROJECT_SOURCE_DIR}/doc/bench/db_bench_sqlite3.cc")
  303. target_link_libraries(db_bench_sqlite3 sqlite3)
  304. endif(HAVE_SQLITE3)
  305. # check_library_exists is insufficient here because the library names have
  306. # different manglings when compiled with clang or gcc, at least when installed
  307. # with Homebrew on Mac.
  308. set(OLD_CMAKE_REQURED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  309. list(APPEND CMAKE_REQUIRED_LIBRARIES kyotocabinet)
  310. check_cxx_source_compiles("
  311. #include <kcpolydb.h>
  312. int main() {
  313. kyotocabinet::TreeDB* db = new kyotocabinet::TreeDB();
  314. delete db;
  315. return 0;
  316. }
  317. " HAVE_KYOTOCABINET)
  318. set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQURED_LIBRARIES})
  319. if(HAVE_KYOTOCABINET)
  320. leveldb_benchmark("${PROJECT_SOURCE_DIR}/doc/bench/db_bench_tree_db.cc")
  321. target_link_libraries(db_bench_tree_db kyotocabinet)
  322. endif(HAVE_KYOTOCABINET)
  323. endif(LEVELDB_BUILD_BENCHMARKS)
  324. if(LEVELDB_INSTALL)
  325. include(GNUInstallDirs)
  326. install(TARGETS leveldb
  327. EXPORT LeveldbTargets
  328. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  329. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  330. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  331. )
  332. install(
  333. FILES
  334. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/c.h"
  335. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/cache.h"
  336. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/comparator.h"
  337. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/db.h"
  338. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/dumpfile.h"
  339. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/env.h"
  340. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/filter_policy.h"
  341. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/iterator.h"
  342. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/options.h"
  343. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/slice.h"
  344. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/status.h"
  345. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/table_builder.h"
  346. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/table.h"
  347. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/write_batch.h"
  348. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/leveldb
  349. )
  350. include(CMakePackageConfigHelpers)
  351. write_basic_package_version_file(
  352. "${PROJECT_BINARY_DIR}/LeveldbConfigVersion.cmake"
  353. COMPATIBILITY SameMajorVersion
  354. )
  355. install(
  356. EXPORT LeveldbTargets
  357. NAMESPACE Leveldb::
  358. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/Leveldb"
  359. )
  360. install(
  361. FILES
  362. "${PROJECT_SOURCE_DIR}/cmake/LeveldbConfig.cmake"
  363. "${PROJECT_BINARY_DIR}/LeveldbConfigVersion.cmake"
  364. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/Leveldb"
  365. )
  366. endif(LEVELDB_INSTALL)