xref: /aosp_15_r20/external/angle/third_party/abseil-cpp/FAQ.md (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker# Abseil FAQ
2*8975f5c5SAndroid Build Coastguard Worker
3*8975f5c5SAndroid Build Coastguard Worker## Is Abseil the right home for my utility library?
4*8975f5c5SAndroid Build Coastguard Worker
5*8975f5c5SAndroid Build Coastguard WorkerMost often the answer to the question is "no." As both the [About
6*8975f5c5SAndroid Build Coastguard WorkerAbseil](https://abseil.io/about/) page and our [contributing
7*8975f5c5SAndroid Build Coastguard Workerguidelines](https://github.com/abseil/abseil-cpp/blob/master/CONTRIBUTING.md#contribution-guidelines)
8*8975f5c5SAndroid Build Coastguard Workerexplain, Abseil contains a variety of core C++ library code that is widely used
9*8975f5c5SAndroid Build Coastguard Workerat [Google](https://www.google.com/). As such, Abseil's primary purpose is to be
10*8975f5c5SAndroid Build Coastguard Workerused as a dependency by Google's open source C++ projects. While we do hope that
11*8975f5c5SAndroid Build Coastguard WorkerAbseil is also useful to the C++ community at large, this added constraint also
12*8975f5c5SAndroid Build Coastguard Workermeans that we are unlikely to accept a contribution of utility code that isn't
13*8975f5c5SAndroid Build Coastguard Workeralready widely used by Google.
14*8975f5c5SAndroid Build Coastguard Worker
15*8975f5c5SAndroid Build Coastguard Worker## How to I set the C++ dialect used to build Abseil?
16*8975f5c5SAndroid Build Coastguard Worker
17*8975f5c5SAndroid Build Coastguard WorkerThe short answer is that whatever mechanism you choose, you need to make sure
18*8975f5c5SAndroid Build Coastguard Workerthat you set this option consistently at the global level for your entire
19*8975f5c5SAndroid Build Coastguard Workerproject. If, for example, you want to set the C++ dialect to C++17, with
20*8975f5c5SAndroid Build Coastguard Worker[Bazel](https://bazel/build/) as the build system and `gcc` or `clang` as the
21*8975f5c5SAndroid Build Coastguard Workercompiler, there several ways to do this:
22*8975f5c5SAndroid Build Coastguard Worker* Pass `--cxxopt=-std=c++17` on the command line (for example, `bazel build
23*8975f5c5SAndroid Build Coastguard Worker  --cxxopt=-std=c++17 ...`)
24*8975f5c5SAndroid Build Coastguard Worker* Set the environment variable `BAZEL_CXXOPTS` (for example,
25*8975f5c5SAndroid Build Coastguard Worker  `BAZEL_CXXOPTS=-std=c++17`)
26*8975f5c5SAndroid Build Coastguard Worker* Add `build --cxxopt=-std=c++17` to your [`.bazelrc`
27*8975f5c5SAndroid Build Coastguard Worker  file](https://docs.bazel.build/versions/master/guide.html#bazelrc)
28*8975f5c5SAndroid Build Coastguard Worker
29*8975f5c5SAndroid Build Coastguard WorkerIf you are using CMake as the build system, you'll need to add a line like
30*8975f5c5SAndroid Build Coastguard Worker`set(CMAKE_CXX_STANDARD 17)` to your top level `CMakeLists.txt` file. If you
31*8975f5c5SAndroid Build Coastguard Workerare developing a library designed to be used by other clients, you should
32*8975f5c5SAndroid Build Coastguard Workerinstead leave `CMAKE_CXX_STANDARD` unset and configure the minimum C++ standard
33*8975f5c5SAndroid Build Coastguard Workerrequired by each of your library targets via `target_compile_features`. See the
34*8975f5c5SAndroid Build Coastguard Worker[CMake build
35*8975f5c5SAndroid Build Coastguard Workerinstructions](https://github.com/abseil/abseil-cpp/blob/master/CMake/README.md)
36*8975f5c5SAndroid Build Coastguard Workerfor more information.
37*8975f5c5SAndroid Build Coastguard Worker
38*8975f5c5SAndroid Build Coastguard WorkerFor a longer answer to this question and to understand why some other approaches
39*8975f5c5SAndroid Build Coastguard Workerdon't work, see the answer to ["What is ABI and why don't you recommend using a
40*8975f5c5SAndroid Build Coastguard Workerpre-compiled version of
41*8975f5c5SAndroid Build Coastguard WorkerAbseil?"](#what-is-abi-and-why-dont-you-recommend-using-a-pre-compiled-version-of-abseil)
42*8975f5c5SAndroid Build Coastguard Worker
43*8975f5c5SAndroid Build Coastguard Worker## What is ABI and why don't you recommend using a pre-compiled version of Abseil?
44*8975f5c5SAndroid Build Coastguard Worker
45*8975f5c5SAndroid Build Coastguard WorkerFor the purposes of this discussion, you can think of
46*8975f5c5SAndroid Build Coastguard Worker[ABI](https://en.wikipedia.org/wiki/Application_binary_interface) as the
47*8975f5c5SAndroid Build Coastguard Workercompiled representation of the interfaces in code. This is in contrast to
48*8975f5c5SAndroid Build Coastguard Worker[API](https://en.wikipedia.org/wiki/Application_programming_interface), which
49*8975f5c5SAndroid Build Coastguard Workeryou can think of as the interfaces as defined by the code itself. [Abseil has a
50*8975f5c5SAndroid Build Coastguard Workerstrong promise of API compatibility, but does not make any promise of ABI
51*8975f5c5SAndroid Build Coastguard Workercompatibility](https://abseil.io/about/compatibility). Let's take a look at what
52*8975f5c5SAndroid Build Coastguard Workerthis means in practice.
53*8975f5c5SAndroid Build Coastguard Worker
54*8975f5c5SAndroid Build Coastguard WorkerYou might be tempted to do something like this in a
55*8975f5c5SAndroid Build Coastguard Worker[Bazel](https://bazel.build/) `BUILD` file:
56*8975f5c5SAndroid Build Coastguard Worker
57*8975f5c5SAndroid Build Coastguard Worker```
58*8975f5c5SAndroid Build Coastguard Worker# DON'T DO THIS!!!
59*8975f5c5SAndroid Build Coastguard Workercc_library(
60*8975f5c5SAndroid Build Coastguard Worker    name = "my_library",
61*8975f5c5SAndroid Build Coastguard Worker    srcs = ["my_library.cc"],
62*8975f5c5SAndroid Build Coastguard Worker    copts = ["-std=c++17"],  # May create a mixed-mode compile!
63*8975f5c5SAndroid Build Coastguard Worker    deps = ["@com_google_absl//absl/strings"],
64*8975f5c5SAndroid Build Coastguard Worker)
65*8975f5c5SAndroid Build Coastguard Worker```
66*8975f5c5SAndroid Build Coastguard Worker
67*8975f5c5SAndroid Build Coastguard WorkerApplying `-std=c++17` to an individual target in your `BUILD` file is going to
68*8975f5c5SAndroid Build Coastguard Workercompile that specific target in C++17 mode, but it isn't going to ensure the
69*8975f5c5SAndroid Build Coastguard WorkerAbseil library is built in C++17 mode, since the Abseil library itself is a
70*8975f5c5SAndroid Build Coastguard Workerdifferent build target. If your code includes an Abseil header, then your
71*8975f5c5SAndroid Build Coastguard Workerprogram may contain conflicting definitions of the same
72*8975f5c5SAndroid Build Coastguard Workerclass/function/variable/enum, etc. As a rule, all compile options that affect
73*8975f5c5SAndroid Build Coastguard Workerthe ABI of a program need to be applied to the entire build on a global basis.
74*8975f5c5SAndroid Build Coastguard Worker
75*8975f5c5SAndroid Build Coastguard WorkerC++ has something called the [One Definition
76*8975f5c5SAndroid Build Coastguard WorkerRule](https://en.wikipedia.org/wiki/One_Definition_Rule) (ODR). C++ doesn't
77*8975f5c5SAndroid Build Coastguard Workerallow multiple definitions of the same class/function/variable/enum, etc. ODR
78*8975f5c5SAndroid Build Coastguard Workerviolations sometimes result in linker errors, but linkers do not always catch
79*8975f5c5SAndroid Build Coastguard Workerviolations. Uncaught ODR violations can result in strange runtime behaviors or
80*8975f5c5SAndroid Build Coastguard Workercrashes that can be hard to debug.
81*8975f5c5SAndroid Build Coastguard Worker
82*8975f5c5SAndroid Build Coastguard WorkerIf you build the Abseil library and your code using different compile options
83*8975f5c5SAndroid Build Coastguard Workerthat affect ABI, there is a good chance you will run afoul of the One Definition
84*8975f5c5SAndroid Build Coastguard WorkerRule. Examples of GCC compile options that affect ABI include (but aren't
85*8975f5c5SAndroid Build Coastguard Workerlimited to) language dialect (e.g. `-std=`), optimization level (e.g. `-O2`),
86*8975f5c5SAndroid Build Coastguard Workercode generation flags (e.g. `-fexceptions`), and preprocessor defines
87*8975f5c5SAndroid Build Coastguard Worker(e.g. `-DNDEBUG`).
88*8975f5c5SAndroid Build Coastguard Worker
89*8975f5c5SAndroid Build Coastguard WorkerIf you use a pre-compiled version of Abseil, (for example, from your Linux
90*8975f5c5SAndroid Build Coastguard Workerdistribution package manager or from something like
91*8975f5c5SAndroid Build Coastguard Worker[vcpkg](https://github.com/microsoft/vcpkg)) you have to be very careful to
92*8975f5c5SAndroid Build Coastguard Workerensure ABI compatibility across the components of your program. The only way you
93*8975f5c5SAndroid Build Coastguard Workercan be sure your program is going to be correct regarding ABI is to ensure
94*8975f5c5SAndroid Build Coastguard Workeryou've used the exact same compile options as were used to build the
95*8975f5c5SAndroid Build Coastguard Workerpre-compiled library. This does not mean that Abseil cannot work as part of a
96*8975f5c5SAndroid Build Coastguard WorkerLinux distribution since a knowledgeable binary packager will have ensured that
97*8975f5c5SAndroid Build Coastguard Workerall packages have been built with consistent compile options. This is one of the
98*8975f5c5SAndroid Build Coastguard Workerreasons we warn against - though do not outright reject - using Abseil as a
99*8975f5c5SAndroid Build Coastguard Workerpre-compiled library.
100*8975f5c5SAndroid Build Coastguard Worker
101*8975f5c5SAndroid Build Coastguard WorkerAnother possible way that you might afoul of ABI issues is if you accidentally
102*8975f5c5SAndroid Build Coastguard Workerinclude two versions of Abseil in your program. Multiple versions of Abseil can
103*8975f5c5SAndroid Build Coastguard Workerend up within the same binary if your program uses the Abseil library and
104*8975f5c5SAndroid Build Coastguard Workeranother library also transitively depends on Abseil (resulting in what is
105*8975f5c5SAndroid Build Coastguard Workersometimes called the diamond dependency problem). In cases such as this you must
106*8975f5c5SAndroid Build Coastguard Workerstructure your build so that all libraries use the same version of Abseil.
107*8975f5c5SAndroid Build Coastguard Worker[Abseil's strong promise of API compatibility between
108*8975f5c5SAndroid Build Coastguard Workerreleases](https://abseil.io/about/compatibility) means the latest "HEAD" release
109*8975f5c5SAndroid Build Coastguard Workerof Abseil is almost certainly the right choice if you are doing as we recommend
110*8975f5c5SAndroid Build Coastguard Workerand building all of your code from source.
111*8975f5c5SAndroid Build Coastguard Worker
112*8975f5c5SAndroid Build Coastguard WorkerFor these reasons we recommend you avoid pre-compiled code and build the Abseil
113*8975f5c5SAndroid Build Coastguard Workerlibrary yourself in a consistent manner with the rest of your code.
114*8975f5c5SAndroid Build Coastguard Worker
115*8975f5c5SAndroid Build Coastguard Worker## What is "live at head" and how do I do it?
116*8975f5c5SAndroid Build Coastguard Worker
117*8975f5c5SAndroid Build Coastguard WorkerFrom Abseil's point-of-view, "live at head" means that every Abseil source
118*8975f5c5SAndroid Build Coastguard Workerrelease (which happens on an almost daily basis) is either API compatible with
119*8975f5c5SAndroid Build Coastguard Workerthe previous release, or comes with an automated tool that you can run over code
120*8975f5c5SAndroid Build Coastguard Workerto make it compatible. In practice, the need to use an automated tool is
121*8975f5c5SAndroid Build Coastguard Workerextremely rare. This means that upgrading from one source release to another
122*8975f5c5SAndroid Build Coastguard Workershould be a routine practice that can and should be performed often.
123*8975f5c5SAndroid Build Coastguard Worker
124*8975f5c5SAndroid Build Coastguard WorkerWe recommend you update to the [latest commit in the `master` branch of
125*8975f5c5SAndroid Build Coastguard WorkerAbseil](https://github.com/abseil/abseil-cpp/commits/master) as often as
126*8975f5c5SAndroid Build Coastguard Workerpossible. Not only will you pick up bug fixes more quickly, but if you have good
127*8975f5c5SAndroid Build Coastguard Workerautomated testing, you will catch and be able to fix any [Hyrum's
128*8975f5c5SAndroid Build Coastguard WorkerLaw](https://www.hyrumslaw.com/) dependency problems on an incremental basis
129*8975f5c5SAndroid Build Coastguard Workerinstead of being overwhelmed by them and having difficulty isolating them if you
130*8975f5c5SAndroid Build Coastguard Workerwait longer between updates.
131*8975f5c5SAndroid Build Coastguard Worker
132*8975f5c5SAndroid Build Coastguard WorkerIf you are using the [Bazel](https://bazel.build/) build system and its
133*8975f5c5SAndroid Build Coastguard Worker[external dependencies](https://docs.bazel.build/versions/master/external.html)
134*8975f5c5SAndroid Build Coastguard Workerfeature, updating the
135*8975f5c5SAndroid Build Coastguard Worker[`http_archive`](https://docs.bazel.build/versions/master/repo/http.html#http_archive)
136*8975f5c5SAndroid Build Coastguard Workerrule in your
137*8975f5c5SAndroid Build Coastguard Worker[`WORKSPACE`](https://docs.bazel.build/versions/master/be/workspace.html) for
138*8975f5c5SAndroid Build Coastguard Worker`com_google_abseil` to point to the [latest commit in the `master` branch of
139*8975f5c5SAndroid Build Coastguard WorkerAbseil](https://github.com/abseil/abseil-cpp/commits/master) is all you need to
140*8975f5c5SAndroid Build Coastguard Workerdo. For example, on February 11, 2020, the latest commit to the master branch
141*8975f5c5SAndroid Build Coastguard Workerwas `98eb410c93ad059f9bba1bf43f5bb916fc92a5ea`. To update to this commit, you
142*8975f5c5SAndroid Build Coastguard Workerwould add the following snippet to your `WORKSPACE` file:
143*8975f5c5SAndroid Build Coastguard Worker
144*8975f5c5SAndroid Build Coastguard Worker```
145*8975f5c5SAndroid Build Coastguard Workerhttp_archive(
146*8975f5c5SAndroid Build Coastguard Worker  name = "com_google_absl",
147*8975f5c5SAndroid Build Coastguard Worker  urls = ["https://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip"],  # 2020-02-11T18:50:53Z
148*8975f5c5SAndroid Build Coastguard Worker  strip_prefix = "abseil-cpp-98eb410c93ad059f9bba1bf43f5bb916fc92a5ea",
149*8975f5c5SAndroid Build Coastguard Worker  sha256 = "aabf6c57e3834f8dc3873a927f37eaf69975d4b28117fc7427dfb1c661542a87",
150*8975f5c5SAndroid Build Coastguard Worker)
151*8975f5c5SAndroid Build Coastguard Worker```
152*8975f5c5SAndroid Build Coastguard Worker
153*8975f5c5SAndroid Build Coastguard WorkerTo get the `sha256` of this URL, run `curl -sL --output -
154*8975f5c5SAndroid Build Coastguard Workerhttps://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip
155*8975f5c5SAndroid Build Coastguard Worker| sha256sum -`.
156*8975f5c5SAndroid Build Coastguard Worker
157*8975f5c5SAndroid Build Coastguard WorkerYou can commit the updated `WORKSPACE` file to your source control every time
158*8975f5c5SAndroid Build Coastguard Workeryou update, and if you have good automated testing, you might even consider
159*8975f5c5SAndroid Build Coastguard Workerautomating this.
160*8975f5c5SAndroid Build Coastguard Worker
161*8975f5c5SAndroid Build Coastguard WorkerOne thing we don't recommend is using GitHub's `master.zip` files (for example
162*8975f5c5SAndroid Build Coastguard Worker[https://github.com/abseil/abseil-cpp/archive/master.zip](https://github.com/abseil/abseil-cpp/archive/master.zip)),
163*8975f5c5SAndroid Build Coastguard Workerwhich are always the latest commit in the `master` branch, to implement live at
164*8975f5c5SAndroid Build Coastguard Workerhead. Since these `master.zip` URLs are not versioned, you will lose build
165*8975f5c5SAndroid Build Coastguard Workerreproducibility. In addition, some build systems, including Bazel, will simply
166*8975f5c5SAndroid Build Coastguard Workercache this file, which means you won't actually be updating to the latest
167*8975f5c5SAndroid Build Coastguard Workerrelease until your cache is cleared or invalidated.
168