]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | # Manifest Mode: CMake Example |
2 | ||
3 | We would like to add [vcpkg manifest support](../users/manifests.md) to an existing cmake project! | |
4 | Let's create a simple project that prints the fibonacci sequence up to a certain number, | |
5 | using some common dependencies. | |
6 | ||
7 | ## Initial Layout | |
8 | ||
9 | Let's create the following file layout: | |
10 | ||
11 | ```no-highlight | |
12 | fibo/ | |
13 | src/ | |
14 | main.cxx | |
15 | CMakeLists.txt | |
16 | ``` | |
17 | ||
18 | And we wish to use [fmt](https://github.com/fmtlib/fmt), [range-v3](https://github.com/ericniebler/range-v3), | |
19 | and [cxxopts](https://github.com/jarro2783/cxxopts). | |
20 | ||
21 | Let's write our `CMakeLists.txt` first: | |
22 | ||
23 | ```cmake | |
24 | cmake_minimum_required(VERSION 3.15) | |
25 | ||
26 | project(fibo CXX) | |
27 | ||
28 | find_package(fmt REQUIRED) | |
29 | find_package(range-v3 REQUIRED) | |
30 | find_package(cxxopts REQUIRED) | |
31 | ||
32 | add_executable(fibo src/main.cxx) | |
33 | target_compile_features(fibo PRIVATE cxx_std_17) | |
34 | ||
35 | target_link_libraries(fibo | |
36 | PRIVATE | |
37 | fmt::fmt | |
38 | range-v3::range-v3 | |
39 | cxxopts::cxxopts) | |
40 | ``` | |
41 | ||
42 | And 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 | ||
49 | namespace view = ranges::views; | |
50 | ||
51 | int 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 | ||
64 | int 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 | ||
78 | This is a simple project of course, but it should give us a clean project to start with. | |
79 | Let's try it out! | |
80 | ||
81 | Let's assume you have `fmt`, `range-v3`, and `cxxopts` installed with vcpkg classic mode; | |
82 | then, you can just do a simple: | |
83 | ||
84 | ```cmd | |
85 | D:\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 | |
97 | D:\src\fibo> cmake --build build | |
98 | Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET Framework | |
99 | Copyright (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 | ||
109 | And now we can try out the `fibo` binary! | |
110 | ||
111 | ```cmd | |
112 | D:\src\fibo> .\build\Debug\fibo.exe -n 7 | |
113 | fib(1) = 1 | |
114 | fib(2) = 1 | |
115 | fib(3) = 2 | |
116 | fib(4) = 3 | |
117 | fib(5) = 5 | |
118 | fib(6) = 8 | |
119 | fib(7) = 13 | |
120 | ``` | |
121 | ||
122 | it works! | |
123 | ||
124 | ## Converting to Manifest Mode | |
125 | ||
126 | We 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 | ||
140 | Let's delete the build directory and rerun the build: | |
141 | ||
142 | ```cmd | |
143 | D:\src\fibo> rmdir /S /Q build | |
144 | D:\src\fibo> cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=D:\src\vcpkg\scripts\buildsystems\vcpkg.cmake | |
145 | -- Running vcpkg install | |
146 | Detecting compiler hash for triplet x64-windows... | |
147 | The following packages will be built and installed: | |
148 | cxxopts[core]:x64-windows | |
149 | fmt[core]:x64-windows | |
150 | range-v3[core]:x64-windows | |
151 | Starting package 1/3: cxxopts:x64-windows | |
152 | Building package cxxopts[core]:x64-windows... | |
153 | Using cached binary package: C:\Users\me\AppData\Local\vcpkg/archives\d2\d2d1e5302cdfefef2fd090d8eda84cc0c1fbe6f1.zip | |
154 | Building package cxxopts[core]:x64-windows... done | |
155 | Installing package cxxopts[core]:x64-windows... | |
156 | Installing package cxxopts[core]:x64-windows... done | |
157 | Elapsed time for package cxxopts:x64-windows: 50.64 ms | |
158 | Starting package 2/3: fmt:x64-windows | |
159 | Building package fmt[core]:x64-windows... | |
160 | Using cached binary package: C:\Users\me\AppData\Local\vcpkg/archives\bf\bf00d5214e912d71414b545b241f54ef87fdf6e5.zip | |
161 | Building package fmt[core]:x64-windows... done | |
162 | Installing package fmt[core]:x64-windows... | |
163 | Installing package fmt[core]:x64-windows... done | |
164 | Elapsed time for package fmt:x64-windows: 225 ms | |
165 | Starting package 3/3: range-v3:x64-windows | |
166 | Building package range-v3[core]:x64-windows... | |
167 | Using cached binary package: C:\Users\me\AppData\Local\vcpkg/archives\fe\fe2cdedef6953bf954e8ddca471bf3cc8d9b06d7.zip | |
168 | Building package range-v3[core]:x64-windows... done | |
169 | Installing package range-v3[core]:x64-windows... | |
170 | Installing package range-v3[core]:x64-windows... done | |
171 | Elapsed time for package range-v3:x64-windows: 1.466 s | |
172 | ||
173 | Total 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 | |
186 | D:\src\fibo> cmake --build build | |
187 | Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET Framework | |
188 | Copyright (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 | ||
198 | You can see that with just a _single file_, we've changed over to manifests without _any_ trouble. | |
199 | The build system doesn't change _at all_! We just add a `vcpkg.json` file, delete the build directory, | |
200 | and reconfigure. And we're done! |