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