xref: /aosp_15_r20/external/boringssl/src/BUILDING.md (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1*8fb009dcSAndroid Build Coastguard Worker# Building BoringSSL
2*8fb009dcSAndroid Build Coastguard Worker
3*8fb009dcSAndroid Build Coastguard Worker## Checking out BoringSSL
4*8fb009dcSAndroid Build Coastguard Worker
5*8fb009dcSAndroid Build Coastguard Worker    git clone "https://boringssl.googlesource.com/boringssl"
6*8fb009dcSAndroid Build Coastguard Worker
7*8fb009dcSAndroid Build Coastguard Worker## Build Prerequisites
8*8fb009dcSAndroid Build Coastguard Worker
9*8fb009dcSAndroid Build Coastguard WorkerThe standalone CMake build is primarily intended for developers. If embedding
10*8fb009dcSAndroid Build Coastguard WorkerBoringSSL into another project with a pre-existing build system, see
11*8fb009dcSAndroid Build Coastguard Worker[INCORPORATING.md](./INCORPORATING.md).
12*8fb009dcSAndroid Build Coastguard Worker
13*8fb009dcSAndroid Build Coastguard WorkerUnless otherwise noted, build tools must at most five years old, matching
14*8fb009dcSAndroid Build Coastguard Worker[Abseil guidelines](https://abseil.io/about/compatibility). If in doubt, use the
15*8fb009dcSAndroid Build Coastguard Workermost recent stable version of each tool.
16*8fb009dcSAndroid Build Coastguard Worker
17*8fb009dcSAndroid Build Coastguard Worker  * [CMake](https://cmake.org/download/) 3.12 or later is required.
18*8fb009dcSAndroid Build Coastguard Worker
19*8fb009dcSAndroid Build Coastguard Worker  * Building with [Ninja](https://ninja-build.org/) instead of Make is
20*8fb009dcSAndroid Build Coastguard Worker    recommended, because it makes builds faster. On Windows, CMake's Visual
21*8fb009dcSAndroid Build Coastguard Worker    Studio generator may also work, but it not tested regularly and requires
22*8fb009dcSAndroid Build Coastguard Worker    recent versions of CMake for assembly support.
23*8fb009dcSAndroid Build Coastguard Worker
24*8fb009dcSAndroid Build Coastguard Worker  * On Windows only, [NASM](https://www.nasm.us/) is required. If not found
25*8fb009dcSAndroid Build Coastguard Worker    by CMake, it may be configured explicitly by setting
26*8fb009dcSAndroid Build Coastguard Worker    `CMAKE_ASM_NASM_COMPILER`.
27*8fb009dcSAndroid Build Coastguard Worker
28*8fb009dcSAndroid Build Coastguard Worker  * Compilers for C11 and C++14, or later, are required. On Windows, MSVC from
29*8fb009dcSAndroid Build Coastguard Worker    Visual Studio 2019 or later with Windows 10 SDK 2104 or later are
30*8fb009dcSAndroid Build Coastguard Worker    supported, but using the latest versions is recommended. Recent versions of
31*8fb009dcSAndroid Build Coastguard Worker    GCC (6.1+) and Clang should work on non-Windows platforms, and maybe on
32*8fb009dcSAndroid Build Coastguard Worker    Windows too.
33*8fb009dcSAndroid Build Coastguard Worker
34*8fb009dcSAndroid Build Coastguard Worker## Building
35*8fb009dcSAndroid Build Coastguard Worker
36*8fb009dcSAndroid Build Coastguard WorkerUsing Ninja (note the 'N' is capitalized in the cmake invocation):
37*8fb009dcSAndroid Build Coastguard Worker
38*8fb009dcSAndroid Build Coastguard Worker    cmake -GNinja -B build
39*8fb009dcSAndroid Build Coastguard Worker    ninja -C build
40*8fb009dcSAndroid Build Coastguard Worker
41*8fb009dcSAndroid Build Coastguard WorkerUsing Make (does not work on Windows):
42*8fb009dcSAndroid Build Coastguard Worker
43*8fb009dcSAndroid Build Coastguard Worker    cmake -B build
44*8fb009dcSAndroid Build Coastguard Worker    make -C build
45*8fb009dcSAndroid Build Coastguard Worker
46*8fb009dcSAndroid Build Coastguard WorkerThis produces a debug build by default. Optimisation isn't enabled, and debug
47*8fb009dcSAndroid Build Coastguard Workerassertions are included. Pass `-DCMAKE_BUILD_TYPE=Release` to `cmake` to
48*8fb009dcSAndroid Build Coastguard Workerconfigure a release build:
49*8fb009dcSAndroid Build Coastguard Worker
50*8fb009dcSAndroid Build Coastguard Worker    cmake -GNinja -B build -DCMAKE_BUILD_TYPE=Release
51*8fb009dcSAndroid Build Coastguard Worker    ninja -C build
52*8fb009dcSAndroid Build Coastguard Worker
53*8fb009dcSAndroid Build Coastguard WorkerIf you want to cross-compile then there is an example toolchain file for 32-bit
54*8fb009dcSAndroid Build Coastguard WorkerIntel in `util/`. Wipe out the build directory, run `cmake` like this:
55*8fb009dcSAndroid Build Coastguard Worker
56*8fb009dcSAndroid Build Coastguard Worker    cmake -B build -DCMAKE_TOOLCHAIN_FILE=../util/32-bit-toolchain.cmake -GNinja
57*8fb009dcSAndroid Build Coastguard Worker
58*8fb009dcSAndroid Build Coastguard WorkerIf you want to build as a shared library, pass `-DBUILD_SHARED_LIBS=1`. On
59*8fb009dcSAndroid Build Coastguard WorkerWindows, where functions need to be tagged with `dllimport` when coming from a
60*8fb009dcSAndroid Build Coastguard Workershared library, define `BORINGSSL_SHARED_LIBRARY` in any code which `#include`s
61*8fb009dcSAndroid Build Coastguard Workerthe BoringSSL headers.
62*8fb009dcSAndroid Build Coastguard Worker
63*8fb009dcSAndroid Build Coastguard WorkerIn order to serve environments where code-size is important as well as those
64*8fb009dcSAndroid Build Coastguard Workerwhere performance is the overriding concern, `OPENSSL_SMALL` can be defined to
65*8fb009dcSAndroid Build Coastguard Workerremove some code that is especially large.
66*8fb009dcSAndroid Build Coastguard Worker
67*8fb009dcSAndroid Build Coastguard WorkerSee [CMake's documentation](https://cmake.org/cmake/help/v3.4/manual/cmake-variables.7.html)
68*8fb009dcSAndroid Build Coastguard Workerfor other variables which may be used to configure the build.
69*8fb009dcSAndroid Build Coastguard Worker
70*8fb009dcSAndroid Build Coastguard WorkerYou usually don't need to run `cmake` again after changing `CMakeLists.txt`
71*8fb009dcSAndroid Build Coastguard Workerfiles because the build scripts will detect changes to them and rebuild
72*8fb009dcSAndroid Build Coastguard Workerthemselves automatically.
73*8fb009dcSAndroid Build Coastguard Worker
74*8fb009dcSAndroid Build Coastguard Worker### Building for Android
75*8fb009dcSAndroid Build Coastguard Worker
76*8fb009dcSAndroid Build Coastguard WorkerIt's possible to build BoringSSL with the Android NDK using CMake. Recent
77*8fb009dcSAndroid Build Coastguard Workerversions of the NDK include a CMake toolchain file which works with CMake 3.6.0
78*8fb009dcSAndroid Build Coastguard Workeror later. This has been tested with version r16b of the NDK.
79*8fb009dcSAndroid Build Coastguard Worker
80*8fb009dcSAndroid Build Coastguard WorkerUnpack the Android NDK somewhere and export `ANDROID_NDK` to point to the
81*8fb009dcSAndroid Build Coastguard Workerdirectory. Then run CMake like this:
82*8fb009dcSAndroid Build Coastguard Worker
83*8fb009dcSAndroid Build Coastguard Worker    cmake -DANDROID_ABI=armeabi-v7a \
84*8fb009dcSAndroid Build Coastguard Worker          -DANDROID_PLATFORM=android-19 \
85*8fb009dcSAndroid Build Coastguard Worker          -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
86*8fb009dcSAndroid Build Coastguard Worker          -GNinja -B build
87*8fb009dcSAndroid Build Coastguard Worker
88*8fb009dcSAndroid Build Coastguard WorkerOnce you've run that, Ninja should produce Android-compatible binaries.  You
89*8fb009dcSAndroid Build Coastguard Workercan replace `armeabi-v7a` in the above with `arm64-v8a` and use API level 21 or
90*8fb009dcSAndroid Build Coastguard Workerhigher to build aarch64 binaries.
91*8fb009dcSAndroid Build Coastguard Worker
92*8fb009dcSAndroid Build Coastguard WorkerFor other options, see the documentation in the toolchain file.
93*8fb009dcSAndroid Build Coastguard Worker
94*8fb009dcSAndroid Build Coastguard WorkerTo debug the resulting binaries on an Android device with `gdb`, run the
95*8fb009dcSAndroid Build Coastguard Workercommands below. Replace `ARCH` with the architecture of the target device, e.g.
96*8fb009dcSAndroid Build Coastguard Worker`arm` or `arm64`.
97*8fb009dcSAndroid Build Coastguard Worker
98*8fb009dcSAndroid Build Coastguard Worker    adb push ${ANDROID_NDK}/prebuilt/android-ARCH/gdbserver/gdbserver \
99*8fb009dcSAndroid Build Coastguard Worker        /data/local/tmp
100*8fb009dcSAndroid Build Coastguard Worker    adb forward tcp:5039 tcp:5039
101*8fb009dcSAndroid Build Coastguard Worker    adb shell /data/local/tmp/gdbserver :5039 /path/on/device/to/binary
102*8fb009dcSAndroid Build Coastguard Worker
103*8fb009dcSAndroid Build Coastguard WorkerThen run the following in a separate shell. Replace `HOST` with the OS and
104*8fb009dcSAndroid Build Coastguard Workerarchitecture of the host machine, e.g. `linux-x86_64`.
105*8fb009dcSAndroid Build Coastguard Worker
106*8fb009dcSAndroid Build Coastguard Worker    ${ANDROID_NDK}/prebuilt/HOST/bin/gdb
107*8fb009dcSAndroid Build Coastguard Worker    target remote :5039  # in gdb
108*8fb009dcSAndroid Build Coastguard Worker
109*8fb009dcSAndroid Build Coastguard Worker### Building for iOS
110*8fb009dcSAndroid Build Coastguard Worker
111*8fb009dcSAndroid Build Coastguard WorkerTo build for iOS, pass `-DCMAKE_OSX_SYSROOT=iphoneos` and
112*8fb009dcSAndroid Build Coastguard Worker`-DCMAKE_OSX_ARCHITECTURES=ARCH` to CMake, where `ARCH` is the desired
113*8fb009dcSAndroid Build Coastguard Workerarchitecture, matching values used in the `-arch` flag in Apple's toolchain.
114*8fb009dcSAndroid Build Coastguard Worker
115*8fb009dcSAndroid Build Coastguard WorkerPassing multiple architectures for a multiple-architecture build is not
116*8fb009dcSAndroid Build Coastguard Workersupported.
117*8fb009dcSAndroid Build Coastguard Worker
118*8fb009dcSAndroid Build Coastguard Worker### Building with Prefixed Symbols
119*8fb009dcSAndroid Build Coastguard Worker
120*8fb009dcSAndroid Build Coastguard WorkerBoringSSL's build system has experimental support for adding a custom prefix to
121*8fb009dcSAndroid Build Coastguard Workerall symbols. This can be useful when linking multiple versions of BoringSSL in
122*8fb009dcSAndroid Build Coastguard Workerthe same project to avoid symbol conflicts. Symbol prefixing requires the most
123*8fb009dcSAndroid Build Coastguard Workerrecent stable version of [Go](https://go.dev/).
124*8fb009dcSAndroid Build Coastguard Worker
125*8fb009dcSAndroid Build Coastguard WorkerIn order to build with prefixed symbols, the `BORINGSSL_PREFIX` CMake variable
126*8fb009dcSAndroid Build Coastguard Workershould specify the prefix to add to all symbols, and the
127*8fb009dcSAndroid Build Coastguard Worker`BORINGSSL_PREFIX_SYMBOLS` CMake variable should specify the path to a file
128*8fb009dcSAndroid Build Coastguard Workerwhich contains a list of symbols which should be prefixed (one per line;
129*8fb009dcSAndroid Build Coastguard Workercomments are supported with `#`). In other words, `cmake -B build
130*8fb009dcSAndroid Build Coastguard Worker-DBORINGSSL_PREFIX=MY_CUSTOM_PREFIX
131*8fb009dcSAndroid Build Coastguard Worker-DBORINGSSL_PREFIX_SYMBOLS=/path/to/symbols.txt` will configure the build to add
132*8fb009dcSAndroid Build Coastguard Workerthe prefix `MY_CUSTOM_PREFIX` to all of the symbols listed in
133*8fb009dcSAndroid Build Coastguard Worker`/path/to/symbols.txt`.
134*8fb009dcSAndroid Build Coastguard Worker
135*8fb009dcSAndroid Build Coastguard WorkerIt is currently the caller's responsibility to create and maintain the list of
136*8fb009dcSAndroid Build Coastguard Workersymbols to be prefixed. Alternatively, `util/read_symbols.go` reads the list of
137*8fb009dcSAndroid Build Coastguard Workerexported symbols from a `.a` file, and can be used in a build script to generate
138*8fb009dcSAndroid Build Coastguard Workerthe symbol list on the fly (by building without prefixing, using
139*8fb009dcSAndroid Build Coastguard Worker`read_symbols.go` to construct a symbol list, and then building again with
140*8fb009dcSAndroid Build Coastguard Workerprefixing).
141*8fb009dcSAndroid Build Coastguard Worker
142*8fb009dcSAndroid Build Coastguard WorkerThis mechanism is under development and may change over time. Please contact the
143*8fb009dcSAndroid Build Coastguard WorkerBoringSSL maintainers if making use of it.
144*8fb009dcSAndroid Build Coastguard Worker
145*8fb009dcSAndroid Build Coastguard Worker## Known Limitations on Windows
146*8fb009dcSAndroid Build Coastguard Worker
147*8fb009dcSAndroid Build Coastguard Worker  * CMake can generate Visual Studio projects, but the generated project files
148*8fb009dcSAndroid Build Coastguard Worker    don't have steps for assembling the assembly language source files, so they
149*8fb009dcSAndroid Build Coastguard Worker    currently cannot be used to build BoringSSL.
150*8fb009dcSAndroid Build Coastguard Worker
151*8fb009dcSAndroid Build Coastguard Worker## ARM CPU Capabilities
152*8fb009dcSAndroid Build Coastguard Worker
153*8fb009dcSAndroid Build Coastguard WorkerARM, unlike Intel, does not have a userspace instruction that allows
154*8fb009dcSAndroid Build Coastguard Workerapplications to discover the capabilities of the processor. Instead, the
155*8fb009dcSAndroid Build Coastguard Workercapability information has to be provided by a combination of compile-time
156*8fb009dcSAndroid Build Coastguard Workerinformation and the operating system.
157*8fb009dcSAndroid Build Coastguard Worker
158*8fb009dcSAndroid Build Coastguard WorkerBoringSSL determines capabilities at compile-time based on `__ARM_NEON`,
159*8fb009dcSAndroid Build Coastguard Worker`__ARM_FEATURE_AES`, and other preprocessor symbols defined in
160*8fb009dcSAndroid Build Coastguard Worker[Arm C Language Extensions (ACLE)](https://developer.arm.com/architectures/system-architectures/software-standards/acle).
161*8fb009dcSAndroid Build Coastguard WorkerThese values are usually controlled by the `-march` flag. You can also define
162*8fb009dcSAndroid Build Coastguard Workerany of the following to enable the corresponding ARM feature, but using the ACLE
163*8fb009dcSAndroid Build Coastguard Workersymbols via `-march` is recommended.
164*8fb009dcSAndroid Build Coastguard Worker
165*8fb009dcSAndroid Build Coastguard Worker  * `OPENSSL_STATIC_ARMCAP_NEON`
166*8fb009dcSAndroid Build Coastguard Worker  * `OPENSSL_STATIC_ARMCAP_AES`
167*8fb009dcSAndroid Build Coastguard Worker  * `OPENSSL_STATIC_ARMCAP_SHA1`
168*8fb009dcSAndroid Build Coastguard Worker  * `OPENSSL_STATIC_ARMCAP_SHA256`
169*8fb009dcSAndroid Build Coastguard Worker  * `OPENSSL_STATIC_ARMCAP_PMULL`
170*8fb009dcSAndroid Build Coastguard Worker
171*8fb009dcSAndroid Build Coastguard WorkerThe resulting binary will assume all such features are always present. This can
172*8fb009dcSAndroid Build Coastguard Workerreduce code size, by allowing the compiler to omit fallbacks. However, if the
173*8fb009dcSAndroid Build Coastguard Workerfeature is not actually supported at runtime, BoringSSL will likely crash.
174*8fb009dcSAndroid Build Coastguard Worker
175*8fb009dcSAndroid Build Coastguard WorkerBoringSSL will additionally query the operating system at runtime for additional
176*8fb009dcSAndroid Build Coastguard Workerfeatures, e.g. with `getauxval` on Linux. This allows a single binary to use
177*8fb009dcSAndroid Build Coastguard Workernewer instructions when present, but still function on CPUs without them. But
178*8fb009dcSAndroid Build Coastguard Workersome environments don't support runtime queries. If building for those, define
179*8fb009dcSAndroid Build Coastguard Worker`OPENSSL_STATIC_ARMCAP` to limit BoringSSL to compile-time capabilities. If not
180*8fb009dcSAndroid Build Coastguard Workerdefined, the target operating system must be known to BoringSSL.
181*8fb009dcSAndroid Build Coastguard Worker
182*8fb009dcSAndroid Build Coastguard Worker## Binary Size
183*8fb009dcSAndroid Build Coastguard Worker
184*8fb009dcSAndroid Build Coastguard WorkerThe implementations of some algorithms require a trade-off between binary size
185*8fb009dcSAndroid Build Coastguard Workerand performance. For instance, BoringSSL's fastest P-256 implementation uses a
186*8fb009dcSAndroid Build Coastguard Worker148 KiB pre-computed table. To optimize instead for binary size, pass
187*8fb009dcSAndroid Build Coastguard Worker`-DOPENSSL_SMALL=1` to CMake or define the `OPENSSL_SMALL` preprocessor symbol.
188*8fb009dcSAndroid Build Coastguard Worker
189*8fb009dcSAndroid Build Coastguard Worker# Running Tests
190*8fb009dcSAndroid Build Coastguard Worker
191*8fb009dcSAndroid Build Coastguard WorkerThere are two additional dependencies for running tests:
192*8fb009dcSAndroid Build Coastguard Worker
193*8fb009dcSAndroid Build Coastguard Worker  * The most recent stable version of [Go](https://go.dev/) is required.
194*8fb009dcSAndroid Build Coastguard Worker    Note Go is exempt from the five year support window. If not found by CMake,
195*8fb009dcSAndroid Build Coastguard Worker    the go executable may be configured explicitly by setting `GO_EXECUTABLE`.
196*8fb009dcSAndroid Build Coastguard Worker
197*8fb009dcSAndroid Build Coastguard Worker  * On x86_64 Linux, the tests have an optional
198*8fb009dcSAndroid Build Coastguard Worker    [libunwind](https://www.nongnu.org/libunwind/) dependency to test the
199*8fb009dcSAndroid Build Coastguard Worker    assembly more thoroughly.
200*8fb009dcSAndroid Build Coastguard Worker
201*8fb009dcSAndroid Build Coastguard WorkerThere are two sets of tests: the C/C++ tests and the blackbox tests. For former
202*8fb009dcSAndroid Build Coastguard Workerare built by Ninja and can be run from the top-level directory with `go run
203*8fb009dcSAndroid Build Coastguard Workerutil/all_tests.go`. The latter have to be run separately by running `go test`
204*8fb009dcSAndroid Build Coastguard Workerfrom within `ssl/test/runner`.
205*8fb009dcSAndroid Build Coastguard Worker
206*8fb009dcSAndroid Build Coastguard WorkerBoth sets of tests may also be run with `ninja -C build run_tests`, but CMake
207*8fb009dcSAndroid Build Coastguard Worker3.2 or later is required to avoid Ninja's output buffering.
208*8fb009dcSAndroid Build Coastguard Worker
209*8fb009dcSAndroid Build Coastguard Worker# Pre-generated Files
210*8fb009dcSAndroid Build Coastguard Worker
211*8fb009dcSAndroid Build Coastguard WorkerIf modifying perlasm files, or `util/pregenerate/build.json`, you will need to
212*8fb009dcSAndroid Build Coastguard Workerrun `go run ./util/pregenerate` to refresh some pre-generated files. To do this,
213*8fb009dcSAndroid Build Coastguard Workeryou will need a recent version of Perl.
214*8fb009dcSAndroid Build Coastguard Worker
215*8fb009dcSAndroid Build Coastguard WorkerOn Windows, [Active State Perl](http://www.activestate.com/activeperl/) has been
216*8fb009dcSAndroid Build Coastguard Workerreported to work, as has MSYS Perl.
217*8fb009dcSAndroid Build Coastguard Worker[Strawberry Perl](http://strawberryperl.com/) also works but it adds GCC
218*8fb009dcSAndroid Build Coastguard Workerto `PATH`, which can confuse some build tools when identifying the compiler
219*8fb009dcSAndroid Build Coastguard Worker(removing `C:\Strawberry\c\bin` from `PATH` should resolve any problems).
220*8fb009dcSAndroid Build Coastguard Worker
221*8fb009dcSAndroid Build Coastguard WorkerSee (gen/README.md)[./gen/README.md] for more details.
222