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