一、版本要求
團(tuán)隊(duì)內(nèi)部對(duì)齊版本號(hào),protoc 3.17.3/protoc-gen-swift 1.17.0。
1.1 查看版本號(hào)
? ~ protoc --version
libprotoc 3.17.3
? ~ protoc-gen-swift --version
protoc-gen-swift 1.17.0
注意:安裝后和執(zhí)行前可以查看是否為對(duì)應(yīng)的版本號(hào),防止版本號(hào)不同導(dǎo)致生成的內(nèi)容不同。如果版本號(hào)不同式散,可以使用 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文件安裝
介紹如何通過(guò)查找并下載pb文件來(lái)安裝。下面以安裝 protoc 為例瘸洛。
2.1.1下載pb文件
使用 brew install protobuf / brew install swift-protobuf 會(huì)安裝最新版本揍移;
使用 brew install protobuf@3.17.3 / brew install swift-protobuf@1.17.0 會(huì)找不到對(duì)應(yīng)的版本。
因?yàn)閷?duì)應(yīng)的版本已經(jīng)不在 Formulae 了反肋。
Formulae 即 homebrew 的核心庫(kù)那伐;Casks 即 homebrew 的補(bǔ)充庫(kù)。
? ~ brew search protobuf
==> Formulae
protobuf protobuf@21 protobuf@3.6
protobuf-c protobuf@3 swift-protobuf
==> Casks
protopie
只能通過(guò)尋找pb文件來(lái)安裝石蔗。
● 執(zhí)行 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)
● 瀏覽器打開(kāi)上面查詢到的鏈接:
https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/protobuf.rb
https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/swift-protobuf.rb
● 查找對(duì)應(yīng)版本的歷史 commit
點(diǎn)擊右側(cè)的 History罕邀,查找到如下 commit 信息
protobuf: update 3.17.3 bottle.
@cho-m
@BrewTestBot
cho-m authored and BrewTestBot committed on Jul 6, 2021
● 下載文件
點(diǎn)擊 commit,進(jìn)入后右側(cè)三點(diǎn)選擇View file养距,然后下個(gè)頁(yè)面點(diǎn)擊右側(cè)的下載按鈕下載诉探。保存命名如 protobuf.pb。
對(duì)應(yīng)的文件鏈接:
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
如果遇到提示已經(jīng)安裝了其他版本棍厌,則先卸載肾胯,或者 unlink竖席。
? ~ brew uninstall protobuf
or
? ~ brew unlink protobuf
安裝完就可以編譯編寫(xiě)的文件了。
注意:安裝后和執(zhí)行前可以查看是否為對(duì)應(yīng)的版本號(hào)敬肚,防止版本號(hào)不同導(dǎo)致生成的內(nèi)容不同毕荐。如果版本號(hào)不同,可以使用 which 命令查看工具所在位置艳馒,以確定是否安裝正常憎亚。
2.2 源碼編譯
介紹如何通過(guò)源碼安裝。
由于 protobuf 早期的版本是沒(méi)有適配 m1 芯片的弄慰,所以 m1 芯片的話需要特殊處理下文件第美。
2.2.1 安裝 protoc
● 在 gifthub 上找到對(duì)應(yīng)的版本鏈接:
https://github.com/protocolbuffers/protobuf/releases/tag/v3.17.3
● 下載對(duì)應(yīng)的文件:
protobuf-all-3.17.3.tar.gz
壓縮包中包含了其他平臺(tái)的,如 java陆爽,項(xiàng)目需要用來(lái)生成對(duì)應(yīng)的 .java 文件斋日。
● 執(zhí)行下面的操作安裝
./configure
make
make check
make install
2.2.1.1 M1 芯片額外處理
打開(kāi) protobuf-3.17.3/src/google/protobuf/stubs/platform_macros.h,找到下面的內(nèi)容:
#else
GOOGLE_PROTOBUF_PLATFORM_ERROR
在 #else 上面添加以下代碼:
#elif defined(__arm64__)
#define GOOGLE_PROTOBUF_ARCH_ARM 1
#define GOOGLE_PROTOBUF_ARCH_64_BIT 1
然后再執(zhí)行安裝的操作墓陈。
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 中第献,或者直接復(fù)制到 protoc 的目錄下贡必,一般是 /usr/local/bin。
三庸毫、編譯文件
在統(tǒng)一的腳本目錄下仔拟,有簡(jiǎn)單的處理文件 playmate/scripts/pb.sh,右鍵執(zhí)行即可飒赃。
具體的文件路徑參考項(xiàng)目架構(gòu)利花。
注意:如果通過(guò)多種方式安裝了,要確認(rèn)好執(zhí)行時(shí)的版本號(hào)和安裝位置是否符合預(yù)期载佳。
四炒事、參考文檔
http://www.reibang.com/p/78a0ed238e14
https://github.com/protocolbuffers/protobuf/issues/8836