Skip to content

Commit

Permalink
add support for cmake find_package (#365)
Browse files Browse the repository at this point in the history
* add support for cmake find_package

* fix

* fix

* [no ci] fix document
  • Loading branch information
poor-circle authored Jan 29, 2024
1 parent b3d7658 commit e1fdcf1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 22 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ enable_testing()
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(libasync_simple INTERFACE)
target_compile_features(libasync_simple INTERFACE cxx_std_20)
target_include_directories(libasync_simple INTERFACE
add_library(async_simple_header_only INTERFACE)
target_compile_features(async_simple_header_only INTERFACE cxx_std_20)
target_include_directories(async_simple_header_only INTERFACE
$<BUILD_INTERFACE:${async_simple_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)
Expand All @@ -19,7 +19,7 @@ list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
message(STATUS "CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}")

find_package(Threads REQUIRED)
target_link_libraries(libasync_simple INTERFACE Threads::Threads)
target_link_libraries(async_simple_header_only INTERFACE Threads::Threads)
find_package(Aio QUIET)

find_package(Benchmark)
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,29 @@ docker build . --no-cache -t async_simple:1.0
docker run -it --name async_simple async_simple:1.0 /bin/bash
```

# Import

After install async_simple, you can import it to your project.

## Manully

async_simple is almost header-only. So you can just pass the include path of the install position to your compiler.

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.

## By cmake find_package

please add those cmake codes:

```cmake
find_package(async_simple REQUIRED)
target_link_libraries(<your-target-name> PRIVATE async_simple::async_simple) # dynamic_link
# async_simple::async_simple_header_only
# async_simple::async_simple_static
```
`<your-target-name>` is the target name which want to use async_simple


# Get Started

Our documents are hosted by GitHub Pages, [go to Get Started](https://alibaba.github.io/async_simple/docs.en/GetStarted.html).
Expand Down
20 changes: 20 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@ docker build . --no-cache -t async_simple:1.0
docker run -it --name async_simple async_simple:1.0 /bin/bash
```

## 导入

当你安装完async_simple以后,你可以在你的项目里导入async_simple。

## 手动导入

async_simple几乎是header-only的. 因此你只需要将安装的include路径传递给编译器即可。

但是async_simple的uthread模块不是header-only的,如果你要使用uthread,我们在安装路径下生成了编译好的库文件,你需要手动链接它。

## 通过cmake find_package
请添加以下cmake代码:
```cmake
find_package(async_simple REQUIRED)
target_link_libraries(<your-target-name> PRIVATE async_simple::async_simple) # dynamic_link
# async_simple::async_simple_header_only
# async_simple::async_simple_static
```
其中,`<your-target-name>` 是你需要使用async_simple的target名

# 更多示例

我们的文档托管在GitHub Pages上,[点击进入快速开始](https://alibaba.github.io/async_simple/docs.cn/GetStarted.html).
Expand Down
48 changes: 30 additions & 18 deletions async_simple/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,24 @@ if(UTHREAD)
endif()

# If there is no Uthread, async_simple is a header only library
if(NOT UTHREAD)
add_library(async_simple INTERFACE ${SRCS})
target_link_libraries(async_simple INTERFACE libasync_simple)
install(TARGETS async_simple DESTINATION lib/)
elseif(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
if(UTHREAD)
add_library(async_simple_static STATIC ${SRCS})
add_library(async_simple SHARED ${SRCS})
target_link_libraries(async_simple PUBLIC libasync_simple)
target_link_libraries(async_simple_static PUBLIC libasync_simple)

set_target_properties(async_simple_static PROPERTIES OUTPUT_NAME "async_simple")

install(TARGETS async_simple DESTINATION lib/)
install(TARGETS async_simple_static DESTINATION lib/)
target_link_libraries(async_simple PUBLIC async_simple_header_only)
target_link_libraries(async_simple_static PUBLIC async_simple_header_only)
install(TARGETS async_simple EXPORT async_simple_targets DESTINATION lib/)
install(TARGETS async_simple_static EXPORT async_simple_targets DESTINATION lib/)
else()
add_library(async_simple STATIC ${SRCS})
target_link_libraries(async_simple PUBLIC libasync_simple)
install(TARGETS async_simple DESTINATION lib/)
add_library(async_simple_static INTERFACE)
target_link_libraries(async_simple_static INTERFACE async_simple_header_only)
install(TARGETS async_simple_static EXPORT async_simple_targets DESTINATION lib/)
add_library(async_simple INTERFACE)
target_link_libraries(async_simple INTERFACE async_simple_header_only)
install(TARGETS async_simple EXPORT async_simple_targets DESTINATION lib/)
endif()

install(TARGETS async_simple_header_only EXPORT async_simple_targets)

set_target_properties(async_simple PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR})
Expand All @@ -57,6 +55,23 @@ if(UTHREAD)
install(FILES ${uthread_internal_header} DESTINATION include/async_simple/uthread/internal)
endif()

install(EXPORT async_simple_targets
FILE async_simple-targets.cmake
NAMESPACE async_simple::
DESTINATION share/async_simple)

# Generate the config file in the current binary dir (this ensures it's not placed directly in source)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/async_simple-config.cmake"
"include(CMakeFindDependencyMacro)\n"
"find_package(Threads REQUIRED)\n"
"include(\"\${CMAKE_CURRENT_LIST_DIR}/async_simple-targets.cmake\")\n"
)

# Install the generated config file
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/async_simple-config.cmake"
DESTINATION share/async_simple)


if (${ASYNC_SIMPLE_ENABLE_TESTS})
add_subdirectory(test)
add_subdirectory(util/test)
Expand All @@ -66,6 +81,3 @@ if (${ASYNC_SIMPLE_ENABLE_TESTS})
add_subdirectory(uthread/test)
endif()
endif()
if (NOT TARGET async_simple::async_simple_header_only)
add_library(async_simple::async_simple_header_only ALIAS libasync_simple)
endif ()

0 comments on commit e1fdcf1

Please sign in to comment.