版本适配:gRPC=v1.21.0, Protobuf=v3.5.0
交叉编译之前,请先编译安装本地版本的gRPC和Protobuf,注意版本适配。
source Yocto 的环境变量 environment-setup-cortexa9hf-vfp-neon-poky-linux-gnueabi
cd third_party/protobuf/
git submodule update --init --recursive #修改本地源方法如前文,更新第三方源码
./autogen.sh
./configure --prefix=/usr --host=arm-poky-linux-gnueabi --with-protoc=/usr/local/bin/protoc
make -j2
make DESTDIR=~/rootfs install
注意:其中DESTDIR指定交叉编译环境的安装目录,而不是本地系统环境。
注意:先修改Makefile文件,后面还需要再修改一次
prefix ?= /usr
export GRPC_CROSS_COMPILE=true
export LDXX=$CXX
export LD=$CXX
export PROTOBUF_CONFIG_OPTS="--host=arm-poky-linux-gnueabi --with-protoc=/usr/local/bin/protoc"
export GRPC_CROSS_LDOPTS=$LDFLAGS
export GRPC_CROSS_AROPTS=rc
export USE_BUILT_PROTOC=true
问题1:src/compiler/ruby_generator.cc和src/cpp/server/channelz/channelz_service.cc依赖了Protobuf 3.6.0的头文件
解决1:替换源文件为旧版本,内容较多放在文章最后面。
问题2:
[GRPC] Generating gRPC's protobuf service CC file from src/proto/grpc/channelz/channelz.proto
/home/help/RPC/grpc/bins/opt/grpc_cpp_plugin: program not found or is not executable
--grpc_out: protoc-gen-grpc: Plugin failed with status code 1.
Makefile:2590: recipe for target '/home/help/RPC/grpc/gens/src/proto/grpc/channelz/channelz.grpc.pb.cc' failed
make: *** [/home/help/RPC/grpc/gens/src/proto/grpc/channelz/channelz.grpc.pb.cc] Error 1
解决2:修改Makefile文件,找到变量PROTOC_PLUGINS_DIR ,覆盖所有变量赋值:PROTOC_PLUGINS_DIR = /usr/local/bin
问题3:如何交叉编译的安装?
解决3:修改Makefile文件,设置变量为: prefix ?= ~/rootfs
以上,已经安装完成。
附件:
src/compiler/ruby_generator.cc
/*
*
* Copyright 2015 gRPC authors.
*
* 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
#include
src/cpp/server/channelz/channelz_service.cc
/*
*
* Copyright 2018 gRPC authors.
*
* 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
#include "src/cpp/server/channelz/channelz_service.h"
#include
#include
namespace grpc {
Status ChannelzService::GetTopChannels(
ServerContext* unused, const channelz::v1::GetTopChannelsRequest* request,
channelz::v1::GetTopChannelsResponse* response) {
char* json_str = grpc_channelz_get_top_channels(request->start_channel_id());
if (json_str == nullptr) {
return Status(StatusCode::INTERNAL,
"grpc_channelz_get_top_channels returned null");
}
grpc::protobuf::util::Status s =
grpc::protobuf::json::JsonStringToMessage(json_str, response);
gpr_free(json_str);
if (!s.ok()) {
return Status(StatusCode::INTERNAL, s.ToString());
}
return Status::OK;
}
Status ChannelzService::GetServers(
ServerContext* unused, const channelz::v1::GetServersRequest* request,
channelz::v1::GetServersResponse* response) {
char* json_str = grpc_channelz_get_servers(request->start_server_id());
if (json_str == nullptr) {
return Status(StatusCode::INTERNAL,
"grpc_channelz_get_servers returned null");
}
grpc::protobuf::util::Status s =
grpc::protobuf::json::JsonStringToMessage(json_str, response);
gpr_free(json_str);
if (!s.ok()) {
return Status(StatusCode::INTERNAL, s.ToString());
}
return Status::OK;
}
Status ChannelzService::GetServer(ServerContext* unused,
const channelz::v1::GetServerRequest* request,
channelz::v1::GetServerResponse* response) {
char* json_str = grpc_channelz_get_server(request->server_id());
if (json_str == nullptr) {
return Status(StatusCode::INTERNAL,
"grpc_channelz_get_server returned null");
}
grpc::protobuf::util::Status s =
grpc::protobuf::json::JsonStringToMessage(json_str, response);
gpr_free(json_str);
if (!s.ok()) {
return Status(StatusCode::INTERNAL, s.ToString());
}
return Status::OK;
}
Status ChannelzService::GetServerSockets(
ServerContext* unused, const channelz::v1::GetServerSocketsRequest* request,
channelz::v1::GetServerSocketsResponse* response) {
char* json_str = grpc_channelz_get_server_sockets(
request->server_id(), request->start_socket_id(), request->max_results());
if (json_str == nullptr) {
return Status(StatusCode::INTERNAL,
"grpc_channelz_get_server_sockets returned null");
}
grpc::protobuf::util::Status s =
grpc::protobuf::json::JsonStringToMessage(json_str, response);
gpr_free(json_str);
if (!s.ok()) {
return Status(StatusCode::INTERNAL, s.ToString());
}
return Status::OK;
}
Status ChannelzService::GetChannel(
ServerContext* unused, const channelz::v1::GetChannelRequest* request,
channelz::v1::GetChannelResponse* response) {
char* json_str = grpc_channelz_get_channel(request->channel_id());
if (json_str == nullptr) {
return Status(StatusCode::NOT_FOUND, "No object found for that ChannelId");
}
grpc::protobuf::util::Status s =
grpc::protobuf::json::JsonStringToMessage(json_str, response);
gpr_free(json_str);
if (!s.ok()) {
return Status(StatusCode::INTERNAL, s.ToString());
}
return Status::OK;
}
Status ChannelzService::GetSubchannel(
ServerContext* unused, const channelz::v1::GetSubchannelRequest* request,
channelz::v1::GetSubchannelResponse* response) {
char* json_str = grpc_channelz_get_subchannel(request->subchannel_id());
if (json_str == nullptr) {
return Status(StatusCode::NOT_FOUND,
"No object found for that SubchannelId");
}
grpc::protobuf::util::Status s =
grpc::protobuf::json::JsonStringToMessage(json_str, response);
gpr_free(json_str);
if (!s.ok()) {
return Status(StatusCode::INTERNAL, s.ToString());
}
return Status::OK;
}
Status ChannelzService::GetSocket(ServerContext* unused,
const channelz::v1::GetSocketRequest* request,
channelz::v1::GetSocketResponse* response) {
char* json_str = grpc_channelz_get_socket(request->socket_id());
if (json_str == nullptr) {
return Status(StatusCode::NOT_FOUND, "No object found for that SocketId");
}
grpc::protobuf::util::Status s =
grpc::protobuf::json::JsonStringToMessage(json_str, response);
gpr_free(json_str);
if (!s.ok()) {
return Status(StatusCode::INTERNAL, s.ToString());
}
return Status::OK;
}
} // namespace grpc