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

debloat the cmakelists.txt and add a bunch of customization for building #1014

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
102 changes: 65 additions & 37 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}
Expand All @@ -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()
Expand Down
Loading