Protobuf 环境配置和安装——通过pb文件和源码安装指定版

2023-07-21  本文已影响0人  zackzheng

一、版本要求

团队内部对齐版本号,protoc 3.17.3/protoc-gen-swift 1.17.0。

1.1 查看版本号

➜  ~ protoc --version         
libprotoc 3.17.3
➜  ~ protoc-gen-swift --version
protoc-gen-swift 1.17.0

注意:安装后和执行前可以查看是否为对应的版本号,防止版本号不同导致生成的内容不同。如果版本号不同,可以使用 which 命令查看工具所在位置,以确定是否安装正常。

1.2 查看工具位置

➜  ~ which protoc
/usr/local/bin/protoc
➜  ~ which protoc-gen-swift
/usr/local/bin/protoc-gen-swift

二、安装 protoc 和 protoc-gen-swift

两种方式安装:
● pb文件安装(homebrew)
● 源码编译

2.1 pb文件安装

介绍如何通过查找并下载pb文件来安装。下面以安装 protoc 为例。

2.1.1下载pb文件

使用 brew install protobuf / brew install swift-protobuf 会安装最新版本;
使用 brew install protobuf@3.17.3 / brew install swift-protobuf@1.17.0 会找不到对应的版本。
因为对应的版本已经不在 Formulae 了。
Formulae 即 homebrew 的核心库;Casks 即 homebrew 的补充库。

➜  ~ brew search protobuf
==> Formulae
protobuf                   protobuf@21                protobuf@3.6
protobuf-c                 protobuf@3                 swift-protobuf

==> Casks
protopie

只能通过寻找pb文件来安装。
● 执行 brew info 命令:

➜  ~ brew info protobuf
==> protobuf: stable 23.3 (bottled), HEAD
Protocol buffers (Google's data interchange format)
https://protobuf.dev/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/protobuf.rb
License: BSD-3-Clause
==> Dependencies
Build: cmake ✘, python@3.10 ✘, python@3.11 ✘
Required: abseil ✔
==> Options
--HEAD
    Install HEAD version
==> Analytics
install: 93,709 (30 days), 135,120 (90 days), 135,140 (365 days)
install-on-request: 23,273 (30 days), 23,273 (90 days), 23,273 (365 days)
build-error: 82 (30 days)
➜  ~ brew info swift-protobuf
==> swift-protobuf: stable 1.22.0 (bottled), HEAD
Plugin and runtime library for using protobuf with Swift
https://github.com/apple/swift-protobuf
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/swift-protobuf.rb
License: Apache-2.0
==> Dependencies
Required: protobuf ✘
==> Requirements
Build: Xcode >= 8.3 (on macOS) ✔
==> Options
--HEAD
    Install HEAD version
==> Analytics
install: 576 (30 days), 576 (90 days), 576 (365 days)
install-on-request: 576 (30 days), 576 (90 days), 576 (365 days)
build-error: 0 (30 days)

● 浏览器打开上面查询到的链接:
https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/protobuf.rb
https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/swift-protobuf.rb
● 查找对应版本的历史 commit
点击右侧的 History,查找到如下 commit 信息

protobuf: update 3.17.3 bottle.
@cho-m
@BrewTestBot
cho-m authored and BrewTestBot committed on Jul 6, 2021

● 下载文件
点击 commit,进入后右侧三点选择View file,然后下个页面点击右侧的下载按钮下载。保存命名如 protobuf.pb。
对应的文件链接:
https://github.com/Homebrew/homebrew-core/blob/ac2a2846981f6c6f9d6df64b9250a1e9f872cbf2/Formula/protobuf.rb
https://github.com/Homebrew/homebrew-core/blob/ac2a2846981f6c6f9d6df64b9250a1e9f872cbf2/Formula/protobuf.rb

2.1.2 安装 pb 文件

➜  ~ brew install ./protobuf.rb

如果遇到提示已经安装了其他版本,则先卸载,或者 unlink。

➜  ~ brew uninstall protobuf
or
➜  ~ brew unlink protobuf

安装完就可以编译编写的文件了。

注意:安装后和执行前可以查看是否为对应的版本号,防止版本号不同导致生成的内容不同。如果版本号不同,可以使用 which 命令查看工具所在位置,以确定是否安装正常。

2.2 源码编译

介绍如何通过源码安装。
由于 protobuf 早期的版本是没有适配 m1 芯片的,所以 m1 芯片的话需要特殊处理下文件。

2.2.1 安装 protoc

● 在 gifthub 上找到对应的版本链接:
https://github.com/protocolbuffers/protobuf/releases/tag/v3.17.3
● 下载对应的文件:
protobuf-all-3.17.3.tar.gz
压缩包中包含了其他平台的,如 java,项目需要用来生成对应的 .java 文件。
● 执行下面的操作安装

./configure
make
make check
make install

2.2.1.1 M1 芯片额外处理

打开 protobuf-3.17.3/src/google/protobuf/stubs/platform_macros.h,找到下面的内容:

#else
GOOGLE_PROTOBUF_PLATFORM_ERROR

在 #else 上面添加以下代码:

#elif defined(__arm64__)
#define GOOGLE_PROTOBUF_ARCH_ARM 1
#define GOOGLE_PROTOBUF_ARCH_64_BIT 1

然后再执行安装的操作。

2.2.2 安装 protoc-gen-swift

https://github.com/apple/swift-protobuf
链接中的 README.md 介绍了如何安装,摘取如下:

$ git clone https://github.com/apple/swift-protobuf.git
$ cd swift-protobuf
$ git tag -l
$ git checkout tags/1.17.0
$ swift build -c release

将 .build/release 中的 protoc-gen-swift 路径添加到 PATH 中,或者直接复制到 protoc 的目录下,一般是 /usr/local/bin。

三、编译文件

在统一的脚本目录下,有简单的处理文件 playmate/scripts/pb.sh,右键执行即可。
具体的文件路径参考项目架构。
注意:如果通过多种方式安装了,要确认好执行时的版本号和安装位置是否符合预期。

四、参考文档

https://www.jianshu.com/p/78a0ed238e14
https://github.com/protocolbuffers/protobuf/issues/8836

上一篇下一篇

猜你喜欢

热点阅读