blob: fb28e899c05081749a6250f424bbb70faef25f13 [file] [log] [blame] [view]
David Benjamin95aaf4a2015-09-03 12:09:36 -04001# Building BoringSSL
2
Hubert Chaoc7b61032023-11-29 16:21:10 +00003## Checking out BoringSSL
4
5 git clone "https://boringssl.googlesource.com/boringssl"
6
David Benjamin95aaf4a2015-09-03 12:09:36 -04007## Build Prerequisites
8
David Benjamin6f9f4cc2018-12-21 16:05:09 -06009The standalone CMake build is primarily intended for developers. If embedding
10BoringSSL into another project with a pre-existing build system, see
David Benjamin90004f02023-11-30 13:10:17 -050011[INCORPORATING.md](./INCORPORATING.md).
David Benjamin95aaf4a2015-09-03 12:09:36 -040012
David Benjamin6f9f4cc2018-12-21 16:05:09 -060013Unless otherwise noted, build tools must at most five years old, matching
14[Abseil guidelines](https://abseil.io/about/compatibility). If in doubt, use the
15most recent stable version of each tool.
16
David Benjamina1843d62023-09-17 10:17:50 -040017 * [CMake](https://cmake.org/download/) 3.12 or later is required.
David Benjamin6f9f4cc2018-12-21 16:05:09 -060018
19 * A recent version of Perl is required. On Windows,
nmittler042e8f72016-02-09 11:25:52 -080020 [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 Benjamin95aaf4a2015-09-03 12:09:36 -040027
David Benjamin6f9f4cc2018-12-21 16:05:09 -060028 * 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 Benjamin95aaf4a2015-09-03 12:09:36 -040032
David Benjamin73d69f42018-11-13 17:49:42 -060033 * On Windows only, [NASM](https://www.nasm.us/) is required. If not found
Brian Smith953cfc82015-10-06 12:51:38 -100034 by CMake, it may be configured explicitly by setting
35 `CMAKE_ASM_NASM_COMPILER`.
David Benjamin95aaf4a2015-09-03 12:09:36 -040036
David Benjaminecb7e9ae2023-09-12 13:41:45 -040037 * 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 Benjamin95aaf4a2015-09-03 12:09:36 -040042
David Benjamin0990a552018-09-07 14:51:08 -050043 * The most recent stable version of [Go](https://golang.org/dl/) is required.
David Benjamin6f9f4cc2018-12-21 16:05:09 -060044 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 Benjaminf6a74c62016-06-10 13:12:20 -040046
David Benjamin17d553d2018-12-21 17:58:36 -060047 * 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 Benjamin95aaf4a2015-09-03 12:09:36 -040051## Building
52
53Using Ninja (note the 'N' is capitalized in the cmake invocation):
54
David Benjamin1a5570b2023-04-19 15:38:47 -040055 cmake -GNinja -B build
56 ninja -C build
David Benjamin95aaf4a2015-09-03 12:09:36 -040057
58Using Make (does not work on Windows):
59
David Benjamin1a5570b2023-04-19 15:38:47 -040060 cmake -B build
61 make -C build
David Benjamin95aaf4a2015-09-03 12:09:36 -040062
David Benjaminb96e8162024-01-16 17:07:50 -050063This produces a debug build by default. Optimisation isn't enabled, and debug
64assertions are included. Pass `-DCMAKE_BUILD_TYPE=Release` to `cmake` to
65configure a release build:
David Benjamin95aaf4a2015-09-03 12:09:36 -040066
David Benjaminb96e8162024-01-16 17:07:50 -050067 cmake -GNinja -B build -DCMAKE_BUILD_TYPE=Release
68 ninja -C build
David Benjamin95aaf4a2015-09-03 12:09:36 -040069
70If you want to cross-compile then there is an example toolchain file for 32-bit
David Benjamin1a5570b2023-04-19 15:38:47 -040071Intel in `util/`. Wipe out the build directory, run `cmake` like this:
David Benjamin95aaf4a2015-09-03 12:09:36 -040072
David Benjamin1a5570b2023-04-19 15:38:47 -040073 cmake -B build -DCMAKE_TOOLCHAIN_FILE=../util/32-bit-toolchain.cmake -GNinja
David Benjamin95aaf4a2015-09-03 12:09:36 -040074
75If you want to build as a shared library, pass `-DBUILD_SHARED_LIBS=1`. On
76Windows, where functions need to be tagged with `dllimport` when coming from a
77shared library, define `BORINGSSL_SHARED_LIBRARY` in any code which `#include`s
78the BoringSSL headers.
79
Adam Langley2e3c9782015-10-27 08:47:11 -070080In order to serve environments where code-size is important as well as those
81where performance is the overriding concern, `OPENSSL_SMALL` can be defined to
82remove some code that is especially large.
83
nmittler042e8f72016-02-09 11:25:52 -080084See [CMake's documentation](https://cmake.org/cmake/help/v3.4/manual/cmake-variables.7.html)
85for other variables which may be used to configure the build.
86
David Benjaminb96e8162024-01-16 17:07:50 -050087You usually don't need to run `cmake` again after changing `CMakeLists.txt`
88files because the build scripts will detect changes to them and rebuild
89themselves automatically.
90
David Benjamin95aaf4a2015-09-03 12:09:36 -040091### Building for Android
92
David Benjamin52887792017-12-13 18:18:28 -050093It's possible to build BoringSSL with the Android NDK using CMake. Recent
94versions of the NDK include a CMake toolchain file which works with CMake 3.6.0
95or later. This has been tested with version r16b of the NDK.
David Benjamin95aaf4a2015-09-03 12:09:36 -040096
97Unpack the Android NDK somewhere and export `ANDROID_NDK` to point to the
David Benjamin1a5570b2023-04-19 15:38:47 -040098directory. Then run CMake like this:
David Benjamin95aaf4a2015-09-03 12:09:36 -040099
David Benjamin75021b72016-04-28 14:51:36 -0400100 cmake -DANDROID_ABI=armeabi-v7a \
David Benjamin92272952023-02-22 11:48:54 -0500101 -DANDROID_PLATFORM=android-19 \
David Benjamin52887792017-12-13 18:18:28 -0500102 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
David Benjamin1a5570b2023-04-19 15:38:47 -0400103 -GNinja -B build
David Benjamin95aaf4a2015-09-03 12:09:36 -0400104
David Benjamin75021b72016-04-28 14:51:36 -0400105Once you've run that, Ninja should produce Android-compatible binaries. You
106can replace `armeabi-v7a` in the above with `arm64-v8a` and use API level 21 or
107higher to build aarch64 binaries.
108
David Benjamin52887792017-12-13 18:18:28 -0500109For other options, see the documentation in the toolchain file.
David Benjamin95aaf4a2015-09-03 12:09:36 -0400110
David Benjamin9978f0a2019-01-30 16:56:54 -0600111To debug the resulting binaries on an Android device with `gdb`, run the
112commands 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
120Then run the following in a separate shell. Replace `HOST` with the OS and
121architecture 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 Benjaminaff72a32017-04-06 23:26:04 -0400126### Building for iOS
127
128To build for iOS, pass `-DCMAKE_OSX_SYSROOT=iphoneos` and
129`-DCMAKE_OSX_ARCHITECTURES=ARCH` to CMake, where `ARCH` is the desired
130architecture, matching values used in the `-arch` flag in Apple's toolchain.
131
132Passing multiple architectures for a multiple-architecture build is not
133supported.
134
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700135### Building with Prefixed Symbols
136
137BoringSSL's build system has experimental support for adding a custom prefix to
138all symbols. This can be useful when linking multiple versions of BoringSSL in
139the same project to avoid symbol conflicts.
140
141In order to build with prefixed symbols, the `BORINGSSL_PREFIX` CMake variable
142should specify the prefix to add to all symbols, and the
143`BORINGSSL_PREFIX_SYMBOLS` CMake variable should specify the path to a file
144which contains a list of symbols which should be prefixed (one per line;
David Benjamin1a5570b2023-04-19 15:38:47 -0400145comments are supported with `#`). In other words, `cmake -B build
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700146-DBORINGSSL_PREFIX=MY_CUSTOM_PREFIX
147-DBORINGSSL_PREFIX_SYMBOLS=/path/to/symbols.txt` will configure the build to add
148the prefix `MY_CUSTOM_PREFIX` to all of the symbols listed in
149`/path/to/symbols.txt`.
150
151It is currently the caller's responsibility to create and maintain the list of
Joshua Liebow-Feeser066b1082018-09-17 15:40:24 -0700152symbols to be prefixed. Alternatively, `util/read_symbols.go` reads the list of
153exported symbols from a `.a` file, and can be used in a build script to generate
154the symbol list on the fly (by building without prefixing, using
155`read_symbols.go` to construct a symbol list, and then building again with
156prefixing).
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700157
158This mechanism is under development and may change over time. Please contact the
159BoringSSL maintainers if making use of it.
160
David Benjamin95aaf4a2015-09-03 12:09:36 -0400161## Known Limitations on Windows
162
David Benjamin95aaf4a2015-09-03 12:09:36 -0400163 * 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 Benjamin1e156822021-12-27 13:27:22 -0500167## ARM CPU Capabilities
Adam Langley6a7cfbe2015-10-16 15:46:46 -0700168
David Benjamin1e156822021-12-27 13:27:22 -0500169ARM, unlike Intel, does not have a userspace instruction that allows
170applications to discover the capabilities of the processor. Instead, the
171capability information has to be provided by a combination of compile-time
172information and the operating system.
Adam Langley6a7cfbe2015-10-16 15:46:46 -0700173
David Benjamin846a2272022-01-06 11:12:06 -0500174BoringSSL 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 Benjamin1e156822021-12-27 13:27:22 -0500177These values are usually controlled by the `-march` flag. You can also define
David Benjamin846a2272022-01-06 11:12:06 -0500178any of the following to enable the corresponding ARM feature, but using the ACLE
179symbols via `-march` is recommended.
Adam Langley6a7cfbe2015-10-16 15:46:46 -0700180
David Benjamin3b33f3e2017-06-08 16:53:28 -0400181 * `OPENSSL_STATIC_ARMCAP_NEON`
Adam Langley6a7cfbe2015-10-16 15:46:46 -0700182 * `OPENSSL_STATIC_ARMCAP_AES`
183 * `OPENSSL_STATIC_ARMCAP_SHA1`
184 * `OPENSSL_STATIC_ARMCAP_SHA256`
185 * `OPENSSL_STATIC_ARMCAP_PMULL`
186
David Benjamin1e156822021-12-27 13:27:22 -0500187The resulting binary will assume all such features are always present. This can
188reduce code size, by allowing the compiler to omit fallbacks. However, if the
189feature is not actually supported at runtime, BoringSSL will likely crash.
190
191BoringSSL will additionally query the operating system at runtime for additional
192features, e.g. with `getauxval` on Linux. This allows a single binary to use
193newer instructions when present, but still function on CPUs without them. But
194some environments don't support runtime queries. If building for those, define
195`OPENSSL_STATIC_ARMCAP` to limit BoringSSL to compile-time capabilities. If not
196defined, the target operating system must be known to BoringSSL.
Adam Langley6a7cfbe2015-10-16 15:46:46 -0700197
David Benjamin6291af42018-03-23 13:49:27 -0400198## Binary Size
199
200The implementations of some algorithms require a trade-off between binary size
201and performance. For instance, BoringSSL's fastest P-256 implementation uses a
202148 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 Langleydc7e9c42015-09-29 15:21:04 -0700206
207There are two sets of tests: the C/C++ tests and the blackbox tests. For former
208are built by Ninja and can be run from the top-level directory with `go run
209util/all_tests.go`. The latter have to be run separately by running `go test`
210from within `ssl/test/runner`.
211
David Benjamin301afaf2015-10-14 21:34:40 -0400212Both sets of tests may also be run with `ninja -C build run_tests`, but CMake
2133.2 or later is required to avoid Ninja's output buffering.