| 1 |
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
| 2 |
# Standalone mode: Building the plugin outside of the OpenCC parent tree |
| 3 |
cmake_minimum_required(VERSION 3.10) |
| 4 |
project(opencc_jieba) |
| 5 |
set(CMAKE_CXX_STANDARD 17) |
| 6 |
set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 7 |
option(OPENCC_ENABLE_INSTALL "Generate install targets" ON) |
| 8 |
|
| 9 |
find_package(OpenCC REQUIRED) |
| 10 |
|
| 11 |
set(_opencc_install_prefix_hint "") |
| 12 |
if (OPENCC_SHARE_DIR AND IS_ABSOLUTE "${OPENCC_SHARE_DIR}") |
| 13 |
get_filename_component(_opencc_share_parent "${OPENCC_SHARE_DIR}" DIRECTORY) |
| 14 |
get_filename_component(_opencc_install_prefix_hint "${_opencc_share_parent}" DIRECTORY) |
| 15 |
elseif (OPENCC_INCLUDE_DIR AND IS_ABSOLUTE "${OPENCC_INCLUDE_DIR}") |
| 16 |
get_filename_component(_opencc_include_parent "${OPENCC_INCLUDE_DIR}" DIRECTORY) |
| 17 |
get_filename_component(_opencc_install_prefix_hint "${_opencc_include_parent}" DIRECTORY) |
| 18 |
endif() |
| 19 |
|
| 20 |
function(opencc_make_install_destination_relative output_var package_path fallback_path) |
| 21 |
if (NOT package_path) |
| 22 |
set(${output_var} "${fallback_path}" PARENT_SCOPE) |
| 23 |
return() |
| 24 |
endif() |
| 25 |
if (NOT IS_ABSOLUTE "${package_path}") |
| 26 |
set(${output_var} "${package_path}" PARENT_SCOPE) |
| 27 |
return() |
| 28 |
endif() |
| 29 |
|
| 30 |
file(RELATIVE_PATH _relative_path "${CMAKE_INSTALL_PREFIX}" "${package_path}") |
| 31 |
if (NOT _relative_path MATCHES "^[.][.]/" AND NOT _relative_path STREQUAL "..") |
| 32 |
set(${output_var} "${_relative_path}" PARENT_SCOPE) |
| 33 |
return() |
| 34 |
endif() |
| 35 |
|
| 36 |
if (_opencc_install_prefix_hint AND IS_ABSOLUTE "${_opencc_install_prefix_hint}") |
| 37 |
file(RELATIVE_PATH _relative_path "${_opencc_install_prefix_hint}" "${package_path}") |
| 38 |
if (NOT _relative_path MATCHES "^[.][.]/" AND NOT _relative_path STREQUAL "..") |
| 39 |
set(${output_var} "${_relative_path}" PARENT_SCOPE) |
| 40 |
return() |
| 41 |
endif() |
| 42 |
endif() |
| 43 |
|
| 44 |
set(${output_var} "${fallback_path}" PARENT_SCOPE) |
| 45 |
endfunction() |
| 46 |
|
| 47 |
if (NOT DEFINED DIR_PLUGIN) |
| 48 |
if (WIN32) |
| 49 |
set(_opencc_default_plugin_dir bin/plugins) |
| 50 |
else() |
| 51 |
set(_opencc_default_plugin_dir lib/opencc/plugins) |
| 52 |
endif() |
| 53 |
opencc_make_install_destination_relative( |
| 54 |
DIR_PLUGIN "${OPENCC_PLUGIN_DIR}" "${_opencc_default_plugin_dir}" |
| 55 |
) |
| 56 |
endif() |
| 57 |
if (NOT DEFINED DIR_SHARE_OPENCC) |
| 58 |
opencc_make_install_destination_relative( |
| 59 |
DIR_SHARE_OPENCC "${OPENCC_SHARE_DIR}" "share/opencc" |
| 60 |
) |
| 61 |
endif() |
| 62 |
|
| 63 |
set(BUILD_OPENCC_JIEBA_PLUGIN ON) |
| 64 |
else() |
| 65 |
# Integrated mode: Included from OpenCC root |
| 66 |
if (NOT BUILD_OPENCC_JIEBA_PLUGIN AND NOT ENABLE_GTEST) |
| 67 |
return() |
| 68 |
endif() |
| 69 |
endif() |
| 70 |
|
| 71 |
if (NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
| 72 |
include_directories("${PROJECT_SOURCE_DIR}") |
| 73 |
include_directories("${PROJECT_BINARY_DIR}/src") |
| 74 |
include_directories("${PROJECT_SOURCE_DIR}/src") |
| 75 |
endif() |
| 76 |
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/deps/cppjieba/include") |
| 77 |
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") |
| 78 |
if (NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
| 79 |
include_directories("${PROJECT_SOURCE_DIR}/deps/rapidjson-1.1.0") |
| 80 |
endif() |
| 81 |
|
| 82 |
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
| 83 |
# OpenCC C++ Headers are provided by find_package |
| 84 |
add_library(opencc_jieba SHARED |
| 85 |
src/JiebaSegmentation.cpp |
| 86 |
src/JiebaSegmentationPlugin.cpp |
| 87 |
) |
| 88 |
target_link_libraries(opencc_jieba PRIVATE OpenCC::OpenCC) |
| 89 |
else() |
| 90 |
add_library(opencc_jieba SHARED |
| 91 |
src/JiebaSegmentation.cpp |
| 92 |
src/JiebaSegmentationPlugin.cpp |
| 93 |
${PROJECT_SOURCE_DIR}/src/plugin/OpenCCPlugin.h |
| 94 |
) |
| 95 |
target_include_directories(opencc_jieba PRIVATE |
| 96 |
${PROJECT_SOURCE_DIR}/src |
| 97 |
${PROJECT_BINARY_DIR}/src |
| 98 |
) |
| 99 |
target_link_libraries(opencc_jieba PRIVATE libopencc) |
| 100 |
endif() |
| 101 |
target_compile_definitions(opencc_jieba PRIVATE OPENCC_PLUGIN_BUILD) |
| 102 |
|
| 103 |
set(JIEBA_MERGED_DICT_DIR "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/jieba_dict") |
| 104 |
set(JIEBA_MERGED_DICT "${JIEBA_MERGED_DICT_DIR}/jieba_merged.ocd2") |
| 105 |
set(OPENCC_CAN_RUN_CPPJIEBA_DICT TRUE) |
| 106 |
|
| 107 |
function(opencc_normalize_arch output_var input_value) |
| 108 |
string(TOUPPER "${input_value}" _arch) |
| 109 |
if (_arch STREQUAL "AMD64" OR _arch STREQUAL "X86_64") |
| 110 |
set(_arch "X64") |
| 111 |
elseif (_arch STREQUAL "AARCH64") |
| 112 |
set(_arch "ARM64") |
| 113 |
elseif (_arch MATCHES "^I[3-6]86$" OR _arch STREQUAL "X86" OR |
| 114 |
_arch STREQUAL "WIN32") |
| 115 |
set(_arch "X86") |
| 116 |
endif() |
| 117 |
set(${output_var} "${_arch}" PARENT_SCOPE) |
| 118 |
endfunction() |
| 119 |
|
| 120 |
function(opencc_can_run_target_on_host output_var host_arch target_arch) |
| 121 |
set(_can_run FALSE) |
| 122 |
if ("${host_arch}" STREQUAL "${target_arch}") |
| 123 |
set(_can_run TRUE) |
| 124 |
elseif ("${host_arch}" STREQUAL "X64" AND "${target_arch}" STREQUAL "X86") |
| 125 |
set(_can_run TRUE) |
| 126 |
endif() |
| 127 |
set(${output_var} "${_can_run}" PARENT_SCOPE) |
| 128 |
endfunction() |
| 129 |
|
| 130 |
if (DEFINED CMAKE_VS_PLATFORM_NAME AND NOT CMAKE_VS_PLATFORM_NAME STREQUAL "") |
| 131 |
set(_opencc_target_arch "${CMAKE_VS_PLATFORM_NAME}") |
| 132 |
else() |
| 133 |
set(_opencc_target_arch "${CMAKE_SYSTEM_PROCESSOR}") |
| 134 |
endif() |
| 135 |
set(_opencc_host_arch "${CMAKE_HOST_SYSTEM_PROCESSOR}") |
| 136 |
opencc_normalize_arch(_opencc_target_arch "${_opencc_target_arch}") |
| 137 |
opencc_normalize_arch(_opencc_host_arch "${_opencc_host_arch}") |
| 138 |
|
| 139 |
if (CMAKE_CROSSCOMPILING) |
| 140 |
set(OPENCC_CAN_RUN_CPPJIEBA_DICT FALSE) |
| 141 |
endif() |
| 142 |
if (WIN32 AND _opencc_target_arch AND _opencc_host_arch) |
| 143 |
opencc_can_run_target_on_host(_opencc_can_run_target "${_opencc_host_arch}" |
| 144 |
"${_opencc_target_arch}") |
| 145 |
if (NOT _opencc_can_run_target) |
| 146 |
set(OPENCC_CAN_RUN_CPPJIEBA_DICT FALSE) |
| 147 |
endif() |
| 148 |
endif() |
| 149 |
|
| 150 |
if (OPENCC_CAN_RUN_CPPJIEBA_DICT) |
| 151 |
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
| 152 |
# Standalone plugin build: use the opencc_dict installed alongside the |
| 153 |
# OpenCC package found by find_package. Merging the bundled Jieba |
| 154 |
# dictionaries requires the cppjieba_utf8 input format (OpenCC >= 1.4.1). |
| 155 |
find_program(OPENCC_DICT_EXECUTABLE opencc_dict |
| 156 |
HINTS "${_opencc_install_prefix_hint}/bin") |
| 157 |
if (NOT OPENCC_DICT_EXECUTABLE) |
| 158 |
message(FATAL_ERROR |
| 159 |
"opencc_dict not found. Building the merged Jieba dictionary requires " |
| 160 |
"the opencc_dict tool from OpenCC 1.4.1 or later.") |
| 161 |
endif() |
| 162 |
# An opencc_dict installed to a non-default prefix carries no rpath, so |
| 163 |
# help the dynamic linker find libopencc from the same installation. |
| 164 |
get_filename_component(_opencc_dict_bindir |
| 165 |
"${OPENCC_DICT_EXECUTABLE}" DIRECTORY) |
| 166 |
get_filename_component(_opencc_dict_prefix |
| 167 |
"${_opencc_dict_bindir}" DIRECTORY) |
| 168 |
if (WIN32) |
| 169 |
# The installed layout keeps opencc.dll next to opencc_dict.exe. |
| 170 |
set(OPENCC_DICT_COMMAND "${OPENCC_DICT_EXECUTABLE}") |
| 171 |
elseif (APPLE) |
| 172 |
set(OPENCC_DICT_COMMAND ${CMAKE_COMMAND} -E env |
| 173 |
"DYLD_LIBRARY_PATH=${_opencc_dict_prefix}/lib:$ENV{DYLD_LIBRARY_PATH}" |
| 174 |
"${OPENCC_DICT_EXECUTABLE}") |
| 175 |
else() |
| 176 |
set(OPENCC_DICT_COMMAND ${CMAKE_COMMAND} -E env |
| 177 |
"LD_LIBRARY_PATH=${_opencc_dict_prefix}/lib:$ENV{LD_LIBRARY_PATH}" |
| 178 |
"${OPENCC_DICT_EXECUTABLE}") |
| 179 |
endif() |
| 180 |
execute_process( |
| 181 |
COMMAND ${OPENCC_DICT_COMMAND} --help |
| 182 |
RESULT_VARIABLE _opencc_dict_help_result |
| 183 |
OUTPUT_VARIABLE _opencc_dict_help |
| 184 |
ERROR_VARIABLE _opencc_dict_help_err |
| 185 |
) |
| 186 |
if (NOT _opencc_dict_help_result EQUAL 0) |
| 187 |
message(FATAL_ERROR |
| 188 |
"Failed to run ${OPENCC_DICT_EXECUTABLE} (exit status " |
| 189 |
"${_opencc_dict_help_result}): ${_opencc_dict_help_err}") |
| 190 |
endif() |
| 191 |
if (NOT "${_opencc_dict_help}${_opencc_dict_help_err}" MATCHES |
| 192 |
"cppjieba_utf8") |
| 193 |
message(FATAL_ERROR |
| 194 |
"${OPENCC_DICT_EXECUTABLE} does not support the cppjieba_utf8 input " |
| 195 |
"format. OpenCC 1.4.1 or later is required.") |
| 196 |
endif() |
| 197 |
set(OPENCC_DICT_TARGET_DEPENDS) |
| 198 |
else() |
| 199 |
set(OPENCC_DICT_COMMAND opencc_dict) |
| 200 |
set(OPENCC_DICT_TARGET_DEPENDS opencc_dict) |
| 201 |
endif() |
| 202 |
endif() |
| 203 |
|
| 204 |
if (WIN32 AND OPENCC_CAN_RUN_CPPJIEBA_DICT AND |
| 205 |
NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
| 206 |
# opencc_dict in the build tree needs libopencc next to it to run; data/ |
| 207 |
# already defines this copy target for its own dictionary generation. An |
| 208 |
# installed opencc_dict (standalone build) ships next to its runtime. |
| 209 |
set(JIEBA_MERGED_DICT_WIN32_DEPENDS copy_libopencc_to_dir_of_opencc_dict) |
| 210 |
else() |
| 211 |
set(JIEBA_MERGED_DICT_WIN32_DEPENDS) |
| 212 |
endif() |
| 213 |
|
| 214 |
if (OPENCC_CAN_RUN_CPPJIEBA_DICT) |
| 215 |
add_custom_command( |
| 216 |
OUTPUT "${JIEBA_MERGED_DICT}" |
| 217 |
COMMAND ${CMAKE_COMMAND} -E make_directory "${JIEBA_MERGED_DICT_DIR}" |
| 218 |
COMMAND ${OPENCC_DICT_COMMAND} |
| 219 |
--from cppjieba_utf8 |
| 220 |
--to ocd2 |
| 221 |
--input "${CMAKE_CURRENT_SOURCE_DIR}/deps/cppjieba/dict/jieba.dict.utf8" |
| 222 |
--input "${CMAKE_CURRENT_SOURCE_DIR}/deps/cppjieba/dict/user.dict.utf8" |
| 223 |
--output "${JIEBA_MERGED_DICT}" |
| 224 |
DEPENDS |
| 225 |
${JIEBA_MERGED_DICT_WIN32_DEPENDS} |
| 226 |
${OPENCC_DICT_TARGET_DEPENDS} |
| 227 |
"${CMAKE_CURRENT_SOURCE_DIR}/deps/cppjieba/dict/jieba.dict.utf8" |
| 228 |
"${CMAKE_CURRENT_SOURCE_DIR}/deps/cppjieba/dict/user.dict.utf8" |
| 229 |
VERBATIM |
| 230 |
) |
| 231 |
|
| 232 |
add_custom_target(jieba_merged_dict ALL DEPENDS "${JIEBA_MERGED_DICT}") |
| 233 |
add_dependencies(opencc_jieba jieba_merged_dict) |
| 234 |
endif() |
| 235 |
|
| 236 |
# On Windows, OpenCC headers must not use dllimport when the plugin links |
| 237 |
# against a static OpenCC library. This affects both integrated builds and |
| 238 |
# standalone plugin builds via find_package(OpenCC). |
| 239 |
if (WIN32) |
| 240 |
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
| 241 |
get_target_property(_opencc_target_type OpenCC::OpenCC TYPE) |
| 242 |
else() |
| 243 |
get_target_property(_opencc_target_type libopencc TYPE) |
| 244 |
endif() |
| 245 |
if (_opencc_target_type STREQUAL "STATIC_LIBRARY") |
| 246 |
target_compile_definitions(opencc_jieba PRIVATE Opencc_BUILT_AS_STATIC) |
| 247 |
endif() |
| 248 |
endif() |
| 249 |
|
| 250 |
set_target_properties(opencc_jieba PROPERTIES |
| 251 |
OUTPUT_NAME opencc-jieba |
| 252 |
POSITION_INDEPENDENT_CODE ON |
| 253 |
) |
| 254 |
target_include_directories(opencc_jieba PRIVATE |
| 255 |
${CMAKE_CURRENT_SOURCE_DIR}/include |
| 256 |
${CMAKE_CURRENT_SOURCE_DIR}/deps/cppjieba/include |
| 257 |
) |
| 258 |
|
| 259 |
if (BUILD_OPENCC_JIEBA_PLUGIN AND OPENCC_ENABLE_INSTALL) |
| 260 |
install( |
| 261 |
TARGETS opencc_jieba |
| 262 |
LIBRARY DESTINATION ${DIR_PLUGIN} |
| 263 |
ARCHIVE DESTINATION ${DIR_PLUGIN} |
| 264 |
RUNTIME DESTINATION ${DIR_PLUGIN} |
| 265 |
) |
| 266 |
|
| 267 |
if (OPENCC_CAN_RUN_CPPJIEBA_DICT) |
| 268 |
if (CMAKE_CONFIGURATION_TYPES) |
| 269 |
install(CODE |
| 270 |
"set(_jieba_merged_dict \"${CMAKE_CURRENT_BINARY_DIR}/\${CMAKE_INSTALL_CONFIG_NAME}/jieba_dict/jieba_merged.ocd2\")\n" |
| 271 |
"if(NOT EXISTS \"\${_jieba_merged_dict}\")\n" |
| 272 |
" message(FATAL_ERROR \"Merged Jieba dictionary not found: \${_jieba_merged_dict}\")\n" |
| 273 |
"endif()\n" |
| 274 |
"file(INSTALL DESTINATION \"\${CMAKE_INSTALL_PREFIX}/${DIR_SHARE_OPENCC}/jieba_dict\" TYPE FILE FILES \"\${_jieba_merged_dict}\")\n" |
| 275 |
) |
| 276 |
else() |
| 277 |
install( |
| 278 |
FILES "${JIEBA_MERGED_DICT}" |
| 279 |
DESTINATION ${DIR_SHARE_OPENCC}/jieba_dict |
| 280 |
) |
| 281 |
endif() |
| 282 |
endif() |
| 283 |
|
| 284 |
install( |
| 285 |
FILES |
| 286 |
${CMAKE_CURRENT_SOURCE_DIR}/data/config/hk2sp_jieba.json |
| 287 |
${CMAKE_CURRENT_SOURCE_DIR}/data/config/s2hk_jieba.json |
| 288 |
${CMAKE_CURRENT_SOURCE_DIR}/data/config/s2hkp_jieba.json |
| 289 |
${CMAKE_CURRENT_SOURCE_DIR}/data/config/s2t_jieba.json |
| 290 |
${CMAKE_CURRENT_SOURCE_DIR}/data/config/s2tw_jieba.json |
| 291 |
${CMAKE_CURRENT_SOURCE_DIR}/data/config/s2twp_jieba.json |
| 292 |
${CMAKE_CURRENT_SOURCE_DIR}/data/config/tw2sp_jieba.json |
| 293 |
DESTINATION |
| 294 |
${DIR_SHARE_OPENCC} |
| 295 |
) |
| 296 |
|
| 297 |
install( |
| 298 |
FILES |
| 299 |
${CMAKE_CURRENT_SOURCE_DIR}/deps/cppjieba/dict/hmm_model.utf8 |
| 300 |
${CMAKE_CURRENT_SOURCE_DIR}/deps/cppjieba/dict/idf.utf8 |
| 301 |
${CMAKE_CURRENT_SOURCE_DIR}/deps/cppjieba/dict/stop_words.utf8 |
| 302 |
${CMAKE_CURRENT_SOURCE_DIR}/deps/cppjieba/dict/jieba.dict.utf8 |
| 303 |
${CMAKE_CURRENT_SOURCE_DIR}/deps/cppjieba/dict/user.dict.utf8 |
| 304 |
DESTINATION |
| 305 |
${DIR_SHARE_OPENCC}/jieba_dict |
| 306 |
) |
| 307 |
endif() |
| 308 |
|
| 309 |
if (ENABLE_GTEST AND NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
| 310 |
file(GENERATE |
| 311 |
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/generated/$<CONFIG>/JiebaPluginIntegrationTestConfig.hpp" |
| 312 |
CONTENT "#pragma once\n#define OPENCC_JIEBA_MERGED_DICT_PATH \"${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/jieba_dict/jieba_merged.ocd2\"\n" |
| 313 |
) |
| 314 |
|
| 315 |
if (WIN32) |
| 316 |
add_custom_target( |
| 317 |
copy_gtest_to_plugins_jieba |
| 318 |
${CMAKE_COMMAND} -E copy $<TARGET_FILE:gtest> ${CMAKE_CURRENT_BINARY_DIR} |
| 319 |
COMMENT "Copying gtest to plugins/jieba" |
| 320 |
) |
| 321 |
add_custom_target( |
| 322 |
copy_gtest_main_to_plugins_jieba |
| 323 |
${CMAKE_COMMAND} -E copy $<TARGET_FILE:gtest_main> ${CMAKE_CURRENT_BINARY_DIR} |
| 324 |
COMMENT "Copying gtest_main to plugins/jieba" |
| 325 |
) |
| 326 |
endif() |
| 327 |
|
| 328 |
add_executable(JiebaPluginIntegrationTest tests/JiebaPluginIntegrationTest.cpp) |
| 329 |
target_compile_definitions(JiebaPluginIntegrationTest PRIVATE |
| 330 |
OPENCC_TEST_COMMAND="$<TARGET_FILE:opencc>" |
| 331 |
OPENCC_PLUGIN_TEST_DIR="$<TARGET_FILE_DIR:opencc_jieba>" |
| 332 |
) |
| 333 |
target_include_directories(JiebaPluginIntegrationTest PRIVATE |
| 334 |
"${CMAKE_CURRENT_BINARY_DIR}/generated/$<CONFIG>" |
| 335 |
) |
| 336 |
target_link_libraries(JiebaPluginIntegrationTest gtest gtest_main) |
| 337 |
add_dependencies(JiebaPluginIntegrationTest opencc opencc_jieba Dictionaries) |
| 338 |
if (TARGET jieba_merged_dict) |
| 339 |
add_dependencies(JiebaPluginIntegrationTest jieba_merged_dict) |
| 340 |
endif() |
| 341 |
add_test(JiebaPluginIntegrationTest JiebaPluginIntegrationTest) |
| 342 |
set_tests_properties( |
| 343 |
JiebaPluginIntegrationTest |
| 344 |
PROPERTIES |
| 345 |
ENVIRONMENT "OPENCC_SEGMENTATION_PLUGIN_PATH=$<TARGET_FILE_DIR:opencc_jieba>" |
| 346 |
) |
| 347 |
if (WIN32) |
| 348 |
add_dependencies(JiebaPluginIntegrationTest |
| 349 |
copy_gtest_to_plugins_jieba |
| 350 |
copy_gtest_main_to_plugins_jieba |
| 351 |
) |
| 352 |
endif() |
| 353 |
endif() |