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

Isolate binary and Boost-dependent build components #418

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
38 changes: 29 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ project(kenlm)

option(FORCE_STATIC "Build static executables" OFF)
option(COMPILE_TESTS "Compile tests" OFF)
option(BUILD_WITH_BOOST "Build binary tools (filter, builder, etc). Requires Boost" ON)
option(ENABLE_PYTHON "Build Python bindings" OFF)
option(BUILD_PYTHON_STANDALONE "Build standalone C++ lib with Python install" OFF)
# Eigen3 less than 3.1.0 has a race condition: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=466
find_package(Eigen3 3.1.0 CONFIG)
include(CMakeDependentOption)
cmake_dependent_option(ENABLE_INTERPOLATE "Build interpolation program (depends on Eigen3)" ON "EIGEN3_FOUND AND NOT WIN32" OFF)
cmake_dependent_option(ENABLE_INTERPOLATE "Build interpolation program (depends on Eigen3)" ON "BUILD_WITH_BOOST AND EIGEN3_FOUND AND NOT WIN32" OFF)
cmake_dependent_option(BUILD_BENCHMARKS "Build benchmarks" ON "BUILD_WITH_BOOST" OFF)

set(KENLM_MAX_ORDER 6 CACHE STRING "Maximum supported ngram order")

Expand All @@ -32,6 +34,9 @@ if (BUILD_PYTHON_STANDALONE)
return()
endif()

if (NOT BUILD_WITH_BOOST AND COMPILE_TEST)
message(FATAL_ERROR "Cannot compile tests with BUILD_WITH_BOOST=OFF. Boost required for building tests")
endif()

if (FORCE_STATIC)
#presumably overkill, is there a better way?
Expand Down Expand Up @@ -93,20 +98,30 @@ endif()
# And our helper modules
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)

# We need boost
find_package(Boost 1.41.0 REQUIRED COMPONENTS
program_options
system
thread
unit_test_framework
)
if (BUILD_WITH_BOOST)
set(BOOST_COMPONENTS
program_options
system
thread
)
endif()
if (BUILD_TESTING OR COMPILE_TESTS)
list(APPEND BOOST_COMPONENTS unit_test_framework)
endif()

if (BUILD_WITH_BOOST OR BUILD_TESTING OR COMPILE_TESTS)
find_package(Boost 1.41.0 REQUIRED COMPONENTS
${BOOST_COMPONENTS}
)
endif()

# Define where include files live
include_directories(${Boost_INCLUDE_DIRS})

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)


# Process subdirectories
add_subdirectory(util)
add_subdirectory(lm)
Expand All @@ -122,7 +137,12 @@ install(EXPORT kenlmTargets
DESTINATION share/kenlm/cmake
)

foreach(SUBDIR IN ITEMS util util/double-conversion util/stream lm lm/builder lm/common lm/filter lm/interpolate)
set(_headers_to_install lm util util/double-conversion )
if (BUILD_WITH_BOOST)
list(APPEND _headers_to_install lm/builder lm/filter lm/interpolate lm/common util/stream)
endif()

foreach(SUBDIR IN ITEMS ${_headers_to_install})
file(GLOB HEADERS ${CMAKE_CURRENT_LIST_DIR}/${SUBDIR}/*.h ${CMAKE_CURRENT_LIST_DIR}/${SUBDIR}/*.hh)
install(FILES ${HEADERS} DESTINATION include/kenlm/${SUBDIR} COMPONENT headers)
endforeach(SUBDIR)
Expand Down
4 changes: 3 additions & 1 deletion cmake/kenlmConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

include(CMakeFindDependencyMacro)

find_dependency(Boost)
if (@BUILD_WITH_BOOST@)
find_dependency(Boost)
endif()
find_dependency(Threads)

# Compression libs
Expand Down
34 changes: 24 additions & 10 deletions lm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Explicitly list the source files for this subdirectory
#
# If you add any source files to this subdirectory
Expand All @@ -22,34 +23,48 @@ set(KENLM_LM_SOURCE
vocab.cc
)


# Group these objects together for later use.
#
# Given add_library(foo OBJECT ${my_foo_sources}),
# refer to these objects as $<TARGET_OBJECTS:foo>
#
add_subdirectory(common)
if (BUILD_WITH_BOOST)
# only needed if building binaries
add_subdirectory(common)
endif()


add_library(kenlm ${KENLM_LM_SOURCE} ${KENLM_LM_COMMON_SOURCE})
set_target_properties(kenlm PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(kenlm PUBLIC kenlm_util Threads::Threads)
# Since headers are relative to `include/kenlm` at install time, not just `include`
target_include_directories(kenlm PUBLIC $<INSTALL_INTERFACE:include/kenlm>)
target_include_directories(kenlm PUBLIC
$<INSTALL_INTERFACE:include/kenlm>
$<INSTALL_INTERFACE:include>
)

if (BUILD_WITH_BOOST)
target_compile_definitions(kenlm PUBLIC -DBUILD_WITH_BOOST=$<BOOL:${BUILD_WITH_BOOST}>)
endif()

target_compile_definitions(kenlm PUBLIC -DKENLM_MAX_ORDER=${KENLM_MAX_ORDER})

# This directory has children that need to be processed
add_subdirectory(builder)
add_subdirectory(filter)
add_subdirectory(interpolate)
if (BUILD_WITH_BOOST)
add_subdirectory(builder)
add_subdirectory(filter)
endif()
add_subdirectory(interpolate) # gated inside by ENABLE_INTERPOLATE

# Explicitly list the executable files to be compiled
set(EXE_LIST
query
fragment
build_binary
kenlm_benchmark
)
)
if (BUILD_BENCHMARKS)
list(APPEND EXE_LIST kenlm_benchmark)
endif()

set(LM_LIBS kenlm kenlm_util Threads::Threads)

Expand All @@ -63,10 +78,9 @@ install(
)

AddExes(EXES ${EXE_LIST}
LIBRARIES ${LM_LIBS})
LIBRARIES ${LM_LIBS})

if(BUILD_TESTING)

set(KENLM_BOOST_TESTS_LIST left_test partial_test)
AddTests(TESTS ${KENLM_BOOST_TESTS_LIST}
LIBRARIES ${LM_LIBS}
Expand Down
25 changes: 19 additions & 6 deletions util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ set(KENLM_UTIL_SOURCE
integer_to_string.cc
mmap.cc
murmur_hash.cc
parallel_read.cc
pool.cc
read_compressed.cc
scoped.cc
Expand All @@ -27,13 +26,21 @@ set(KENLM_UTIL_SOURCE
usage.cc
)

if (BUILD_WITH_BOOST AND Boost_FOUND)
list(APPEND KENLM_UTIL_SOURCE parallel_read.cc)
endif()

if (WIN32)
set(KENLM_UTIL_SOURCE ${KENLM_UTIL_SOURCE} getopt.c)
endif()

# This directory has children that need to be processed
add_subdirectory(double-conversion)
add_subdirectory(stream)

if (BUILD_WITH_BOOST)
# only required for building tools/binaries
add_subdirectory(stream)
endif()

add_library(kenlm_util ${KENLM_UTIL_DOUBLECONVERSION_SOURCE} ${KENLM_UTIL_STREAM_SOURCE} ${KENLM_UTIL_SOURCE})
# Since headers are relative to `include/kenlm` at install time, not just `include`
Expand Down Expand Up @@ -83,13 +90,19 @@ endif()
# Group these objects together for later use.
set_target_properties(kenlm_util PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(kenlm_util
PUBLIC
# Boost is required for building binaries and tests
"$<BUILD_INTERFACE:${Boost_LIBRARIES}>"
PRIVATE
Threads::Threads
${RT})

if (BUILD_TESTING OR COMPILE_TESTS OR BUILD_WITH_BOOST)
target_link_libraries(
kenlm_util
PUBLIC
# Boost is required for building binaries and tests
"$<BUILD_INTERFACE:${Boost_LIBRARIES}>"
)
endif()

install(
TARGETS kenlm_util
EXPORT kenlmTargets
Expand All @@ -99,7 +112,7 @@ install(
INCLUDES DESTINATION include
)

if (NOT WIN32)
if (NOT WIN32 AND BUILD_BENCHMARKS)
AddExes(EXES probing_hash_table_benchmark
LIBRARIES kenlm_util Threads::Threads)
endif()
Expand Down
6 changes: 3 additions & 3 deletions util/parallel_read.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "file.hh"

#ifdef WITH_THREADS
#if BUILD_WITH_BOOST
#include "thread_pool.hh"

namespace util {
Expand Down Expand Up @@ -58,12 +58,12 @@ void ParallelRead(int fd, void *to, std::size_t amount, uint64_t offset) {

} // namespace util

#else // WITH_THREADS
#else // defined(BUILD_WITH_BOOST)

namespace util {
void ParallelRead(int fd, void *to, std::size_t amount, uint64_t offset) {
util::ErsatzPRead(fd, to, amount, offset);
}
} // namespace util

#endif
#endif // defined(BUILD_WITH_BOOST)