blob: b1ccf458d56c8048c408dba09e83c73e67bbd37f [file] [log] [blame] [view]
David Benjamin95aaf4a2015-09-03 12:09:36 -04001# Building BoringSSL
2
3## Build Prerequisites
4
David Benjamin6f9f4cc2018-12-21 16:05:09 -06005The standalone CMake build is primarily intended for developers. If embedding
6BoringSSL into another project with a pre-existing build system, see
7[INCORPORATING.md](/INCORPORATING.md).
David Benjamin95aaf4a2015-09-03 12:09:36 -04008
David Benjamin6f9f4cc2018-12-21 16:05:09 -06009Unless otherwise noted, build tools must at most five years old, matching
10[Abseil guidelines](https://abseil.io/about/compatibility). If in doubt, use the
11most recent stable version of each tool.
12
David Benjamin39707fe2022-11-21 15:23:17 -050013 * [CMake](https://cmake.org/download/) 3.10 or later is required.
David Benjamin6f9f4cc2018-12-21 16:05:09 -060014
15 * A recent version of Perl is required. On Windows,
nmittler042e8f72016-02-09 11:25:52 -080016 [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 Benjamin95aaf4a2015-09-03 12:09:36 -040023
David Benjamin6f9f4cc2018-12-21 16:05:09 -060024 * 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 Benjamin95aaf4a2015-09-03 12:09:36 -040028
David Benjamin73d69f42018-11-13 17:49:42 -060029 * On Windows only, [NASM](https://www.nasm.us/) is required. If not found
Brian Smith953cfc82015-10-06 12:51:38 -100030 by CMake, it may be configured explicitly by setting
31 `CMAKE_ASM_NASM_COMPILER`.
David Benjamin95aaf4a2015-09-03 12:09:36 -040032
David Benjaminf961de52022-04-18 17:39:00 -040033 * 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 Benjamin21440762022-03-19 18:39:18 -040035 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 Benjamin95aaf4a2015-09-03 12:09:36 -040038
David Benjamin0990a552018-09-07 14:51:08 -050039 * The most recent stable version of [Go](https://golang.org/dl/) is required.
David Benjamin6f9f4cc2018-12-21 16:05:09 -060040 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 Benjaminf6a74c62016-06-10 13:12:20 -040042
David Benjamin17d553d2018-12-21 17:58:36 -060043 * 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 Benjamin95aaf4a2015-09-03 12:09:36 -040047## Building
48
49Using Ninja (note the 'N' is capitalized in the cmake invocation):
50
51 mkdir build
52 cd build
53 cmake -GNinja ..
54 ninja
55
56Using Make (does not work on Windows):
57
58 mkdir build
59 cd build
60 cmake ..
61 make
62
63You usually don't need to run `cmake` again after changing `CMakeLists.txt`
64files because the build scripts will detect changes to them and rebuild
65themselves automatically.
66
67Note that the default build flags in the top-level `CMakeLists.txt` are for
nmittler042e8f72016-02-09 11:25:52 -080068debuggingoptimisation isn't enabled. Pass `-DCMAKE_BUILD_TYPE=Release` to
69`cmake` to configure a release build.
David Benjamin95aaf4a2015-09-03 12:09:36 -040070
71If you want to cross-compile then there is an example toolchain file for 32-bit
72Intel in `util/`. Wipe out the build directory, recreate it and run `cmake` like
73this:
74
75 cmake -DCMAKE_TOOLCHAIN_FILE=../util/32-bit-toolchain.cmake -GNinja ..
76
77If you want to build as a shared library, pass `-DBUILD_SHARED_LIBS=1`. On
78Windows, where functions need to be tagged with `dllimport` when coming from a
79shared library, define `BORINGSSL_SHARED_LIBRARY` in any code which `#include`s
80the BoringSSL headers.
81
Adam Langley2e3c9782015-10-27 08:47:11 -070082In order to serve environments where code-size is important as well as those
83where performance is the overriding concern, `OPENSSL_SMALL` can be defined to
84remove some code that is especially large.
85
nmittler042e8f72016-02-09 11:25:52 -080086See [CMake's documentation](https://cmake.org/cmake/help/v3.4/manual/cmake-variables.7.html)
87for other variables which may be used to configure the build.
88
David Benjamin95aaf4a2015-09-03 12:09:36 -040089### Building for Android
90
David Benjamin52887792017-12-13 18:18:28 -050091It's possible to build BoringSSL with the Android NDK using CMake. Recent
92versions of the NDK include a CMake toolchain file which works with CMake 3.6.0
93or later. This has been tested with version r16b of the NDK.
David Benjamin95aaf4a2015-09-03 12:09:36 -040094
95Unpack the Android NDK somewhere and export `ANDROID_NDK` to point to the
David Benjamin75021b72016-04-28 14:51:36 -040096directory. Then make a build directory as above and run CMake like this:
David Benjamin95aaf4a2015-09-03 12:09:36 -040097
David Benjamin75021b72016-04-28 14:51:36 -040098 cmake -DANDROID_ABI=armeabi-v7a \
David Benjamin52887792017-12-13 18:18:28 -050099 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
David Benjamin95aaf4a2015-09-03 12:09:36 -0400100 -DANDROID_NATIVE_API_LEVEL=16 \
101 -GNinja ..
102
David Benjamin75021b72016-04-28 14:51:36 -0400103Once you've run that, Ninja should produce Android-compatible binaries. You
104can replace `armeabi-v7a` in the above with `arm64-v8a` and use API level 21 or
105higher to build aarch64 binaries.
106
David Benjamin52887792017-12-13 18:18:28 -0500107For other options, see the documentation in the toolchain file.
David Benjamin95aaf4a2015-09-03 12:09:36 -0400108
David Benjamin9978f0a2019-01-30 16:56:54 -0600109To debug the resulting binaries on an Android device with `gdb`, run the
110commands 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
118Then run the following in a separate shell. Replace `HOST` with the OS and
119architecture 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 Benjaminaff72a32017-04-06 23:26:04 -0400124### Building for iOS
125
126To build for iOS, pass `-DCMAKE_OSX_SYSROOT=iphoneos` and
127`-DCMAKE_OSX_ARCHITECTURES=ARCH` to CMake, where `ARCH` is the desired
128architecture, matching values used in the `-arch` flag in Apple's toolchain.
129
130Passing multiple architectures for a multiple-architecture build is not
131supported.
132
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700133### Building with Prefixed Symbols
134
135BoringSSL's build system has experimental support for adding a custom prefix to
136all symbols. This can be useful when linking multiple versions of BoringSSL in
137the same project to avoid symbol conflicts.
138
139In order to build with prefixed symbols, the `BORINGSSL_PREFIX` CMake variable
140should specify the prefix to add to all symbols, and the
141`BORINGSSL_PREFIX_SYMBOLS` CMake variable should specify the path to a file
142which contains a list of symbols which should be prefixed (one per line;
143comments 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
146the prefix `MY_CUSTOM_PREFIX` to all of the symbols listed in
147`/path/to/symbols.txt`.
148
149It is currently the caller's responsibility to create and maintain the list of
Joshua Liebow-Feeser066b1082018-09-17 15:40:24 -0700150symbols to be prefixed. Alternatively, `util/read_symbols.go` reads the list of
151exported symbols from a `.a` file, and can be used in a build script to generate
152the symbol list on the fly (by building without prefixing, using
153`read_symbols.go` to construct a symbol list, and then building again with
154prefixing).
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700155
156This mechanism is under development and may change over time. Please contact the
157BoringSSL maintainers if making use of it.
158
David Benjamin95aaf4a2015-09-03 12:09:36 -0400159## Known Limitations on Windows
160
David Benjamin95aaf4a2015-09-03 12:09:36 -0400161 * 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 Benjamin1e156822021-12-27 13:27:22 -0500165## ARM CPU Capabilities
Adam Langley6a7cfbe2015-10-16 15:46:46 -0700166
David Benjamin1e156822021-12-27 13:27:22 -0500167ARM, unlike Intel, does not have a userspace instruction that allows
168applications to discover the capabilities of the processor. Instead, the
169capability information has to be provided by a combination of compile-time
170information and the operating system.
Adam Langley6a7cfbe2015-10-16 15:46:46 -0700171
David Benjamin846a2272022-01-06 11:12:06 -0500172BoringSSL 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 Benjamin1e156822021-12-27 13:27:22 -0500175These values are usually controlled by the `-march` flag. You can also define
David Benjamin846a2272022-01-06 11:12:06 -0500176any of the following to enable the corresponding ARM feature, but using the ACLE
177symbols via `-march` is recommended.
Adam Langley6a7cfbe2015-10-16 15:46:46 -0700178
David Benjamin3b33f3e2017-06-08 16:53:28 -0400179 * `OPENSSL_STATIC_ARMCAP_NEON`
Adam Langley6a7cfbe2015-10-16 15:46:46 -0700180 * `OPENSSL_STATIC_ARMCAP_AES`
181 * `OPENSSL_STATIC_ARMCAP_SHA1`
182 * `OPENSSL_STATIC_ARMCAP_SHA256`
183 * `OPENSSL_STATIC_ARMCAP_PMULL`
184
David Benjamin1e156822021-12-27 13:27:22 -0500185The resulting binary will assume all such features are always present. This can
186reduce code size, by allowing the compiler to omit fallbacks. However, if the
187feature is not actually supported at runtime, BoringSSL will likely crash.
188
189BoringSSL will additionally query the operating system at runtime for additional
190features, e.g. with `getauxval` on Linux. This allows a single binary to use
191newer instructions when present, but still function on CPUs without them. But
192some environments don't support runtime queries. If building for those, define
193`OPENSSL_STATIC_ARMCAP` to limit BoringSSL to compile-time capabilities. If not
194defined, the target operating system must be known to BoringSSL.
Adam Langley6a7cfbe2015-10-16 15:46:46 -0700195
David Benjamin6291af42018-03-23 13:49:27 -0400196## Binary Size
197
198The implementations of some algorithms require a trade-off between binary size
199and performance. For instance, BoringSSL's fastest P-256 implementation uses a
200148 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 Langleydc7e9c42015-09-29 15:21:04 -0700204
205There are two sets of tests: the C/C++ tests and the blackbox tests. For former
206are built by Ninja and can be run from the top-level directory with `go run
207util/all_tests.go`. The latter have to be run separately by running `go test`
208from within `ssl/test/runner`.
209
David Benjamin301afaf2015-10-14 21:34:40 -0400210Both sets of tests may also be run with `ninja -C build run_tests`, but CMake
2113.2 or later is required to avoid Ninja's output buffering.