1gRPC C++ - Building from source 2=========================== 3 4This document has detailed instructions on how to build gRPC C++ from source. Note that it only covers the build of gRPC itself and is meant for gRPC C++ contributors and/or power users. 5Other should follow the user instructions. See the [How to use](https://github.com/grpc/grpc/tree/master/src/cpp#to-start-using-grpc-c) instructions for guidance on how to add gRPC as a dependency to a C++ application (there are several ways and system-wide installation is often not the best choice). 6 7# Pre-requisites 8 9## Linux 10 11```sh 12 $ [sudo] apt-get install build-essential autoconf libtool pkg-config 13``` 14 15If you plan to build using CMake 16```sh 17 $ [sudo] apt-get install cmake 18``` 19 20If you are a contributor and plan to build and run tests, install the following as well: 21```sh 22 $ # clang and LLVM C++ lib is only required for sanitizer builds 23 $ [sudo] apt-get install clang libc++-dev 24``` 25 26## MacOS 27 28On a Mac, you will first need to 29install Xcode or 30[Command Line Tools for Xcode](https://developer.apple.com/download/more/) 31and then run the following command from a terminal: 32 33```sh 34 $ [sudo] xcode-select --install 35``` 36 37To build gRPC from source, you may need to install the following 38packages from [Homebrew](https://brew.sh): 39 40```sh 41 $ brew install autoconf automake libtool shtool 42``` 43 44If you plan to build using CMake, follow the instructions from https://cmake.org/download/ 45 46*Tip*: when building, 47you *may* want to explicitly set the `LIBTOOL` and `LIBTOOLIZE` 48environment variables when running `make` to ensure the version 49installed by `brew` is being used: 50 51```sh 52 $ LIBTOOL=glibtool LIBTOOLIZE=glibtoolize make 53``` 54 55## Windows 56 57To prepare for cmake + Microsoft Visual C++ compiler build 58- Install Visual Studio 2019 or later (Visual C++ compiler will be used). 59- Install [Git](https://git-scm.com/). 60- Install [CMake](https://cmake.org/download/). 61- Install [nasm](https://www.nasm.us/) and add it to `PATH` (`choco install nasm`) - *required by boringssl* 62- (Optional) Install [Ninja](https://ninja-build.org/) (`choco install ninja`) 63 64# Clone the repository (including submodules) 65 66Before building, you need to clone the gRPC github repository and download submodules containing source code 67for gRPC's dependencies (that's done by the `submodule` command or `--recursive` flag). Use following commands 68to clone the gRPC repository at the [latest stable release tag](https://github.com/grpc/grpc/releases) 69 70## Unix 71 72```sh 73 $ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc 74 $ cd grpc 75 $ git submodule update --init 76 ``` 77 78## Windows 79 80``` 81> git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc 82> cd grpc 83> git submodule update --init 84``` 85 86NOTE: The `bazel` build tool uses a different model for dependencies. You only need to worry about downloading submodules if you're building 87with something else than `bazel` (e.g. `cmake`). 88 89# Build from source 90 91In the C++ world, there's no "standard" build system that would work for all supported use cases and on all supported platforms. 92Therefore, gRPC supports several major build systems, which should satisfy most users. Depending on your needs 93we recommend building using `bazel` or `cmake`. 94 95## Building with bazel (recommended) 96 97Bazel is the primary build system for gRPC C++. If you're comfortable using bazel, we can certainly recommend it. 98Using bazel will give you the best developer experience in addition to faster and cleaner builds. 99 100You'll need `bazel` version `1.0.0` or higher to build gRPC. 101See [Installing Bazel](https://docs.bazel.build/versions/master/install.html) for instructions how to install bazel on your system. 102We support building with `bazel` on Linux, MacOS and Windows. 103 104From the grpc repository root 105``` 106# Build gRPC C++ 107$ bazel build :all 108``` 109 110``` 111# Run all the C/C++ tests 112$ bazel test --config=dbg //test/... 113``` 114 115NOTE: If you're using Bazel 7 or newer and working with gRPC, you'll need to turn off bzlmod. 116This is because gRPC isn't fully compatible with bzlmod yet. To do this, add --enable_bzlmod=false to your Bazel commands. 117 118NOTE: If you are a gRPC maintainer and you have access to our test cluster, you should use our [gRPC's Remote Execution environment](tools/remote_build/README.md) 119to get significant improvement to the build and test speed (and a bunch of other very useful features). 120 121## Building with CMake 122 123### Linux/Unix, Using Make 124 125Run from the grpc directory after cloning the repo with --recursive or updating submodules. 126``` 127$ mkdir -p cmake/build 128$ cd cmake/build 129$ cmake ../.. 130$ make 131``` 132 133If you want to build shared libraries (`.so` files), run `cmake` with `-DBUILD_SHARED_LIBS=ON`. 134 135### Windows, Using Visual Studio 2019 or later 136 137When using the "Visual Studio" generator, 138cmake will generate a solution (`grpc.sln`) that contains a VS project for 139every target defined in `CMakeLists.txt` (+ a few extra convenience projects 140added automatically by cmake). After opening the solution with Visual Studio 141you will be able to browse and build the code. 142``` 143> @rem Run from grpc directory after cloning the repo with --recursive or updating submodules. 144> md .build 145> cd .build 146> cmake .. -G "Visual Studio 16 2019" 147> cmake --build . --config Release 148``` 149 150Using gRPC C++ as a DLL is not recommended, but you can still enable it by running `cmake` with `-DBUILD_SHARED_LIBS=ON`. 151 152### Windows, Using Ninja (faster build). 153 154Please note that when using Ninja, you will still need Visual C++ (part of Visual Studio) 155installed to be able to compile the C/C++ sources. 156``` 157> @rem Run from grpc directory after cloning the repo with --recursive or updating submodules. 158> cd cmake 159> md build 160> cd build 161> call "%VS140COMNTOOLS%..\..\VC\vcvarsall.bat" x64 162> cmake ..\.. -GNinja -DCMAKE_BUILD_TYPE=Release 163> cmake --build . 164``` 165 166Using gRPC C++ as a DLL is not recommended, but you can still enable it by running `cmake` with `-DBUILD_SHARED_LIBS=ON`. 167 168### Windows: A note on building shared libs (DLLs) 169 170Windows DLL build is supported at a "best effort" basis and we don't recommend using gRPC C++ as a DLL as there are some known drawbacks around how C++ DLLs work on Windows. For example, there is no stable C++ ABI and you can't safely allocate memory in one DLL, and free it in another etc. 171 172That said, we don't actively prohibit building DLLs on windows (it can be enabled in cmake with `-DBUILD_SHARED_LIBS=ON`), and are free to use the DLL builds 173at your own risk. 174- you've been warned that there are some important drawbacks and some things might not work at all or will be broken in interesting ways. 175- we don't have extensive testing for DLL builds in place (to avoid maintenance costs, increased test duration etc.) so regressions / build breakages might occur 176 177### Dependency management 178 179gRPC's CMake build system has two options for handling dependencies. 180CMake can build the dependencies for you, or it can search for libraries 181that are already installed on your system and use them to build gRPC. 182 183This behavior is controlled by the `gRPC_<depname>_PROVIDER` CMake variables, 184e.g. `gRPC_CARES_PROVIDER`. The options that these variables take are as follows: 185 186* module - build dependencies alongside gRPC. The source code is obtained from 187gRPC's git submodules. 188* package - use external copies of dependencies that are already available 189on your system. These could come from your system package manager, or perhaps 190you pre-installed them using CMake with the `CMAKE_INSTALL_PREFIX` option. 191 192For example, if you set `gRPC_CARES_PROVIDER=module`, then CMake will build 193c-ares before building gRPC. On the other hand, if you set 194`gRPC_CARES_PROVIDER=package`, then CMake will search for a copy of c-ares 195that's already installed on your system and use it to build gRPC. 196 197### Install after build 198 199Perform the following steps to install gRPC using CMake. 200* Set `-DgRPC_INSTALL=ON` 201* Build the `install` target 202 203The install destination is controlled by the 204[`CMAKE_INSTALL_PREFIX`](https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html) variable. 205 206If you are running CMake v3.13 or newer you can build gRPC's dependencies 207in "module" mode and install them alongside gRPC in a single step. 208[Example](test/distrib/cpp/run_distrib_test_cmake_module_install.sh) 209 210If you are building gRPC < 1.27 or if you are using CMake < 3.13 you will need 211to select "package" mode (rather than "module" mode) for the dependencies. 212This means you will need to have external copies of these libraries available 213on your system. This [example](test/distrib/cpp/run_distrib_test_cmake.sh) shows 214how to install dependencies with cmake before proceeding to installing gRPC itself. 215 216``` 217# NOTE: all of gRPC's dependencies need to be already installed 218$ cmake ../.. -DgRPC_INSTALL=ON \ 219 -DCMAKE_BUILD_TYPE=Release \ 220 -DgRPC_ABSL_PROVIDER=package \ 221 -DgRPC_CARES_PROVIDER=package \ 222 -DgRPC_PROTOBUF_PROVIDER=package \ 223 -DgRPC_RE2_PROVIDER=package \ 224 -DgRPC_SSL_PROVIDER=package \ 225 -DgRPC_ZLIB_PROVIDER=package 226$ make 227$ make install 228``` 229 230### Cross-compiling 231 232You can use CMake to cross-compile gRPC for another architecture. In order to 233do so, you will first need to build `protoc` and `grpc_cpp_plugin` 234for the host architecture. These tools are used during the build of gRPC, so 235we need copies of executables that can be run natively. 236 237You will likely need to install the toolchain for the platform you are 238targeting for your cross-compile. Once you have done so, you can write a 239toolchain file to tell CMake where to find the compilers and system tools 240that will be used for this build. 241 242This toolchain file is specified to CMake by setting the `CMAKE_TOOLCHAIN_FILE` 243variable. 244``` 245$ cmake ../.. -DCMAKE_TOOLCHAIN_FILE=path/to/file 246$ make 247``` 248 249[Cross-compile example](test/distrib/cpp/run_distrib_test_cmake_aarch64_cross.sh) 250 251### A note on SONAME and its ABI compatibility implications in the cmake build 252 253Best efforts are made to bump the SONAME revision during ABI breaches. While a 254change in the SONAME clearly indicates an ABI incompatibility, no hard guarantees 255can be made about any sort of ABI stability across the same SONAME version. 256 257## Building with make on UNIX systems (deprecated) 258 259NOTE: `make` used to be gRPC's default build system, but we're no longer recommending it. You should use `bazel` or `cmake` instead. The `Makefile` is only intended for internal usage and is not meant for public consumption. 260 261From the grpc repository root 262```sh 263 $ make 264``` 265 266NOTE: if you get an error on linux such as 'aclocal-1.15: command not found', which can happen if you ran 'make' before installing the pre-reqs, try the following: 267```sh 268$ git clean -f -d -x && git submodule foreach --recursive git clean -f -d -x 269$ [sudo] apt-get install build-essential autoconf libtool pkg-config 270$ make 271``` 272 273### A note on `protoc` 274 275By default gRPC uses [protocol buffers](https://github.com/protocolbuffers/protobuf), 276you will need the `protoc` compiler to generate stub server and client code. 277 278If you compile gRPC from source, as described above, the Makefile will 279automatically try compiling the `protoc` in third_party if you cloned the 280repository recursively and it detects that you do not already have 'protoc' compiler 281installed. 282