Skip to content

Commit e1fdcf1

Browse files
authored
add support for cmake find_package (#365)
* add support for cmake find_package * fix * fix * [no ci] fix document
1 parent b3d7658 commit e1fdcf1

File tree

4 files changed

+77
-22
lines changed

4 files changed

+77
-22
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ enable_testing()
88
set(CMAKE_CXX_STANDARD 20)
99
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1010

11-
add_library(libasync_simple INTERFACE)
12-
target_compile_features(libasync_simple INTERFACE cxx_std_20)
13-
target_include_directories(libasync_simple INTERFACE
11+
add_library(async_simple_header_only INTERFACE)
12+
target_compile_features(async_simple_header_only INTERFACE cxx_std_20)
13+
target_include_directories(async_simple_header_only INTERFACE
1414
$<BUILD_INTERFACE:${async_simple_SOURCE_DIR}>
1515
$<INSTALL_INTERFACE:include>
1616
)
@@ -19,7 +19,7 @@ list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
1919
message(STATUS "CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}")
2020

2121
find_package(Threads REQUIRED)
22-
target_link_libraries(libasync_simple INTERFACE Threads::Threads)
22+
target_link_libraries(async_simple_header_only INTERFACE Threads::Threads)
2323
find_package(Aio QUIET)
2424

2525
find_package(Benchmark)

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,29 @@ docker build . --no-cache -t async_simple:1.0
180180
docker run -it --name async_simple async_simple:1.0 /bin/bash
181181
```
182182

183+
# Import
184+
185+
After install async_simple, you can import it to your project.
186+
187+
## Manully
188+
189+
async_simple is almost header-only. So you can just pass the include path of the install position to your compiler.
190+
191+
But the uthread part of async_simple is not head-only. If you want to use uthread, You need link it manully. The library file is in the install path.
192+
193+
## By cmake find_package
194+
195+
please add those cmake codes:
196+
197+
```cmake
198+
find_package(async_simple REQUIRED)
199+
target_link_libraries(<your-target-name> PRIVATE async_simple::async_simple) # dynamic_link
200+
# async_simple::async_simple_header_only
201+
# async_simple::async_simple_static
202+
```
203+
`<your-target-name>` is the target name which want to use async_simple
204+
205+
183206
# Get Started
184207

185208
Our documents are hosted by GitHub Pages, [go to Get Started](https://alibaba.github.io/async_simple/docs.en/GetStarted.html).

README_CN.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ docker build . --no-cache -t async_simple:1.0
173173
docker run -it --name async_simple async_simple:1.0 /bin/bash
174174
```
175175

176+
## 导入
177+
178+
当你安装完async_simple以后,你可以在你的项目里导入async_simple。
179+
180+
## 手动导入
181+
182+
async_simple几乎是header-only的. 因此你只需要将安装的include路径传递给编译器即可。
183+
184+
但是async_simple的uthread模块不是header-only的,如果你要使用uthread,我们在安装路径下生成了编译好的库文件,你需要手动链接它。
185+
186+
## 通过cmake find_package
187+
请添加以下cmake代码:
188+
```cmake
189+
find_package(async_simple REQUIRED)
190+
target_link_libraries(<your-target-name> PRIVATE async_simple::async_simple) # dynamic_link
191+
# async_simple::async_simple_header_only
192+
# async_simple::async_simple_static
193+
```
194+
其中,`<your-target-name>` 是你需要使用async_simple的target名
195+
176196
# 更多示例
177197

178198
我们的文档托管在GitHub Pages上,[点击进入快速开始](https://alibaba.github.io/async_simple/docs.cn/GetStarted.html).

async_simple/CMakeLists.txt

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,24 @@ if(UTHREAD)
2323
endif()
2424

2525
# If there is no Uthread, async_simple is a header only library
26-
if(NOT UTHREAD)
27-
add_library(async_simple INTERFACE ${SRCS})
28-
target_link_libraries(async_simple INTERFACE libasync_simple)
29-
install(TARGETS async_simple DESTINATION lib/)
30-
elseif(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
26+
if(UTHREAD)
3127
add_library(async_simple_static STATIC ${SRCS})
3228
add_library(async_simple SHARED ${SRCS})
33-
target_link_libraries(async_simple PUBLIC libasync_simple)
34-
target_link_libraries(async_simple_static PUBLIC libasync_simple)
35-
36-
set_target_properties(async_simple_static PROPERTIES OUTPUT_NAME "async_simple")
37-
38-
install(TARGETS async_simple DESTINATION lib/)
39-
install(TARGETS async_simple_static DESTINATION lib/)
29+
target_link_libraries(async_simple PUBLIC async_simple_header_only)
30+
target_link_libraries(async_simple_static PUBLIC async_simple_header_only)
31+
install(TARGETS async_simple EXPORT async_simple_targets DESTINATION lib/)
32+
install(TARGETS async_simple_static EXPORT async_simple_targets DESTINATION lib/)
4033
else()
41-
add_library(async_simple STATIC ${SRCS})
42-
target_link_libraries(async_simple PUBLIC libasync_simple)
43-
install(TARGETS async_simple DESTINATION lib/)
34+
add_library(async_simple_static INTERFACE)
35+
target_link_libraries(async_simple_static INTERFACE async_simple_header_only)
36+
install(TARGETS async_simple_static EXPORT async_simple_targets DESTINATION lib/)
37+
add_library(async_simple INTERFACE)
38+
target_link_libraries(async_simple INTERFACE async_simple_header_only)
39+
install(TARGETS async_simple EXPORT async_simple_targets DESTINATION lib/)
4440
endif()
4541

42+
install(TARGETS async_simple_header_only EXPORT async_simple_targets)
43+
4644
set_target_properties(async_simple PROPERTIES
4745
VERSION ${PROJECT_VERSION}
4846
SOVERSION ${PROJECT_VERSION_MAJOR})
@@ -57,6 +55,23 @@ if(UTHREAD)
5755
install(FILES ${uthread_internal_header} DESTINATION include/async_simple/uthread/internal)
5856
endif()
5957

58+
install(EXPORT async_simple_targets
59+
FILE async_simple-targets.cmake
60+
NAMESPACE async_simple::
61+
DESTINATION share/async_simple)
62+
63+
# Generate the config file in the current binary dir (this ensures it's not placed directly in source)
64+
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/async_simple-config.cmake"
65+
"include(CMakeFindDependencyMacro)\n"
66+
"find_package(Threads REQUIRED)\n"
67+
"include(\"\${CMAKE_CURRENT_LIST_DIR}/async_simple-targets.cmake\")\n"
68+
)
69+
70+
# Install the generated config file
71+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/async_simple-config.cmake"
72+
DESTINATION share/async_simple)
73+
74+
6075
if (${ASYNC_SIMPLE_ENABLE_TESTS})
6176
add_subdirectory(test)
6277
add_subdirectory(util/test)
@@ -66,6 +81,3 @@ if (${ASYNC_SIMPLE_ENABLE_TESTS})
6681
add_subdirectory(uthread/test)
6782
endif()
6883
endif()
69-
if (NOT TARGET async_simple::async_simple_header_only)
70-
add_library(async_simple::async_simple_header_only ALIAS libasync_simple)
71-
endif ()

0 commit comments

Comments
 (0)