Debian Codegraph @

CMakeLists.txt

raw ◀ ▶
1 Debian patch touch this file
1 #
2 # Open Chinese Convert
3 #
4 # Copyright 2010-2020 Carbo Kuo <[email protected]>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 ######## Project settings
20 cmake_minimum_required(VERSION 3.12)
21 set (PACKAGE_NAME opencc)
22 project (${PACKAGE_NAME} CXX)
23 include (CTest)
24 include(GNUInstallDirs)
25
26 ######## Options
27
28 # PROJECT_IS_TOP_LEVEL is available since CMake 3.21; provide a fallback.
29 if(CMAKE_VERSION VERSION_LESS 3.21)
30 if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
31 set(PROJECT_IS_TOP_LEVEL TRUE)
32 else()
33 set(PROJECT_IS_TOP_LEVEL FALSE)
34 endif()
35 endif()
36
37 # When used via FetchContent / add_subdirectory, build a static library and
38 # skip install rules by default.
39 if(PROJECT_IS_TOP_LEVEL)
40 set(_OPENCC_DEFAULT_BUILD_SHARED_LIBS ON)
41 set(_OPENCC_DEFAULT_ENABLE_INSTALL ON)
42 else()
43 set(_OPENCC_DEFAULT_BUILD_SHARED_LIBS OFF)
44 set(_OPENCC_DEFAULT_ENABLE_INSTALL OFF)
45 endif()
46
47 option(BUILD_DOCUMENTATION "Use Doxygen to create the HTML based API documentation" OFF)
48 option(BUILD_SHARED_LIBS "Build opencc as shared library" ${_OPENCC_DEFAULT_BUILD_SHARED_LIBS})
49 option(OPENCC_ENABLE_INSTALL "Generate install targets" ${_OPENCC_DEFAULT_ENABLE_INSTALL})
50 option(ENABLE_GTEST "Build all tests." OFF)
51 option(ENABLE_BENCHMARK "Build benchmark tests." OFF)
52 # Top-level macOS builds enable the Jieba plugin by default so package
53 # managers that build with default options (e.g. Homebrew) ship it. Other
54 # platforms and subproject builds (FetchContent / add_subdirectory) keep it
55 # opt-in.
56 if (APPLE AND CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
57 set(OPENCC_JIEBA_PLUGIN_DEFAULT ON)
58 else()
59 set(OPENCC_JIEBA_PLUGIN_DEFAULT OFF)
60 endif()
61 option(BUILD_OPENCC_JIEBA_PLUGIN "Build Jieba segmentation as a loadable plugin." ${OPENCC_JIEBA_PLUGIN_DEFAULT})
62 option(BUILD_PYTHON "Build python library" OFF)
63
64 # Built-in dictionaries and configs can be produced as compiled ocd2
65 # (marisa-trie, default) or legacy ocd (Darts) dictionaries, or as plain text
66 # dictionaries with configs that reference them directly (slower to load,
67 # but human-readable and easy to interoperate with other toolchains).
68 set(OPENCC_DICT_FORMAT "ocd2" CACHE STRING "Format of built-in dictionaries and configs to generate/install: ocd2, ocd, or text")
69 set_property(CACHE OPENCC_DICT_FORMAT PROPERTY STRINGS ocd2 ocd text)
70 if(NOT OPENCC_DICT_FORMAT STREQUAL "ocd2" AND NOT OPENCC_DICT_FORMAT STREQUAL "ocd" AND NOT OPENCC_DICT_FORMAT STREQUAL "text")
71 message(FATAL_ERROR "OPENCC_DICT_FORMAT must be one of 'ocd2', 'ocd', or 'text', got '${OPENCC_DICT_FORMAT}'")
72 endif()
73 if(OPENCC_DICT_FORMAT STREQUAL "ocd" AND NOT ENABLE_DARTS)
74 message(FATAL_ERROR "OPENCC_DICT_FORMAT=ocd requires ENABLE_DARTS=ON (DartsDict support)")
75 endif()
76 option(USE_SYSTEM_DARTS "Use system version of Darts" OFF)
77 option(USE_SYSTEM_GOOGLE_BENCHMARK "Use system version of Google Benchmark" OFF)
78 option(USE_SYSTEM_GTEST "Use system version of GoogleTest" OFF)
79 option(USE_SYSTEM_MARISA "Use system version of Marisa" OFF)
80 option(USE_SYSTEM_PYBIND11 "Use system version of pybind11" OFF)
81 option(USE_SYSTEM_RAPIDJSON "Use system version of RapidJSON" OFF)
82 option(USE_SYSTEM_TCLAP "Use system version of TCLAP" OFF)
83 set(OPENCC_SOURCE_PACKAGE_PROFILE "full" CACHE STRING "Source package profile: full, opencc, or opencc-jieba")
84 set_property(CACHE OPENCC_SOURCE_PACKAGE_PROFILE PROPERTY STRINGS full opencc opencc-jieba)
85
86 ######## Package information
87 set (PACKAGE_URL https://github.com/BYVoid/OpenCC)
88 set (PACKAGE_BUGREPORT https://github.com/BYVoid/OpenCC/issues)
89
90 # Derive version from Git tags (falls back to hardcoded default when Git is unavailable)
91 list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
92 include(GitVersion)
93
94 # C++ interface/layout changes after 1.3.1 require downstream C++ consumers
95 # such as librime to relink instead of loading an ABI-incompatible libopencc.
96 set (OPENCC_ABI_VERSION 1.4)
97
98 set(_OPENCC_SOURCE_PACKAGE_SUFFIX "")
99 set(_OPENCC_SOURCE_PACKAGE_IGNORE_FILES "")
100 set(_OPENCC_TRIMMED_SOURCE_PACKAGE_IGNORE_FILES
101 "/.appveyor.yml"
102 "/.bazel[^/]*"
103 "/.agents/"
104 "/.claude/"
105 "/.npmignore"
106 "/AGENTS.md"
107 "/BUILD.bazel"
108 "/CLAUDE.md"
109 "/CONTRIBUTING.md"
110 "/DESIGN_PRINCIPLES.md"
111 "/MODULE.bazel"
112 "/MODULE.bazel.lock"
113 "/MANIFEST.in"
114 "/Makefile"
115 "/OpenCC.egg-info/"
116 "/PUBLICATIONS.md"
117 "/binding.gyp"
118 "/build.cmd"
119 "/data/icon/"
120 "/deps/google-benchmark/"
121 "/deps/pybind11-2.13.1/"
122 "/doc/"
123 "/examples/"
124 "/node/"
125 "/package.json"
126 "/package-lock.json"
127 "/patches/"
128 "/pyproject.toml"
129 "/python[^/]*/"
130 "/release-pypi-[^/]*"
131 "${PROJECT_SOURCE_DIR}/scripts/"
132 "/setup.py"
133 "/test.cmd"
134 )
135 if(OPENCC_SOURCE_PACKAGE_PROFILE STREQUAL "opencc")
136 set(_OPENCC_SOURCE_PACKAGE_SUFFIX "-opencc-src")
137 set(_OPENCC_SOURCE_PACKAGE_IGNORE_FILES
138 ${_OPENCC_TRIMMED_SOURCE_PACKAGE_IGNORE_FILES}
139 "/plugins/"
140 )
141 elseif(OPENCC_SOURCE_PACKAGE_PROFILE STREQUAL "opencc-jieba")
142 set(CPACK_SOURCE_PACKAGE_FILE_NAME
143 "opencc-jieba-${OPENCC_VERSION_MAJOR}.${OPENCC_VERSION_MINOR}.${OPENCC_VERSION_REVISION}-src"
144 )
145 set(_OPENCC_SOURCE_PACKAGE_IGNORE_FILES
146 ${_OPENCC_TRIMMED_SOURCE_PACKAGE_IGNORE_FILES}
147 "/plugins/jieba/node/"
148 )
149 elseif(NOT OPENCC_SOURCE_PACKAGE_PROFILE STREQUAL "full")
150 message(FATAL_ERROR "OPENCC_SOURCE_PACKAGE_PROFILE must be 'full', 'opencc', or 'opencc-jieba'.")
151 endif()
152
153 if(NOT CPACK_SOURCE_PACKAGE_FILE_NAME)
154 set(CPACK_SOURCE_PACKAGE_FILE_NAME
155 "${PACKAGE_NAME}-${OPENCC_VERSION_MAJOR}.${OPENCC_VERSION_MINOR}.${OPENCC_VERSION_REVISION}${_OPENCC_SOURCE_PACKAGE_SUFFIX}"
156 )
157 endif()
158 set(CPACK_SOURCE_IGNORE_FILES
159 "/build[^/]*/;/test/dict.ocd;/test/dict.txt;/test/dict.bin;/dict.ocd;/.DS_Store;/.zig-[^/]*/;/dist[^/]*/;/other/;/opencc.xcodeproj/;/.git/;.gitignore;~$;.pyc;/bazel*;/node_modules;/.github;/.pytest_cache;/.vscode;/packaging/;${_OPENCC_SOURCE_PACKAGE_IGNORE_FILES};${CPACK_SOURCE_IGNORE_FILES}"
160 )
161 include(CPack)
162
163 ######## Windows
164
165 #if (WIN32)
166 # set(CMAKE_SHARED_LIBRARY_PREFIX ${CMAKE_INSTALL_PREFIX})
167 # set(CMAKE_STATIC_LIBRARY_PREFIX ${CMAKE_INSTALL_PREFIX})
168 #endif (WIN32)
169
170 ######## macOS
171
172 if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
173 set(CMAKE_MACOSX_RPATH 1)
174 set(CMAKE_CXX_STANDARD 17)
175 set(CMAKE_CXX_STANDARD_REQUIRED ON)
176 endif()
177
178 ######## Directory
179 # Installation directories follow GNU Coding Standards via CMake's GNUInstallDirs module:
180 # https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html
181 #
182 # Default values and parity with previously hardcoded paths
183 # (shown for CMAKE_INSTALL_PREFIX=/usr/local and /usr):
184 #
185 # CMAKE_INSTALL_INCLUDEDIR "include" -> /usr/local/include or /usr/include
186 # (replaces: ${CMAKE_INSTALL_PREFIX}/include)
187 #
188 # CMAKE_INSTALL_SYSCONFDIR "etc" -> /usr/local/etc or /etc
189 # (replaces: ${CMAKE_INSTALL_PREFIX}/etc)
190 # NOTE: when prefix is /usr the full path is /etc per GNU Coding Standards,
191 # whereas the old code would have produced /usr/etc.
192 #
193 # CMAKE_INSTALL_LIBDIR "lib" -> /usr/local/lib or /usr/lib
194 # (also resolves to lib64 or lib/<multiarch> on applicable platforms)
195 # (replaces: ${CMAKE_INSTALL_PREFIX}/lib[64])
196 #
197 # CMAKE_INSTALL_DATAROOTDIR "share" -> /usr/local/share or /usr/share
198 # (replaces: ${CMAKE_INSTALL_PREFIX}/share)
199
200 # All DIR_* variables use relative paths so that install() destinations and
201 # CMake package config files remain relocatable.
202 set (DIR_INCLUDE ${CMAKE_INSTALL_INCLUDEDIR})
203 set (DIR_ETC ${CMAKE_INSTALL_SYSCONFDIR})
204 set (DIR_LIBRARY ${CMAKE_INSTALL_LIBDIR})
205 set (DIR_SHARE ${CMAKE_INSTALL_DATAROOTDIR})
206
207 if (DEFINED SHARE_INSTALL_PREFIX)
208 set (DIR_SHARE ${SHARE_INSTALL_PREFIX})
209 endif (DEFINED SHARE_INSTALL_PREFIX)
210
211 if (DEFINED INCLUDE_INSTALL_DIR)
212 set (DIR_INCLUDE ${INCLUDE_INSTALL_DIR})
213 endif (DEFINED INCLUDE_INSTALL_DIR)
214
215 if (DEFINED SYSCONF_INSTALL_DIR)
216 set (DIR_ETC ${SYSCONF_INSTALL_DIR})
217 endif (DEFINED SYSCONF_INSTALL_DIR)
218
219 if (DEFINED LIB_INSTALL_DIR)
220 set (DIR_LIBRARY ${LIB_INSTALL_DIR})
221 endif (DEFINED LIB_INSTALL_DIR)
222
223 set (DIR_SHARE_OPENCC ${DIR_SHARE}/opencc)
224 if (WIN32)
225 set (DIR_PLUGIN bin/plugins)
226 else ()
227 set (DIR_PLUGIN ${DIR_LIBRARY}/opencc/plugins)
228 endif ()
229
230 # Compute absolute paths needed for compile-time macros (PKGDATADIR/LOCALEDIR)
231 # and for the pkg-config file. We must handle both the GNUInstallDirs relative
232 # defaults and user-supplied absolute override paths (SHARE_INSTALL_PREFIX etc.).
233 foreach (_gnu_dir SHARE INCLUDE LIBRARY)
234 if (IS_ABSOLUTE "${DIR_${_gnu_dir}}")
235 set (DIR_${_gnu_dir}_FULL "${DIR_${_gnu_dir}}")
236 else ()
237 set (DIR_${_gnu_dir}_FULL "${CMAKE_INSTALL_PREFIX}/${DIR_${_gnu_dir}}")
238 endif ()
239 endforeach ()
240
241 set (DIR_SHARE_OPENCC_FULL ${DIR_SHARE_FULL}/opencc)
242 set (DIR_SHARE_LOCALE_FULL ${DIR_SHARE_FULL}/locale)
243 if (IS_ABSOLUTE "${DIR_PLUGIN}")
244 set (DIR_PLUGIN_FULL "${DIR_PLUGIN}")
245 else ()
246 set (DIR_PLUGIN_FULL "${CMAKE_INSTALL_PREFIX}/${DIR_PLUGIN}")
247 endif ()
248
249 ######## Configuration
250
251 include(CMakePackageConfigHelpers)
252
253 set(OPENCC_TARGETS_EXPORT_NAME OpenCCTargets)
254
255 configure_file(
256 opencc.pc.in
257 opencc.pc
258 @ONLY
259 )
260
261 if(OPENCC_ENABLE_INSTALL)
262 install(
263 FILES
264 ${CMAKE_CURRENT_BINARY_DIR}/opencc.pc
265 DESTINATION
266 ${DIR_LIBRARY}/pkgconfig
267 )
268 endif()
269
270 write_basic_package_version_file(
271 OpenCCConfigVersion.cmake
272 VERSION ${OPENCC_VERSION}
273 COMPATIBILITY SameMajorVersion
274 )
275
276 configure_package_config_file(
277 OpenCCConfig.cmake.in
278 ${CMAKE_CURRENT_BINARY_DIR}/OpenCCConfig.cmake
279 INSTALL_DESTINATION ${DIR_LIBRARY}/cmake/opencc
280 PATH_VARS DIR_INCLUDE DIR_PLUGIN DIR_SHARE_OPENCC
281 )
282
283 if(OPENCC_ENABLE_INSTALL)
284 install(
285 FILES
286 ${CMAKE_CURRENT_BINARY_DIR}/OpenCCConfig.cmake
287 ${CMAKE_CURRENT_BINARY_DIR}/OpenCCConfigVersion.cmake
288 DESTINATION
289 ${DIR_LIBRARY}/cmake/opencc
290 )
291 endif()
292
293 ######## Compiler flags
294
295 add_definitions(
296 -DPKGDATADIR="${DIR_SHARE_OPENCC_FULL}"
297 -DLOCALEDIR="${DIR_SHARE_LOCALE_FULL}"
298 -DOPENCC_VERSION="${OPENCC_VERSION}"
299 -DPACKAGE_NAME="${PACKAGE_NAME}"
300 )
301
302 set(CMAKE_CXX_STANDARD 17) # default C++ version for new target afterward
303 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
304 add_compile_options(
305 -Wall
306 )
307 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
308 if (CMAKE_BUILD_TYPE MATCHES Debug)
309 add_compile_options(-O0 -g3)
310 endif ()
311 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
312 add_compile_options(
313 -Wall
314 )
315 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
316 if (CMAKE_BUILD_TYPE MATCHES Debug)
317 add_compile_options(-O0 -g3)
318 endif ()
319 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
320 add_compile_options(/W4)
321 add_definitions(/D "_CRT_SECURE_NO_WARNINGS")
322 endif()
323
324 if (NOT BUILD_SHARED_LIBS)
325 add_definitions(
326 -DOpencc_BUILT_AS_STATIC
327 )
328 endif (NOT BUILD_SHARED_LIBS)
329
330 if (ENABLE_GTEST)
331 add_definitions(
332 -DPROJECT_BINARY_DIR="${PROJECT_BINARY_DIR}"
333 -DPROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}"
334 )
335 endif()
336
337 if (ENABLE_BENCHMARK)
338 add_definitions(
339 -DPROJECT_BINARY_DIR="${PROJECT_BINARY_DIR}"
340 -DPROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}"
341 )
342 endif()
343
344 add_definitions(
345 -DOPENCC_SEGMENTATION_PLUGIN_DIR="${DIR_PLUGIN_FULL}"
346 )
347
348
349 ######## Dependencies
350
351 if(NOT USE_SYSTEM_MARISA)
352 message(STATUS "Use bundled marisa library.")
353 add_subdirectory(deps/marisa-0.3.1)
354 else()
355 find_library(LIBMARISA NAMES marisa)
356 if (LIBMARISA)
357 message(STATUS "libmarisa found: ${LIBMARISA}")
358 else()
359 message(FATAL_ERROR "libmarisa not found.")
360 endif()
361 endif()
362
363 ######## Subdirectories
364
365 add_subdirectory(src)
366 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/doc/CMakeLists.txt")
367 add_subdirectory(doc)
368 elseif(BUILD_DOCUMENTATION)
369 message(FATAL_ERROR "BUILD_DOCUMENTATION requires the doc source directory.")
370 endif()
371 add_subdirectory(data)
372 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/plugins/CMakeLists.txt")
373 add_subdirectory(plugins)
374 elseif(BUILD_OPENCC_JIEBA_PLUGIN)
375 message(FATAL_ERROR "BUILD_OPENCC_JIEBA_PLUGIN requires the plugins source directory.")
376 endif()
377 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt")
378 add_subdirectory(test)
379 elseif(ENABLE_GTEST)
380 message(FATAL_ERROR "ENABLE_GTEST requires the test source directory.")
381 endif()
382
383 if (ENABLE_BENCHMARK AND TARGET performance AND TARGET opencc_jieba)
384 target_compile_definitions(performance PRIVATE
385 OPENCC_BENCHMARK_JIEBA_CONFIG_DIR="${PROJECT_SOURCE_DIR}/plugins/jieba/data/config"
386 OPENCC_BENCHMARK_JIEBA_PLUGIN_DIR="$<TARGET_FILE_DIR:opencc_jieba>"
387 )
388 add_dependencies(performance opencc_jieba)
389 if (WIN32)
390 add_custom_target(
391 copy_opencc_jieba_to_benchmark
392 ${CMAKE_COMMAND} -E copy $<TARGET_FILE:opencc_jieba> ${PROJECT_BINARY_DIR}/src/benchmark
393 COMMENT "Copying opencc_jieba to src/benchmark"
394 )
395 add_dependencies(performance copy_opencc_jieba_to_benchmark)
396 endif()
397 endif()
398
399 ######## Testing
400
401 if (ENABLE_GTEST)
402 if(NOT USE_SYSTEM_GTEST AND NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/deps/googletest-1.15.0/CMakeLists.txt")
403 message(FATAL_ERROR "ENABLE_GTEST with bundled GoogleTest requires deps/googletest-1.15.0.")
404 endif()
405 if(NOT USE_SYSTEM_GTEST)
406 add_subdirectory(deps/googletest-1.15.0)
•407 else()
408 add_subdirectory(/usr/src/googletest/googletest ${CMAKE_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL)
409 endif()
410 enable_testing()
411 endif()
412
413 if (ENABLE_BENCHMARK)
414 if(NOT USE_SYSTEM_GOOGLE_BENCHMARK AND NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/deps/google-benchmark/CMakeLists.txt")
415 message(FATAL_ERROR "ENABLE_BENCHMARK with bundled Google Benchmark requires deps/google-benchmark.")
416 endif()
417 set(BENCHMARK_ENABLE_TESTING OFF)
418 if(NOT USE_SYSTEM_GOOGLE_BENCHMARK)
419 add_subdirectory(deps/google-benchmark)
420 endif()
421 enable_testing()
422 endif()
423
424 ######## Python
425
426 if (BUILD_PYTHON)
427 if(USE_SYSTEM_PYBIND11)
428 find_package(pybind11 CONFIG)
429 else()
430 if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/deps/pybind11-2.13.1/CMakeLists.txt")
431 message(FATAL_ERROR "BUILD_PYTHON with bundled pybind11 requires deps/pybind11-2.13.1.")
432 endif()
433 add_subdirectory(deps/pybind11-2.13.1)
434 endif()
435 pybind11_add_module(opencc_clib src/py_opencc.cpp)
436 target_link_libraries(opencc_clib PRIVATE libopencc)
437 endif()