Skip to content

Commit

Permalink
install a cmake config for libcuckoo so that users can cleanly
Browse files Browse the repository at this point in the history
access us with find_package(libcuckoo).

while I'm here, clean up CMakeLists.txt files:
 - add BUILD_EXAMPLES and BUILD_TESTS as cache variables so that
   they show up in ccmake (there is prob more than could be added)
 - remove unneeded cuckoo-c targets
 - don't put header files in add_executable lists
    (cmake's auto depend features will catch changes to files
     that are #included)
 - use target_compile_features to kick on c++11 instead of trying
   to directly set compiler flags
  • Loading branch information
chuckcranor committed Oct 5, 2017
1 parent 57129c8 commit a1a5db8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 48 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
cmake_minimum_required(VERSION 3.1.0)
project(libcuckoo LANGUAGES C CXX)

set(libcuckoo_VERSION_MAJOR 0)
set(libcuckoo_VERSION_MINOR 2)
set(libcuckoo_VERSION_PATCH 0)

# put these in the cache so they show up in ccmake
option (BUILD_EXAMPLES "build example libcuckoo programs")
option (BUILD_TESTS "build libcuckoo test programs")

# Add the libcuckoo interface target
add_subdirectory(libcuckoo)
Expand Down
20 changes: 6 additions & 14 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,13 @@ target_link_libraries(hellohash libcuckoo)
add_executable(count_freq count_freq.cc)
target_link_libraries(count_freq libcuckoo)

add_library(int_str_table STATIC int_str_table.h int_str_table.cc)
target_link_libraries(int_str_table
PUBLIC libcuckoo-c-intf
PRIVATE libcuckoo-c-impl
)
add_library(int_str_table STATIC int_str_table.cc)
target_link_libraries(int_str_table libcuckoo)

add_library(blob_blob_table STATIC blob_blob_table.h blob_blob_table.cc)
target_link_libraries(blob_blob_table
PUBLIC libcuckoo-c-intf
PRIVATE libcuckoo-c-impl
)
add_library(blob_blob_table STATIC blob_blob_table.cc)
target_link_libraries(blob_blob_table libcuckoo)

add_executable(c_hash c_hash.c)
target_link_libraries(c_hash
PRIVATE int_str_table
PRIVATE blob_blob_table
)
target_link_libraries(c_hash int_str_table blob_blob_table)

set_property(TARGET c_hash PROPERTY C_STANDARD 99)
16 changes: 2 additions & 14 deletions libcuckoo-c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
add_library(libcuckoo-c-intf INTERFACE)
add_library(libcuckoo-c-impl INTERFACE)

# Include relative to the base directory
target_include_directories(libcuckoo-c-intf INTERFACE
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)

target_include_directories(libcuckoo-c-impl INTERFACE
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)
add_library(libcuckoo-c INTERFACE)

# Link the implementation to libcuckoo
target_link_libraries(libcuckoo-c-impl INTERFACE libcuckoo)
target_link_libraries(libcuckoo-c INTERFACE libcuckoo)

install(
FILES
Expand Down
42 changes: 32 additions & 10 deletions libcuckoo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
# for write_basic_package_version_file()
include(CMakePackageConfigHelpers)

# we require the use of threads
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

# generate a version for cmake to use with find_package(libcuckoo)
set (libcuckoo_VERSION "${libcuckoo_VERSION_MAJOR}.${libcuckoo_VERSION_MINOR}")
set (libcuckoo_VERSION "${libcuckoo_VERSION}.${libcuckoo_VERSION_PATCH}")

# libcuckoo is an interface (all headers) library target
add_library(libcuckoo INTERFACE)

# tag libcuckoo target with a c++11 feature so that libcuckoo users
# will have c++11 turned on in their compile when they use this target.
# XXX: newer cmakes have a "cxx_std_11" feature that could be used
target_compile_features (libcuckoo INTERFACE cxx_constexpr)

# Include relative to the base directory
target_include_directories(libcuckoo INTERFACE
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)

# Enable C++11 for all targets that link with libcuckoo
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(libcuckoo INTERFACE -std=c++11 -stdlib=libc++)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(libcuckoo INTERFACE -std=gnu++11)
endif()

# Enable threading for all targets that link with libcuckoo
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
# switch on threading for all targets that link with libcuckoo
target_link_libraries(libcuckoo INTERFACE Threads::Threads)

# cmake packaging
set (libcuckoo_pkgloc "share/cmake/libcuckoo")

write_basic_package_version_file(
"libcuckoo-config-version.cmake" VERSION ${libcuckoo_VERSION}
COMPATIBILITY AnyNewerVersion)

install(TARGETS libcuckoo EXPORT libcuckoo-targets)
install(EXPORT libcuckoo-targets
DESTINATION ${libcuckoo_pkgloc}
FILE "libcuckoo-targets.cmake")
install(FILES libcuckoo-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/libcuckoo-config-version.cmake
DESTINATION ${libcuckoo_pkgloc})
install(
FILES
cuckoohash_config.hh
Expand Down
10 changes: 10 additions & 0 deletions libcuckoo/libcuckoo-config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# libcuckoo-config.cmake
#

include (CMakeFindDependencyMacro)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_dependency(Threads)

include ("${CMAKE_CURRENT_LIST_DIR}/libcuckoo-targets.cmake")
7 changes: 2 additions & 5 deletions tests/unit-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
add_library(int_int_table STATIC int_int_table.h int_int_table.cc)
target_link_libraries(int_int_table
PUBLIC libcuckoo-c-intf
PRIVATE libcuckoo-c-impl
)
add_library(int_int_table STATIC int_int_table.cc)
target_link_libraries(int_int_table libcuckoo)

add_executable(unit_tests
test_constructor.cc
Expand Down
6 changes: 1 addition & 5 deletions tests/universal-benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
add_executable(universal_benchmark
universal_benchmark.cc
universal_gen.hh
universal_table_wrapper.hh
)
add_executable(universal_benchmark universal_benchmark.cc)

target_link_libraries(universal_benchmark
PRIVATE test_util
Expand Down

0 comments on commit a1a5db8

Please sign in to comment.