Skip to content

Commit

Permalink
Add support of CMake config modules (#763)
Browse files Browse the repository at this point in the history
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(<project_library_name> OpenCC::OpenCC)` to add OpenCC as subproject / dependency.
  • Loading branch information
WhiredPlanck authored Feb 6, 2023
1 parent 200d01f commit a13ed0a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions OpenCCConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PACKAGE_INIT@

include(${CMAKE_CURRENT_LIST_DIR}/@[email protected])
check_required_components(OpenCC)
22 changes: 19 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a13ed0a

Please sign in to comment.