CMakeLists.txt 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright The OpenTelemetry Authors
  2. # SPDX-License-Identifier: Apache-2.0
  3. # Proto file
  4. get_filename_component(proto_file "./protos/messages.proto" ABSOLUTE)
  5. get_filename_component(proto_file_path "${proto_file}" PATH)
  6. message(STATUS "PATH:${proto_file_path}:${proto_file}")
  7. # Generated sources
  8. set(example_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/messages.pb.cc")
  9. set(example_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/messages.pb.h")
  10. set(example_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/messages.grpc.pb.cc")
  11. set(example_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/messages.grpc.pb.h")
  12. add_custom_command(
  13. OUTPUT "${example_proto_srcs}" "${example_proto_hdrs}" "${example_grpc_srcs}"
  14. "${example_grpc_hdrs}"
  15. COMMAND
  16. ${PROTOBUF_PROTOC_EXECUTABLE} ARGS "--grpc_out=${CMAKE_CURRENT_BINARY_DIR}"
  17. "--cpp_out=${CMAKE_CURRENT_BINARY_DIR}" "--proto_path=${proto_file_path}"
  18. "--plugin=protoc-gen-grpc=${gRPC_CPP_PLUGIN_EXECUTABLE}" "${proto_file}")
  19. add_library(example_grpc_proto ${example_grpc_srcs} ${example_grpc_hdrs}
  20. ${example_proto_srcs} ${example_proto_hdrs})
  21. patch_protobuf_targets(example_grpc_proto)
  22. # Disable include-what-you-use on generated code.
  23. set_target_properties(example_grpc_proto PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "")
  24. target_include_directories(
  25. example_grpc_proto PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>")
  26. if(TARGET protobuf::libprotobuf)
  27. target_link_libraries(example_grpc_proto PUBLIC gRPC::grpc++
  28. protobuf::libprotobuf)
  29. else()
  30. target_include_directories(example_grpc_proto PUBLIC ${Protobuf_INCLUDE_DIRS})
  31. target_link_libraries(example_grpc_proto PUBLIC gRPC::grpc++
  32. ${Protobuf_LIBRARIES})
  33. endif()
  34. foreach(_target client server)
  35. add_executable(${_target} "${_target}.cc")
  36. target_link_libraries(
  37. ${_target} PRIVATE example_grpc_proto
  38. opentelemetry-cpp::ostream_span_exporter)
  39. patch_protobuf_targets(${_target})
  40. endforeach()