David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 1 | # Building BoringSSL |
| 2 | |
| 3 | ## Build Prerequisites |
| 4 | |
David Benjamin | 9662843 | 2017-01-19 19:05:47 -0500 | [diff] [blame] | 5 | * [CMake](https://cmake.org/download/) 2.8.11 or later is required. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 6 | |
Brian Smith | 953cfc8 | 2015-10-06 12:51:38 -1000 | [diff] [blame] | 7 | * Perl 5.6.1 or later is required. On Windows, |
nmittler | 042e8f7 | 2016-02-09 11:25:52 -0800 | [diff] [blame] | 8 | [Active State Perl](http://www.activestate.com/activeperl/) has been |
| 9 | reported to work, as has MSYS Perl. |
| 10 | [Strawberry Perl](http://strawberryperl.com/) also works but it adds GCC |
| 11 | to `PATH`, which can confuse some build tools when identifying the compiler |
| 12 | (removing `C:\Strawberry\c\bin` from `PATH` should resolve any problems). |
| 13 | If Perl is not found by CMake, it may be configured explicitly by setting |
| 14 | `PERL_EXECUTABLE`. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 15 | |
David Benjamin | 2c71ce1 | 2016-02-05 22:08:45 -0500 | [diff] [blame] | 16 | * On Windows you currently must use [Ninja](https://ninja-build.org/) |
Brian Smith | 953cfc8 | 2015-10-06 12:51:38 -1000 | [diff] [blame] | 17 | to build; on other platforms, it is not required, but recommended, because |
| 18 | it makes builds faster. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 19 | |
| 20 | * If you need to build Ninja from source, then a recent version of |
Brian Smith | 953cfc8 | 2015-10-06 12:51:38 -1000 | [diff] [blame] | 21 | [Python](https://www.python.org/downloads/) is required (Python 2.7.5 works). |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 22 | |
Brian Smith | 953cfc8 | 2015-10-06 12:51:38 -1000 | [diff] [blame] | 23 | * On Windows only, [Yasm](http://yasm.tortall.net/) is required. If not found |
| 24 | by CMake, it may be configured explicitly by setting |
| 25 | `CMAKE_ASM_NASM_COMPILER`. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 26 | |
David Benjamin | bbe6af0 | 2016-04-29 14:15:39 -0400 | [diff] [blame] | 27 | * A C compiler is required. On Windows, MSVC 14 (Visual Studio 2015) or later |
nmittler | 042e8f7 | 2016-02-09 11:25:52 -0800 | [diff] [blame] | 28 | with Platform SDK 8.1 or later are supported. Recent versions of GCC (4.8+) |
| 29 | and Clang should work on non-Windows platforms, and maybe on Windows too. |
David Benjamin | f6a74c6 | 2016-06-10 13:12:20 -0400 | [diff] [blame] | 30 | To build the tests, you also need a C++ compiler with C++11 support. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 31 | |
Brian Smith | 953cfc8 | 2015-10-06 12:51:38 -1000 | [diff] [blame] | 32 | * [Go](https://golang.org/dl/) is required. If not found by CMake, the go |
| 33 | executable may be configured explicitly by setting `GO_EXECUTABLE`. |
| 34 | |
David Benjamin | 874c738 | 2017-08-15 12:56:30 -0400 | [diff] [blame] | 35 | * To build the x86 and x86\_64 assembly, your assembler must support AVX2 |
| 36 | instructions and MOVBE. If using GNU binutils, you must have 2.22 or later |
David Benjamin | f6a74c6 | 2016-06-10 13:12:20 -0400 | [diff] [blame] | 37 | |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 38 | ## Building |
| 39 | |
| 40 | Using Ninja (note the 'N' is capitalized in the cmake invocation): |
| 41 | |
| 42 | mkdir build |
| 43 | cd build |
| 44 | cmake -GNinja .. |
| 45 | ninja |
| 46 | |
| 47 | Using Make (does not work on Windows): |
| 48 | |
| 49 | mkdir build |
| 50 | cd build |
| 51 | cmake .. |
| 52 | make |
| 53 | |
| 54 | You usually don't need to run `cmake` again after changing `CMakeLists.txt` |
| 55 | files because the build scripts will detect changes to them and rebuild |
| 56 | themselves automatically. |
| 57 | |
| 58 | 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] | 59 | debugging—optimisation isn't enabled. Pass `-DCMAKE_BUILD_TYPE=Release` to |
| 60 | `cmake` to configure a release build. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 61 | |
| 62 | If you want to cross-compile then there is an example toolchain file for 32-bit |
| 63 | Intel in `util/`. Wipe out the build directory, recreate it and run `cmake` like |
| 64 | this: |
| 65 | |
| 66 | cmake -DCMAKE_TOOLCHAIN_FILE=../util/32-bit-toolchain.cmake -GNinja .. |
| 67 | |
| 68 | If you want to build as a shared library, pass `-DBUILD_SHARED_LIBS=1`. On |
| 69 | Windows, where functions need to be tagged with `dllimport` when coming from a |
| 70 | shared library, define `BORINGSSL_SHARED_LIBRARY` in any code which `#include`s |
| 71 | the BoringSSL headers. |
| 72 | |
Adam Langley | 2e3c978 | 2015-10-27 08:47:11 -0700 | [diff] [blame] | 73 | In order to serve environments where code-size is important as well as those |
| 74 | where performance is the overriding concern, `OPENSSL_SMALL` can be defined to |
| 75 | remove some code that is especially large. |
| 76 | |
nmittler | 042e8f7 | 2016-02-09 11:25:52 -0800 | [diff] [blame] | 77 | See [CMake's documentation](https://cmake.org/cmake/help/v3.4/manual/cmake-variables.7.html) |
| 78 | for other variables which may be used to configure the build. |
| 79 | |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 80 | ### Building for Android |
| 81 | |
David Benjamin | 5288779 | 2017-12-13 18:18:28 -0500 | [diff] [blame] | 82 | It's possible to build BoringSSL with the Android NDK using CMake. Recent |
| 83 | versions of the NDK include a CMake toolchain file which works with CMake 3.6.0 |
| 84 | or later. This has been tested with version r16b of the NDK. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 85 | |
| 86 | 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] | 87 | 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] | 88 | |
David Benjamin | 75021b7 | 2016-04-28 14:51:36 -0400 | [diff] [blame] | 89 | cmake -DANDROID_ABI=armeabi-v7a \ |
David Benjamin | 5288779 | 2017-12-13 18:18:28 -0500 | [diff] [blame] | 90 | -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \ |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 91 | -DANDROID_NATIVE_API_LEVEL=16 \ |
| 92 | -GNinja .. |
| 93 | |
David Benjamin | 75021b7 | 2016-04-28 14:51:36 -0400 | [diff] [blame] | 94 | Once you've run that, Ninja should produce Android-compatible binaries. You |
| 95 | can replace `armeabi-v7a` in the above with `arm64-v8a` and use API level 21 or |
| 96 | higher to build aarch64 binaries. |
| 97 | |
David Benjamin | 5288779 | 2017-12-13 18:18:28 -0500 | [diff] [blame] | 98 | For older NDK versions, BoringSSL ships a third-party CMake toolchain file. Use |
| 99 | `../third_party/android-cmake/android.toolchain.cmake` for |
| 100 | `CMAKE_TOOLCHAIN_FILE` instead. |
| 101 | |
| 102 | For other options, see the documentation in the toolchain file. |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 103 | |
David Benjamin | aff72a3 | 2017-04-06 23:26:04 -0400 | [diff] [blame] | 104 | ### Building for iOS |
| 105 | |
| 106 | To build for iOS, pass `-DCMAKE_OSX_SYSROOT=iphoneos` and |
| 107 | `-DCMAKE_OSX_ARCHITECTURES=ARCH` to CMake, where `ARCH` is the desired |
| 108 | architecture, matching values used in the `-arch` flag in Apple's toolchain. |
| 109 | |
| 110 | Passing multiple architectures for a multiple-architecture build is not |
| 111 | supported. |
| 112 | |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 113 | ## Known Limitations on Windows |
| 114 | |
| 115 | * Versions of CMake since 3.0.2 have a bug in its Ninja generator that causes |
| 116 | yasm to output warnings |
| 117 | |
| 118 | yasm: warning: can open only one input file, only the last file will be processed |
| 119 | |
| 120 | These warnings can be safely ignored. The cmake bug is |
| 121 | http://www.cmake.org/Bug/view.php?id=15253. |
| 122 | |
| 123 | * CMake can generate Visual Studio projects, but the generated project files |
| 124 | don't have steps for assembling the assembly language source files, so they |
| 125 | currently cannot be used to build BoringSSL. |
| 126 | |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 127 | ## Embedded ARM |
| 128 | |
| 129 | ARM, unlike Intel, does not have an instruction that allows applications to |
| 130 | discover the capabilities of the processor. Instead, the capability information |
| 131 | has to be provided by the operating system somehow. |
| 132 | |
David Benjamin | 3b33f3e | 2017-06-08 16:53:28 -0400 | [diff] [blame] | 133 | By default, on Linux-based systems, BoringSSL will try to use `getauxval` and |
| 134 | `/proc` to discover the capabilities. But some environments don't support that |
| 135 | sort of thing and, for them, it's possible to configure the CPU capabilities at |
| 136 | compile time. |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 137 | |
David Benjamin | 3b33f3e | 2017-06-08 16:53:28 -0400 | [diff] [blame] | 138 | On iOS or builds which define `OPENSSL_STATIC_ARMCAP`, features will be |
| 139 | determined based on the `__ARM_NEON__` and `__ARM_FEATURE_CRYPTO` preprocessor |
| 140 | symbols reported by the compiler. These values are usually controlled by the |
| 141 | `-march` flag. You can also define any of the following to enable the |
| 142 | corresponding ARM feature. |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 143 | |
David Benjamin | 3b33f3e | 2017-06-08 16:53:28 -0400 | [diff] [blame] | 144 | * `OPENSSL_STATIC_ARMCAP_NEON` |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 145 | * `OPENSSL_STATIC_ARMCAP_AES` |
| 146 | * `OPENSSL_STATIC_ARMCAP_SHA1` |
| 147 | * `OPENSSL_STATIC_ARMCAP_SHA256` |
| 148 | * `OPENSSL_STATIC_ARMCAP_PMULL` |
| 149 | |
| 150 | Note that if a feature is enabled in this way, but not actually supported at |
| 151 | run-time, BoringSSL will likely crash. |
| 152 | |
David Benjamin | 6291af4 | 2018-03-23 13:49:27 -0400 | [diff] [blame] | 153 | ## Binary Size |
| 154 | |
| 155 | The implementations of some algorithms require a trade-off between binary size |
| 156 | and performance. For instance, BoringSSL's fastest P-256 implementation uses a |
| 157 | 148 KiB pre-computed table. To optimize instead for binary size, pass |
| 158 | `-DOPENSSL_SMALL=1` to CMake or define the `OPENSSL_SMALL` preprocessor symbol. |
| 159 | |
| 160 | # Running Tests |
Adam Langley | dc7e9c4 | 2015-09-29 15:21:04 -0700 | [diff] [blame] | 161 | |
| 162 | There are two sets of tests: the C/C++ tests and the blackbox tests. For former |
| 163 | are built by Ninja and can be run from the top-level directory with `go run |
| 164 | util/all_tests.go`. The latter have to be run separately by running `go test` |
| 165 | from within `ssl/test/runner`. |
| 166 | |
David Benjamin | 301afaf | 2015-10-14 21:34:40 -0400 | [diff] [blame] | 167 | Both sets of tests may also be run with `ninja -C build run_tests`, but CMake |
| 168 | 3.2 or later is required to avoid Ninja's output buffering. |