David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 1 | # Building BoringSSL |
| 2 | |
| 3 | ## Build Prerequisites |
| 4 | |
David Benjamin | 6f9f4cc | 2018-12-21 16:05:09 -0600 | [diff] [blame] | 5 | The standalone CMake build is primarily intended for developers. If embedding |
| 6 | BoringSSL into another project with a pre-existing build system, see |
| 7 | [INCORPORATING.md](/INCORPORATING.md). |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 8 | |
David Benjamin | 6f9f4cc | 2018-12-21 16:05:09 -0600 | [diff] [blame] | 9 | Unless otherwise noted, build tools must at most five years old, matching |
| 10 | [Abseil guidelines](https://abseil.io/about/compatibility). If in doubt, use the |
| 11 | most recent stable version of each tool. |
| 12 | |
David Benjamin | 39707fe | 2022-11-21 15:23:17 -0500 | [diff] [blame] | 13 | * [CMake](https://cmake.org/download/) 3.10 or later is required. |
David Benjamin | 6f9f4cc | 2018-12-21 16:05:09 -0600 | [diff] [blame] | 14 | |
| 15 | * A recent version of Perl is required. On Windows, |
nmittler | 042e8f7 | 2016-02-09 11:25:52 -0800 | [diff] [blame] | 16 | [Active State Perl](http://www.activestate.com/activeperl/) has been |
| 17 | reported to work, as has MSYS Perl. |
| 18 | [Strawberry Perl](http://strawberryperl.com/) also works but it adds GCC |
| 19 | to `PATH`, which can confuse some build tools when identifying the compiler |
| 20 | (removing `C:\Strawberry\c\bin` from `PATH` should resolve any problems). |
| 21 | If Perl is not found by CMake, it may be configured explicitly by setting |
| 22 | `PERL_EXECUTABLE`. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 23 | |
David Benjamin | 6f9f4cc | 2018-12-21 16:05:09 -0600 | [diff] [blame] | 24 | * Building with [Ninja](https://ninja-build.org/) instead of Make is |
| 25 | recommended, because it makes builds faster. On Windows, CMake's Visual |
| 26 | Studio generator may also work, but it not tested regularly and requires |
| 27 | recent versions of CMake for assembly support. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 28 | |
David Benjamin | 73d69f4 | 2018-11-13 17:49:42 -0600 | [diff] [blame] | 29 | * On Windows only, [NASM](https://www.nasm.us/) is required. If not found |
Brian Smith | 953cfc8 | 2015-10-06 12:51:38 -1000 | [diff] [blame] | 30 | by CMake, it may be configured explicitly by setting |
| 31 | `CMAKE_ASM_NASM_COMPILER`. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 32 | |
David Benjamin | f961de5 | 2022-04-18 17:39:00 -0400 | [diff] [blame] | 33 | * C and C++ compilers with C++14 support are required. If using a C compiler |
| 34 | other than MSVC, C11 support is also requried. On Windows, MSVC from |
David Benjamin | 2144076 | 2022-03-19 18:39:18 -0400 | [diff] [blame] | 35 | Visual Studio 2017 or later with Platform SDK 8.1 or later are supported, |
| 36 | but newer versions are recommended. Recent versions of GCC (6.1+) and Clang |
| 37 | should work on non-Windows platforms, and maybe on Windows too. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 38 | |
David Benjamin | 0990a55 | 2018-09-07 14:51:08 -0500 | [diff] [blame] | 39 | * The most recent stable version of [Go](https://golang.org/dl/) is required. |
David Benjamin | 6f9f4cc | 2018-12-21 16:05:09 -0600 | [diff] [blame] | 40 | Note Go is exempt from the five year support window. If not found by CMake, |
| 41 | the go executable may be configured explicitly by setting `GO_EXECUTABLE`. |
David Benjamin | f6a74c6 | 2016-06-10 13:12:20 -0400 | [diff] [blame] | 42 | |
David Benjamin | 17d553d | 2018-12-21 17:58:36 -0600 | [diff] [blame] | 43 | * On x86_64 Linux, the tests have an optional |
| 44 | [libunwind](https://www.nongnu.org/libunwind/) dependency to test the |
| 45 | assembly more thoroughly. |
| 46 | |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 47 | ## Building |
| 48 | |
| 49 | Using Ninja (note the 'N' is capitalized in the cmake invocation): |
| 50 | |
| 51 | mkdir build |
| 52 | cd build |
| 53 | cmake -GNinja .. |
| 54 | ninja |
| 55 | |
| 56 | Using Make (does not work on Windows): |
| 57 | |
| 58 | mkdir build |
| 59 | cd build |
| 60 | cmake .. |
| 61 | make |
| 62 | |
| 63 | You usually don't need to run `cmake` again after changing `CMakeLists.txt` |
| 64 | files because the build scripts will detect changes to them and rebuild |
| 65 | themselves automatically. |
| 66 | |
| 67 | Note that the default build flags in the top-level `CMakeLists.txt` are for |
nmittler | 042e8f7 | 2016-02-09 11:25:52 -0800 | [diff] [blame] | 68 | debugging—optimisation isn't enabled. Pass `-DCMAKE_BUILD_TYPE=Release` to |
| 69 | `cmake` to configure a release build. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 70 | |
| 71 | If you want to cross-compile then there is an example toolchain file for 32-bit |
| 72 | Intel in `util/`. Wipe out the build directory, recreate it and run `cmake` like |
| 73 | this: |
| 74 | |
| 75 | cmake -DCMAKE_TOOLCHAIN_FILE=../util/32-bit-toolchain.cmake -GNinja .. |
| 76 | |
| 77 | If you want to build as a shared library, pass `-DBUILD_SHARED_LIBS=1`. On |
| 78 | Windows, where functions need to be tagged with `dllimport` when coming from a |
| 79 | shared library, define `BORINGSSL_SHARED_LIBRARY` in any code which `#include`s |
| 80 | the BoringSSL headers. |
| 81 | |
Adam Langley | 2e3c978 | 2015-10-27 08:47:11 -0700 | [diff] [blame] | 82 | In order to serve environments where code-size is important as well as those |
| 83 | where performance is the overriding concern, `OPENSSL_SMALL` can be defined to |
| 84 | remove some code that is especially large. |
| 85 | |
nmittler | 042e8f7 | 2016-02-09 11:25:52 -0800 | [diff] [blame] | 86 | See [CMake's documentation](https://cmake.org/cmake/help/v3.4/manual/cmake-variables.7.html) |
| 87 | for other variables which may be used to configure the build. |
| 88 | |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 89 | ### Building for Android |
| 90 | |
David Benjamin | 5288779 | 2017-12-13 18:18:28 -0500 | [diff] [blame] | 91 | It's possible to build BoringSSL with the Android NDK using CMake. Recent |
| 92 | versions of the NDK include a CMake toolchain file which works with CMake 3.6.0 |
| 93 | or later. This has been tested with version r16b of the NDK. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 94 | |
| 95 | Unpack the Android NDK somewhere and export `ANDROID_NDK` to point to the |
David Benjamin | 75021b7 | 2016-04-28 14:51:36 -0400 | [diff] [blame] | 96 | directory. Then make a build directory as above and run CMake like this: |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 97 | |
David Benjamin | 75021b7 | 2016-04-28 14:51:36 -0400 | [diff] [blame] | 98 | cmake -DANDROID_ABI=armeabi-v7a \ |
David Benjamin | 5288779 | 2017-12-13 18:18:28 -0500 | [diff] [blame] | 99 | -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \ |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 100 | -DANDROID_NATIVE_API_LEVEL=16 \ |
| 101 | -GNinja .. |
| 102 | |
David Benjamin | 75021b7 | 2016-04-28 14:51:36 -0400 | [diff] [blame] | 103 | Once you've run that, Ninja should produce Android-compatible binaries. You |
| 104 | can replace `armeabi-v7a` in the above with `arm64-v8a` and use API level 21 or |
| 105 | higher to build aarch64 binaries. |
| 106 | |
David Benjamin | 5288779 | 2017-12-13 18:18:28 -0500 | [diff] [blame] | 107 | For other options, see the documentation in the toolchain file. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 108 | |
David Benjamin | 9978f0a | 2019-01-30 16:56:54 -0600 | [diff] [blame] | 109 | To debug the resulting binaries on an Android device with `gdb`, run the |
| 110 | commands below. Replace `ARCH` with the architecture of the target device, e.g. |
| 111 | `arm` or `arm64`. |
| 112 | |
| 113 | adb push ${ANDROID_NDK}/prebuilt/android-ARCH/gdbserver/gdbserver \ |
| 114 | /data/local/tmp |
| 115 | adb forward tcp:5039 tcp:5039 |
| 116 | adb shell /data/local/tmp/gdbserver :5039 /path/on/device/to/binary |
| 117 | |
| 118 | Then run the following in a separate shell. Replace `HOST` with the OS and |
| 119 | architecture of the host machine, e.g. `linux-x86_64`. |
| 120 | |
| 121 | ${ANDROID_NDK}/prebuilt/HOST/bin/gdb |
| 122 | target remote :5039 # in gdb |
| 123 | |
David Benjamin | aff72a3 | 2017-04-06 23:26:04 -0400 | [diff] [blame] | 124 | ### Building for iOS |
| 125 | |
| 126 | To build for iOS, pass `-DCMAKE_OSX_SYSROOT=iphoneos` and |
| 127 | `-DCMAKE_OSX_ARCHITECTURES=ARCH` to CMake, where `ARCH` is the desired |
| 128 | architecture, matching values used in the `-arch` flag in Apple's toolchain. |
| 129 | |
| 130 | Passing multiple architectures for a multiple-architecture build is not |
| 131 | supported. |
| 132 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 133 | ### Building with Prefixed Symbols |
| 134 | |
| 135 | BoringSSL's build system has experimental support for adding a custom prefix to |
| 136 | all symbols. This can be useful when linking multiple versions of BoringSSL in |
| 137 | the same project to avoid symbol conflicts. |
| 138 | |
| 139 | In order to build with prefixed symbols, the `BORINGSSL_PREFIX` CMake variable |
| 140 | should specify the prefix to add to all symbols, and the |
| 141 | `BORINGSSL_PREFIX_SYMBOLS` CMake variable should specify the path to a file |
| 142 | which contains a list of symbols which should be prefixed (one per line; |
| 143 | comments are supported with `#`). In other words, `cmake .. |
| 144 | -DBORINGSSL_PREFIX=MY_CUSTOM_PREFIX |
| 145 | -DBORINGSSL_PREFIX_SYMBOLS=/path/to/symbols.txt` will configure the build to add |
| 146 | the prefix `MY_CUSTOM_PREFIX` to all of the symbols listed in |
| 147 | `/path/to/symbols.txt`. |
| 148 | |
| 149 | It is currently the caller's responsibility to create and maintain the list of |
Joshua Liebow-Feeser | 066b108 | 2018-09-17 15:40:24 -0700 | [diff] [blame] | 150 | symbols to be prefixed. Alternatively, `util/read_symbols.go` reads the list of |
| 151 | exported symbols from a `.a` file, and can be used in a build script to generate |
| 152 | the symbol list on the fly (by building without prefixing, using |
| 153 | `read_symbols.go` to construct a symbol list, and then building again with |
| 154 | prefixing). |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 155 | |
| 156 | This mechanism is under development and may change over time. Please contact the |
| 157 | BoringSSL maintainers if making use of it. |
| 158 | |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 159 | ## Known Limitations on Windows |
| 160 | |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 161 | * CMake can generate Visual Studio projects, but the generated project files |
| 162 | don't have steps for assembling the assembly language source files, so they |
| 163 | currently cannot be used to build BoringSSL. |
| 164 | |
David Benjamin | 1e15682 | 2021-12-27 13:27:22 -0500 | [diff] [blame] | 165 | ## ARM CPU Capabilities |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 166 | |
David Benjamin | 1e15682 | 2021-12-27 13:27:22 -0500 | [diff] [blame] | 167 | ARM, unlike Intel, does not have a userspace instruction that allows |
| 168 | applications to discover the capabilities of the processor. Instead, the |
| 169 | capability information has to be provided by a combination of compile-time |
| 170 | information and the operating system. |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 171 | |
David Benjamin | 846a227 | 2022-01-06 11:12:06 -0500 | [diff] [blame] | 172 | BoringSSL determines capabilities at compile-time based on `__ARM_NEON`, |
| 173 | `__ARM_FEATURE_AES`, and other preprocessor symbols defined in |
| 174 | [Arm C Language Extensions (ACLE)](https://developer.arm.com/architectures/system-architectures/software-standards/acle). |
David Benjamin | 1e15682 | 2021-12-27 13:27:22 -0500 | [diff] [blame] | 175 | These values are usually controlled by the `-march` flag. You can also define |
David Benjamin | 846a227 | 2022-01-06 11:12:06 -0500 | [diff] [blame] | 176 | any of the following to enable the corresponding ARM feature, but using the ACLE |
| 177 | symbols via `-march` is recommended. |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 178 | |
David Benjamin | 3b33f3e | 2017-06-08 16:53:28 -0400 | [diff] [blame] | 179 | * `OPENSSL_STATIC_ARMCAP_NEON` |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 180 | * `OPENSSL_STATIC_ARMCAP_AES` |
| 181 | * `OPENSSL_STATIC_ARMCAP_SHA1` |
| 182 | * `OPENSSL_STATIC_ARMCAP_SHA256` |
| 183 | * `OPENSSL_STATIC_ARMCAP_PMULL` |
| 184 | |
David Benjamin | 1e15682 | 2021-12-27 13:27:22 -0500 | [diff] [blame] | 185 | The resulting binary will assume all such features are always present. This can |
| 186 | reduce code size, by allowing the compiler to omit fallbacks. However, if the |
| 187 | feature is not actually supported at runtime, BoringSSL will likely crash. |
| 188 | |
| 189 | BoringSSL will additionally query the operating system at runtime for additional |
| 190 | features, e.g. with `getauxval` on Linux. This allows a single binary to use |
| 191 | newer instructions when present, but still function on CPUs without them. But |
| 192 | some environments don't support runtime queries. If building for those, define |
| 193 | `OPENSSL_STATIC_ARMCAP` to limit BoringSSL to compile-time capabilities. If not |
| 194 | defined, the target operating system must be known to BoringSSL. |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 195 | |
David Benjamin | 6291af4 | 2018-03-23 13:49:27 -0400 | [diff] [blame] | 196 | ## Binary Size |
| 197 | |
| 198 | The implementations of some algorithms require a trade-off between binary size |
| 199 | and performance. For instance, BoringSSL's fastest P-256 implementation uses a |
| 200 | 148 KiB pre-computed table. To optimize instead for binary size, pass |
| 201 | `-DOPENSSL_SMALL=1` to CMake or define the `OPENSSL_SMALL` preprocessor symbol. |
| 202 | |
| 203 | # Running Tests |
Adam Langley | dc7e9c4 | 2015-09-29 15:21:04 -0700 | [diff] [blame] | 204 | |
| 205 | There are two sets of tests: the C/C++ tests and the blackbox tests. For former |
| 206 | are built by Ninja and can be run from the top-level directory with `go run |
| 207 | util/all_tests.go`. The latter have to be run separately by running `go test` |
| 208 | from within `ssl/test/runner`. |
| 209 | |
David Benjamin | 301afaf | 2015-10-14 21:34:40 -0400 | [diff] [blame] | 210 | Both sets of tests may also be run with `ninja -C build run_tests`, but CMake |
| 211 | 3.2 or later is required to avoid Ninja's output buffering. |