Skip to content

Commit

Permalink
simplify test package
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceIm committed Jan 23, 2022
1 parent 29f366c commit 800802c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
49 changes: 19 additions & 30 deletions recipes/grpc/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,26 @@ message(STATUS "Using protobuf ${protobuf_VERSION}")
find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")

# Proto file
get_filename_component(hw_proto "helloworld.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)

# Generated sources
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h")
add_custom_command(
OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
COMMAND $<TARGET_FILE:protobuf::protoc>
ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
--cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
-I "${hw_proto_path}"
--plugin=protoc-gen-grpc="$<TARGET_FILE:gRPC::grpc_cpp_plugin>"
"${hw_proto}"
DEPENDS "${hw_proto}"
)

# Build generated sources as an object library
add_library(hw_proto_obj OBJECT ${hw_proto_srcs} ${hw_grpc_srcs})
target_include_directories(hw_proto_obj PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(hw_proto_obj PRIVATE gRPC::grpc protobuf::libprotobuf)
target_compile_features(hw_proto_obj PUBLIC cxx_std_11)

# Targets greeter_[async_](client)
add_executable(greeter_client_server greeter_client_server.cc)
target_link_libraries(greeter_client_server PRIVATE hw_proto_obj)
add_executable(${PROJECT_NAME} test_package.cpp helloworld.proto)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_BINARY_DIR})
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11)
target_link_libraries(${PROJECT_NAME} PRIVATE protobuf::libprotobuf)
if(NOT SECURE)
target_link_libraries(greeter_client_server PRIVATE gRPC::grpc++_unsecure)
target_link_libraries(${PROJECT_NAME} PRIVATE gRPC::grpc++_unsecure)
else()
target_link_libraries(greeter_client_server PRIVATE gRPC::grpc++)
target_link_libraries(${PROJECT_NAME} PRIVATE gRPC::grpc++)
endif()

protobuf_generate(TARGET ${PROJECT_NAME} LANGUAGE cpp)
protobuf_generate(
TARGET
${PROJECT_NAME}
LANGUAGE
grpc
GENERATE_EXTENSIONS
.grpc.pb.h
.grpc.pb.cc
PLUGIN
protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>
)
6 changes: 3 additions & 3 deletions recipes/grpc/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class TestPackageConan(ConanFile):
generators = "cmake", "cmake_find_package_multi"

def build_requirements(self):
if hasattr(self, "settings_build") and tools.cross_building(self):
self.build_requires(str(self.requires['grpc']))
if hasattr(self, "settings_build"):
self.build_requires(str(self.requires["grpc"]))

def build(self):
cmake = CMake(self)
Expand All @@ -18,5 +18,5 @@ def build(self):

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "greeter_client_server")
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)

0 comments on commit 800802c

Please sign in to comment.