Skip to content

Commit

Permalink
feat: Disable CMake auto find_package+linkage with backend libraries.…
Browse files Browse the repository at this point in the history
… Only link with gRPC::grpc++ by default now (instead of gRPC::grpc++_unsecure)
  • Loading branch information
Tradias committed Dec 26, 2023
1 parent b2d997f commit eb104bf
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 137 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ on:
- 'CMakePresets.json'

env:
VCPKG_VERSION: '0e47c1985273129e4d0ee52ff73bed9125555de8' # Jul 02, 2023
CMAKE_ARGS: '-DCMAKE_BUILD_TYPE=Release -DVCPKG_MANIFEST_INSTALL=off -DASIO_GRPC_ENABLE_PKGCONFIG_FALLBACK=off -DCMAKE_COMPILE_WARNING_AS_ERROR=on'
VCPKG_VERSION: '0e47c1985273129e4d0ee52ff73bed9125555de8'
CMAKE_ARGS: '-DCMAKE_BUILD_TYPE=Release -DVCPKG_MANIFEST_INSTALL=off -DASIO_GRPC_ENABLE_PKGCONFIG_FALLBACK=off -DCMAKE_COMPILE_WARNING_AS_ERROR=on -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=on'
CTEST_ARGS: '-T test --output-on-failure --timeout 180 --no-tests=error --parallel 25'

jobs:
Expand Down
157 changes: 82 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,67 @@ The library can be added to a CMake project using either `add_subdirectory` or `
#include <agrpc/asio_grpc.hpp>
```

<details><summary><b>As a CMake subdirectory</b></summary>
<details><summary><b>Using vcpkg</b></summary>
<p>

Clone the repository into a subdirectory of your CMake project. Then add it and link it to your target.

Using [Boost.Asio](https://www.boost.org/doc/libs/1_84_0/doc/html/boost_asio.html):

```cmake
add_subdirectory(/path/to/asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc)
Add [asio-grpc](https://github.com/microsoft/vcpkg/blob/master/ports/asio-grpc/vcpkg.json) to the dependencies inside your `vcpkg.json`:

# Also link with the equivalents of gRPC::grpc++ and Boost::headers
```
{
"name": "your_app",
"version": "0.1.0",
"dependencies": [
"asio-grpc",
// To use the Boost.Asio backend add
// "boost-asio",
// To use the standalone Asio backend add
// "asio",
// To use the libunifex backend add
// "libunifex",
// To use the stdexec backend add
// "stdexec"
]
}
```

Or using [standalone Asio](https://github.com/chriskohlhoff/asio):
Locate asio-grpc and link it to your target in your `CMakeLists.txt`:

```cmake
add_subdirectory(/path/to/asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-standalone-asio)
# Also link with the equivalents of gRPC::grpc++ and asio::asio
find_package(asio-grpc)
# Using the Boost.Asio backend
find_package(Boost)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc Boost::headers)
# Or use the standalone Asio backend
find_package(asio)
#target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-standalone-asio asio::asio)
# Or use the libunifex backend
find_package(unifex)
#target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-unifex unifex::unifex)
# Or use the stdexec backend
find_package(stdexec)
#target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-stdexec STDEXEC::stdexec)
```

Or using [libunifex](https://github.com/facebookexperimental/libunifex):
</p>
</details>

```cmake
add_subdirectory(/path/to/asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-unifex)
<details><summary><b>Using Hunter</b></summary>
<p>

# Also link with the equivalents of gRPC::grpc++ and unifex::unifex
```
See asio-grpc's documentation on the Hunter website: [https://hunter.readthedocs.io/en/latest/packages/pkg/asio-grpc.html](https://hunter.readthedocs.io/en/latest/packages/pkg/asio-grpc.html).

Or using [stdexec](https://github.com/NVIDIA/stdexec):
</p>
</details>

```cmake
add_subdirectory(/path/to/asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-stdexec)
<details><summary><b>Using conan</b></summary>
<p>

# Also link with the equivalents of gRPC::grpc++ and STDEXEC::stdexec
The recipe in conan-center is called [asio-grpc](https://conan.io/center/recipes/asio-grpc).
If you are using conan's CMake generator then link with `asio-grpc::asio-grpc` independent of the backend that you choose:

```cmake
find_package(asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc)
```

</p>
Expand All @@ -95,98 +117,83 @@ Using [Boost.Asio](https://www.boost.org/doc/libs/1_84_0/doc/html/boost_asio.htm
```cmake
# Make sure CMAKE_PREFIX_PATH contains /desired/installation/directory
find_package(asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc)
find_package(Boost)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc Boost::headers)
```

Or using [standalone Asio](https://github.com/chriskohlhoff/asio):

```cmake
# Make sure CMAKE_PREFIX_PATH contains /desired/installation/directory
find_package(asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-standalone-asio)
find_package(asio)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-standalone-asio asio::asio)
```

Or using [libunifex](https://github.com/facebookexperimental/libunifex):

```cmake
# Make sure CMAKE_PREFIX_PATH contains /desired/installation/directory
find_package(asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-unifex)
find_package(unifex)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-unifex unifex::unifex)
```

Or using [stdexec](https://github.com/NVIDIA/stdexec):

```cmake
# Make sure CMAKE_PREFIX_PATH contains /desired/installation/directory
find_package(asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-stdexec)
find_package(stdexec)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-stdexec STDEXEC::stdexec)
```

</p>
</details>

<details><summary><b>Using vcpkg</b></summary>
<details><summary><b>As a CMake subdirectory</b></summary>
<p>

Add [asio-grpc](https://github.com/microsoft/vcpkg/blob/master/ports/asio-grpc/vcpkg.json) to the dependencies inside your `vcpkg.json`:

```
{
"name": "your_app",
"version": "0.1.0",
"dependencies": [
"asio-grpc",
// To use the Boost.Asio backend add
// "boost-asio",
// To use the standalone Asio backend add
// "asio",
// To use the libunifex backend add
// "libunifex",
// To use the stdexec backend add
// "stdexec"
]
}
```
Clone the repository into a subdirectory of your CMake project. Then add it and link it to your target.

Locate asio-grpc and link it to your target in your `CMakeLists.txt`:
Independent of the backend you chose, find and link with gRPC:

```cmake
find_package(asio-grpc)
# Using the Boost.Asio backend
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc)
# Or use the standalone Asio backend
#target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-standalone-asio)
# Or use the libunifex backend
#target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-unifex)
# Or use the stdexec backend
#target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-stdexec)
find_package(gRPC)
target_link_libraries(your_app PUBLIC gRPC::grpc++)
```

</p>
</details>

<details><summary><b>Using Hunter</b></summary>
<p>
Using [Boost.Asio](https://www.boost.org/doc/libs/1_84_0/doc/html/boost_asio.html):

See asio-grpc's documentation on the Hunter website: [https://hunter.readthedocs.io/en/latest/packages/pkg/asio-grpc.html](https://hunter.readthedocs.io/en/latest/packages/pkg/asio-grpc.html).
```cmake
add_subdirectory(/path/to/asio-grpc)
find_package(Boost)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc Boost::headers)
```

</p>
</details>
Or using [standalone Asio](https://github.com/chriskohlhoff/asio):

<details><summary><b>Using conan</b></summary>
<p>
```cmake
add_subdirectory(/path/to/asio-grpc)
find_package(asio)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-standalone-asio asio::asio)
```

The recipe in conan-center is called [asio-grpc](https://conan.io/center/recipes/asio-grpc).
If you are using conan's CMake generator then link with `asio-grpc::asio-grpc` independent of the backend that you choose:
Or using [libunifex](https://github.com/facebookexperimental/libunifex):

```cmake
find_package(asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc)
add_subdirectory(/path/to/asio-grpc)
find_package(unifex)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-unifex unifex::unifex)
```

### Available options
Or using [stdexec](https://github.com/NVIDIA/stdexec):

`backend` - One of "boost" for Boost.Asio, "asio" for standalone Asio or "unifex" for libunifex.
```cmake
add_subdirectory(/path/to/asio-grpc)
find_package(stdexec)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-stdexec STDEXEC::stdexec)
```

</p>
</details>
Expand Down
2 changes: 1 addition & 1 deletion cmake/AsioGrpcFindPackages.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if(ASIO_GRPC_ENABLE_CPP20_TESTS_AND_EXAMPLES)
endif()

if(ASIO_GRPC_ENABLE_STDEXEC_TESTS)
find_package(stdexec CONFIG)
find_package(stdexec)
endif()

# Fallback to pkg-config
Expand Down
65 changes: 11 additions & 54 deletions cmake/asio-grpcConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,22 @@ include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
include("${CMAKE_CURRENT_LIST_DIR}/AsioGrpcProtobufGenerator.cmake")

if("${CMAKE_GENERATOR}" STRGREATER_EQUAL "Visual Studio")
target_sources(@PROJECT_NAME@::asio-grpc INTERFACE "${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@.natvis")
target_sources(@PROJECT_NAME@::asio-grpc-standalone-asio
INTERFACE "${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@.natvis")
target_sources(@PROJECT_NAME@::asio-grpc-unifex INTERFACE "${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@.natvis")
target_sources(@PROJECT_NAME@::asio-grpc INTERFACE "${CMAKE_CURRENT_LIST_DIR}/asio-grpc.natvis")
target_sources(@PROJECT_NAME@::asio-grpc-standalone-asio INTERFACE "${CMAKE_CURRENT_LIST_DIR}/asio-grpc.natvis")
target_sources(@PROJECT_NAME@::asio-grpc-unifex INTERFACE "${CMAKE_CURRENT_LIST_DIR}/asio-grpc.natvis")
target_sources(@PROJECT_NAME@::asio-grpc-stdexec INTERFACE "${CMAKE_CURRENT_LIST_DIR}/asio-grpc.natvis")
endif()

if(ASIO_GRPC_DISABLE_AUTOLINK)
return()
endif()

unset(ASIO_GRPC_BOOST_ASIO_LINK_LIBRARIES)
unset(ASIO_GRPC_STANDALONE_ASIO_LINK_LIBRARIES)
unset(ASIO_GRPC_UNIFEX_LINK_LIBRARIES)
include(CMakeFindDependencyMacro)

find_package(Boost QUIET)
if(TARGET "Boost::headers")
list(APPEND ASIO_GRPC_BOOST_ASIO_LINK_LIBRARIES "Boost::headers")
elseif(TARGET "Boost::boost")
list(APPEND ASIO_GRPC_BOOST_ASIO_LINK_LIBRARIES "Boost::boost")
find_dependency(gRPC)
if(TARGET "gRPC::grpc++")
set_target_properties(@PROJECT_NAME@::asio-grpc PROPERTIES INTERFACE_LINK_LIBRARIES "gRPC::grpc++")
set_target_properties(@PROJECT_NAME@::asio-grpc-standalone-asio PROPERTIES INTERFACE_LINK_LIBRARIES "gRPC::grpc++")
set_target_properties(@PROJECT_NAME@::asio-grpc-unifex PROPERTIES INTERFACE_LINK_LIBRARIES "gRPC::grpc++")
set_target_properties(@PROJECT_NAME@::asio-grpc-stdexec PROPERTIES INTERFACE_LINK_LIBRARIES "gRPC::grpc++")
endif()

find_package(gRPC QUIET)
if(TARGET "gRPC::grpc++_unsecure")
list(APPEND ASIO_GRPC_BOOST_ASIO_LINK_LIBRARIES "gRPC::grpc++_unsecure")
list(APPEND ASIO_GRPC_STANDALONE_ASIO_LINK_LIBRARIES "gRPC::grpc++_unsecure")
list(APPEND ASIO_GRPC_UNIFEX_LINK_LIBRARIES "gRPC::grpc++_unsecure")
elseif(TARGET "gRPC::grpc++")
list(APPEND ASIO_GRPC_BOOST_ASIO_LINK_LIBRARIES "gRPC::grpc++")
list(APPEND ASIO_GRPC_STANDALONE_ASIO_LINK_LIBRARIES "gRPC::grpc++")
list(APPEND ASIO_GRPC_UNIFEX_LINK_LIBRARIES "gRPC::grpc++")
endif()

find_package(asio QUIET)
if(TARGET "asio::asio")
list(APPEND ASIO_GRPC_STANDALONE_ASIO_LINK_LIBRARIES "asio::asio")
elseif(TARGET "asio::asio_headeronly")
list(APPEND ASIO_GRPC_STANDALONE_ASIO_LINK_LIBRARIES "asio::asio_headeronly")
endif()

find_package(unifex QUIET)
if(TARGET "unifex::unifex")
list(APPEND ASIO_GRPC_UNIFEX_LINK_LIBRARIES "unifex::unifex")
elseif(TARGET "unofficial::unifex")
list(APPEND ASIO_GRPC_UNIFEX_LINK_LIBRARIES "unofficial::unifex")
endif()

if(DEFINED ASIO_GRPC_BOOST_ASIO_LINK_LIBRARIES)
set_target_properties(@PROJECT_NAME@::asio-grpc PROPERTIES INTERFACE_LINK_LIBRARIES
"${ASIO_GRPC_BOOST_ASIO_LINK_LIBRARIES}")
endif()
if(DEFINED ASIO_GRPC_STANDALONE_ASIO_LINK_LIBRARIES)
set_target_properties(@PROJECT_NAME@::asio-grpc-standalone-asio
PROPERTIES INTERFACE_LINK_LIBRARIES "${ASIO_GRPC_STANDALONE_ASIO_LINK_LIBRARIES}")
endif()
if(DEFINED ASIO_GRPC_UNIFEX_LINK_LIBRARIES)
set_target_properties(@PROJECT_NAME@::asio-grpc-unifex PROPERTIES INTERFACE_LINK_LIBRARIES
"${ASIO_GRPC_UNIFEX_LINK_LIBRARIES}")
endif()

unset(ASIO_GRPC_BOOST_ASIO_LINK_LIBRARIES)
unset(ASIO_GRPC_STANDALONE_ASIO_LINK_LIBRARIES)
unset(ASIO_GRPC_UNIFEX_LINK_LIBRARIES)
4 changes: 2 additions & 2 deletions test/cmake/subdirectory/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ project(asio-grpc-cmake-test-subdirectory LANGUAGES CXX)

add_subdirectory(a-g)

find_package(gRPC)
find_package(asio)
find_package(gRPC REQUIRED)
find_package(asio REQUIRED)

# main
add_executable(main)
Expand Down
11 changes: 8 additions & 3 deletions test/cmake/superbuild/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ find_package(
NO_SYSTEM_ENVIRONMENT_PATH
NO_CMAKE_SYSTEM_PATH)

set(Boost_NO_SYSTEM_PATHS on)
find_package(Boost REQUIRED)

find_package(asio REQUIRED)

# common compile options
add_library(compile-options INTERFACE)

Expand All @@ -42,7 +47,7 @@ endfunction()
# TARGET option
create_object_library(target-option target.cpp)

target_link_libraries(target-option PRIVATE asio-grpc::asio-grpc-standalone-asio)
target_link_libraries(target-option PRIVATE asio-grpc::asio-grpc-standalone-asio asio::asio)

# /* [asio_grpc_protobuf_generate-example] */
asio_grpc_protobuf_generate(
Expand All @@ -66,14 +71,14 @@ asio_grpc_protobuf_generate(

create_object_library(out-var-option out_var.cpp ${OUT_VAR_GENERATED_SOURCES})

target_link_libraries(out-var-option PRIVATE asio-grpc::asio-grpc)
target_link_libraries(out-var-option PRIVATE asio-grpc::asio-grpc Boost::boost)

target_include_directories(out-var-option PRIVATE "${OUT_VAR_GENERATED_PROTOS_OUT_DIR}")

# DESCRIPTOR option
create_object_library(descriptor-option descriptor.cpp)

target_link_libraries(descriptor-option PRIVATE asio-grpc::asio-grpc)
target_link_libraries(descriptor-option PRIVATE asio-grpc::asio-grpc Boost::boost)

set(DESCRIPTOR_GENERATED_PROTOS_OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/descriptor")

Expand Down

0 comments on commit eb104bf

Please sign in to comment.