Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: Use find_package() to find Snappy #686

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.sw*
.vscode
.DS_Store
.idea

# Build directory.
build/
Expand Down
12 changes: 8 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ cmake_minimum_required(VERSION 3.9)
# Keep the version below in sync with the one in db.h
project(leveldb VERSION 1.22.0 LANGUAGES C CXX)

# Include local CMake modules.
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

# This project can use C11, but will gracefully decay down to C89.
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED OFF)
Expand All @@ -31,12 +34,13 @@ option(LEVELDB_INSTALL "Install LevelDB's header and library" ON)
include(TestBigEndian)
test_big_endian(LEVELDB_IS_BIG_ENDIAN)

find_package(Snappy)

include(CheckIncludeFile)
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)
Expand Down Expand Up @@ -251,9 +255,9 @@ 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(TARGET Snappy::snappy)
target_link_libraries(leveldb Snappy::snappy)
endif()
if(HAVE_TCMALLOC)
target_link_libraries(leveldb tcmalloc)
endif(HAVE_TCMALLOC)
Expand Down
31 changes: 31 additions & 0 deletions cmake/FindSnappy.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2019 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.

find_library(SNAPPY_LIBRARY
NAMES snappy
HINTS ${SNAPPY_ROOT_DIR}/lib
)

find_path(SNAPPY_INCLUDE_DIR
NAMES snappy.h
HINTS ${SNAPPY_ROOT_DIR}/include
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Snappy DEFAULT_MSG SNAPPY_LIBRARY SNAPPY_INCLUDE_DIR)

mark_as_advanced(SNAPPY_LIBRARY SNAPPY_INCLUDE_DIR)

if(SNAPPY_FOUND)
set(HAVE_SNAPPY TRUE) # For compatibity with generating port_config.h.

# Add imported targets.
# Follow the package naming convetion 'Snappy::' from
# https://github.com/google/snappy/blob/master/CMakeLists.txt#L211.
add_library(Snappy::snappy UNKNOWN IMPORTED)
set_target_properties(Snappy::snappy PROPERTIES
IMPORTED_LOCATION ${SNAPPY_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${SNAPPY_INCLUDE_DIR}
)
endif()