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