Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Commit

Permalink
Rewriting leveldb's cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
pjh5 committed Feb 14, 2018
1 parent b0961ca commit 6bf1947
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 84 deletions.
8 changes: 6 additions & 2 deletions caffe2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ if(USE_CUDA)
target_link_libraries(
caffe2_gpu
PRIVATE
caffe2
${Caffe2_DEPENDENCY_LIBS}
${Caffe2_CUDA_DEPENDENCY_LIBS})
endif()
Expand Down Expand Up @@ -232,9 +233,12 @@ if (BUILD_PYTHON)
set_target_properties(
caffe2_pybind11_state PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/caffe2/python)
# Add dependency of caffe2_pybind11 on caffe2
target_link_libraries(
caffe2_pybind11_state ${Caffe2_CPU_LINK} ${Caffe2_DEPENDENCY_LIBS}
${Caffe2_PYTHON_DEPENDENCY_LIBS})
caffe2_pybind11_state PUBLIC
caffe2
${Caffe2_PYTHON_DEPENDENCY_LIBS}
)
install(TARGETS caffe2_pybind11_state DESTINATION "${python_site_packages}/caffe2/python")

if(USE_CUDA)
Expand Down
19 changes: 10 additions & 9 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ if(BUILD_TEST)
set(gtest_force_shared_crt ON)
endif()
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/googletest)
caffe2_include_directories(${PROJECT_SOURCE_DIR}/third_party/googletest/googletest/include)
include_directories(${PROJECT_SOURCE_DIR}/third_party/googletest/googletest/include)

# We will not need to test benchmark lib itself.
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Disable benchmark testing as we don't need it.")
# We will not need to install benchmark since we link it statically.
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "Disable benchmark install to avoid overwriting vendor install.")
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/benchmark)
caffe2_include_directories(${PROJECT_SOURCE_DIR}/third_party/benchmark/include)
include_directories(${PROJECT_SOURCE_DIR}/third_party/benchmark/include)

# Recover the build shared libs option.
set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS})
Expand All @@ -157,13 +157,13 @@ endif()
# ---[ LevelDB
# ---[ Snappy
if(USE_LEVELDB)
find_package(LevelDB)
find_package(Snappy)
if(LEVELDB_FOUND AND SNAPPY_FOUND)
caffe2_include_directories(${LevelDB_INCLUDE})
list(APPEND Caffe2_DEPENDENCY_LIBS ${LevelDB_LIBRARIES})
caffe2_include_directories(${Snappy_INCLUDE_DIR})
list(APPEND Caffe2_DEPENDENCY_LIBS ${Snappy_LIBRARIES})
include(cmake/Modules/leveldb.cmake)
include(cmake/Modules/snappy.cmake)
if(TARGET leveldb::leveldb AND TARGET snappy::snappy)
SET(CAFFE2_USE_LEVELDB=1)
list(APPEND Caffe2_DEPENDENCY_LIBS leveldb::leveldb)
SET(CAFFE2_USE_SNAPPY=1)
list(APPEND Caffe2_DEPENDENCY_LIBS snappy::snappy)
else()
message(WARNING "Not compiling with LevelDB. Suppress this warning with -DUSE_LEVELDB=OFF")
set(USE_LEVELDB OFF)
Expand Down Expand Up @@ -428,6 +428,7 @@ if(USE_GLOO)
if(NCCL_EXTERNAL)
add_dependencies(gloo_cuda nccl_external)
endif()
message(INFO "Built GLOO from third-party")
endif()
# Pick the right dependency depending on USE_CUDA
list(APPEND Caffe2_DEPENDENCY_LIBS gloo)
Expand Down
44 changes: 0 additions & 44 deletions cmake/Modules/FindLevelDB.cmake

This file was deleted.

27 changes: 0 additions & 27 deletions cmake/Modules/FindSnappy.cmake

This file was deleted.

72 changes: 72 additions & 0 deletions cmake/Modules/leveldb.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# ---[ LevelDB
# - Find LevelDB
#
# LevelDB_INCLUDES - List of LevelDB includes
# LevelDB_LIBRARIES - List of libraries when using LevelDB.
# LevelDB_FOUND - True if LevelDB found.

# Use pkg-config to help find libraries if it's available
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_check_modules(LevelDB LevelDB)
endif()

# Look for the header files.
find_path(LevelDB_INCLUDE_DIR
NAMES leveldb/db.h
PATHS $ENV{LEVELDB_ROOT}/include ${LevelDB_INCLUDE_DIRS} /opt/local/include /usr/local/include /usr/include
DOC "Path in which the file leveldb/db.h is located."
)

# Look for the library.
find_library(LevelDB_LIBRARY
NAMES leveldb
PATHS $ENV{LEVELDB_ROOT}/lib ${LevelDB_LIBRARY_DIRS} /opt/local/lib /usr/local/lib /usr/lib
DOC "Path to leveldb library."
)

# Now delegate to this cmake function to make sure all paths exist
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LevelDB
FOUND_VAR LevelDB_FOUND
REQUIRED_VARS LevelDB_LIBRARY LevelDB_INCLUDE_DIR
)

if (NOT LevelDB_FOUND)
return()
endif()

message(STATUS "Found LevelDB (include: ${LevelDB_INCLUDE}, library: ${LevelDB_LIBRARY})")
mark_as_advanced(LevelDB_INCLUDE_DIR LevelDB_LIBRARY)

# Create a leveldb target
if (NOT TARGET leveldb::leveldb)
add_library(leveldb::leveldb UNKNOWN IMPORTED)
endif()

# Set target properties for the new leveldb target
set_target_properties(leveldb::leveldb PROPERTIES
IMPORTED_LOCATION "${LevelDB_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${LevelDB_CFLAGS_OTHER}"
INTERFACE_INCLUDE_DIRECTORIES "${LevelDB_INCLUDE_DIR}"
)

# Read the LevelDB version from the header
if(EXISTS "${LevelDB_INCLUDE_DIR}/leveldb/db.h")
file(STRINGS "${LevelDB_INCLUDE_DIR}/leveldb/db.h" __version_lines
REGEX "static const int k[^V]+Version[ \t]+=[ \t]+[0-9]+;")

foreach(__line ${__version_lines})
if(__line MATCHES "[^k]+kMajorVersion[ \t]+=[ \t]+([0-9]+);")
set(LEVELDB_VERSION_MAJOR ${CMAKE_MATCH_1})
elseif(__line MATCHES "[^k]+kMinorVersion[ \t]+=[ \t]+([0-9]+);")
set(LEVELDB_VERSION_MINOR ${CMAKE_MATCH_1})
endif()
endforeach()

if(LEVELDB_VERSION_MAJOR AND LEVELDB_VERSION_MINOR)
set(LEVELDB_VERSION "${LEVELDB_VERSION_MAJOR}.${LEVELDB_VERSION_MINOR}")
endif()

# caffe_clear_vars(__line __version_lines)
endif()
60 changes: 60 additions & 0 deletions cmake/Modules/snappy.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# ---[ snappy
# Find the Snappy libraries
#
# The following variables are optionally searched for defaults
# SNAPPY_ROOT_DIR: Base directory where all Snappy components are found
#
# The following are set after configuration is done:
# SNAPPY_FOUND
# Snappy_INCLUDE_DIR
# Snappy_LIBRARIES

# Use pkg-config to help find libraries if it's available
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_check_modules(Snappy Snappy)
endif()

# Look for the header files.
find_path(Snappy_INCLUDE_DIR
NAMES snappy.h
PATHS ${SNAPPY_ROOT_DIR}/include ${Snappy_INCLUDE_DIRS}
)

# Look for the library.
find_library(Snappy_LIBRARY
NAMES snappy
PATHS ${SNAPPY_ROOT_DIR}/lib ${Snappy_LIBRARY_DIRS}
)

# Now delegate to this cmake function to make sure all paths exist
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Snappy
FOUND_VAR SNAPPY_FOUND
REQUIRED_VARS Snappy_LIBRARY Snappy_INCLUDE_DIR
)

if(NOT SNAPPY_FOUND)
return()
endif()

message(STATUS "Found Snappy (include: ${Snappy_INCLUDE_DIR}, library: ${Snappy_LIBRARY})")
mark_as_advanced(Snappy_INCLUDE_DIR Snappy_LIBRARY)

# Create a Snappy target
if (NOT TARGET snappy::snappy)
add_library(snappy::snappy UNKNOWN IMPORTED)
endif()

# Set target properties for the new snappy target
set_target_properties(snappy::snappy PROPERTIES
IMPORTED_LOCATION "${Snappy_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${Snappy_CFLAGS_OTHER}"
INTERFACE_INCLUDE_DIRECTORIES "${Snappy_INCLUDE_DIR}"
)

# Read the Snappy version from the header
caffe_parse_header(${Snappy_INCLUDE_DIR}/snappy-stubs-public.h
SNAPPY_VERION_LINES SNAPPY_MAJOR SNAPPY_MINOR SNAPPY_PATCHLEVEL)
set(Snappy_VERSION "${SNAPPY_MAJOR}.${SNAPPY_MINOR}.${SNAPPY_PATCHLEVEL}")

4 changes: 2 additions & 2 deletions cmake/Utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,15 @@ function(caffe2_include_directories)
if (${DEPRIORITIZE_ANACONDA})
# When not preferring anaconda, always search system header files before
# anaconda include directories
if (${path} MATCHES "/anaconda")
if (${path} MATCHES "/anaconda" OR ${path} MATCHES "/conda-bld/")
include_directories(AFTER ${path})
else()
include_directories(BEFORE ${path})
endif()
else()
# When prefering Anaconda, always search anaconda for header files before
# system include directories
if (${path} MATCHES "/anaconda")
if (${path} MATCHES "/anaconda" OR ${path} MATCHES "/conda-bld/")
include_directories(BEFORE ${path})
else()
include_directories(AFTER ${path})
Expand Down

0 comments on commit 6bf1947

Please sign in to comment.