Skip to content

Commit

Permalink
fix: Honor ASIO_GRPC_USE_BOOST_CONTAINER in add_subdirectory mode. Al…
Browse files Browse the repository at this point in the history
…so add test for add_subdirectory
  • Loading branch information
Tradias committed Dec 8, 2022
1 parent 70ff1cb commit cb28de1
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ ClangBuildAnalyzer.ini

# asio-grpc specific
docs/
test/cmake/superbuild/CMakeLists.txt
test/cmake/superbuild/CMakeLists.txt
test/cmake/subdirectory/CMakeLists.txt
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,31 @@ Clone the repository into a subdirectory of your CMake project. Then add it and
Using [Boost.Asio](https://www.boost.org/doc/libs/1_80_0/doc/html/boost_asio.html):

```cmake
find_package(gRPC)
find_package(Boost)
add_subdirectory(/path/to/repository/root)
target_link_libraries(your_app PUBLIC gRPC::grpc++_unsecure asio-grpc::asio-grpc Boost::headers)
add_subdirectory(/path/to/asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc)
# Also link with the equivalents of gRPC::grpc++_unsecure, Boost::headers and
# Boost::container (if ASIO_GRPC_USE_BOOST_CONTAINER has been set)
```

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

```cmake
find_package(gRPC)
find_package(asio)
add_subdirectory(/path/to/repository/root)
target_link_libraries(your_app PUBLIC gRPC::grpc++_unsecure asio-grpc::asio-grpc-standalone-asio asio::asio)
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++_unsecure, asio::asio and
# Boost::container (if ASIO_GRPC_USE_BOOST_CONTAINER has been set)
```

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

```cmake
find_package(gRPC)
find_package(unifex)
add_subdirectory(/path/to/repository/root)
target_link_libraries(your_app PUBLIC gRPC::grpc++_unsecure asio-grpc::asio-grpc-unifex unifex::unifex)
add_subdirectory(/path/to/asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-unifex)
# Also link with the equivalents of gRPC::grpc++_unsecure, unifex::unifex and
# Boost::container (if ASIO_GRPC_USE_BOOST_CONTAINER has been set)
```

</p>
Expand Down
2 changes: 0 additions & 2 deletions cmake/AsioGrpcCompileOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ target_sources(asio-grpc-compile-options

if(ASIO_GRPC_USE_BOOST_CONTAINER)
target_link_libraries(asio-grpc-compile-options INTERFACE Boost::container)

target_compile_definitions(asio-grpc-compile-options INTERFACE AGRPC_USE_BOOST_CONTAINER)
endif()

# C++20 compile options
Expand Down
24 changes: 15 additions & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,27 @@ function(asio_grpc_create_interface_target _asio_grpc_name)
target_include_directories(${_asio_grpc_name} INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
$<INSTALL_INTERFACE:include>)

if(ASIO_GRPC_USE_BOOST_CONTAINER)
set(ASIO_GRPC_MEMORY_RESOURCE_INDLUCES
[[#include <boost/container/pmr/memory_resource.hpp>
if(ASIO_GRPC_USE_BOOST_CONTAINER AND (NOT ASIO_GRPC_PROJECT_IS_TOP_LEVEL OR ASIO_GRPC_BUILD_TESTS))
target_compile_definitions(${_asio_grpc_name} INTERFACE AGRPC_USE_BOOST_CONTAINER)
endif()

if(ASIO_GRPC_INSTALL)
if(ASIO_GRPC_USE_BOOST_CONTAINER)
set(ASIO_GRPC_MEMORY_RESOURCE_INDLUCES
[[#include <boost/container/pmr/memory_resource.hpp>
#include <boost/container/pmr/polymorphic_allocator.hpp>
#include <boost/container/pmr/unsynchronized_pool_resource.hpp>
#include <boost/container/uses_allocator.hpp>]])
set(ASIO_GRPC_MEMORY_RESOURCE_NAMESPACE_ALIAS [[namespace pmr = boost::container::pmr;
set(ASIO_GRPC_MEMORY_RESOURCE_NAMESPACE_ALIAS [[namespace pmr = boost::container::pmr;
namespace container = boost::container;]])
else()
set(ASIO_GRPC_MEMORY_RESOURCE_INDLUCES "#include <memory_resource>")
set(ASIO_GRPC_MEMORY_RESOURCE_NAMESPACE_ALIAS [[namespace pmr = std::pmr;
else()
set(ASIO_GRPC_MEMORY_RESOURCE_INDLUCES "#include <memory_resource>")
set(ASIO_GRPC_MEMORY_RESOURCE_NAMESPACE_ALIAS [[namespace pmr = std::pmr;
namespace container = std;]])
endif()
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/agrpc/detail/memory_resource.hpp.in"
"${CMAKE_CURRENT_BINARY_DIR}/generated/agrpc/detail/memory_resource.hpp" @ONLY)
endif()
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/agrpc/detail/memory_resource.hpp.in"
"${CMAKE_CURRENT_BINARY_DIR}/generated/agrpc/detail/memory_resource.hpp" @ONLY)
endfunction()

asio_grpc_create_interface_target(asio-grpc)
Expand Down
29 changes: 17 additions & 12 deletions test/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set(ASIO_GRPC_CMAKE_INSTALL_TEST_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/superbuild")

configure_file("${ASIO_GRPC_CMAKE_INSTALL_TEST_SOURCE_DIR}/CMakeLists.txt.in"
"${ASIO_GRPC_CMAKE_INSTALL_TEST_SOURCE_DIR}/CMakeLists.txt" @ONLY)

if("${CMAKE_GENERATOR}" STREQUAL "Ninja Multi-Config")
if(WIN32)
set(ASIO_GRPC_CMAKE_INSTALL_TEST_GENERATOR "NMake Makefiles")
Expand All @@ -27,10 +22,20 @@ else()
set(ASIO_GRPC_CMAKE_INSTALL_TEST_GENERATOR "${CMAKE_GENERATOR}")
endif()

add_test(
NAME asio-grpc-cmake-test
COMMAND
"${ASIO_GRPC_CMAKE_INSTALL_TEST_CTEST_COMMAND}" "--build-and-test" "${ASIO_GRPC_CMAKE_INSTALL_TEST_SOURCE_DIR}"
"${CMAKE_CURRENT_BINARY_DIR}/cmake-test" "--build-generator" "${ASIO_GRPC_CMAKE_INSTALL_TEST_GENERATOR}"
"--build-project" "asio-grpc-cmake-test-superbuild" "--test-command"
"${CMAKE_CURRENT_BINARY_DIR}/cmake-test/test-install/main${CMAKE_EXECUTABLE_SUFFIX}")
function(add_cmake_test _name)
set(ASIO_GRPC_CMAKE_INSTALL_TEST_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${_name}")

configure_file("${ASIO_GRPC_CMAKE_INSTALL_TEST_SOURCE_DIR}/CMakeLists.txt.in"
"${ASIO_GRPC_CMAKE_INSTALL_TEST_SOURCE_DIR}/CMakeLists.txt" @ONLY)

add_test(
NAME asio-grpc-cmake-test-${_name}
COMMAND
"${ASIO_GRPC_CMAKE_INSTALL_TEST_CTEST_COMMAND}" "--build-and-test"
"${ASIO_GRPC_CMAKE_INSTALL_TEST_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/${_name}" "--build-generator"
"${ASIO_GRPC_CMAKE_INSTALL_TEST_GENERATOR}" "--build-project" "asio-grpc-cmake-test-${_name}"
"--test-command" "${CMAKE_CURRENT_BINARY_DIR}/${_name}/test-install/main${CMAKE_EXECUTABLE_SUFFIX}")
endfunction()

add_cmake_test(superbuild)
add_cmake_test(subdirectory)
51 changes: 51 additions & 0 deletions test/cmake/subdirectory/CMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2022 Dennis Hezel
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.14)

project(asio-grpc-cmake-test-subdirectory)

file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/s" "${CMAKE_CURRENT_BINARY_DIR}/t"
"${CMAKE_CURRENT_BINARY_DIR}/test-install")

file(COPY "@ASIO_GRPC_PROJECT_ROOT@/src" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/s/a-g/")
file(COPY "@ASIO_GRPC_PROJECT_ROOT@/cmake" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/s/a-g/")
file(COPY "@ASIO_GRPC_PROJECT_ROOT@/CMakeLists.txt" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/s/a-g/")
file(COPY "${CMAKE_CURRENT_LIST_DIR}/src/." DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/s/")

include(ExternalProject)

externalproject_add(
t
PREFIX t
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/s"
CMAKE_ARGS "-DCMAKE_CXX_COMPILER=@CMAKE_CXX_COMPILER@"
"-DCMAKE_CXX_FLAGS=@CMAKE_CXX_FLAGS@"
"-DCMAKE_EXE_LINKER_FLAGS=@CMAKE_EXE_LINKER_FLAGS@"
"-DCMAKE_BUILD_TYPE=Release"
"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_CURRENT_BINARY_DIR}/test-install"
"-DCMAKE_MSVC_RUNTIME_LIBRARY=@CMAKE_MSVC_RUNTIME_LIBRARY@"
"-DBoost_USE_STATIC_RUNTIME=@ASIO_GRPC_BOOST_USE_STATIC_RUNTIME@"
"-DCMAKE_TOOLCHAIN_FILE=@CMAKE_TOOLCHAIN_FILE@"
"-DVCPKG_TARGET_TRIPLET=@VCPKG_TARGET_TRIPLET@"
"-DVCPKG_HOST_TRIPLET=@VCPKG_HOST_TRIPLET@"
"-DVCPKG_OVERLAY_TRIPLETS=@VCPKG_OVERLAY_TRIPLETS@"
"-DVCPKG_MANIFEST_MODE=@VCPKG_MANIFEST_MODE@"
"-DVCPKG_MANIFEST_INSTALL=@VCPKG_MANIFEST_INSTALL@"
"-DVCPKG_OVERLAY_PORTS=@VCPKG_OVERLAY_PORTS@"
"-DVCPKG_INSTALL_OPTIONS=@VCPKG_INSTALL_OPTIONS@"
"-DVCPKG_INSTALLED_DIR=@VCPKG_INSTALLED_DIR@"
"-DASIO_GRPC_USE_BOOST_CONTAINER=@ASIO_GRPC_USE_BOOST_CONTAINER@"
BUILD_COMMAND "${CMAKE_COMMAND}" "--build" "<BINARY_DIR>" "--config" "Release"
INSTALL_COMMAND "")
40 changes: 40 additions & 0 deletions test/cmake/subdirectory/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2022 Dennis Hezel
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.14)

project(asio-grpc-cmake-test-subdirectory LANGUAGES CXX)

add_subdirectory(a-g)

find_package(gRPC)
find_package(asio)
find_package(Boost COMPONENTS container)

# main
add_executable(main)

target_sources(main PRIVATE main.cpp)

asio_grpc_protobuf_generate(
GENERATE_GRPC
TARGET main
OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/target"
PROTOS "${CMAKE_CURRENT_SOURCE_DIR}/proto/target.proto")

target_compile_definitions(main INTERFACE $<$<CXX_COMPILER_ID:MSVC>:_WIN32_WINNT=0x0A00> # Windows 10
ASIO_NO_DEPRECATED)

target_link_libraries(main PRIVATE asio-grpc::asio-grpc-standalone-asio asio::asio Boost::container
gRPC::grpc++_unsecure)
32 changes: 32 additions & 0 deletions test/cmake/subdirectory/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2022 Dennis Hezel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "target.grpc.pb.h"

#include <agrpc/asio_grpc.hpp>
#include <grpcpp/completion_queue.h>

int main()
{
agrpc::GrpcContext grpc_context{std::make_unique<grpc::CompletionQueue>()};

auto rpc = &target::Test::AsyncService::RequestUnary;

target::Request request;
request.set_integer(42);

target::Request response;

grpc_context.run();
}
29 changes: 29 additions & 0 deletions test/cmake/subdirectory/src/proto/target.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2022 Dennis Hezel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package target;

service Test {
rpc Unary(Request) returns (Response) {}
}

message Request {
int32 integer = 1;
}

message Response {
int32 integer = 1;
}
30 changes: 30 additions & 0 deletions test/cmake/subdirectory/src/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/cafd398be781144787573ca78390e951673c7055/scripts/vcpkg.schema.json",
"name": "asio-grpc-cmake-test",
"version": "1",
"dependencies": [
{
"name": "abseil",
"features": [
"cxx17"
]
},
"doctest",
"grpc",
"boost-coroutine",
"boost-asio",
"boost-interprocess",
"boost-thread",
"boost-container",
"boost-process",
{
"name": "asio",
"features": [
"coroutine"
]
},
"libunifex",
"gtest"
],
"builtin-baseline": "cafd398be781144787573ca78390e951673c7055"
}

0 comments on commit cb28de1

Please sign in to comment.