-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
CMakeLists.txt
316 lines (272 loc) · 8.88 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
cmake_minimum_required(VERSION 3.12)
project(kiwi VERSION 0.20.3 DESCRIPTION "Kiwi, Korean Intelligent Word Identifier")
set ( CMAKE_CXX_STANDARD 14 )
set ( CMAKE_VERBOSE_MAKEFILE true )
option(KIWI_USE_MIMALLOC "Use mimalloc for faster memory allocation" ON)
option(KIWI_USE_CPUINFO "Use cpuinfo for dynamic CPU dispatching" ON)
option(KIWI_STATIC_WITHOUT_MT "Use /MT Option in building kiwi_static" OFF)
option(KIWI_BUILD_CLI "Build CLI tool" ON)
option(KIWI_BUILD_EVALUATOR "Build Evaluator" ON)
option(KIWI_BUILD_MODEL_BUILDER "Build Model Builder" ON)
option(KIWI_BUILD_TEST "Build Test sets" ON)
option(KIWI_JAVA_BINDING "Build Java binding" OFF)
set(KIWI_CPU_ARCH "" CACHE STRING "Set architecture type for macOS")
if (NOT CMAKE_BUILD_TYPE)
if ("${CMAKE_BINARY_DIR}" MATCHES ".*(D|d)ebug$")
message(STATUS "No build type selected, default to: Debug")
set(CMAKE_BUILD_TYPE "Debug")
else()
message(STATUS "No build type selected, default to: Release")
set(CMAKE_BUILD_TYPE "Release")
endif()
endif()
if(NOT KIWI_CPU_ARCH)
if (EMSCRIPTEN)
set(KIWI_CPU_ARCH "wasm")
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
set(KIWI_CPU_ARCH "x86_64")
elseif (HOST_ARCHITECTURE MATCHES "^arm64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm|aarch64)")
set(KIWI_CPU_ARCH "arm64")
else()
set(KIWI_CPU_ARCH "other")
endif()
set(KIWI_CPU_ARCH "${KIWI_CPU_ARCH}" PARENT_SCOPE)
endif()
if(APPLE)
set(CMAKE_OSX_ARCHITECTURES "${KIWI_CPU_ARCH}")
endif()
set ( CORE_SRCS
src/ArchUtils.cpp
src/Combiner.cpp
src/Form.cpp
src/FeatureTestor.cpp
src/FileUtils.cpp
src/Dataset.cpp
src/Joiner.cpp
src/Kiwi.cpp
src/KiwiBuilder.cpp
src/Knlm.cpp
src/KTrie.cpp
src/PatternMatcher.cpp
src/search.cpp
src/ScriptType.cpp
src/SubstringExtractor.cpp
src/SwTokenizer.cpp
src/TagUtils.cpp
src/TypoTransformer.cpp
src/UnicodeCase.cpp
src/Utils.cpp
src/WordDetector.cpp
src/archImpl/none.cpp
)
if(KIWI_USE_MIMALLOC)
message(STATUS "Use mimalloc allocators")
set ( ADDITIONAL_FLAGS "-DKIWI_USE_MIMALLOC" )
include_directories( third_party/mimalloc/include )
set ( CORE_SRCS "${CORE_SRCS}"
third_party/mimalloc/src/static.c
)
endif()
include_directories( include/ )
include_directories( third_party/tclap/include )
include_directories( third_party/cpp-btree )
include_directories( third_party/variant/include )
include_directories( third_party/eigen )
include_directories( third_party/json/include )
if(KIWI_USE_CPUINFO)
message(STATUS "Use cpuinfo")
include_directories( third_party/cpuinfo/include )
set(CPUINFO_LIBRARY_TYPE "shared" CACHE STRING "")
set(CPUINFO_BUILD_TOOLS OFF CACHE BOOL "")
set(CPUINFO_BUILD_UNIT_TESTS OFF CACHE BOOL "")
set(CPUINFO_BUILD_MOCK_TESTS OFF CACHE BOOL "")
set(CPUINFO_BUILD_BENCHMARKS OFF CACHE BOOL "")
add_subdirectory( third_party/cpuinfo )
set ( ADDITIONAL_FLAGS ${ADDITIONAL_FLAGS} "-DKIWI_USE_CPUINFO" )
if(MSVC)
target_compile_options("clog" PUBLIC
/MT
)
target_compile_options("cpuinfo" PUBLIC
/MT
)
target_compile_options("cpuinfo_internals" PUBLIC
/MT
)
endif()
set ( CPUINFO_OBJECTS_STATIC
$<TARGET_OBJECTS:clog>
$<TARGET_OBJECTS:cpuinfo_internals>
)
set ( CPUINFO_OBJECTS_SHARED
$<TARGET_OBJECTS:clog>
$<TARGET_OBJECTS:cpuinfo>
)
endif()
if(MSVC)
set ( CMAKE_C_FLAGS_DEBUG "-DDEBUG -DC_FLAGS -Zi -Od /utf-8 /bigobj" )
set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}" )
set ( CMAKE_C_FLAGS_RELEASE "-DNDEBUG -DRELEASE -DC_FLAGS -O2 -Oi -Gy /utf-8 /bigobj -DKIWI_USE_BTREE" )
set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" )
set ( CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELEASE} -Zi")
set ( CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
set ( CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELEASE}" )
else()
link_libraries ( pthread )
set ( CMAKE_C_FLAGS_DEBUG "-DDEBUG -DC_FLAGS -g3 -O0" )
set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}" )
set ( CMAKE_EXE_LINKER_FLAGS_DEBUG "-DDEBUG -DLINKER_FLAGS" )
set ( CMAKE_C_FLAGS_RELEASE "-DNDEBUG -DRELEASE -DC_FLAGS -O3 -DKIWI_USE_BTREE" )
set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" )
set ( CMAKE_EXE_LINKER_FLAGS_RELEASE "-DRELEASE -DLINKER_FLAGS" )
set ( CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELEASE} -g3")
set ( CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
set ( CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELEASE}" )
endif()
if (KIWI_CPU_ARCH MATCHES "x86_64")
message("Compiling for x86_64")
set( CORE_SRCS
${CORE_SRCS}
src/archImpl/sse2.cpp
src/archImpl/sse4_1.cpp
)
if (KIWI_USE_CPUINFO)
set( CORE_SRCS
${CORE_SRCS}
src/archImpl/avx2.cpp
src/archImpl/avx512bw.cpp
)
endif()
if(MSVC)
set_source_files_properties(src/archImpl/sse2.cpp PROPERTIES COMPILE_FLAGS "/arch:SSE2")
set_source_files_properties(src/archImpl/sse4_1.cpp PROPERTIES COMPILE_FLAGS "/arch:SSE2")
if (KIWI_USE_CPUINFO)
set_source_files_properties(src/archImpl/avx2.cpp PROPERTIES COMPILE_FLAGS "/arch:AVX2")
set_source_files_properties(src/archImpl/avx512bw.cpp PROPERTIES COMPILE_FLAGS "/arch:AVX512")
endif()
else()
set_source_files_properties(src/archImpl/sse2.cpp PROPERTIES COMPILE_FLAGS "-msse2")
set_source_files_properties(src/archImpl/sse4_1.cpp PROPERTIES COMPILE_FLAGS "-msse2 -msse4.1")
if (KIWI_USE_CPUINFO)
set_source_files_properties(src/archImpl/avx2.cpp PROPERTIES COMPILE_FLAGS "-mavx -mavx2 -mfma")
set_source_files_properties(src/archImpl/avx512bw.cpp PROPERTIES COMPILE_FLAGS "-mavx512f -mavx512bw")
endif()
endif()
elseif (KIWI_CPU_ARCH MATCHES "arm64")
message("Compiling for arm64")
set( CORE_SRCS
${CORE_SRCS}
src/archImpl/neon.cpp
)
set_source_files_properties(src/archImpl/neon.cpp PROPERTIES COMPILE_FLAGS "-march=armv8-a")
elseif (KIWI_CPU_ARCH MATCHES "wasm")
message("Compiling for wasm")
else()
message("Compiling for other")
endif()
add_library( "${PROJECT_NAME}_static" STATIC
${CORE_SRCS}
src/capi/kiwi_c.cpp
${CPUINFO_OBJECTS_STATIC}
)
add_library( "${PROJECT_NAME}" SHARED
${CORE_SRCS}
src/capi/kiwi_c.cpp
${CPUINFO_OBJECTS_SHARED}
)
# Install the kiwi library as well as header files to (`include/kiwi` directory)
# so that a user can use it in their own projects that are not cmake projects.
file(GLOB KIWI_INCLUDE_FILES "include/kiwi/*.h" "include/kiwi/*.hpp")
set_target_properties("${PROJECT_NAME}" PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
PUBLIC_HEADER "${KIWI_INCLUDE_FILES}"
)
install(TARGETS ${PROJECT_NAME}_static PUBLIC_HEADER DESTINATION include/kiwi)
install(TARGETS ${PROJECT_NAME} PUBLIC_HEADER DESTINATION include/kiwi)
target_compile_options("${PROJECT_NAME}_static" PRIVATE "${ADDITIONAL_FLAGS}")
target_compile_options("${PROJECT_NAME}" PRIVATE "${ADDITIONAL_FLAGS}")
#target_link_libraries("${PROJECT_NAME}_static" cpuinfo_internals)
#target_link_libraries("${PROJECT_NAME}" cpuinfo)
if (KIWI_BUILD_CLI)
add_executable( "${PROJECT_NAME}-cli-${PROJECT_VERSION}"
tools/runner.cpp
)
target_link_libraries( "${PROJECT_NAME}-cli-${PROJECT_VERSION}"
"${PROJECT_NAME}_static"
)
endif()
if (KIWI_BUILD_EVALUATOR)
add_executable( "${PROJECT_NAME}-evaluator"
tools/Evaluator.cpp
tools/evaluator_main.cpp
)
target_link_libraries( "${PROJECT_NAME}-evaluator"
"${PROJECT_NAME}_static"
)
endif()
if (KIWI_BUILD_MODEL_BUILDER)
add_executable( "${PROJECT_NAME}-model-builder"
tools/model_builder.cpp
)
target_link_libraries( "${PROJECT_NAME}-model-builder"
"${PROJECT_NAME}_static"
)
endif()
if(MSVC)
if(KIWI_STATIC_WITHOUT_MT)
message(STATUS "Use /MD at kiwi_static")
add_library( "${PROJECT_NAME}_mt_static" STATIC
${CORE_SRCS}
src/capi/kiwi_c.cpp
${CPUINFO_OBJECTS_STATIC}
)
target_compile_options("${PROJECT_NAME}_mt_static" PUBLIC
/MT
)
else()
message(STATUS "Use /MT at kiwi_static")
target_compile_options("${PROJECT_NAME}_static" PUBLIC
/MT
)
endif()
target_compile_options("${PROJECT_NAME}" PUBLIC
/MT
)
endif()
if(UNIX AND NOT APPLE)
target_link_libraries( "${PROJECT_NAME}_static"
rt
)
if (KIWI_BUILD_CLI)
target_link_libraries( "${PROJECT_NAME}-cli-${PROJECT_VERSION}"
rt
)
endif()
if (KIWI_BUILD_EVALUATOR)
target_link_libraries( "${PROJECT_NAME}-evaluator"
rt
)
endif()
endif()
target_compile_definitions("${PROJECT_NAME}"
PUBLIC DLL_EXPORT=1
)
if(KIWI_BUILD_TEST)
add_subdirectory( third_party/googletest )
add_subdirectory( test )
if(MSVC)
target_compile_options("gtest_main" PUBLIC
/MT
)
target_compile_options("gtest" PUBLIC
/MT
)
endif()
endif()
if(KIWI_JAVA_BINDING)
add_subdirectory( bindings/java )
endif()
if(EMSCRIPTEN)
add_subdirectory( bindings/wasm )
endif()