From 739c25100e46576cdcdfff2d6f43f9f7008103c7 Mon Sep 17 00:00:00 2001 From: costan Date: Fri, 16 Mar 2018 19:10:13 -0700 Subject: [PATCH] Add CMake build support. Fixes https://github.com/google/leveldb/issues/466 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=189425354 --- .travis.yml | 17 +- CMakeLists.txt | 412 ++++++++++++++++++++++++++++++++++++++++++++++ cmake/LeveldbConfig.cmake | 1 + db/dumpfile.cc | 3 + helpers/memenv/memenv.h | 4 +- include/leveldb/export.h | 6 +- port/port_config.h.in | 36 ++++ port/port_posix.h | 61 +++---- 8 files changed, 493 insertions(+), 47 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 cmake/LeveldbConfig.cmake create mode 100644 port/port_config.h.in diff --git a/.travis.yml b/.travis.yml index 77ea0a3..5999274 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,10 @@ os: - linux - osx +env: + - BUILD_TYPE=Debug + - BUILD_TYPE=RelWithDebInfo + addons: apt: # List of whitelisted in travis packages for ubuntu-trusty can be found here: @@ -23,6 +27,7 @@ addons: - ubuntu-toolchain-r-test - llvm-toolchain-trusty-5.0 packages: + - cmake - gcc-7 - g++-7 - clang-5.0 @@ -49,6 +54,16 @@ install: - echo ${CC} - echo ${CXX} - ${CXX} --version +- cmake --version + +before_script: +- mkdir -p build && cd build +- cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE +- cmake --build . +- cd .. script: -- make -j 4 check +- cd build ; ctest --verbose ; cd .. +- "if [ -f build/db_bench ] ; then build/db_bench ; fi" +- "if [ -f build/db_bench_sqlite3 ] ; then build/db_bench_sqlite3 ; fi" +- "if [ -f build/db_bench_tree_db ] ; then build/db_bench_tree_db ; fi" diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..df486ea --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,412 @@ +# 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) +project(Leveldb VERSION 0.1.0 LANGUAGES C CXX) + +# This project can take advantage of C++11. +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED OFF) +set(CMAKE_CXX_EXTENSIONS 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) + +include(TestBigEndian) +test_big_endian(LEVELDB_IS_BIG_ENDIAN) + +include(CheckIncludeFile) +check_include_file("unistd.h" HAVE_UNISTD_H) + +include(CheckIncludeFileCXX) +check_include_file_cxx("atomic" LEVELDB_ATOMIC_PRESENT) + +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(CheckSymbolExists) +check_symbol_exists(fdatasync "unistd.h" HAVE_FDATASYNC) + +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}) +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}) + +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" +) + +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) + +# POSIX code is specified separately so we can leave it out in the future. +add_library(leveldb_port_posix OBJECT "") +target_sources(leveldb_port_posix + PRIVATE + "${PROJECT_SOURCE_DIR}/port/port_posix.cc" + + PUBLIC + # The headers below are dependencies for leveldb, but aren't needed by users + # that link to the installed version of leveldb and rely on its public API. + $ + $ + $ + $ +) +if(BUILD_SHARED_LIBS) + set_property(TARGET leveldb_port_posix PROPERTY POSITION_INDEPENDENT_CODE ON) +endif(BUILD_SHARED_LIBS) + +add_library(leveldb "") +target_sources(leveldb + PRIVATE + "${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.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_posix.cc" + "${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/options.cc" + "${PROJECT_SOURCE_DIR}/util/posix_logger.h" + "${PROJECT_SOURCE_DIR}/util/random.h" + "${PROJECT_SOURCE_DIR}/util/status.cc" + $ + + # Only CMake 3.3+ supports PUBLIC sources in targets exported by "install". + $<$: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}/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" +) +# 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" +) +target_include_directories(leveldb + PUBLIC + $ + $ +) +target_compile_definitions(leveldb + PRIVATE + # Used by include/export.h when building shared libraries. + LEVELDB_COMPILE_LIBRARY + # Used by port/port.h. + LEVELDB_PLATFORM_POSIX=1 +) +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) + +# TODO(costan): This is only needed for port_posix. +set(THREADS_PREFER_PTHREAD_FLAG ON) +find_package(Threads REQUIRED) +target_link_libraries(leveldb Threads::Threads) + +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) + +add_executable(leveldbutil + "${PROJECT_SOURCE_DIR}/db/leveldbutil.cc" +) +target_link_libraries(leveldbutil leveldb) + +if(LEVELDB_BUILD_TESTS) + enable_testing() + + function(leveldb_test test_file) + 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}" + ) + if(BUILD_SHARED_LIBS) + # Port functions aren't exposed in the shared library build. + target_sources("${test_target_name}" + PRIVATE + $ + ) + endif(BUILD_SHARED_LIBS) + + target_link_libraries("${test_target_name}" leveldb) + target_compile_definitions("${test_target_name}" + PRIVATE + LEVELDB_PLATFORM_POSIX=1 + ) + + 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") + + 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") + + # TODO(costan): This test also uses + # "${PROJECT_SOURCE_DIR}/util/env_posix_test_helper.h" + leveldb_test("${PROJECT_SOURCE_DIR}/util/env_posix_test.cc") + endif(NOT BUILD_SHARED_LIBS) +endif(LEVELDB_BUILD_TESTS) + +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}" + ) + if(BUILD_SHARED_LIBS) + # Port functions aren't exposed in the shared library build. + target_sources("${bench_target_name}" + PRIVATE + $ + ) + endif(BUILD_SHARED_LIBS) + + target_link_libraries("${bench_target_name}" leveldb) + target_compile_definitions("${bench_target_name}" + PRIVATE + LEVELDB_PLATFORM_POSIX=1 + ) + 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 + +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) + install(TARGETS leveldb + 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}/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) diff --git a/cmake/LeveldbConfig.cmake b/cmake/LeveldbConfig.cmake new file mode 100644 index 0000000..8f3e78b --- /dev/null +++ b/cmake/LeveldbConfig.cmake @@ -0,0 +1 @@ +include("${CMAKE_CURRENT_LIST_DIR}/LeveldbTargets.cmake") diff --git a/db/dumpfile.cc b/db/dumpfile.cc index 61c47c2..2f1b5d9 100644 --- a/db/dumpfile.cc +++ b/db/dumpfile.cc @@ -2,7 +2,10 @@ // 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. +#include "leveldb/dumpfile.h" + #include + #include "db/dbformat.h" #include "db/filename.h" #include "db/log_reader.h" diff --git a/helpers/memenv/memenv.h b/helpers/memenv/memenv.h index 03b88de..3d929e4 100644 --- a/helpers/memenv/memenv.h +++ b/helpers/memenv/memenv.h @@ -5,6 +5,8 @@ #ifndef STORAGE_LEVELDB_HELPERS_MEMENV_MEMENV_H_ #define STORAGE_LEVELDB_HELPERS_MEMENV_MEMENV_H_ +#include "leveldb/export.h" + namespace leveldb { class Env; @@ -13,7 +15,7 @@ class Env; // all non-file-storage tasks to base_env. The caller must delete the result // when it is no longer needed. // *base_env must remain live while the result is in use. -Env* NewMemEnv(Env* base_env); +LEVELDB_EXPORT Env* NewMemEnv(Env* base_env); } // namespace leveldb diff --git a/include/leveldb/export.h b/include/leveldb/export.h index 39b2097..6ba9b18 100644 --- a/include/leveldb/export.h +++ b/include/leveldb/export.h @@ -8,7 +8,7 @@ #if !defined(LEVELDB_EXPORT) #if defined(LEVELDB_SHARED_LIBRARY) -#if defined(OS_WIN) +#if defined(_WIN32) #if defined(LEVELDB_COMPILE_LIBRARY) #define LEVELDB_EXPORT __declspec(dllexport) @@ -16,13 +16,13 @@ #define LEVELDB_EXPORT __declspec(dllimport) #endif // defined(LEVELDB_COMPILE_LIBRARY) -#else // defined(OS_WIN) +#else // defined(_WIN32) #if defined(LEVELDB_COMPILE_LIBRARY) #define LEVELDB_EXPORT __attribute__((visibility("default"))) #else #define LEVELDB_EXPORT #endif -#endif // defined(OS_WIN) +#endif // defined(_WIN32) #else // defined(LEVELDB_SHARED_LIBRARY) #define LEVELDB_EXPORT diff --git a/port/port_config.h.in b/port/port_config.h.in new file mode 100644 index 0000000..549f179 --- /dev/null +++ b/port/port_config.h.in @@ -0,0 +1,36 @@ +// 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. + +#ifndef STORAGE_LEVELDB_PORT_PORT_CONFIG_H_ +#define STORAGE_LEVELDB_PORT_PORT_CONFIG_H_ + +// Define to 1 if you have a definition for fdatasync() in . +#if !defined(HAVE_FUNC_FDATASYNC) +#cmakedefine01 HAVE_FUNC_FDATASYNC +#endif // !defined(HAVE_FUNC_FDATASYNC) + +// Define to 1 if you have Google CRC32C. +#if !defined(HAVE_CRC32C) +#cmakedefine01 HAVE_CRC32C +#endif // !defined(HAVE_CRC32C) + +// Define to 1 if you have Google Snappy. +#if !defined(HAVE_SNAPPY) +#cmakedefine01 HAVE_SNAPPY +#endif // !defined(HAVE_SNAPPY) + +// Define to 1 if your processor stores words with the most significant byte +// first (like Motorola and SPARC, unlike Intel and VAX). +#if !defined(LEVELDB_IS_BIG_ENDIAN) +#cmakedefine01 LEVELDB_IS_BIG_ENDIAN +#endif // !defined(LEVELDB_IS_BIG_ENDIAN) + +// Define to 1 if you have the header. +// NOTE: was standardized in C++11, which will be required to build +// LevelDB soon. +#if !defined(LEVELDB_ATOMIC_PRESENT) +#cmakedefine01 LEVELDB_ATOMIC_PRESENT +#endif // !defined(LEVELDB_ATOMIC_PRESENT) + +#endif // STORAGE_LEVELDB_PORT_PORT_CONFIG_H_ \ No newline at end of file diff --git a/port/port_posix.h b/port/port_posix.h index ce52042..2fb2269 100644 --- a/port/port_posix.h +++ b/port/port_posix.h @@ -7,36 +7,22 @@ #ifndef STORAGE_LEVELDB_PORT_PORT_POSIX_H_ #define STORAGE_LEVELDB_PORT_PORT_POSIX_H_ -#undef PLATFORM_IS_LITTLE_ENDIAN -#if defined(__APPLE__) - #include - #if defined(__DARWIN_LITTLE_ENDIAN) && defined(__DARWIN_BYTE_ORDER) - #define PLATFORM_IS_LITTLE_ENDIAN \ - (__DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN) - #endif -#elif defined(OS_SOLARIS) - #include - #ifdef _LITTLE_ENDIAN - #define PLATFORM_IS_LITTLE_ENDIAN true - #else - #define PLATFORM_IS_LITTLE_ENDIAN false - #endif -#elif defined(OS_FREEBSD) || defined(OS_OPENBSD) ||\ - defined(OS_NETBSD) || defined(OS_DRAGONFLYBSD) - #include - #include - #define PLATFORM_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN) -#elif defined(OS_HPUX) - #define PLATFORM_IS_LITTLE_ENDIAN false -#elif defined(OS_ANDROID) - // Due to a bug in the NDK x86 definition, - // _BYTE_ORDER must be used instead of __BYTE_ORDER on Android. - // See http://code.google.com/p/android/issues/detail?id=39824 - #include - #define PLATFORM_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN) -#else - #include -#endif +// port/port_config.h availability is automatically detected via __has_include +// in newer compilers. If LEVELDB_HAS_PORT_CONFIG_H is defined, it overrides the +// configuration detection. +#if defined(LEVELDB_HAS_PORT_CONFIG_H) + +#if LEVELDB_HAS_PORT_CONFIG_H +#include "port/port_config.h" +#endif // LEVELDB_HAS_PORT_CONFIG_H + +#elif defined(__has_include) + +#if __has_include("port/port_config.h") +#include "port/port_config.h" +#endif // __has_include("port/port_config.h") + +#endif // defined(LEVELDB_HAS_PORT_CONFIG_H) #include #if HAVE_CRC32C @@ -54,23 +40,14 @@ #define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN) #endif -#if defined(__APPLE__) || defined(OS_FREEBSD) ||\ - defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD) -// Use fsync() on platforms without fdatasync() +#if !HAVE_FDATASYNC #define fdatasync fsync -#endif - -#if defined(OS_ANDROID) && __ANDROID_API__ < 9 -// fdatasync() was only introduced in API level 9 on Android. Use fsync() -// when targetting older platforms. -#define fdatasync fsync -#endif +#endif // !HAVE_FDATASYNC namespace leveldb { namespace port { -static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN; -#undef PLATFORM_IS_LITTLE_ENDIAN +static const bool kLittleEndian = !LEVELDB_IS_BIG_ENDIAN; class CondVar;