xref: /aosp_15_r20/external/grpc-grpc/examples/cpp/otel/CMakeLists.txt (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1# Copyright 2018 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15# cmake build file for C++ gRPC OpenTelemetry example.
16# Assumes absl, protobuf, prometheus-cpp, opentelemetry-cpp and gRPC (with -DgRPC_BUILD_OPENTELEMETRY_PLUGIN=ON) have been installed using cmake.
17# See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
18# that automatically builds all the dependencies before building helloworld.
19
20cmake_minimum_required(VERSION 3.13)
21
22project(grpc_opentelemetry_example C CXX)
23
24include(../cmake/common.cmake)
25
26# Find prometheus-cpp package
27find_package(prometheus-cpp CONFIG REQUIRED)
28
29# Find opentelemetry-cpp package
30find_package(opentelemetry-cpp CONFIG REQUIRED)
31
32# Proto file
33get_filename_component(hw_proto "../../protos/helloworld.proto" ABSOLUTE)
34get_filename_component(hw_proto_path "${hw_proto}" PATH)
35
36# Generated sources
37set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc")
38set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h")
39set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc")
40set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h")
41add_custom_command(
42      OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
43      COMMAND ${_PROTOBUF_PROTOC}
44      ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
45        --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
46        -I "${hw_proto_path}"
47        --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
48        "${hw_proto}"
49      DEPENDS "${hw_proto}")
50
51# Include generated *.pb.h files
52include_directories("${CMAKE_CURRENT_BINARY_DIR}")
53include_directories("${CMAKE_SOURCE_DIR}")
54
55# hw_grpc_proto
56add_library(hw_grpc_proto
57  ${hw_grpc_srcs}
58  ${hw_grpc_hdrs}
59  ${hw_proto_srcs}
60  ${hw_proto_hdrs})
61target_link_libraries(hw_grpc_proto
62  ${_REFLECTION}
63  ${_GRPC_GRPCPP}
64  ${_PROTOBUF_LIBPROTOBUF})
65
66# util
67add_library(util
68  "util.cc")
69target_link_libraries(util
70  hw_grpc_proto
71  opentelemetry-cpp::metrics
72  ${_GRPC_GRPCPP}
73  ${_REFLECTION}
74  ${_PROTOBUF_LIBPROTOBUF})
75
76# Targets greeter_callback_(client|server)
77foreach(_target
78  greeter_callback_client greeter_callback_server)
79  add_executable(${_target} "${_target}.cc")
80  target_link_libraries(${_target}
81    absl::flags
82    absl::flags_parse
83    opentelemetry-cpp::metrics
84    opentelemetry-cpp::prometheus_exporter
85    gRPC::grpcpp_otel_plugin
86    util)
87endforeach()
88