1、編譯前的准備工作
1、下載源碼
git clone https://github.com/grpc/grpc.git
2、下載依賴項,grpc的依賴項都以子模塊的方式記錄在third_party
目錄下,所以直接同步子模塊即可。
git submodule update --init
# 可以使用 --recursive 選項遞歸子模塊
文件數比較多,完整下載后打包大小有804MB(PS.都是完整克隆)。
3、安裝go
如果不按照go,則在生成編譯腳本的時候會報以下錯誤:
CMake Error at third_party/boringssl/CMakeLists.txt:38 (message):
Could not find Go
Windows上直接下載安裝包 go1.12.5.windows-amd64 安裝即可。
Linux下也可以直接下載安裝包安裝,或者是使用相應發行版的包管理器安裝。
4、安裝perl
2、Windows下使用VS2019編譯
編譯過程很簡單,就是編譯的東西比較多,速度比較慢。下面記錄下編譯的具體過程。
2.1、使用cmake生成VS2019解決方案
在生成VS2015解決方案前需要先安裝cmake 3.x
工具,已經VS開發環境。我這里是使用VS2019進行的編譯,但也適用於VS2015/VS2017,這是執行cmake
命令行工具的時候指定的參數稍有不同。
2.1.1、生成時使用的基本選項設定
指定編譯平台工具集和編譯配置類型等
# 下面-G/-A 選項僅適用於VS2019,如果是VS2017或2015等,則需要使用-G "Visual Studio 15 2017 Win64"形式
—G "Visual Studio 16 2019" -A x64 \
-DCMAKE_CONFIGURATION_TYPES=Release # 或者 DCMAKE_BUILD_TYPE=Release
指定go.exe路徑
-DGO_EXECUTABLE="C:/Go/bin/go.exe"
指定安裝輸出路徑
-DCMAKE_INSTALL_PREFIX="E:/complier/grpc/install" \
# 下面幾個也可以不指定
-DINSTALL_BIN_DIR="E:/complier/grpc/install/grpc/bin" \
-DINSTALL_MAN_DIR="E:/complier/grpc/install/grpc/share/man" \
-DINSTALL_INC_DIR="E:/complier/grpc/install/grpc/include" \
-DINSTALL_PKGCONFIG_DIR="E:/complier/grpc/install/grpc/share/pkgconfig" \
指定使用靜態運行時庫
-DgRPC_MSVC_STATIC_RUNTIME=1
這里和下面編譯第三方庫時候都編譯使用靜態(MT)運行時的是對應的,如果下面編譯第三方庫都指定動態運行時庫(MD),則無需指定該選項。
2.1.2、僅使用上面選項,執行后報如下ZLIB
警告。
意思就是因為gRPC_ZLIB_PROVIDER
是module
,所以強制設置gRPC_INSTALL
為FALSE
。
CMake Warning at cmake/zlib.cmake:32 (message):
gRPC_INSTALL will be forced to FALSE because gRPC_ZLIB_PROVIDER is "module"
Call Stack (most recent call first):
CMakeLists.txt:140 (include)
解決這個警告需要使用到zlib庫,關於它的編譯這里就不再說了,可以查看 VS編譯 x64版本zlib庫 的內容。
我這里就沒有去編譯它,直接使用了以前vcpkg安裝的。然后指定如下選項繼續。
-DgRPC_ZLIB_PROVIDER=package \
-DZLIB_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/zlibd.lib" \
-DZLIB_INCLUDE_DIR="E:/complier/x64-windows-static/include" \
-DZLIB_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/zlib.lib"
2.1.3、繼續,報如下CARES
警告。這個警告可以不用管,也能正常編譯通過。
CMake Warning at cmake/cares.cmake:33 (message):
gRPC_INSTALL will be forced to FALSE because gRPC_CARES_PROVIDER is
"module"
Call Stack (most recent call first):
CMakeLists.txt:141 (include)
但我這里還是解決它,先下載 c-ares 編譯安裝。
# 下載源碼(在grpc\third_party\cares\cares目錄下也是一樣的,可以不用下載)
git clone https://github.com/c-ares/c-ares.git
# 生成VS工程
cd c-ares
# 注意,下面必須指定CARES_MSVC_STATIC_RUNTIME選項,否則后面編譯grpc時候會通不過(找不到__imp__xxx)
cmake . -G "Visual Studio 16 2019" -A x64 -DCMAKE_INSTALL_PREFIX=E:/complier/x64-windows-static -DCARES_STATIC=1 -DCARES_SHARED=0 -DCARES_MSVC_STATIC_RUNTIME=1
# 編譯
msbuild /maxcpucount:4 /p:Configuration=Release /p:Platform=x64 c-ares.sln
# 安裝,上面生成VS工程時候指定了安裝在當E:/complier/x64-windows-static目錄下
msbuild /maxcpucount:4 /p:Configuration=Release /p:Platform=x64 INSTALL.vcxproj
然后指定下面幾個選項后繼續。
-DgRPC_CARES_PROVIDER=package \
-Dc-ares_DIR="E:/complier/x64-windows-static/lib/cmake/c-ares" # 這里是指定到c-ares-config.cmake文件的路徑
2.1.4、繼續,報如下PROTOBUF
警告
-- 3.8.0.0
CMake Warning (dev) at third_party/protobuf/cmake/install.cmake:60 (message):
The file
"E:/complier/grpc/grpc/third_party/protobuf/src/google/protobuf/stubs/io_win32.h"
is listed in
"E:/complier/grpc/grpc/third_party/protobuf/cmake/cmake/extract_includes.bat.in"
but there not exists. The file will not be installed.
Call Stack (most recent call first):
third_party/protobuf/cmake/CMakeLists.txt:231 (include)
This warning is for project developers. Use -Wno-dev to suppress it.
CMake Warning at cmake/protobuf.cmake:51 (message):
gRPC_INSTALL will be forced to FALSE because gRPC_PROTOBUF_PROVIDER is
"module"
Call Stack (most recent call first):
CMakeLists.txt:142 (include)
解決這個問題的步驟如下:
1、進入grpc/third_party/protobuf
目錄,使用git
命令更新下子模塊git submodule update --init
。或者在一開始對grpc
更新子模塊的時候,使用--recursive
進行遞歸更新。
2、生成編譯配置,編譯安裝(參考:protocolbuffers/protobuf/appveyor.bat)
# 參考protobuf目錄下的 appveyor.bat 腳本文件
mkdir build_msvc
cd build_msvc
# 指定 protobuf_BUILD_SHARED_LIBS 為0,以便生成靜態庫
cmake -G "Visual Studio 14 2015 Win64" -Dprotobuf_BUILD_SHARED_LIBS=0 -Dprotobuf_UNICODE=1 -DCMAKE_INSTALL_PREFIX=E:/complier/x64-windows-static ../cmake
# 編譯,這里指定生成了Release版本的,默認是生成的Debug版本(實際上我兩個版本都編譯了)
msbuild ALL_BUILD.vcxproj /maxcpucount:4 /p:Configuration=Release
# 安裝
msbuild INSTALL.vcxproj /p:Configuration=Release
3、指定下面幾個選項后,繼續使用cmake命令生成
-DgRPC_CARES_PROVIDER="package" \
-DProtobuf_INCLUDE_DIR="E:/complier/x64-windows-static/include" \
-DProtobuf_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf.lib" \
-DProtobuf_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobufd.lib" \
-DProtobuf_LITE_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf-lite.lib" \
-DProtobuf_LITE_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobuf-lited.lib" \
-DProtobuf_PROTOC_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotoc.lib" \
-DProtobuf_PROTOC_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotocd.lib" \
-DProtobuf_PROTOC_EXECUTABLE="E:/complier/x64-windows-static/bin/protoc.exe" \
-DProtobuf_SRC_ROOT_FOLDER="third_party/protobuf" \
2.1.5、繼續,報SSL
警告
CMake Warning at cmake/ssl.cmake:37 (message):
gRPC_INSTALL will be forced to FALSE because gRPC_SSL_PROVIDER is "module"
Call Stack (most recent call first):
CMakeLists.txt:143 (include)
這里可以忽略它,不使用SSL即可。也可以編譯openssl庫,然后指定以下選項后,繼續執行cmake。
-DgRPC_SSL_PROVIDER="package" \
-DOPENSSL_INCLUDE_DIR="E:/complier/x64-windows-static/include" \
-DSSL_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" \
-DLIB_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" \
-DLIB_EAY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" \
-DLIB_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" \
-DLIB_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" \
-DSSL_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" \
-DSSL_EAY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" \
-DSSL_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" \
編譯openssl也很簡單,可以參考 VS2015編譯Openssl-1.1.0f 的內容,也可以直接使用vcpkg
安裝。
我這里沒有編譯了,直接使用vcpkg安裝后,備份出來后使用。
vcpkg install openssl:x64-windows-static
vcpkg export openssl:x64-windows-static --zip --output=openssl.x64-windows-static
2.1.6、VS2015環境下可能遇到的錯誤
下面兩個錯誤,是我在指定編譯器為VS2015的時候遇到的,VS2019並沒有遇到。
1、繼續,報錯誤INTTYPES_FORMAT
沒有設置
CMake Error at third_party/gflags/CMakeLists.txt:286 (message):
Do not know how to define a 32-bit integer quantity on your system! Neither
uint32_t, u_int32_t, nor __int32 seem to be available. Set
[GFLAGS_]INTTYPES_FORMAT to either C99, BSD, or VC7 and try again.
這里可以打開grpc/third_party/gflags/CMakeLists.txt 查看
這里在cmake命令參數中添加一個變量,指定使用C99或者VC7格式。
-DINTTYPES_FORMAT:STRING=C99
2、繼續,報benchmark
相關錯誤
-- git Version: v1.4.0-e776aa02-dirty
-- Version: 1.4.0
-- Performing Test HAVE_STD_REGEX -- failed to compile
-- Performing Test HAVE_GNU_POSIX_REGEX -- failed to compile
-- Performing Test HAVE_POSIX_REGEX -- failed to compile
CMake Error at third_party/benchmark/CMakeLists.txt:229 (message):
Failed to determine the source files for the regular expression backend
我直接打開grpc/third_party/benchmark/CMakeLists.txt做了如下修改,然后繼續執行cmake,就通過了
完整的cmake執行命令如下:
cmake —G "Visual Studio 16 2019" -DCMAKE_CONFIGURATION_TYPES=Release -DGO_EXECUTABLE="C:/Go/bin/go.exe" -DCMAKE_INSTALL_PREFIX="E:/complier/grpc/install" -DgRPC_MSVC_STATIC_RUNTIME=1 -DgRPC_ZLIB_PROVIDER=package -DZLIB_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/zlibd.lib" -DZLIB_INCLUDE_DIR="E:/complier/x64-windows-static/include" -DZLIB_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/zlib.lib" -DgRPC_CARES_PROVIDER=package -Dc-ares_DIR="E:/complier/x64-windows-static/lib/cmake/c-ares" -DgRPC_PROTOBUF_PROVIDER="package" -DProtobuf_INCLUDE_DIR="E:/complier/x64-windows-static/include" -DProtobuf_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf.lib" -DProtobuf_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobufd.lib" -DProtobuf_LITE_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf-lite.lib" -DProtobuf_LITE_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobuf-lited.lib" -DProtobuf_PROTOC_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotoc.lib" -DProtobuf_PROTOC_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotocd.lib" -DProtobuf_PROTOC_EXECUTABLE="E:/complier/x64-windows-static/bin/protoc.exe" -DProtobuf_SRC_ROOT_FOLDER="third_party/protobuf" -DgRPC_SSL_PROVIDER="package" -DOPENSSL_INCLUDE_DIR="E:/complier/x64-windows-static/include" -DSSL_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" -DLIB_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" -DLIB_EAY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" -DLIB_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" -DLIB_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" -DSSL_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" -DSSL_EAY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" -DSSL_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" -DINTTYPES_FORMAT:STRING=C99 ..\grpc
2.2、使用msbuild工具進行編譯
直接使用VS2019命令行工具進入生成的工程目錄,執行下面命令即可
msbuild /maxcpucount:4 /p:Configuration=Release ALL_BUILD.vcxproj
編譯耗時會比較久。
注意,上面編譯第三方庫的時候,一定要指定使用使用靜態運行時(/MT),否則可能這里編譯完鏈接會出錯。或者可以干脆不編譯靜態庫,全部編譯為動態庫,使用默認的(/MD)參數。
3、linux下編譯
這里只做了CentOS7下的編譯,如果是其他發行版應該更簡單。
uname -a
Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
下面很多操作需要root權限,下面的命令沒有使用sudo
,需要根據情況添加。
3.1 CentO S下基本編譯環境安裝
這里主要是安裝以下gcc編譯器和cmake等工具。
# 安裝cmake3、make(默認cmake版本太低)
yum -y install epel-release # 默認情況下是沒有cmake3的,必須安裝epel(企業版 Linux 附加軟件包)源才行
yum install cmake3 make
# 配置SLC源
yum install centos-release-scl-rh centos-release-scl
yum check-update
# 安裝devtoolset,因為默認安裝gcc版本為4.8.5,版本太低
yum install devtoolset-8-gcc devtoolset-8-gcc-c++
# 安裝完成之后執行下面語句,把相關環境變量導入
source /opt/rh/devtoolset-4/enable
# 也可以把上面文件的內容追加到/etc/profile中,就不用每次都要導入了
# cat /opt/rh/devtoolset-4/enable >> /etc/profile
# 也可以把這個文件放入 /etc/profile.d 目錄下
# ln -s /opt/rh/devtoolset-8/enable /etc/profile.d/devtoolset-8.sh
# 安裝go,也可直接下載安裝包
yum install go-toolset-7-golang
# 啟用go,這里和上面一樣,也可以直接寫到/etc/profile里面去
source /opt/rh/go-toolset-7/enable
3.2 編譯三方依賴庫
1、編譯安裝zlib
cd grpc/third_party/zlib
# 編譯安裝
./configure --prefix=/usr
make && make install
2、編譯安裝c-ares
git clone https://github.com/c-ares/c-ares.git
cd c-ares
# 可以不下載,直接 cd grpc/third_party/cares/cares
cmake3 . -DCMAKE_INSTALL_PREFIX=/usr
make && make install
3、編譯安裝protobuf
# 進入grpc下源碼目錄,或者下載 git clone https://github.com/protocolbuffers/protobuf.git
cd grpc/third_party/protobuf
# 同步下子模塊
git submodule update --init --recursive
# 創建cmake構建目錄
mkdir build && cd build
# 生成Makefile
cmake3 -DCMAKE_INSTALL_PREFIX=/usr ../cmake
# 編譯安裝
make -j4
make install
4、編譯安裝openssl
這個也可以不安裝的,不影響編譯。
# 下載安裝包
wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
tar -xzf openssl-1.0.2s.tar.gz
cd openssl-1.0.2s
# 配置編譯參數
./config --prefix=/usr
# 編譯安裝
make -j4
make install
注意,因為我上面編譯的幾個庫都是安裝到/usr
目錄的,所以下面cmake參數中沒有再指定。如果是安裝到自定義的目錄的,則需要像上面Windows下編譯一樣,指定相關的路徑。
3.3編譯grpc
這里沒什么直接使用
# 創建cmake構建目錄
mkdir build && cd build
# 執行cmake命令,生成Makefile腳本
cmake3 ../grpc -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/pure/grpc/install -DgRPC_ZLIB_PROVIDER=package -DgRPC_CARES_PROVIDER=package -DgRPC_PROTOBUF_PROVIDER=package -DgRPC_SSL_PROVIDER=package
# 編譯
make -j4