-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
281 lines (238 loc) · 7.96 KB
/
CMakeLists.txt
File metadata and controls
281 lines (238 loc) · 7.96 KB
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
#
# Open Chinese Convert
#
# Copyright 2010-2020 Carbo Kuo <byvoid@byvoid.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
######## Project settings
cmake_minimum_required(VERSION 3.5)
set (PACKAGE_NAME opencc)
project (${PACKAGE_NAME} CXX)
include (CTest)
include(GNUInstallDirs)
######## Options
option(BUILD_DOCUMENTATION "Use Doxygen to create the HTML based API documentation" OFF)
option(BUILD_SHARED_LIBS "Build opencc as shared library" ON)
option(ENABLE_GTEST "Build all tests." OFF)
option(ENABLE_BENCHMARK "Build benchmark tests." OFF)
option(ENABLE_DARTS "Build DartsDict (ocd format)." ON)
option(BUILD_PYTHON "Build python library" OFF)
option(USE_SYSTEM_DARTS "Use system version of Darts" OFF)
option(USE_SYSTEM_GOOGLE_BENCHMARK "Use system version of Google Benchmark" OFF)
option(USE_SYSTEM_GTEST "Use system version of GoogleTest" OFF)
option(USE_SYSTEM_MARISA "Use system version of Marisa" OFF)
option(USE_SYSTEM_PYBIND11 "Use system version of pybind11" OFF)
option(USE_SYSTEM_RAPIDJSON "Use system version of RapidJSON" OFF)
option(USE_SYSTEM_TCLAP "Use system version of TCLAP" OFF)
######## Package information
set (PACKAGE_URL https://github.com/BYVoid/OpenCC)
set (PACKAGE_BUGREPORT https://github.com/BYVoid/OpenCC/issues)
# Derive version from Git tags (falls back to hardcoded default when Git is unavailable)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(GitVersion)
set(CPACK_SOURCE_PACKAGE_FILE_NAME
"${PACKAGE_NAME}-${OPENCC_VERSION_MAJOR}.${OPENCC_VERSION_MINOR}.${OPENCC_VERSION_REVISION}"
)
set(CPACK_SOURCE_IGNORE_FILES
"/build/;/test/dict.ocd;/test/dict.txt;/test/dict.bin;/other/;/opencc.xcodeproj/;/.git/;.gitignore;~$;.pyc;/bazel*;/node_modules;/.github;/.pytest_cache;/.vscode;${CPACK_SOURCE_IGNORE_FILES}"
)
include(CPack)
######## Windows
#if (WIN32)
# set(CMAKE_SHARED_LIBRARY_PREFIX ${CMAKE_INSTALL_PREFIX})
# set(CMAKE_STATIC_LIBRARY_PREFIX ${CMAKE_INSTALL_PREFIX})
#endif (WIN32)
######## macOS
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(CMAKE_MACOSX_RPATH 1)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
######## Directory
# Installation directories follow GNU Coding Standards via CMake's GNUInstallDirs module:
# https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html
#
# Default values and parity with previously hardcoded paths
# (shown for CMAKE_INSTALL_PREFIX=/usr/local and /usr):
#
# CMAKE_INSTALL_INCLUDEDIR "include" -> /usr/local/include or /usr/include
# (replaces: ${CMAKE_INSTALL_PREFIX}/include)
#
# CMAKE_INSTALL_SYSCONFDIR "etc" -> /usr/local/etc or /etc
# (replaces: ${CMAKE_INSTALL_PREFIX}/etc)
# NOTE: when prefix is /usr the full path is /etc per GNU Coding Standards,
# whereas the old code would have produced /usr/etc.
#
# CMAKE_INSTALL_LIBDIR "lib" -> /usr/local/lib or /usr/lib
# (also resolves to lib64 or lib/<multiarch> on applicable platforms)
# (replaces: ${CMAKE_INSTALL_PREFIX}/lib[64])
#
# CMAKE_INSTALL_DATAROOTDIR "share" -> /usr/local/share or /usr/share
# (replaces: ${CMAKE_INSTALL_PREFIX}/share)
# All DIR_* variables use relative paths so that install() destinations and
# CMake package config files remain relocatable.
set (DIR_INCLUDE ${CMAKE_INSTALL_INCLUDEDIR})
set (DIR_ETC ${CMAKE_INSTALL_SYSCONFDIR})
set (DIR_LIBRARY ${CMAKE_INSTALL_LIBDIR})
set (DIR_SHARE ${CMAKE_INSTALL_DATAROOTDIR})
if (DEFINED SHARE_INSTALL_PREFIX)
set (DIR_SHARE ${SHARE_INSTALL_PREFIX})
endif (DEFINED SHARE_INSTALL_PREFIX)
if (DEFINED INCLUDE_INSTALL_DIR)
set (DIR_INCLUDE ${INCLUDE_INSTALL_DIR})
endif (DEFINED INCLUDE_INSTALL_DIR)
if (DEFINED SYSCONF_INSTALL_DIR)
set (DIR_ETC ${SYSCONF_INSTALL_DIR})
endif (DEFINED SYSCONF_INSTALL_DIR)
if (DEFINED LIB_INSTALL_DIR)
set (DIR_LIBRARY ${LIB_INSTALL_DIR})
endif (DEFINED LIB_INSTALL_DIR)
set (DIR_SHARE_OPENCC ${DIR_SHARE}/opencc)
# Compute absolute paths needed for compile-time macros (PKGDATADIR/LOCALEDIR)
# and for the pkg-config file. We must handle both the GNUInstallDirs relative
# defaults and user-supplied absolute override paths (SHARE_INSTALL_PREFIX etc.).
foreach (_gnu_dir SHARE INCLUDE LIBRARY)
if (IS_ABSOLUTE "${DIR_${_gnu_dir}}")
set (DIR_${_gnu_dir}_FULL "${DIR_${_gnu_dir}}")
else ()
set (DIR_${_gnu_dir}_FULL "${CMAKE_INSTALL_PREFIX}/${DIR_${_gnu_dir}}")
endif ()
endforeach ()
set (DIR_SHARE_OPENCC_FULL ${DIR_SHARE_FULL}/opencc)
set (DIR_SHARE_LOCALE_FULL ${DIR_SHARE_FULL}/locale)
######## Configuration
include(CMakePackageConfigHelpers)
set(targets_export_name OpenCCTargets)
configure_file(
opencc.pc.in
opencc.pc
@ONLY
)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/opencc.pc
DESTINATION
${DIR_LIBRARY}/pkgconfig
)
write_basic_package_version_file(
OpenCCConfigVersion.cmake
VERSION ${OPENCC_VERSION}
COMPATIBILITY SameMajorVersion
)
configure_package_config_file(
OpenCCConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/OpenCCConfig.cmake
INSTALL_DESTINATION ${DIR_LIBRARY}/cmake/opencc
PATH_VARS DIR_INCLUDE
)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/OpenCCConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/OpenCCConfigVersion.cmake
DESTINATION
${DIR_LIBRARY}/cmake/opencc
)
######## Compiler flags
add_definitions(
-DPKGDATADIR="${DIR_SHARE_OPENCC_FULL}"
-DLOCALEDIR="${DIR_SHARE_LOCALE_FULL}"
-DVERSION="${OPENCC_VERSION}"
-DPACKAGE_NAME="${PACKAGE_NAME}"
)
set(CMAKE_CXX_STANDARD 17) # default C++ version for new target afterward
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
add_definitions(
-Wall
)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
if (CMAKE_BUILD_TYPE MATCHES Debug)
add_definitions(-O0 -g3)
endif ()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_definitions(
-Wall
)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
if (CMAKE_BUILD_TYPE MATCHES Debug)
add_definitions(-O0 -g3)
endif ()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_definitions(
/W4
/D "_CRT_SECURE_NO_WARNINGS"
)
endif()
if (NOT BUILD_SHARED_LIBS)
add_definitions(
-DOpencc_BUILT_AS_STATIC
)
endif (NOT BUILD_SHARED_LIBS)
if (ENABLE_GTEST)
add_definitions(
-DPROJECT_BINARY_DIR="${PROJECT_BINARY_DIR}"
-DCMAKE_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
)
endif()
if (ENABLE_BENCHMARK)
add_definitions(
-DPROJECT_BINARY_DIR="${PROJECT_BINARY_DIR}"
-DCMAKE_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
)
endif()
if (ENABLE_DARTS)
add_definitions(
-DENABLE_DARTS
)
endif()
######## Dependencies
if(NOT USE_SYSTEM_MARISA)
message(STATUS "Use bundled marisa library.")
add_subdirectory(deps/marisa-0.2.6)
else()
find_library(LIBMARISA NAMES marisa)
if (LIBMARISA)
message(STATUS "libmarisa found: ${LIBMARISA}")
else()
message(FATAL_ERROR "libmarisa not found.")
endif()
endif()
######## Subdirectories
add_subdirectory(src)
add_subdirectory(doc)
add_subdirectory(data)
add_subdirectory(test)
######## Testing
if (ENABLE_GTEST)
if(NOT USE_SYSTEM_GTEST)
add_subdirectory(deps/googletest-1.15.0)
endif()
enable_testing()
endif()
if (ENABLE_BENCHMARK)
set(BENCHMARK_ENABLE_TESTING OFF)
if(NOT USE_SYSTEM_GOOGLE_BENCHMARK)
add_subdirectory(deps/google-benchmark)
endif()
enable_testing()
endif()
######## Python
if (BUILD_PYTHON)
if(USE_SYSTEM_PYBIND11)
find_package(pybind11 CONFIG)
else()
add_subdirectory(deps/pybind11-2.13.1)
endif()
pybind11_add_module(opencc_clib src/py_opencc.cpp)
target_link_libraries(opencc_clib PRIVATE libopencc)
endif()