|
| 1 | +cmake_minimum_required(VERSION 3.1) |
| 2 | + |
| 3 | +if (WIN32) |
| 4 | + set(Boost_USE_STATIC_LIBS OFF) |
| 5 | + # The auto-linking feature has problems with USE_STATIC_LIBS off, so we use |
| 6 | + # BOOST_ALL_NO_LIB to turn it off. |
| 7 | + # Several boost libraries headers aren't configured correctly if |
| 8 | + # USE_STATIC_LIBS is off, so we explicitly say they are dynamic with the |
| 9 | + # remaining definitions. |
| 10 | + add_definitions(-DBOOST_ALL_NO_LIB -DBOOST_PROGRAM_OPTIONS_DYN_LINK -DBOOST_IOSTREAMS_DYN_LINK -DBOOST_THREAD_DYN_LINK) |
| 11 | +endif( ) |
| 12 | + |
| 13 | +# Define a single cmake project |
| 14 | +project(kenlm) |
| 15 | + |
| 16 | +option(FORCE_STATIC "Build static executables" OFF) |
| 17 | +option(COMPILE_TESTS "Compile tests" OFF) |
| 18 | +option(ENABLE_PYTHON "Build Python bindings" OFF) |
| 19 | +# Eigen3 less than 3.1.0 has a race condition: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=466 |
| 20 | +find_package(Eigen3 3.1.0 CONFIG) |
| 21 | +include(CMakeDependentOption) |
| 22 | +cmake_dependent_option(ENABLE_INTERPOLATE "Build interpolation program (depends on Eigen3)" ON "EIGEN3_FOUND AND NOT WIN32" OFF) |
| 23 | + |
| 24 | +if (FORCE_STATIC) |
| 25 | + #presumably overkill, is there a better way? |
| 26 | + #http://cmake.3232098.n2.nabble.com/Howto-compile-static-executable-td5580269.html |
| 27 | + set(Boost_USE_STATIC_LIBS ON) |
| 28 | + set_property(GLOBAL PROPERTY LINK_SEARCH_START_STATIC ON) |
| 29 | + set_property(GLOBAL PROPERTY LINK_SEARCH_END_STATIC ON) |
| 30 | + set(BUILD_SHARED_LIBRARIES OFF) |
| 31 | + if (MSVC) |
| 32 | + set(flag_vars |
| 33 | + CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE |
| 34 | + CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO |
| 35 | + CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE |
| 36 | + CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO) |
| 37 | + foreach(flag_var ${flag_vars}) |
| 38 | + if(${flag_var} MATCHES "/MD") |
| 39 | + string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") |
| 40 | + endif(${flag_var} MATCHES "/MD") |
| 41 | + endforeach(flag_var) |
| 42 | + else (MSVC) |
| 43 | + if (NOT CMAKE_C_COMPILER_ID MATCHES ".*Clang") |
| 44 | + set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static") |
| 45 | + endif () |
| 46 | + set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") |
| 47 | + endif () |
| 48 | + #Annoyingly the exectuables say "File not found" unless these are set |
| 49 | + set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) |
| 50 | + set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS) |
| 51 | + set(CMAKE_SHARED_LIBRARY_C_FLAGS) |
| 52 | + set(CMAKE_SHARED_LIBRARY_CXX_FLAGS) |
| 53 | + set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS) |
| 54 | + set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS) |
| 55 | +endif () |
| 56 | + |
| 57 | +# Compile all executables into bin/ |
| 58 | +set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) |
| 59 | + |
| 60 | +# Compile all libraries into lib/ |
| 61 | +set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) |
| 62 | + |
| 63 | +if (NOT CMAKE_BUILD_TYPE) |
| 64 | + set(CMAKE_BUILD_TYPE Release) |
| 65 | +endif() |
| 66 | + |
| 67 | +if (COMPILE_TESTS) |
| 68 | + # Tell cmake that we want unit tests to be compiled |
| 69 | + include(CTest) |
| 70 | + enable_testing() |
| 71 | +endif() |
| 72 | + |
| 73 | +# Add our CMake helper functions |
| 74 | +include(cmake/KenLMFunctions.cmake) |
| 75 | + |
| 76 | +if(MSVC) |
| 77 | + set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} /w34716") |
| 78 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /w34716") |
| 79 | +endif() |
| 80 | + |
| 81 | +# And our helper modules |
| 82 | +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules) |
| 83 | + |
| 84 | +# We need boost |
| 85 | +find_package(Boost 1.41.0 REQUIRED COMPONENTS |
| 86 | + program_options |
| 87 | + system |
| 88 | + thread |
| 89 | + unit_test_framework |
| 90 | +) |
| 91 | + |
| 92 | +# Define where include files live |
| 93 | +include_directories( |
| 94 | + ${PROJECT_SOURCE_DIR} |
| 95 | + ${Boost_INCLUDE_DIRS} |
| 96 | +) |
| 97 | + |
| 98 | +set(THREADS_PREFER_PTHREAD_FLAG ON) |
| 99 | +find_package(Threads REQUIRED) |
| 100 | + |
| 101 | +# Process subdirectories |
| 102 | +add_subdirectory(util) |
| 103 | +add_subdirectory(lm) |
| 104 | + |
| 105 | +foreach(SUBDIR IN ITEMS util util/double-conversion util/stream lm lm/builder lm/common lm/filter lm/interpolate) |
| 106 | + file(GLOB HEADERS ${CMAKE_CURRENT_LIST_DIR}/${SUBDIR}/*.h ${CMAKE_CURRENT_LIST_DIR}/${SUBDIR}/*.hh) |
| 107 | + install(FILES ${HEADERS} DESTINATION include/kenlm/${SUBDIR}) |
| 108 | +endforeach(SUBDIR) |
| 109 | + |
| 110 | +if(ENABLE_PYTHON) |
| 111 | + add_subdirectory(python) |
| 112 | +endif() |
| 113 | + |
0 commit comments