From a13ed0a609fe1d2dd7dc7fe9af4802c9554cbf6c Mon Sep 17 00:00:00 2001 From: WhiredPlanck Date: Mon, 6 Feb 2023 13:37:32 +0800 Subject: [PATCH] Add support of CMake config modules (#763) Currently OpenCC only has a pkg-config file, it's good for systems to locate standalone OpenCC package. But if a project using CMake that want to make OpenCC as its subproject / dependency, it will need to write a FindOpenCC.cmake to find package, which is a hassle to do so. And for Android platform, pkg-config is unavailable since NDK doesn't support it. So it's necessary to add support of CMake modules, then a project is able to just `find_package(OpenCC)` and `target_link_libraries( OpenCC::OpenCC)` to add OpenCC as subproject / dependency. --- CMakeLists.txt | 24 ++++++++++++++++++++++++ OpenCCConfig.cmake.in | 4 ++++ src/CMakeLists.txt | 22 +++++++++++++++++++--- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 OpenCCConfig.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b3b15d48..0e38cf01e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -105,6 +105,10 @@ set (DIR_SHARE_LOCALE ${DIR_SHARE}/locale/) ######## Configuration +include(CMakePackageConfigHelpers) + +set(targets_export_name OpenCCTargets) + configure_file( opencc.pc.in opencc.pc @@ -118,6 +122,26 @@ install( ${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 lib/cmake/opencc +) + +install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/OpenCCConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/OpenCCConfigVersion.cmake + DESTINATION + lib/cmake/opencc +) + ######## Compiler flags add_definitions( diff --git a/OpenCCConfig.cmake.in b/OpenCCConfig.cmake.in new file mode 100644 index 000000000..0d2eaa1df --- /dev/null +++ b/OpenCCConfig.cmake.in @@ -0,0 +1,4 @@ +@PACKAGE_INIT@ + +include(${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake) +check_required_components(OpenCC) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c0a0b10c2..62519b54d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -112,6 +112,7 @@ configure_file( "${PROJECT_BINARY_DIR}/src/opencc_config.h") add_library(libopencc ${LIBOPENCC_SOURCES} ${LIBOPENCC_HEADERS}) +add_library(OpenCC::OpenCC ALIAS libopencc) set_target_properties(libopencc PROPERTIES POSITION_INDEPENDENT_CODE ON) source_group(libopencc FILES ${LIBOPENCC_SOURCES} ${LIBOPENCC_HEADERS}) target_link_libraries(libopencc marisa) @@ -131,6 +132,8 @@ set_target_properties( CXX OUTPUT_NAME opencc + EXPORT_NAME + OpenCC VERSION ${OPENCC_VERSION_MAJOR}.${OPENCC_VERSION_MINOR}.${OPENCC_VERSION_REVISION} SOVERSION @@ -139,13 +142,26 @@ set_target_properties( # Installation +if (USE_SYSTEM_MARISA) + set(INSTALL_TARGETS libopencc) +else() + set(INSTALL_TARGETS libopencc marisa) +endif() + install( - TARGETS libopencc - LIBRARY DESTINATION ${DIR_LIBRARY} - ARCHIVE DESTINATION ${DIR_LIBRARY} + TARGETS ${INSTALL_TARGETS} EXPORT ${targets_export_name} + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib RUNTIME DESTINATION bin ) +install( + EXPORT ${targets_export_name} + FILE ${targets_export_name}.cmake + DESTINATION lib/cmake/opencc + NAMESPACE OpenCC:: +) + install( FILES ${LIBOPENCC_HEADERS} DESTINATION ${DIR_INCLUDE}/opencc