MacOS Ventura 编译 protobuf-3.21.12©🚩🌱

macOS中尝试微信OCR划词功能,需要自编译protobuf-3.21.12开发包的Universal版本。这个过程颇费了点儿力气,划词终究没有成功,但欢喜的是有此成果留存了下来。

cflags="-Wno-unused-local-typedef -Wno-unused-function -DNDEBUG -g -O3 -pipe -fPIC -fcxx-exceptions"
cxxflags="$cflags -std=c++11 -stdlib=libc++"
isysroot=`xcrun --sdk macosx --show-sdk-path`

# darwin=darwin`uname -r`

rm -rf protobuf-3.21.12
tar -xf protobuf-cpp-3.21.12.tar.gz
cd protobuf-3.21.12

./autogen.sh

build_dir=`pwd`

./configure \
--prefix=$build_dir/../protoc-3.21.12-macOS-x86_64 \
--build=x86_64-apple-macos \
--host=x86_64-apple-macos \
--enable-shared=no \
"CC=clang" \
"CFLAGS=$cflags -arch x86_64 -isysroot $isysroot" \
"CXX=clang" \
"CXXFLAGS=$cxxflags -arch x86_64 -isysroot $isysroot" \
"LDFLAGS=-arch x86_64 -stdlib=libc++" \
"LIBS=-lc++ -lc++abi" && make -j4 && make install


cd ..

rm -rf protobuf-3.21.12
tar -xf protobuf-cpp-3.21.12.tar.gz
cd protobuf-3.21.12

./autogen.sh

build_dir=`pwd`

./configure \
--prefix=$build_dir/../protoc-3.21.12-macOS-arm64 \
--build=x86_64-apple-macos \
--host=arm64-apple-macos \
--target=arm64-apple-macos \
--enable-shared=no \
"CC=clang" \
"CFLAGS=$cflags -arch arm64 -isysroot $isysroot" \
"CXX=clang" \
"CXXFLAGS=$cxxflags -arch arm64 -isysroot $isysroot" \
"LDFLAGS=-arch arm64 -stdlib=libc++" \
"LIBS=-lc++ -lc++abi" && make -j4 && make install


cd ..
rm -rf protobuf-3.21.12

cur_dir=`pwd`

mkdir -p $cur_dir/protoc-3.21.12-macOS-Universal/lib
mkdir -p $cur_dir/protoc-3.21.12-macOS-Universal/bin

lipo \
$cur_dir/protoc-3.21.12-macOS-x86_64/lib/libprotobuf-lite.a \
$cur_dir/protoc-3.21.12-macOS-arm64/lib/libprotobuf-lite.a \
-create -output $cur_dir/protoc-3.21.12-macOS-Universal/lib/libprotobuf-lite-universal.a

# lipo \
# $cur_dir/protoc-3.21.12-macOS-x86_64/lib/libprotobuf-lite.dylib \
# $cur_dir/protoc-3.21.12-macOS-arm64/lib/libprotobuf-lite.dylib \
# -create -output $cur_dir/protoc-3.21.12-macOS-Universal/lib/libprotobuf-lite.dylib

lipo \
$cur_dir/protoc-3.21.12-macOS-x86_64/lib/libprotobuf.a \
$cur_dir/protoc-3.21.12-macOS-arm64/lib/libprotobuf.a \
-create -output $cur_dir/protoc-3.21.12-macOS-Universal/lib/libprotobuf-universal.a

# lipo \
# $cur_dir/protoc-3.21.12-macOS-x86_64/lib/libprotobuf.dylib \
# $cur_dir/protoc-3.21.12-macOS-arm64/lib/libprotobuf.dylib \
# -create -output $cur_dir/protoc-3.21.12-macOS-Universal/lib/libprotobuf.dylib

lipo \
$cur_dir/protoc-3.21.12-macOS-x86_64/bin/protoc \
$cur_dir/protoc-3.21.12-macOS-arm64/bin/arm64-apple-macos-protoc \
-create -output $cur_dir/protoc-3.21.12-macOS-Universal/bin/protoc

cp -R $cur_dir/protoc-3.21.12-macOS-x86_64/include $cur_dir/protoc-3.21.12-macOS-Universal

rm -rf $cur_dir/protoc-3.21.12-macOS-x86_64 $cur_dir/protoc-3.21.12-macOS-arm64