1# CMake for Tink HOW-TO 2 3# Incorporating Tink into your project 4 5If you are developing a project that uses Tink, you might incorporate the 6library using one of the following approaches. At the moment, only the in-tree 7dependency is supported, although all of them should work. 8 9## In-tree dependency 10 11Tink can be embedded directly in your CMake project and statically linked in 12your executable. This is the approach we currently recommend. Assuming the Tink 13source tree has been copied in the `third_party/tink` directory of your project, 14your top-level CMake script should look like this: 15 16 cmake_minimum_required(VERSION 3.13) 17 project(YourProject CXX) 18 set(CMAKE_CXX_STANDARD_REQUIRED ON) 19 set(CMAKE_CXX_STANDARD 14) 20 21 add_subdirectory(third_party/tink) 22 23 add_executable(your_app your_app.cc) 24 target_link_libraries(your_app tink::static) 25 26NOTES: 27 28* You need at least CMake 3.13 to build Tink and its dependencies. 29* Tink defines the C++ standard to use via the `TINK_CXX_STANDARD` variable, 30 which is `14` by default. If you want to propagate to the value of 31 `CMAKE_CXX_STANDARD` to Tink use `set(CMAKE_CXX_STANDARD_REQUIRED ON)`. 32 33Include Tink headers in `your_app.cc` as follows: 34 35 #include "tink/config.h" 36 #include "tink/json_keyset_reader.h" 37 38 // ... 39 40NOTE: `tink::static` provides the `tink/...` include path. It is just a shortcut 41for your convenience, and you might still refer to Tink headers using a 42filesystem path, such as `third_party/tink/cc/...`, if you prefer or need to. 43 44You can see a full example in `examples/cc/helloworld/hello_world.cc`. 45 46Generate the build directory as you normally would and invoke your build system 47of choice: 48 49 $ ls 50 CMakeLists.txt your_app.cc third_party/ 51 $ mkdir build && cd build 52 $ cmake .. 53 $ make 54 $ ./your_app 55 56If you have the option, we recommend using [Ninja](https://ninja-build.org/) to 57build your project: 58 59 $ cmake -DCMAKE_GENERATOR=Ninja .. 60 $ ninja 61 62 63# Developing Tink with CMake 64 65If you are developing Tink, Bazel is the primary build system, but you should 66test all your changes with CMake too. Build Tink as a regular CMake project, but 67enable tests and build the shared library as well: 68 69 $ ls 70 tink/ 71 $ mkdir tink-build && cd tink-build 72 $ cmake ../tink -DTINK_BUILD_TESTS=ON -DCMAKE_GENERATOR=Ninja 73 $ ninja 74 $ CTEST_OUTPUT_ON_FAILURE=1 ninja test 75 $ ninja package 76 77This combination of options ensures that the entire CMake configuration is 78evaluated. 79 80WARNING: When editing a `BUILD.bazel` file, remember to keep it in sync with the 81corresponding `CMakeLists.txt` file. 82