Skip to content

Commit 995e2d5

Browse files
committed
Add support of CMake config modules
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.
1 parent 200d01f commit 995e2d5

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

CMakeLists.txt

+24
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ set (DIR_SHARE_LOCALE ${DIR_SHARE}/locale/)
105105

106106
######## Configuration
107107

108+
include(CMakePackageConfigHelpers)
109+
110+
set(targets_export_name OpenCCTargets)
111+
108112
configure_file(
109113
opencc.pc.in
110114
opencc.pc
@@ -118,6 +122,26 @@ install(
118122
${DIR_LIBRARY}/pkgconfig
119123
)
120124

125+
write_basic_package_version_file(
126+
OpenCCConfigVersion.cmake
127+
VERSION ${OPENCC_VERSION}
128+
COMPATIBILITY SameMajorVersion
129+
)
130+
131+
configure_package_config_file(
132+
OpenCCConfig.cmake.in
133+
${CMAKE_CURRENT_BINARY_DIR}/OpenCCConfig.cmake
134+
INSTALL_DESTINATION lib/cmake/opencc
135+
)
136+
137+
install(
138+
FILES
139+
${CMAKE_CURRENT_BINARY_DIR}/OpenCCConfig.cmake
140+
${CMAKE_CURRENT_BINARY_DIR}/OpenCCConfigVersion.cmake
141+
DESTINATION
142+
lib/cmake/opencc
143+
)
144+
121145
######## Compiler flags
122146

123147
add_definitions(

OpenCCConfig.cmake.in

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@PACKAGE_INIT@
2+
3+
include(${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake)
4+
check_required_components(OpenCC)

src/CMakeLists.txt

+19-3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ configure_file(
112112
"${PROJECT_BINARY_DIR}/src/opencc_config.h")
113113

114114
add_library(libopencc ${LIBOPENCC_SOURCES} ${LIBOPENCC_HEADERS})
115+
add_library(OpenCC::OpenCC ALIAS libopencc)
115116
set_target_properties(libopencc PROPERTIES POSITION_INDEPENDENT_CODE ON)
116117
source_group(libopencc FILES ${LIBOPENCC_SOURCES} ${LIBOPENCC_HEADERS})
117118
target_link_libraries(libopencc marisa)
@@ -131,6 +132,8 @@ set_target_properties(
131132
CXX
132133
OUTPUT_NAME
133134
opencc
135+
EXPORT_NAME
136+
OpenCC
134137
VERSION
135138
${OPENCC_VERSION_MAJOR}.${OPENCC_VERSION_MINOR}.${OPENCC_VERSION_REVISION}
136139
SOVERSION
@@ -139,13 +142,26 @@ set_target_properties(
139142

140143
# Installation
141144

145+
if (USE_SYSTEM_MARISA)
146+
set(INSTALL_TARGETS libopencc)
147+
else()
148+
set(INSTALL_TARGETS libopencc marisa)
149+
endif()
150+
142151
install(
143-
TARGETS libopencc
144-
LIBRARY DESTINATION ${DIR_LIBRARY}
145-
ARCHIVE DESTINATION ${DIR_LIBRARY}
152+
TARGETS ${INSTALL_TARGETS} EXPORT ${targets_export_name}
153+
LIBRARY DESTINATION lib
154+
ARCHIVE DESTINATION lib
146155
RUNTIME DESTINATION bin
147156
)
148157

158+
install(
159+
EXPORT ${targets_export_name}
160+
FILE ${targets_export_name}.cmake
161+
DESTINATION lib/cmake/opencc
162+
NAMESPACE OpenCC::
163+
)
164+
149165
install(
150166
FILES ${LIBOPENCC_HEADERS}
151167
DESTINATION ${DIR_INCLUDE}/opencc

0 commit comments

Comments
 (0)