diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c6c8639..57d275ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") @@ -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? @@ -93,13 +98,22 @@ 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}) @@ -107,6 +121,7 @@ include_directories(${Boost_INCLUDE_DIRS}) set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) + # Process subdirectories add_subdirectory(util) add_subdirectory(lm) @@ -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) diff --git a/cmake/kenlmConfig.cmake.in b/cmake/kenlmConfig.cmake.in index 0fbf0c64..e92fc100 100644 --- a/cmake/kenlmConfig.cmake.in +++ b/cmake/kenlmConfig.cmake.in @@ -2,7 +2,9 @@ include(CMakeFindDependencyMacro) -find_dependency(Boost) +if (@BUILD_WITH_BOOST@) + find_dependency(Boost) +endif() find_dependency(Threads) # Compression libs diff --git a/lm/CMakeLists.txt b/lm/CMakeLists.txt index 6dcd2a43..0471aaac 100644 --- a/lm/CMakeLists.txt +++ b/lm/CMakeLists.txt @@ -1,3 +1,4 @@ + # Explicitly list the source files for this subdirectory # # If you add any source files to this subdirectory @@ -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 $ # -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 $) +target_include_directories(kenlm PUBLIC + $ + $ +) + +if (BUILD_WITH_BOOST) + target_compile_definitions(kenlm PUBLIC -DBUILD_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) @@ -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} diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index 7a96ef52..2b4df014 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -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 @@ -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` @@ -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 - "$" 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 + "$" + ) +endif() + install( TARGETS kenlm_util EXPORT kenlmTargets @@ -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() diff --git a/util/parallel_read.cc b/util/parallel_read.cc index 5c6a2ead..1cf927a0 100644 --- a/util/parallel_read.cc +++ b/util/parallel_read.cc @@ -2,7 +2,7 @@ #include "file.hh" -#ifdef WITH_THREADS +#if BUILD_WITH_BOOST #include "thread_pool.hh" namespace util { @@ -58,7 +58,7 @@ 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) { @@ -66,4 +66,4 @@ void ParallelRead(int fd, void *to, std::size_t amount, uint64_t offset) { } } // namespace util -#endif +#endif // defined(BUILD_WITH_BOOST)