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 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)
116to get significant improvement to the build and test speed (and a bunch of other very useful features).
117
118## Building with CMake
119
120### Linux/Unix, Using Make
121
122Run from the grpc directory after cloning the repo with --recursive or updating submodules.
123```
124$ mkdir -p cmake/build
125$ cd cmake/build
126$ cmake ../..
127$ make
128```
129
130If you want to build shared libraries (`.so` files), run `cmake` with `-DBUILD_SHARED_LIBS=ON`.
131
132### Windows, Using Visual Studio 2019 or later
133
134When using the "Visual Studio" generator,
135cmake will generate a solution (`grpc.sln`) that contains a VS project for
136every target defined in `CMakeLists.txt` (+ a few extra convenience projects
137added automatically by cmake). After opening the solution with Visual Studio
138you will be able to browse and build the code.
139```
140> @rem Run from grpc directory after cloning the repo with --recursive or updating submodules.
141> md .build
142> cd .build
143> cmake .. -G "Visual Studio 16 2019"
144> cmake --build . --config Release
145```
146
147Using gRPC C++ as a DLL is not recommended, but you can still enable it by running `cmake` with `-DBUILD_SHARED_LIBS=ON`.
148
149### Windows, Using Ninja (faster build).
150
151Please note that when using Ninja, you will still need Visual C++ (part of Visual Studio)
152installed to be able to compile the C/C++ sources.
153```
154> @rem Run from grpc directory after cloning the repo with --recursive or updating submodules.
155> cd cmake
156> md build
157> cd build
158> call "%VS140COMNTOOLS%..\..\VC\vcvarsall.bat" x64
159> cmake ..\.. -GNinja -DCMAKE_BUILD_TYPE=Release
160> cmake --build .
161```
162
163Using gRPC C++ as a DLL is not recommended, but you can still enable it by running `cmake` with `-DBUILD_SHARED_LIBS=ON`.
164
165### Windows: A note on building shared libs (DLLs)
166
167Windows 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.
168
169That 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
170at your own risk.
171- 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.
172- 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
173
174### Dependency management
175
176gRPC's CMake build system has two options for handling dependencies.
177CMake can build the dependencies for you, or it can search for libraries
178that are already installed on your system and use them to build gRPC.
179
180This behavior is controlled by the `gRPC_<depname>_PROVIDER` CMake variables,
181e.g. `gRPC_CARES_PROVIDER`. The options that these variables take are as follows:
182
183* module - build dependencies alongside gRPC. The source code is obtained from
184gRPC's git submodules.
185* package - use external copies of dependencies that are already available
186on your system. These could come from your system package manager, or perhaps
187you pre-installed them using CMake with the `CMAKE_INSTALL_PREFIX` option.
188
189For example, if you set `gRPC_CARES_PROVIDER=module`, then CMake will build
190c-ares before building gRPC. On the other hand, if you set
191`gRPC_CARES_PROVIDER=package`, then CMake will search for a copy of c-ares
192that's already installed on your system and use it to build gRPC.
193
194### Install after build
195
196Perform the following steps to install gRPC using CMake.
197* Set `-DgRPC_INSTALL=ON`
198* Build the `install` target
199
200The install destination is controlled by the
201[`CMAKE_INSTALL_PREFIX`](https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html) variable.
202
203If you are running CMake v3.13 or newer you can build gRPC's dependencies
204in "module" mode and install them alongside gRPC in a single step.
205[Example](test/distrib/cpp/run_distrib_test_cmake_module_install.sh)
206
207If you are building gRPC < 1.27 or if you are using CMake < 3.13 you will need
208to select "package" mode (rather than "module" mode) for the dependencies.
209This means you will need to have external copies of these libraries available
210on your system. This [example](test/distrib/cpp/run_distrib_test_cmake.sh) shows
211how to install dependencies with cmake before proceeding to installing gRPC itself.
212
213```
214# NOTE: all of gRPC's dependencies need to be already installed
215$ cmake ../.. -DgRPC_INSTALL=ON                \
216              -DCMAKE_BUILD_TYPE=Release       \
217              -DgRPC_ABSL_PROVIDER=package     \
218              -DgRPC_CARES_PROVIDER=package    \
219              -DgRPC_PROTOBUF_PROVIDER=package \
220              -DgRPC_RE2_PROVIDER=package      \
221              -DgRPC_SSL_PROVIDER=package      \
222              -DgRPC_ZLIB_PROVIDER=package
223$ make
224$ make install
225```
226
227### Cross-compiling
228
229You can use CMake to cross-compile gRPC for another architecture. In order to
230do so, you will first need to build `protoc` and `grpc_cpp_plugin`
231for the host architecture. These tools are used during the build of gRPC, so
232we need copies of executables that can be run natively.
233
234You will likely need to install the toolchain for the platform you are
235targeting for your cross-compile. Once you have done so, you can write a
236toolchain file to tell CMake where to find the compilers and system tools
237that will be used for this build.
238
239This toolchain file is specified to CMake by setting the `CMAKE_TOOLCHAIN_FILE`
240variable.
241```
242$ cmake ../.. -DCMAKE_TOOLCHAIN_FILE=path/to/file
243$ make
244```
245
246[Cross-compile example](test/distrib/cpp/run_distrib_test_cmake_aarch64_cross.sh)
247
248### A note on SONAME and its ABI compatibility implications in the cmake build
249
250Best efforts are made to bump the SONAME revision during ABI breaches. While a
251change in the SONAME clearly indicates an ABI incompatibility, no hard guarantees
252can be made about any sort of ABI stability across the same SONAME version.
253
254## Building with make on UNIX systems (deprecated)
255
256NOTE: `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.
257
258From the grpc repository root
259```sh
260 $ make
261```
262
263NOTE: 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:
264```sh
265$ git clean -f -d -x && git submodule foreach --recursive git clean -f -d -x
266$ [sudo] apt-get install build-essential autoconf libtool pkg-config
267$ make
268```
269
270### A note on `protoc`
271
272By default gRPC uses [protocol buffers](https://github.com/protocolbuffers/protobuf),
273you will need the `protoc` compiler to generate stub server and client code.
274
275If you compile gRPC from source, as described above, the Makefile will
276automatically try compiling the `protoc` in third_party if you cloned the
277repository recursively and it detects that you do not already have 'protoc' compiler
278installed.
279