xref: /aosp_15_r20/external/abseil-cpp/CMake/README.md (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker# Abseil CMake Build Instructions
2*9356374aSAndroid Build Coastguard Worker
3*9356374aSAndroid Build Coastguard WorkerAbseil comes with a CMake build script ([CMakeLists.txt](../CMakeLists.txt))
4*9356374aSAndroid Build Coastguard Workerthat can be used on a wide range of platforms ("C" stands for cross-platform.).
5*9356374aSAndroid Build Coastguard WorkerIf you don't have CMake installed already, you can download it for free from
6*9356374aSAndroid Build Coastguard Worker<https://www.cmake.org/>.
7*9356374aSAndroid Build Coastguard Worker
8*9356374aSAndroid Build Coastguard WorkerCMake works by generating native makefiles or build projects that can
9*9356374aSAndroid Build Coastguard Workerbe used in the compiler environment of your choice.
10*9356374aSAndroid Build Coastguard Worker
11*9356374aSAndroid Build Coastguard WorkerFor API/ABI compatibility reasons, we strongly recommend building Abseil in a
12*9356374aSAndroid Build Coastguard Workersubdirectory of your project or as an embedded dependency.
13*9356374aSAndroid Build Coastguard Worker
14*9356374aSAndroid Build Coastguard Worker## Incorporating Abseil Into a CMake Project
15*9356374aSAndroid Build Coastguard Worker
16*9356374aSAndroid Build Coastguard WorkerThe recommendations below are similar to those for using CMake within the
17*9356374aSAndroid Build Coastguard Workergoogletest framework
18*9356374aSAndroid Build Coastguard Worker(<https://github.com/google/googletest/blob/master/googletest/README.md#incorporating-into-an-existing-cmake-project>)
19*9356374aSAndroid Build Coastguard Worker
20*9356374aSAndroid Build Coastguard Worker### Step-by-Step Instructions
21*9356374aSAndroid Build Coastguard Worker
22*9356374aSAndroid Build Coastguard Worker1. If you want to build the Abseil tests, integrate the Abseil dependency
23*9356374aSAndroid Build Coastguard Worker[Google Test](https://github.com/google/googletest) into your CMake
24*9356374aSAndroid Build Coastguard Workerproject. To disable Abseil tests, you have to pass either
25*9356374aSAndroid Build Coastguard Worker`-DBUILD_TESTING=OFF` or `-DABSL_BUILD_TESTING=OFF` when configuring your
26*9356374aSAndroid Build Coastguard Workerproject with CMake.
27*9356374aSAndroid Build Coastguard Worker
28*9356374aSAndroid Build Coastguard Worker2. Download Abseil and copy it into a subdirectory in your CMake project or add
29*9356374aSAndroid Build Coastguard WorkerAbseil as a [git submodule](https://git-scm.com/docs/git-submodule) in your
30*9356374aSAndroid Build Coastguard WorkerCMake project.
31*9356374aSAndroid Build Coastguard Worker
32*9356374aSAndroid Build Coastguard Worker3. You can then use the CMake command
33*9356374aSAndroid Build Coastguard Worker[`add_subdirectory()`](https://cmake.org/cmake/help/latest/command/add_subdirectory.html)
34*9356374aSAndroid Build Coastguard Workerto include Abseil directly in your CMake project.
35*9356374aSAndroid Build Coastguard Worker
36*9356374aSAndroid Build Coastguard Worker4. Add the **absl::** target you wish to use to the
37*9356374aSAndroid Build Coastguard Worker[`target_link_libraries()`](https://cmake.org/cmake/help/latest/command/target_link_libraries.html)
38*9356374aSAndroid Build Coastguard Workersection of your executable or of your library.<br>
39*9356374aSAndroid Build Coastguard WorkerHere is a short CMakeLists.txt example of an application project using Abseil.
40*9356374aSAndroid Build Coastguard Worker
41*9356374aSAndroid Build Coastguard Worker```cmake
42*9356374aSAndroid Build Coastguard Workercmake_minimum_required(VERSION 3.16)
43*9356374aSAndroid Build Coastguard Workerproject(my_app_project)
44*9356374aSAndroid Build Coastguard Worker
45*9356374aSAndroid Build Coastguard Worker# Pick the C++ standard to compile with.
46*9356374aSAndroid Build Coastguard Worker# Abseil currently supports C++14, C++17, and C++20.
47*9356374aSAndroid Build Coastguard Workerset(CMAKE_CXX_STANDARD 14)
48*9356374aSAndroid Build Coastguard Workerset(CMAKE_CXX_STANDARD_REQUIRED ON)
49*9356374aSAndroid Build Coastguard Worker
50*9356374aSAndroid Build Coastguard Workeradd_subdirectory(abseil-cpp)
51*9356374aSAndroid Build Coastguard Worker
52*9356374aSAndroid Build Coastguard Workeradd_executable(my_exe source.cpp)
53*9356374aSAndroid Build Coastguard Workertarget_link_libraries(my_exe absl::base absl::synchronization absl::strings)
54*9356374aSAndroid Build Coastguard Worker```
55*9356374aSAndroid Build Coastguard Worker
56*9356374aSAndroid Build Coastguard WorkerNote that if you are developing a library designed for use by other clients, you
57*9356374aSAndroid Build Coastguard Workershould instead leave `CMAKE_CXX_STANDARD` unset (or only set if being built as
58*9356374aSAndroid Build Coastguard Workerthe current top-level CMake project) and configure the minimum required C++
59*9356374aSAndroid Build Coastguard Workerstandard at the target level. If you require a later minimum C++ standard than
60*9356374aSAndroid Build Coastguard WorkerAbseil does, it's a good idea to also enforce that `CMAKE_CXX_STANDARD` (which
61*9356374aSAndroid Build Coastguard Workerwill control Abseil library targets) is set to at least that minimum. For
62*9356374aSAndroid Build Coastguard Workerexample:
63*9356374aSAndroid Build Coastguard Worker
64*9356374aSAndroid Build Coastguard Worker```cmake
65*9356374aSAndroid Build Coastguard Workercmake_minimum_required(VERSION 3.16)
66*9356374aSAndroid Build Coastguard Workerproject(my_lib_project)
67*9356374aSAndroid Build Coastguard Worker
68*9356374aSAndroid Build Coastguard Worker# Leave C++ standard up to the root application, so set it only if this is the
69*9356374aSAndroid Build Coastguard Worker# current top-level CMake project.
70*9356374aSAndroid Build Coastguard Workerif(CMAKE_SOURCE_DIR STREQUAL my_lib_project_SOURCE_DIR)
71*9356374aSAndroid Build Coastguard Worker  set(CMAKE_CXX_STANDARD 17)
72*9356374aSAndroid Build Coastguard Worker  set(CMAKE_CXX_STANDARD_REQUIRED ON)
73*9356374aSAndroid Build Coastguard Workerendif()
74*9356374aSAndroid Build Coastguard Worker
75*9356374aSAndroid Build Coastguard Workeradd_subdirectory(abseil-cpp)
76*9356374aSAndroid Build Coastguard Worker
77*9356374aSAndroid Build Coastguard Workeradd_library(my_lib source.cpp)
78*9356374aSAndroid Build Coastguard Workertarget_link_libraries(my_lib absl::base absl::synchronization absl::strings)
79*9356374aSAndroid Build Coastguard Worker
80*9356374aSAndroid Build Coastguard Worker# Enforce that my_lib requires C++17. Important to document for clients that they
81*9356374aSAndroid Build Coastguard Worker# must set CMAKE_CXX_STANDARD to 17 or higher for proper Abseil ABI compatibility
82*9356374aSAndroid Build Coastguard Worker# (since otherwise, Abseil library targets could be compiled with a lower C++
83*9356374aSAndroid Build Coastguard Worker# standard than my_lib).
84*9356374aSAndroid Build Coastguard Workertarget_compile_features(my_lib PUBLIC cxx_std_17)
85*9356374aSAndroid Build Coastguard Workerif(CMAKE_CXX_STANDARD LESS 17)
86*9356374aSAndroid Build Coastguard Worker  message(FATAL_ERROR
87*9356374aSAndroid Build Coastguard Worker      "my_lib_project requires CMAKE_CXX_STANDARD >= 17 (got: ${CMAKE_CXX_STANDARD})")
88*9356374aSAndroid Build Coastguard Workerendif()
89*9356374aSAndroid Build Coastguard Worker```
90*9356374aSAndroid Build Coastguard Worker
91*9356374aSAndroid Build Coastguard WorkerThen the top-level application project that uses your library is responsible for
92*9356374aSAndroid Build Coastguard Workersetting a consistent `CMAKE_CXX_STANDARD` that is sufficiently high.
93*9356374aSAndroid Build Coastguard Worker
94*9356374aSAndroid Build Coastguard Worker### Running Abseil Tests with CMake
95*9356374aSAndroid Build Coastguard Worker
96*9356374aSAndroid Build Coastguard WorkerUse the `-DABSL_BUILD_TESTING=ON` flag to run Abseil tests.  Note that
97*9356374aSAndroid Build Coastguard WorkerBUILD_TESTING must also be on (the default).
98*9356374aSAndroid Build Coastguard Worker
99*9356374aSAndroid Build Coastguard WorkerYou will need to provide Abseil with a Googletest dependency.  There are two
100*9356374aSAndroid Build Coastguard Workeroptions for how to do this:
101*9356374aSAndroid Build Coastguard Worker
102*9356374aSAndroid Build Coastguard Worker* Use `-DABSL_USE_GOOGLETEST_HEAD`.  This will automatically download the latest
103*9356374aSAndroid Build Coastguard WorkerGoogletest source into the build directory at configure time.  Googletest will
104*9356374aSAndroid Build Coastguard Workerthen be compiled directly alongside Abseil's tests.
105*9356374aSAndroid Build Coastguard Worker* Manually integrate Googletest with your build.  See
106*9356374aSAndroid Build Coastguard Workerhttps://github.com/google/googletest/blob/master/googletest/README.md#using-cmake
107*9356374aSAndroid Build Coastguard Workerfor more information on using Googletest in a CMake project.
108*9356374aSAndroid Build Coastguard Worker
109*9356374aSAndroid Build Coastguard WorkerFor example, to run just the Abseil tests, you could use this script:
110*9356374aSAndroid Build Coastguard Worker
111*9356374aSAndroid Build Coastguard Worker```
112*9356374aSAndroid Build Coastguard Workercd path/to/abseil-cpp
113*9356374aSAndroid Build Coastguard Workermkdir build
114*9356374aSAndroid Build Coastguard Workercd build
115*9356374aSAndroid Build Coastguard Workercmake -DABSL_BUILD_TESTING=ON -DABSL_USE_GOOGLETEST_HEAD=ON ..
116*9356374aSAndroid Build Coastguard Workermake -j
117*9356374aSAndroid Build Coastguard Workerctest
118*9356374aSAndroid Build Coastguard Worker```
119*9356374aSAndroid Build Coastguard Worker
120*9356374aSAndroid Build Coastguard WorkerCurrently, we only run our tests with CMake in a Linux environment, but we are
121*9356374aSAndroid Build Coastguard Workerworking on the rest of our supported platforms. See
122*9356374aSAndroid Build Coastguard Workerhttps://github.com/abseil/abseil-cpp/projects/1 and
123*9356374aSAndroid Build Coastguard Workerhttps://github.com/abseil/abseil-cpp/issues/109 for more information.
124*9356374aSAndroid Build Coastguard Worker
125*9356374aSAndroid Build Coastguard Worker### Available Abseil CMake Public Targets
126*9356374aSAndroid Build Coastguard Worker
127*9356374aSAndroid Build Coastguard WorkerHere's a non-exhaustive list of Abseil CMake public targets:
128*9356374aSAndroid Build Coastguard Worker
129*9356374aSAndroid Build Coastguard Worker```cmake
130*9356374aSAndroid Build Coastguard Workerabsl::algorithm
131*9356374aSAndroid Build Coastguard Workerabsl::base
132*9356374aSAndroid Build Coastguard Workerabsl::debugging
133*9356374aSAndroid Build Coastguard Workerabsl::flat_hash_map
134*9356374aSAndroid Build Coastguard Workerabsl::flags
135*9356374aSAndroid Build Coastguard Workerabsl::memory
136*9356374aSAndroid Build Coastguard Workerabsl::meta
137*9356374aSAndroid Build Coastguard Workerabsl::numeric
138*9356374aSAndroid Build Coastguard Workerabsl::random_random
139*9356374aSAndroid Build Coastguard Workerabsl::strings
140*9356374aSAndroid Build Coastguard Workerabsl::synchronization
141*9356374aSAndroid Build Coastguard Workerabsl::time
142*9356374aSAndroid Build Coastguard Workerabsl::utility
143*9356374aSAndroid Build Coastguard Worker```
144*9356374aSAndroid Build Coastguard Worker
145*9356374aSAndroid Build Coastguard Worker## Traditional CMake Set-Up
146*9356374aSAndroid Build Coastguard Worker
147*9356374aSAndroid Build Coastguard WorkerFor larger projects, it may make sense to use the traditional CMake set-up where you build and install projects separately.
148*9356374aSAndroid Build Coastguard Worker
149*9356374aSAndroid Build Coastguard WorkerFirst, you'd need to build and install Google Test:
150*9356374aSAndroid Build Coastguard Worker```
151*9356374aSAndroid Build Coastguard Workercmake -S /source/googletest -B /build/googletest -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/installation/dir -DBUILD_GMOCK=ON
152*9356374aSAndroid Build Coastguard Workercmake --build /build/googletest --target install
153*9356374aSAndroid Build Coastguard Worker```
154*9356374aSAndroid Build Coastguard Worker
155*9356374aSAndroid Build Coastguard WorkerThen you need to configure and build Abseil. Make sure you enable `ABSL_USE_EXTERNAL_GOOGLETEST` and `ABSL_FIND_GOOGLETEST`. You also need to enable `ABSL_ENABLE_INSTALL` so that you can install Abseil itself.
156*9356374aSAndroid Build Coastguard Worker```
157*9356374aSAndroid Build Coastguard Workercmake -S /source/abseil-cpp -B /build/abseil-cpp -DCMAKE_PREFIX_PATH=/installation/dir -DCMAKE_INSTALL_PREFIX=/installation/dir -DABSL_ENABLE_INSTALL=ON -DABSL_USE_EXTERNAL_GOOGLETEST=ON -DABSL_FIND_GOOGLETEST=ON
158*9356374aSAndroid Build Coastguard Workercmake --build /temporary/build/abseil-cpp
159*9356374aSAndroid Build Coastguard Worker```
160*9356374aSAndroid Build Coastguard Worker
161*9356374aSAndroid Build Coastguard Worker(`CMAKE_PREFIX_PATH` is where you already have Google Test installed; `CMAKE_INSTALL_PREFIX` is where you want to have Abseil installed; they can be different.)
162*9356374aSAndroid Build Coastguard Worker
163*9356374aSAndroid Build Coastguard WorkerRun the tests:
164*9356374aSAndroid Build Coastguard Worker```
165*9356374aSAndroid Build Coastguard Workerctest --test-dir /temporary/build/abseil-cpp
166*9356374aSAndroid Build Coastguard Worker```
167*9356374aSAndroid Build Coastguard Worker
168*9356374aSAndroid Build Coastguard WorkerAnd finally install:
169*9356374aSAndroid Build Coastguard Worker```
170*9356374aSAndroid Build Coastguard Workercmake --build /temporary/build/abseil-cpp --target install
171*9356374aSAndroid Build Coastguard Worker```
172*9356374aSAndroid Build Coastguard Worker
173*9356374aSAndroid Build Coastguard Worker# CMake Option Synopsis
174*9356374aSAndroid Build Coastguard Worker
175*9356374aSAndroid Build Coastguard Worker## Enable Standard CMake Installation
176*9356374aSAndroid Build Coastguard Worker
177*9356374aSAndroid Build Coastguard Worker`-DABSL_ENABLE_INSTALL=ON`
178*9356374aSAndroid Build Coastguard Worker
179*9356374aSAndroid Build Coastguard Worker## Google Test Options
180*9356374aSAndroid Build Coastguard Worker
181*9356374aSAndroid Build Coastguard Worker`-DABSL_BUILD_TESTING=ON` must be set to enable testing
182*9356374aSAndroid Build Coastguard Worker
183*9356374aSAndroid Build Coastguard Worker- Have Abseil download and build Google Test for you: `-DABSL_USE_EXTERNAL_GOOGLETEST=OFF` (default)
184*9356374aSAndroid Build Coastguard Worker  - Download and build latest Google Test: `-DABSL_USE_GOOGLETEST_HEAD=ON`
185*9356374aSAndroid Build Coastguard Worker  - Download specific Google Test version (ZIP archive): `-DABSL_GOOGLETEST_DOWNLOAD_URL=https://.../version.zip`
186*9356374aSAndroid Build Coastguard Worker  - Use Google Test from specific local directory: `-DABSL_LOCAL_GOOGLETEST_DIR=/path/to/googletest`
187*9356374aSAndroid Build Coastguard Worker- Use Google Test included elsewhere in your project: `-DABSL_USE_EXTERNAL_GOOGLETEST=ON`
188*9356374aSAndroid Build Coastguard Worker- Use standard CMake `find_package(CTest)` to find installed Google Test: `-DABSL_USE_EXTERNAL_GOOGLETEST=ON -DABSL_FIND_GOOGLETEST=ON`
189