xref: /aosp_15_r20/external/grpc-grpc/examples/cpp/helloworld/Makefile (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1#
2# Copyright 2015 gRPC authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17# TODO(jtattermusch): Remove the hack to workaround protobuf bug. See https://github.com/protocolbuffers/protobuf/issues/12439
18# Hack: protobuf currently doesn't declare it's absl dependencies when protobuf.pc pkgconfig file is used.
19PROTOBUF_ABSL_DEPS = absl_absl_check absl_absl_log absl_algorithm absl_base absl_bind_front absl_bits absl_btree absl_cleanup absl_cord absl_core_headers absl_debugging absl_die_if_null absl_dynamic_annotations absl_flags absl_flat_hash_map absl_flat_hash_set absl_function_ref absl_hash absl_layout absl_log_initialize absl_log_severity absl_memory absl_node_hash_map absl_node_hash_set absl_optional absl_span absl_status absl_statusor absl_strings absl_synchronization absl_time absl_type_traits absl_utility absl_variant
20# TODO(jtattermusch): Remove the hack to workaround protobuf/utf8_range bug. See https://github.com/protocolbuffers/utf8_range/issues/20
21# Hack: utf8_range (which is protobuf's dependency) currently doesn't have a pkgconfig file, so we need to explicitly
22# tweak the list of libraries to link against to fix the build.
23PROTOBUF_UTF8_RANGE_LINK_LIBS = -lutf8_validity
24
25HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
26SYSTEM ?= $(HOST_SYSTEM)
27CXX = g++
28CPPFLAGS += `pkg-config --cflags protobuf grpc absl_flags absl_flags_parse`
29CXXFLAGS += -std=c++14
30ifeq ($(SYSTEM),Darwin)
31LDFLAGS += -L/usr/local/lib `pkg-config --libs --static protobuf grpc++ absl_flags absl_flags_parse $(PROTOBUF_ABSL_DEPS)`\
32           $(PROTOBUF_UTF8_RANGE_LINK_LIBS) \
33           -pthread\
34           -lgrpc++_reflection\
35           -ldl
36else
37LDFLAGS += -L/usr/local/lib `pkg-config --libs --static protobuf grpc++ absl_flags absl_flags_parse $(PROTOBUF_ABSL_DEPS)`\
38           $(PROTOBUF_UTF8_RANGE_LINK_LIBS) \
39           -pthread\
40           -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
41           -ldl
42endif
43PROTOC = protoc
44GRPC_CPP_PLUGIN = grpc_cpp_plugin
45GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
46
47PROTOS_PATH = ../../protos
48
49vpath %.proto $(PROTOS_PATH)
50
51all: system-check greeter_client greeter_server greeter_async_client greeter_async_client2 greeter_async_server greeter_callback_client greeter_callback_server
52
53greeter_client: helloworld.pb.o helloworld.grpc.pb.o greeter_client.o
54	$(CXX) $^ $(LDFLAGS) -o $@
55
56greeter_server: helloworld.pb.o helloworld.grpc.pb.o greeter_server.o
57	$(CXX) $^ $(LDFLAGS) -o $@
58
59greeter_async_client: helloworld.pb.o helloworld.grpc.pb.o greeter_async_client.o
60	$(CXX) $^ $(LDFLAGS) -o $@
61
62greeter_async_client2: helloworld.pb.o helloworld.grpc.pb.o greeter_async_client2.o
63	$(CXX) $^ $(LDFLAGS) -o $@
64
65greeter_async_server: helloworld.pb.o helloworld.grpc.pb.o greeter_async_server.o
66	$(CXX) $^ $(LDFLAGS) -o $@
67
68greeter_callback_client: helloworld.pb.o helloworld.grpc.pb.o greeter_callback_client.o
69	$(CXX) $^ $(LDFLAGS) -o $@
70
71greeter_callback_server: helloworld.pb.o helloworld.grpc.pb.o greeter_callback_server.o
72	$(CXX) $^ $(LDFLAGS) -o $@
73
74.PRECIOUS: %.grpc.pb.cc
75%.grpc.pb.cc: %.proto
76	$(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
77
78.PRECIOUS: %.pb.cc
79%.pb.cc: %.proto
80	$(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
81
82clean:
83	rm -f *.o *.pb.cc *.pb.h greeter_client greeter_server greeter_async_client greeter_async_client2 greeter_async_server greeter_callback_client greeter_callback_server
84
85
86# The following is to test your system and ensure a smoother experience.
87# They are by no means necessary to actually compile a grpc-enabled software.
88
89PROTOC_CMD = which $(PROTOC)
90PROTOC_CHECK_CMD = $(PROTOC) --version | grep -q 'libprotoc.3\|libprotoc [0-9][0-9]\.'
91PLUGIN_CHECK_CMD = which $(GRPC_CPP_PLUGIN)
92HAS_PROTOC = $(shell $(PROTOC_CMD) > /dev/null && echo true || echo false)
93ifeq ($(HAS_PROTOC),true)
94HAS_VALID_PROTOC = $(shell $(PROTOC_CHECK_CMD) 2> /dev/null && echo true || echo false)
95endif
96HAS_PLUGIN = $(shell $(PLUGIN_CHECK_CMD) > /dev/null && echo true || echo false)
97
98SYSTEM_OK = false
99ifeq ($(HAS_VALID_PROTOC),true)
100ifeq ($(HAS_PLUGIN),true)
101SYSTEM_OK = true
102endif
103endif
104
105system-check:
106ifneq ($(HAS_VALID_PROTOC),true)
107	@echo " DEPENDENCY ERROR"
108	@echo
109	@echo "You don't have protoc 3.0.0 or newer installed in your path."
110	@echo "Please install an up-to-date version of Google protocol buffers."
111	@echo "You can find it here:"
112	@echo
113	@echo "   https://github.com/protocolbuffers/protobuf/releases"
114	@echo
115	@echo "Here is what I get when trying to evaluate your version of protoc:"
116	@echo
117	-$(PROTOC) --version
118	@echo
119	@echo
120endif
121ifneq ($(HAS_PLUGIN),true)
122	@echo " DEPENDENCY ERROR"
123	@echo
124	@echo "You don't have the grpc c++ protobuf plugin installed in your path."
125	@echo "Please install grpc. You can find it here:"
126	@echo
127	@echo "   https://github.com/grpc/grpc"
128	@echo
129	@echo "Here is what I get when trying to detect if you have the plugin:"
130	@echo
131	-which $(GRPC_CPP_PLUGIN)
132	@echo
133	@echo
134endif
135ifneq ($(SYSTEM_OK),true)
136	@false
137endif
138