]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/opentelemetry-cpp/tools/vcpkg/docs/examples/manifest-mode-cmake.md
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / tools / vcpkg / docs / examples / manifest-mode-cmake.md
CommitLineData
1e59de90
TL
1# Manifest Mode: CMake Example
2
3We would like to add [vcpkg manifest support](../users/manifests.md) to an existing cmake project!
4Let's create a simple project that prints the fibonacci sequence up to a certain number,
5using some common dependencies.
6
7## Initial Layout
8
9Let's create the following file layout:
10
11```no-highlight
12fibo/
13 src/
14 main.cxx
15 CMakeLists.txt
16```
17
18And we wish to use [fmt](https://github.com/fmtlib/fmt), [range-v3](https://github.com/ericniebler/range-v3),
19and [cxxopts](https://github.com/jarro2783/cxxopts).
20
21Let's write our `CMakeLists.txt` first:
22
23```cmake
24cmake_minimum_required(VERSION 3.15)
25
26project(fibo CXX)
27
28find_package(fmt REQUIRED)
29find_package(range-v3 REQUIRED)
30find_package(cxxopts REQUIRED)
31
32add_executable(fibo src/main.cxx)
33target_compile_features(fibo PRIVATE cxx_std_17)
34
35target_link_libraries(fibo
36 PRIVATE
37 fmt::fmt
38 range-v3::range-v3
39 cxxopts::cxxopts)
40```
41
42And then we should add `main.cxx`:
43
44```cxx
45#include <cxxopts.hpp>
46#include <fmt/format.h>
47#include <range/v3/view.hpp>
48
49namespace view = ranges::views;
50
51int fib(int x) {
52 int a = 0, b = 1;
53
54 for (int it : view::repeat(0) | view::take(x)) {
55 (void)it;
56 int tmp = a;
57 a += b;
58 b = tmp;
59 }
60
61 return a;
62}
63
64int main(int argc, char** argv) {
65 cxxopts::Options options("fibo", "Print the fibonacci sequence up to a value 'n'");
66 options.add_options()
67 ("n,value", "The value to print to", cxxopts::value<int>()->default_value("10"));
68
69 auto result = options.parse(argc, argv);
70 auto n = result["value"].as<int>();
71
72 for (int x : view::iota(1) | view::take(n)) {
73 fmt::print("fib({}) = {}\n", x, fib(x));
74 }
75}
76```
77
78This is a simple project of course, but it should give us a clean project to start with.
79Let's try it out!
80
81Let's assume you have `fmt`, `range-v3`, and `cxxopts` installed with vcpkg classic mode;
82then, you can just do a simple:
83
84```cmd
85D:\src\fibo> cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=D:\src\vcpkg\scripts\buildsystems\vcpkg.cmake
86-- Building for: Visual Studio 16 2019
87-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19041.
88-- The CXX compiler identification is MSVC 19.27.29111.0
89-- Detecting CXX compiler ABI info
90-- Detecting CXX compiler ABI info - done
91-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe - skipped
92-- Detecting CXX compile features
93-- Detecting CXX compile features - done
94-- Configuring done
95-- Generating done
96-- Build files have been written to: D:/src/fibo/build
97D:\src\fibo> cmake --build build
98Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET Framework
99Copyright (C) Microsoft Corporation. All rights reserved.
100
101 Checking Build System
102 Building Custom Rule D:/src/fibo/CMakeLists.txt
103 main.cxx
104 The contents of <span> are available only with C++20 or later.
105 fibo.vcxproj -> D:\src\fibo\build\Debug\fibo.exe
106 Building Custom Rule D:/src/fibo/CMakeLists.txt
107```
108
109And now we can try out the `fibo` binary!
110
111```cmd
112D:\src\fibo> .\build\Debug\fibo.exe -n 7
113fib(1) = 1
114fib(2) = 1
115fib(3) = 2
116fib(4) = 3
117fib(5) = 5
118fib(6) = 8
119fib(7) = 13
120```
121
122it works!
123
124## Converting to Manifest Mode
125
126We now wish to use manifest mode, so all of our dependencies are managed for us! Let's write a `vcpkg.json`:
127
128```json
129{
130 "name": "fibo",
131 "version-string": "0.1.0",
132 "dependencies": [
133 "cxxopts",
134 "fmt",
135 "range-v3"
136 ]
137}
138```
139
140Let's delete the build directory and rerun the build:
141
142```cmd
143D:\src\fibo> rmdir /S /Q build
144D:\src\fibo> cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=D:\src\vcpkg\scripts\buildsystems\vcpkg.cmake
145-- Running vcpkg install
146Detecting compiler hash for triplet x64-windows...
147The following packages will be built and installed:
148 cxxopts[core]:x64-windows
149 fmt[core]:x64-windows
150 range-v3[core]:x64-windows
151Starting package 1/3: cxxopts:x64-windows
152Building package cxxopts[core]:x64-windows...
153Using cached binary package: C:\Users\me\AppData\Local\vcpkg/archives\d2\d2d1e5302cdfefef2fd090d8eda84cc0c1fbe6f1.zip
154Building package cxxopts[core]:x64-windows... done
155Installing package cxxopts[core]:x64-windows...
156Installing package cxxopts[core]:x64-windows... done
157Elapsed time for package cxxopts:x64-windows: 50.64 ms
158Starting package 2/3: fmt:x64-windows
159Building package fmt[core]:x64-windows...
160Using cached binary package: C:\Users\me\AppData\Local\vcpkg/archives\bf\bf00d5214e912d71414b545b241f54ef87fdf6e5.zip
161Building package fmt[core]:x64-windows... done
162Installing package fmt[core]:x64-windows...
163Installing package fmt[core]:x64-windows... done
164Elapsed time for package fmt:x64-windows: 225 ms
165Starting package 3/3: range-v3:x64-windows
166Building package range-v3[core]:x64-windows...
167Using cached binary package: C:\Users\me\AppData\Local\vcpkg/archives\fe\fe2cdedef6953bf954e8ddca471bf3cc8d9b06d7.zip
168Building package range-v3[core]:x64-windows... done
169Installing package range-v3[core]:x64-windows...
170Installing package range-v3[core]:x64-windows... done
171Elapsed time for package range-v3:x64-windows: 1.466 s
172
173Total elapsed time: 1.742 s
174
175-- Running vcpkg install - done
176-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19041.
177-- The CXX compiler identification is MSVC 19.27.29111.0
178-- Detecting CXX compiler ABI info
179-- Detecting CXX compiler ABI info - done
180-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe - skipped
181-- Detecting CXX compile features
182-- Detecting CXX compile features - done
183-- Configuring done
184-- Generating done
185-- Build files have been written to: D:/src/fibo/build
186D:\src\fibo> cmake --build build
187Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET Framework
188Copyright (C) Microsoft Corporation. All rights reserved.
189
190 Checking Build System
191 Building Custom Rule D:/src/fibo/CMakeLists.txt
192 main.cxx
193 The contents of <span> are available only with C++20 or later.
194 fibo.vcxproj -> D:\src\fibo\build\Debug\fibo.exe
195 Building Custom Rule D:/src/fibo/CMakeLists.txt
196```
197
198You can see that with just a _single file_, we've changed over to manifests without _any_ trouble.
199The build system doesn't change _at all_! We just add a `vcpkg.json` file, delete the build directory,
200and reconfigure. And we're done!