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