From 573d679e28e9213a1a831c97f74336e7e9996ff3 Mon Sep 17 00:00:00 2001 From: Michel Zou Date: Wed, 13 May 2020 12:51:29 +0200 Subject: [PATCH] Add python/CMakeLists --- CMakeLists.txt | 5 +++++ lm/CMakeLists.txt | 1 + python/CMakeLists.txt | 28 ++++++++++++++++++++++++++++ util/CMakeLists.txt | 1 + 4 files changed, 35 insertions(+) create mode 100644 python/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 742edbf56..c6d357173 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,3 +83,8 @@ include_directories( add_subdirectory(util) add_subdirectory(lm) +option (ENABLE_PYTHON "python bindings" OFF) +if(ENABLE_PYTHON) + add_subdirectory(python) +endif() + diff --git a/lm/CMakeLists.txt b/lm/CMakeLists.txt index cb76e57ee..f931db8ef 100644 --- a/lm/CMakeLists.txt +++ b/lm/CMakeLists.txt @@ -35,6 +35,7 @@ if (NOT MSVC) endif() add_library(kenlm ${KENLM_LM_SOURCE} ${KENLM_LM_COMMON_SOURCE}) +set_target_properties(kenlm PROPERTIES POSITION_INDEPENDENT_CODE ON) target_link_libraries(kenlm kenlm_util ${Boost_LIBRARIES} ${THREADS}) set(KENLM_MAX_ORDER 6 CACHE STRING "Maximum supported ngram order") diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt new file mode 100644 index 000000000..d835e5c66 --- /dev/null +++ b/python/CMakeLists.txt @@ -0,0 +1,28 @@ +find_package(PythonInterp REQUIRED) +find_package(PythonLibs ${PYTHON_VERSION_STRING} EXACT REQUIRED) +include_directories(${PYTHON_INCLUDE_DIRS}) + +add_library(kenlm_python MODULE kenlm.cpp score_sentence.cc) +set_target_properties(kenlm_python PROPERTIES OUTPUT_NAME kenlm) +set_target_properties(kenlm_python PROPERTIES PREFIX "") + +if(APPLE) + set_target_properties(kenlm_python PROPERTIES SUFFIX ".so") +elseif(WIN32) + set_target_properties(kenlm_python PROPERTIES SUFFIX ".pyd") +endif() + +target_link_libraries(kenlm_python kenlm) +if(WIN32) + target_link_libraries(kenlm_python ${PYTHON_LIBRARIES}) +elseif(APPLE) + set_target_properties(kenlm_python PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") +endif() + +if (WIN32) + set (PYTHON_SITE_PACKAGES Lib/site-packages) +else () + set (PYTHON_SITE_PACKAGES lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/site-packages) +endif () + +install(TARGETS kenlm_python DESTINATION ${PYTHON_SITE_PACKAGES}) diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index a148ad457..0f0324cd6 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -69,6 +69,7 @@ endif() # Group these objects together for later use. add_library(kenlm_util ${KENLM_UTIL_DOUBLECONVERSION_SOURCE} ${KENLM_UTIL_STREAM_SOURCE} ${KENLM_UTIL_SOURCE}) +set_target_properties(kenlm_util PROPERTIES POSITION_INDEPENDENT_CODE ON) target_link_libraries(kenlm_util ${Boost_LIBRARIES} ${READ_COMPRESSED_LIBS} ${THREADS} ${TIMER_LINK}) AddExes(EXES probing_hash_table_benchmark