From b7b3a0b7f50678c3a3de919024b78ba559b14648 Mon Sep 17 00:00:00 2001 From: Alex Murkoff <79400603+alexlnkp@users.noreply.github.com> Date: Sat, 25 May 2024 14:46:54 +0700 Subject: [PATCH] add options for customizing build --- CMakeLists.txt | 9 +++- src/CMakeLists.txt | 102 +++++++++++++++++++++++++++++---------------- 2 files changed, 73 insertions(+), 38 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d3ecfc7..e411258e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License.! -cmake_minimum_required(VERSION 3.1 FATAL_ERROR) +cmake_minimum_required(VERSION 3.5 FATAL_ERROR) file(STRINGS "VERSION.txt" SPM_VERSION) message(STATUS "VERSION: ${SPM_VERSION}") @@ -24,6 +24,9 @@ project(sentencepiece VERSION ${SPM_VERSION} LANGUAGES C CXX) option(SPM_ENABLE_NFKC_COMPILE "Enables NFKC compile" OFF) option(SPM_ENABLE_SHARED "Builds shared libaries in addition to static libraries." ON) +option(SPM_ENABLE_STATIC "Builds static libaries." ON) +option(SPM_BUILD_EXECUTABLES "Builds executables such as spm_encode and spm_decode." OFF) +option(SPM_BUILD_TRAINING "Builds training libraries." ON) option(SPM_BUILD_TEST "Builds test binaries." OFF) option(SPM_COVERAGE "Runs gcov to test coverage." OFF) option(SPM_ENABLE_TENSORFLOW_SHARED "Makes a tensorflow compatible shared file." OFF) @@ -38,6 +41,10 @@ set_property(CACHE SPM_PROTOBUF_PROVIDER PROPERTY STRINGS "internal" "package") set(SPM_ABSL_PROVIDER "internal" CACHE STRING "Provider of absl library") set_property(CACHE SPM_ABSL_PROVIDER PROPERTY STRINGS "internal" "module" "package") +if (SPM_BUILD_TEST) + set(SPM_BUILD_TRAINING ON) +endif() + if (SPM_CROSS_SYSTEM_PROCESSOR) set(CMAKE_SYSTEM_PROCESSOR ${SPM_CROSS_SYSTEM_PROCESSOR}) endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2f70fd60..4fb68909 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -223,41 +223,54 @@ endif() if (SPM_ENABLE_SHARED) add_library(sentencepiece SHARED ${SPM_SRCS}) - add_library(sentencepiece_train SHARED ${SPM_TRAIN_SRCS}) + set(SPM_INSTALLTARGETS sentencepiece) + if (SPM_BUILD_TRAINING) + add_library(sentencepiece_train SHARED ${SPM_TRAIN_SRCS}) + target_link_libraries(sentencepiece_train ${SPM_LIBS} sentencepiece) + set_target_properties(sentencepiece sentencepiece_train PROPERTIES SOVERSION 0 VERSION 0.0.0) + set_target_properties(sentencepiece_train PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS YES) + list(APPEND SPM_INSTALLTARGETS sentencepiece_train) + endif() if (ANDROID) target_link_libraries(sentencepiece log) - target_link_libraries(sentencepiece_train log) + if (SPM_BUILD_TRAINING) + target_link_libraries(sentencepiece_train log) + endif() endif() -endif() - -add_library(sentencepiece-static STATIC ${SPM_SRCS}) -add_library(sentencepiece_train-static STATIC ${SPM_TRAIN_SRCS}) -target_link_libraries(sentencepiece-static INTERFACE ${SPM_LIBS}) -target_link_libraries(sentencepiece_train-static INTERFACE sentencepiece-static ${SPM_LIBS}) - -if (SPM_ENABLE_SHARED) target_link_libraries(sentencepiece ${SPM_LIBS}) - target_link_libraries(sentencepiece_train ${SPM_LIBS} sentencepiece) - set(SPM_INSTALLTARGETS sentencepiece sentencepiece_train sentencepiece-static sentencepiece_train-static) - set_target_properties(sentencepiece sentencepiece_train PROPERTIES SOVERSION 0 VERSION 0.0.0) set_target_properties(sentencepiece PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS YES) - set_target_properties(sentencepiece_train PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS YES) + if (MSVC) set_target_properties(sentencepiece PROPERTIES IMPORT_SUFFIX "_import.lib") - set_target_properties(sentencepiece_train PROPERTIES IMPORT_SUFFIX "_import.lib") + if (SPM_BUILD_TRAINING) + set_target_properties(sentencepiece_train PROPERTIES IMPORT_SUFFIX "_import.lib") + endif() elseif (MINGW) set_target_properties(sentencepiece PROPERTIES IMPORT_SUFFIX ".dll.a") - set_target_properties(sentencepiece_train PROPERTIES IMPORT_SUFFIX ".dll.a") + if (SPM_BUILD_TRAINING) + set_target_properties(sentencepiece_train PROPERTIES IMPORT_SUFFIX ".dll.a") + endif() endif() else() + add_library(sentencepiece-static STATIC ${SPM_SRCS}) + target_link_libraries(sentencepiece-static INTERFACE ${SPM_LIBS}) + add_library(sentencepiece ALIAS sentencepiece-static) - add_library(sentencepiece_train ALIAS sentencepiece_train-static) - set(SPM_INSTALLTARGETS sentencepiece-static sentencepiece_train-static) -endif() + set_target_properties(sentencepiece-static PROPERTIES OUTPUT_NAME "sentencepiece") -set_target_properties(sentencepiece-static PROPERTIES OUTPUT_NAME "sentencepiece") -set_target_properties(sentencepiece_train-static PROPERTIES OUTPUT_NAME "sentencepiece_train") + if (SPM_BUILD_TRAINING) + add_library(sentencepiece_train-static STATIC ${SPM_TRAIN_SRCS}) + target_link_libraries(sentencepiece_train-static INTERFACE sentencepiece-static ${SPM_LIBS}) + + set(SPM_INSTALLTARGETS sentencepiece-static sentencepiece_train-static) + + add_library(sentencepiece_train ALIAS sentencepiece_train-static) + set_target_properties(sentencepiece_train-static PROPERTIES OUTPUT_NAME "sentencepiece_train") + else() + set(SPM_INSTALLTARGETS sentencepiece-static) + endif() +endif() if (NOT MSVC) if (SPM_COVERAGE) @@ -279,30 +292,43 @@ if (NOT MSVC) PROPERTIES COMPILE_FLAGS "-Wno-sign-compare") if (SPM_ENABLE_SHARED) set_property(TARGET sentencepiece APPEND_STRING PROPERTY COMPILE_FLAGS " -DPIC") - set_property(TARGET sentencepiece_train APPEND_STRING PROPERTY COMPILE_FLAGS " -DPIC") + if (SPM_BUILD_TRAINING) + set_property(TARGET sentencepiece_train APPEND_STRING PROPERTY COMPILE_FLAGS " -DPIC") + endif() endif() endif() -add_executable(spm_encode spm_encode_main.cc) -add_executable(spm_decode spm_decode_main.cc) -add_executable(spm_normalize spm_normalize_main.cc) -add_executable(spm_train spm_train_main.cc) -add_executable(spm_export_vocab spm_export_vocab_main.cc) +if (SPM_BUILD_EXECUTABLES) + add_executable(spm_encode spm_encode_main.cc) + add_executable(spm_decode spm_decode_main.cc) + if (SPM_BUILD_TRAINING) + add_executable(spm_normalize spm_normalize_main.cc) + add_executable(spm_train spm_train_main.cc) + endif() + add_executable(spm_export_vocab spm_export_vocab_main.cc) -target_link_libraries(spm_encode sentencepiece) -target_link_libraries(spm_decode sentencepiece) -target_link_libraries(spm_normalize sentencepiece sentencepiece_train) -target_link_libraries(spm_train sentencepiece sentencepiece_train) -target_link_libraries(spm_export_vocab sentencepiece) + target_link_libraries(spm_encode sentencepiece) + target_link_libraries(spm_decode sentencepiece) + if (SPM_BUILD_TRAINING) + target_link_libraries(spm_normalize sentencepiece sentencepiece_train) + target_link_libraries(spm_train sentencepiece sentencepiece_train) + endif() + target_link_libraries(spm_export_vocab sentencepiece) + if (SPM_BUILD_EXECUTABLES) + list(APPEND SPM_INSTALLTARGETS + spm_encode spm_decode spm_export_vocab) + if (SPM_BUILD_TRAINING) + list(APPEND SPM_INSTALLTARGETS + spm_normalize spm_train) + endif() + endif() +endif() if (SPM_ENABLE_NFKC_COMPILE) add_executable(compile_charsmap compile_charsmap_main.cc) target_link_libraries(compile_charsmap sentencepiece sentencepiece_train) endif() -list(APPEND SPM_INSTALLTARGETS - spm_encode spm_decode spm_normalize spm_train spm_export_vocab) - if (CMAKE_SYSTEM_NAME STREQUAL "iOS") install(TARGETS ${SPM_INSTALLTARGETS} BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR} @@ -316,8 +342,10 @@ install(TARGETS ${SPM_INSTALLTARGETS} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) endif() -install(FILES sentencepiece_trainer.h sentencepiece_processor.h - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +if (SPM_BUILD_TRAINING) + install(FILES sentencepiece_trainer.h sentencepiece_processor.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +endif() if (NOT SPM_PROTOBUF_PROVIDER STREQUAL "internal") install(FILES ${SPM_PROTO_HDRS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) endif()