-
Notifications
You must be signed in to change notification settings - Fork 70
/
CMakeLists.txt
135 lines (117 loc) · 3.92 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
cmake_minimum_required(VERSION 3.1.0)
# Set policy for setting the MSVC runtime library for static MSVC builds
if(POLICY CMP0091)
cmake_policy(SET CMP0091 NEW)
endif()
project(OpenNMTTokenizer)
option(WITH_ICU "Compile with ICU" OFF)
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
if(MSVC)
if(NOT BUILD_SHARED_LIBS)
if(CMAKE_VERSION VERSION_LESS "3.15.0")
message(FATAL_ERROR "Use CMake 3.15 or later when setting BUILD_SHARED_LIBS to OFF")
endif()
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
endif()
set(INCLUDE_DIRECTORIES
${CMAKE_CURRENT_SOURCE_DIR}/include
${PROJECT_BINARY_DIR}
)
set(PUBLIC_HEADERS
include/onmt/Alphabet.h
include/onmt/Token.h
include/onmt/BPE.h
include/onmt/BPELearner.h
include/onmt/Casing.h
include/onmt/ITokenizer.h
include/onmt/SpaceTokenizer.h
include/onmt/SubwordEncoder.h
include/onmt/SubwordLearner.h
include/onmt/Tokenizer.h
)
set(SOURCES
src/Alphabet.cc
src/BPE.cc
src/BPELearner.cc
src/Casing.cc
src/ITokenizer.cc
src/SpaceTokenizer.cc
src/SubwordEncoder.cc
src/SubwordLearner.cc
src/Tokenizer.cc
src/unicode/Unicode.cc
)
list(APPEND LINK_LIBRARIES "")
if (WITH_ICU)
find_package(ICU REQUIRED)
add_definitions(-DWITH_ICU)
list(APPEND INCLUDE_DIRECTORIES ${ICU_INCLUDE_DIRS})
list(APPEND LINK_LIBRARIES ${ICU_LIBRARIES})
else()
list(APPEND SOURCES src/unicode/Data.cc)
endif()
find_library(SP_LIBRARY NAMES sentencepiece)
find_path(SP_INCLUDE_DIR NAMES sentencepiece_processor.h)
if(NOT SP_LIBRARY OR NOT SP_INCLUDE_DIR)
message(WARNING "sentencepiece not found; will not be supported")
else()
message(STATUS "Found sentencepiece: ${SP_LIBRARY}")
add_definitions(-DWITH_SP)
list(APPEND PUBLIC_HEADERS include/onmt/SentencePiece.h)
list(APPEND SOURCES src/SentencePiece.cc)
list(APPEND INCLUDE_DIRECTORIES ${SP_INCLUDE_DIR})
list(APPEND LINK_LIBRARIES ${SP_LIBRARY})
find_library(SP_TRAIN_LIBRARY NAMES sentencepiece_train)
if(SP_TRAIN_LIBRARY)
find_package(Threads)
add_definitions(-DWITH_SP_TRAIN)
message(STATUS "Found sentencepiece_train: ${SP_TRAIN_LIBRARY}")
list(APPEND PUBLIC_HEADERS include/onmt/SPMLearner.h)
list(APPEND SOURCES src/SPMLearner.cc)
list(APPEND LINK_LIBRARIES
${SP_TRAIN_LIBRARY}
${CMAKE_THREAD_LIBS_INIT})
else()
message(WARNING "sentencepiece_train not found: training SentencePiece models will not be supported")
endif()
file(STRINGS ${SP_INCLUDE_DIR}/sentencepiece_processor.h HAS_SAMPLE_ENCODE REGEX "SampleEncode")
if(HAS_SAMPLE_ENCODE)
add_definitions(-DSP_HAS_SAMPLE_ENCODE)
endif()
file(STRINGS ${SP_INCLUDE_DIR}/sentencepiece_processor.h HAS_VOCAB_RESTRICTION REGEX "SetVocabulary")
if(HAS_VOCAB_RESTRICTION)
add_definitions(-DSP_HAS_VOCAB_RESTRICTION)
endif()
endif()
add_library(${PROJECT_NAME} ${SOURCES})
include(GNUInstallDirs)
include(GenerateExportHeader)
string(TOLOWER ${PROJECT_NAME} PROJECT_NAME_LOWER)
generate_export_header(${PROJECT_NAME} EXPORT_FILE_NAME ${PROJECT_BINARY_DIR}/onmt/${PROJECT_NAME_LOWER}_export.h)
target_include_directories(${PROJECT_NAME} PUBLIC ${INCLUDE_DIRECTORIES})
target_link_libraries(${PROJECT_NAME} ${LINK_LIBRARIES})
if (NOT LIB_ONLY)
add_subdirectory(cli)
add_subdirectory(test)
endif()
install(
TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(FILES
${PUBLIC_HEADERS} "${PROJECT_BINARY_DIR}/onmt/${PROJECT_NAME_LOWER}_export.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/onmt"
)
install(FILES
include/onmt/unicode/Unicode.h
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/onmt/unicode"
)