1*8fb009dcSAndroid Build Coastguard Worker# Incorporating BoringSSL into a project 2*8fb009dcSAndroid Build Coastguard Worker 3*8fb009dcSAndroid Build Coastguard Worker**Note**: if your target project is not a Google project then first read the 4*8fb009dcSAndroid Build Coastguard Worker[main README](./README.md) about the purpose of BoringSSL. 5*8fb009dcSAndroid Build Coastguard Worker 6*8fb009dcSAndroid Build Coastguard WorkerIf you are porting BoringSSL to a new platform see 7*8fb009dcSAndroid Build Coastguard Worker["go/boringssl-on-new-platform"](https://goto.corp.google.com/boringssl-on-new-platform) (Google 8*8fb009dcSAndroid Build Coastguard WorkerInternal) for information about porting BoringSSL to a new platform for a Google 9*8fb009dcSAndroid Build Coastguard Workerproject. 10*8fb009dcSAndroid Build Coastguard Worker 11*8fb009dcSAndroid Build Coastguard Worker## Which branch to use 12*8fb009dcSAndroid Build Coastguard Worker 13*8fb009dcSAndroid Build Coastguard WorkerBoringSSL usage typically follows a 14*8fb009dcSAndroid Build Coastguard Worker["live at head"](https://abseil.io/about/philosophy#we-recommend-that-you-choose-to-live-at-head) 15*8fb009dcSAndroid Build Coastguard Workermodel. Projects pin to whatever the current latest of BoringSSL is at the time 16*8fb009dcSAndroid Build Coastguard Workerof update, and regularly update it to pick up new changes. 17*8fb009dcSAndroid Build Coastguard Worker 18*8fb009dcSAndroid Build Coastguard WorkerWhile the BoringSSL repository may contain project-specific branches, e.g. 19*8fb009dcSAndroid Build Coastguard Worker`chromium-2214`, those are _not_ supported release branches and must not as 20*8fb009dcSAndroid Build Coastguard Workersuch. In rare cases, BoringSSL will temporarily maintain a short-lived branch on 21*8fb009dcSAndroid Build Coastguard Workerbehalf of a project. Most such branches are no longer updated, because the 22*8fb009dcSAndroid Build Coastguard Workercorresponding project no longer needs them, and we do not create new ones to 23*8fb009dcSAndroid Build Coastguard Workerreplace the ones that are no longer updated. E.g., not every Chromium release 24*8fb009dcSAndroid Build Coastguard Workerbranch has a corresponding BoringSSL `chromium-*` branch. Even while active, the 25*8fb009dcSAndroid Build Coastguard Workerbranch may not contain all changes relevant to a general BoringSSL consumer. 26*8fb009dcSAndroid Build Coastguard Worker 27*8fb009dcSAndroid Build Coastguard Worker## Bazel 28*8fb009dcSAndroid Build Coastguard Worker 29*8fb009dcSAndroid Build Coastguard WorkerIf you are using [Bazel](https://bazel.build) then you can incorporate 30*8fb009dcSAndroid Build Coastguard WorkerBoringSSL as an external repository by using a commit from the 31*8fb009dcSAndroid Build Coastguard Worker`master-with-bazel` branch. That branch is maintained by a bot from `master` 32*8fb009dcSAndroid Build Coastguard Workerand includes the needed generated files and a top-level BUILD file. 33*8fb009dcSAndroid Build Coastguard Worker 34*8fb009dcSAndroid Build Coastguard WorkerFor example: 35*8fb009dcSAndroid Build Coastguard Worker 36*8fb009dcSAndroid Build Coastguard Worker git_repository( 37*8fb009dcSAndroid Build Coastguard Worker name = "boringssl", 38*8fb009dcSAndroid Build Coastguard Worker commit = "_some commit_", 39*8fb009dcSAndroid Build Coastguard Worker remote = "https://boringssl.googlesource.com/boringssl", 40*8fb009dcSAndroid Build Coastguard Worker ) 41*8fb009dcSAndroid Build Coastguard Worker 42*8fb009dcSAndroid Build Coastguard WorkerYou would still need to keep the referenced commit up to date if a specific 43*8fb009dcSAndroid Build Coastguard Workercommit is referred to. 44*8fb009dcSAndroid Build Coastguard Worker 45*8fb009dcSAndroid Build Coastguard Worker## Directory layout 46*8fb009dcSAndroid Build Coastguard Worker 47*8fb009dcSAndroid Build Coastguard WorkerTypically projects create a `third_party/boringssl` directory to put 48*8fb009dcSAndroid Build Coastguard WorkerBoringSSL-specific files into. The source code of BoringSSL itself goes into 49*8fb009dcSAndroid Build Coastguard Worker`third_party/boringssl/src`, either by copying or as a 50*8fb009dcSAndroid Build Coastguard Worker[submodule](https://git-scm.com/docs/git-submodule). 51*8fb009dcSAndroid Build Coastguard Worker 52*8fb009dcSAndroid Build Coastguard WorkerIt's generally a mistake to put BoringSSL's source code into 53*8fb009dcSAndroid Build Coastguard Worker`third_party/boringssl` directly because pre-built files and custom build files 54*8fb009dcSAndroid Build Coastguard Workerneed to go somewhere and merging these with the BoringSSL source code makes 55*8fb009dcSAndroid Build Coastguard Workerupdating things more complex. 56*8fb009dcSAndroid Build Coastguard Worker 57*8fb009dcSAndroid Build Coastguard Worker## Build support 58*8fb009dcSAndroid Build Coastguard Worker 59*8fb009dcSAndroid Build Coastguard WorkerBoringSSL is designed to work with many different build systems. Currently, 60*8fb009dcSAndroid Build Coastguard Workerdifferent projects use [GYP](https://gyp.gsrc.io/), 61*8fb009dcSAndroid Build Coastguard Worker[GN](https://gn.googlesource.com/gn/+/master/docs/quick_start.md), 62*8fb009dcSAndroid Build Coastguard Worker[Bazel](https://bazel.build/) and [Make](https://www.gnu.org/software/make/) to 63*8fb009dcSAndroid Build Coastguard Workerbuild BoringSSL, without too much pain. 64*8fb009dcSAndroid Build Coastguard Worker 65*8fb009dcSAndroid Build Coastguard WorkerThe development build system is CMake and the CMake build knows how to 66*8fb009dcSAndroid Build Coastguard Workerautomatically generate the intermediate files that BoringSSL needs. However, 67*8fb009dcSAndroid Build Coastguard Workeroutside of the CMake environment, these intermediates are generated once and 68*8fb009dcSAndroid Build Coastguard Workerchecked into the incorporating project's source repository. This avoids 69*8fb009dcSAndroid Build Coastguard Workerincorporating projects needing to support Perl and Go in their build systems. 70*8fb009dcSAndroid Build Coastguard Worker 71*8fb009dcSAndroid Build Coastguard WorkerThe script [`util/generate_build_files.py`](./util/generate_build_files.py) 72*8fb009dcSAndroid Build Coastguard Workerexpects to be run from the `third_party/boringssl` directory and to find the 73*8fb009dcSAndroid Build Coastguard WorkerBoringSSL source code in `src/`. You should pass it a single argument: the name 74*8fb009dcSAndroid Build Coastguard Workerof the build system that you're using. If you don't use any of the supported 75*8fb009dcSAndroid Build Coastguard Workerbuild systems then you should augment `generate_build_files.py` with support 76*8fb009dcSAndroid Build Coastguard Workerfor it. 77*8fb009dcSAndroid Build Coastguard Worker 78*8fb009dcSAndroid Build Coastguard WorkerThe script will pregenerate the intermediate files (see 79*8fb009dcSAndroid Build Coastguard Worker[BUILDING.md](./BUILDING.md) for details about which tools will need to be 80*8fb009dcSAndroid Build Coastguard Workerinstalled) and output helper files for that build system. It doesn't generate a 81*8fb009dcSAndroid Build Coastguard Workercomplete build script, just file and test lists, which change often. For 82*8fb009dcSAndroid Build Coastguard Workerexample, see the 83*8fb009dcSAndroid Build Coastguard Worker[file](https://code.google.com/p/chromium/codesearch#chromium/src/third_party/boringssl/BUILD.generated.gni) 84*8fb009dcSAndroid Build Coastguard Workerand 85*8fb009dcSAndroid Build Coastguard Worker[test](https://code.google.com/p/chromium/codesearch#chromium/src/third_party/boringssl/BUILD.generated_tests.gni) 86*8fb009dcSAndroid Build Coastguard Workerlists generated for GN in Chromium. 87*8fb009dcSAndroid Build Coastguard Worker 88*8fb009dcSAndroid Build Coastguard WorkerGenerally one checks in these generated files alongside the hand-written build 89*8fb009dcSAndroid Build Coastguard Workerfiles. Periodically an engineer updates the BoringSSL revision, regenerates 90*8fb009dcSAndroid Build Coastguard Workerthese files and checks in the updated result. As an example, see how this is 91*8fb009dcSAndroid Build Coastguard Workerdone [in Chromium](https://code.google.com/p/chromium/codesearch#chromium/src/third_party/boringssl/). 92*8fb009dcSAndroid Build Coastguard Worker 93*8fb009dcSAndroid Build Coastguard Worker## Defines 94*8fb009dcSAndroid Build Coastguard Worker 95*8fb009dcSAndroid Build Coastguard WorkerBoringSSL does not present a lot of configurability in order to reduce the 96*8fb009dcSAndroid Build Coastguard Workernumber of configurations that need to be tested. But there are a couple of 97*8fb009dcSAndroid Build Coastguard Worker\#defines that you may wish to set: 98*8fb009dcSAndroid Build Coastguard Worker 99*8fb009dcSAndroid Build Coastguard Worker`OPENSSL_NO_ASM` prevents the use of assembly code (although it's up to you to 100*8fb009dcSAndroid Build Coastguard Workerensure that the build system doesn't link it in if you wish to reduce binary 101*8fb009dcSAndroid Build Coastguard Workersize). This will have a significant performance impact but can be useful if you 102*8fb009dcSAndroid Build Coastguard Workerwish to use tools like 103*8fb009dcSAndroid Build Coastguard Worker[AddressSanitizer](http://clang.llvm.org/docs/AddressSanitizer.html) that 104*8fb009dcSAndroid Build Coastguard Workerinteract poorly with assembly code. 105*8fb009dcSAndroid Build Coastguard Worker 106*8fb009dcSAndroid Build Coastguard Worker`OPENSSL_SMALL` removes some code that is especially large at some performance 107*8fb009dcSAndroid Build Coastguard Workercost. 108*8fb009dcSAndroid Build Coastguard Worker 109*8fb009dcSAndroid Build Coastguard Worker## Symbols 110*8fb009dcSAndroid Build Coastguard Worker 111*8fb009dcSAndroid Build Coastguard WorkerYou cannot link multiple versions of BoringSSL or OpenSSL into a single binary 112*8fb009dcSAndroid Build Coastguard Workerwithout dealing with symbol conflicts. If you are statically linking multiple 113*8fb009dcSAndroid Build Coastguard Workerversions together, there's not a lot that can be done because C doesn't have a 114*8fb009dcSAndroid Build Coastguard Workermodule system. 115*8fb009dcSAndroid Build Coastguard Worker 116*8fb009dcSAndroid Build Coastguard WorkerIf you are using multiple versions in a single binary, in different shared 117*8fb009dcSAndroid Build Coastguard Workerobjects, ensure you build BoringSSL with `-fvisibility=hidden` and do not 118*8fb009dcSAndroid Build Coastguard Workerexport any of BoringSSL's symbols. This will prevent any collisions with other 119*8fb009dcSAndroid Build Coastguard Workerverisons that may be included in other shared objects. Note that this requires 120*8fb009dcSAndroid Build Coastguard Workerthat all callers of BoringSSL APIs live in the same shared object as BoringSSL. 121*8fb009dcSAndroid Build Coastguard Worker 122*8fb009dcSAndroid Build Coastguard WorkerIf you require that BoringSSL APIs be used across shared object boundaries, 123*8fb009dcSAndroid Build Coastguard Workercontinue to build with `-fvisibility=hidden` but define 124*8fb009dcSAndroid Build Coastguard Worker`BORINGSSL_SHARED_LIBRARY` in both BoringSSL and consumers. BoringSSL's own 125*8fb009dcSAndroid Build Coastguard Workersource files (but *not* consumers' source files) must also build with 126*8fb009dcSAndroid Build Coastguard Worker`BORINGSSL_IMPLEMENTATION` defined. This will export BoringSSL's public symbols 127*8fb009dcSAndroid Build Coastguard Workerin the resulting shared object while hiding private symbols. However note that, 128*8fb009dcSAndroid Build Coastguard Workeras with a static link, this precludes dynamically linking with another version 129*8fb009dcSAndroid Build Coastguard Workerof BoringSSL or OpenSSL. 130