1CMAKE-DEVELOPER(7) CMake CMAKE-DEVELOPER(7)
2
3
4
6 cmake-developer - CMake Developer Reference
7
9 This manual is intended for reference by developers working with
10 cmake-language(7) code, whether writing their own modules, authoring
11 their own build systems, or working on CMake itself.
12
13 See https://cmake.org/get-involved/ to get involved in development of
14 CMake upstream. It includes links to contribution instructions, which
15 in turn link to developer guides for CMake itself.
16
18 A "find module" is a Find<PackageName>.cmake file to be loaded by the
19 find_package() command when invoked for <PackageName>.
20
21 The primary task of a find module is to determine whether a package is
22 available, set the <PackageName>_FOUND variable to reflect this and
23 provide any variables, macros and imported targets required to use the
24 package. A find module is useful in cases where an upstream library
25 does not provide a config file package.
26
27 The traditional approach is to use variables for everything, including
28 libraries and executables: see the Standard Variable Names section be‐
29 low. This is what most of the existing find modules provided by CMake
30 do.
31
32 The more modern approach is to behave as much like config file packages
33 files as possible, by providing imported target. This has the advan‐
34 tage of propagating Target Usage Requirements to consumers.
35
36 In either case (or even when providing both variables and imported tar‐
37 gets), find modules should provide backwards compatibility with old
38 versions that had the same name.
39
40 A FindFoo.cmake module will typically be loaded by the command:
41
42 find_package(Foo [major[.minor[.patch[.tweak]]]]
43 [EXACT] [QUIET] [REQUIRED]
44 [[COMPONENTS] [components...]]
45 [OPTIONAL_COMPONENTS components...]
46 [NO_POLICY_SCOPE])
47
48 See the find_package() documentation for details on what variables are
49 set for the find module. Most of these are dealt with by using Find‐
50 PackageHandleStandardArgs.
51
52 Briefly, the module should only locate versions of the package compati‐
53 ble with the requested version, as described by the Foo_FIND_VERSION
54 family of variables. If Foo_FIND_QUIETLY is set to true, it should
55 avoid printing messages, including anything complaining about the pack‐
56 age not being found. If Foo_FIND_REQUIRED is set to true, the module
57 should issue a FATAL_ERROR if the package cannot be found. If neither
58 are set to true, it should print a non-fatal message if it cannot find
59 the package.
60
61 Packages that find multiple semi-independent parts (like bundles of li‐
62 braries) should search for the components listed in Foo_FIND_COMPONENTS
63 if it is set , and only set Foo_FOUND to true if for each searched-for
64 component <c> that was not found, Foo_FIND_REQUIRED_<c> is not set to
65 true. The HANDLE_COMPONENTS argument of find_package_handle_stan‐
66 dard_args() can be used to implement this.
67
68 If Foo_FIND_COMPONENTS is not set, which modules are searched for and
69 required is up to the find module, but should be documented.
70
71 For internal implementation, it is a generally accepted convention that
72 variables starting with underscore are for temporary use only.
73
74 Standard Variable Names
75 For a FindXxx.cmake module that takes the approach of setting variables
76 (either instead of or in addition to creating imported targets), the
77 following variable names should be used to keep things consistent be‐
78 tween Find modules. Note that all variables start with Xxx_, which
79 (unless otherwise noted) must match exactly the name of the Find‐
80 Xxx.cmake file, including upper/lowercase. This prefix on the variable
81 names ensures that they do not conflict with variables of other Find
82 modules. The same pattern should also be followed for any macros,
83 functions and imported targets defined by the Find module.
84
85 Xxx_INCLUDE_DIRS
86 The final set of include directories listed in one variable for
87 use by client code. This should not be a cache entry (note that
88 this also means this variable should not be used as the result
89 variable of a find_path() command - see Xxx_INCLUDE_DIR below
90 for that).
91
92 Xxx_LIBRARIES
93 The libraries to use with the module. These may be CMake tar‐
94 gets, full absolute paths to a library binary or the name of a
95 library that the linker must find in its search path. This
96 should not be a cache entry (note that this also means this
97 variable should not be used as the result variable of a find_li‐
98 brary() command - see Xxx_LIBRARY below for that).
99
100 Xxx_DEFINITIONS
101 The compile definitions to use when compiling code that uses the
102 module. This really shouldn't include options such as
103 -DHAS_JPEG that a client source-code file uses to decide whether
104 to #include <jpeg.h>
105
106 Xxx_EXECUTABLE
107 The full absolute path to an executable. In this case, Xxx
108 might not be the name of the module, it might be the name of the
109 tool (usually converted to all uppercase), assuming that tool
110 has such a well-known name that it is unlikely that another tool
111 with the same name exists. It would be appropriate to use this
112 as the result variable of a find_program() command.
113
114 Xxx_YYY_EXECUTABLE
115 Similar to Xxx_EXECUTABLE except here the Xxx is always the mod‐
116 ule name and YYY is the tool name (again, usually fully upper‐
117 case). Prefer this form if the tool name is not very widely
118 known or has the potential to clash with another tool. For
119 greater consistency, also prefer this form if the module pro‐
120 vides more than one executable.
121
122 Xxx_LIBRARY_DIRS
123 Optionally, the final set of library directories listed in one
124 variable for use by client code. This should not be a cache en‐
125 try.
126
127 Xxx_ROOT_DIR
128 Where to find the base directory of the module.
129
130 Xxx_VERSION_VV
131 Variables of this form specify whether the Xxx module being pro‐
132 vided is version VV of the module. There should not be more
133 than one variable of this form set to true for a given module.
134 For example, a module Barry might have evolved over many years
135 and gone through a number of different major versions. Version
136 3 of the Barry module might set the variable Barry_VERSION_3 to
137 true, whereas an older version of the module might set
138 Barry_VERSION_2 to true instead. It would be an error for both
139 Barry_VERSION_3 and Barry_VERSION_2 to both be set to true.
140
141 Xxx_WRAP_YY
142 When a variable of this form is set to false, it indicates that
143 the relevant wrapping command should not be used. The wrapping
144 command depends on the module, it may be implied by the module
145 name or it might be specified by the YY part of the variable.
146
147 Xxx_Yy_FOUND
148 For variables of this form, Yy is the name of a component for
149 the module. It should match exactly one of the valid component
150 names that may be passed to the find_package() command for the
151 module. If a variable of this form is set to false, it means
152 that the Yy component of module Xxx was not found or is not
153 available. Variables of this form would typically be used for
154 optional components so that the caller can check whether an op‐
155 tional component is available.
156
157 Xxx_FOUND
158 When the find_package() command returns to the caller, this
159 variable will be set to true if the module was deemed to have
160 been found successfully.
161
162 Xxx_NOT_FOUND_MESSAGE
163 Should be set by config-files in the case that it has set
164 Xxx_FOUND to FALSE. The contained message will be printed by
165 the find_package() command and by find_package_handle_stan‐
166 dard_args() to inform the user about the problem. Use this in‐
167 stead of calling message() directly to report a reason for fail‐
168 ing to find the module or package.
169
170 Xxx_RUNTIME_LIBRARY_DIRS
171 Optionally, the runtime library search path for use when running
172 an executable linked to shared libraries. The list should be
173 used by user code to create the PATH on windows or LD_LI‐
174 BRARY_PATH on UNIX. This should not be a cache entry.
175
176 Xxx_VERSION
177 The full version string of the package found, if any. Note that
178 many existing modules provide Xxx_VERSION_STRING instead.
179
180 Xxx_VERSION_MAJOR
181 The major version of the package found, if any.
182
183 Xxx_VERSION_MINOR
184 The minor version of the package found, if any.
185
186 Xxx_VERSION_PATCH
187 The patch version of the package found, if any.
188
189 The following names should not usually be used in CMakeLists.txt files.
190 They are intended for use by Find modules to specify and cache the lo‐
191 cations of specific files or directories. Users are typically able to
192 set and edit these variables to control the behavior of Find modules
193 (like entering the path to a library manually):
194
195 Xxx_LIBRARY
196 The path of the library. Use this form only when the module
197 provides a single library. It is appropriate to use this as the
198 result variable in a find_library() command.
199
200 Xxx_Yy_LIBRARY
201 The path of library Yy provided by the module Xxx. Use this
202 form when the module provides more than one library or where
203 other modules may also provide a library of the same name. It is
204 also appropriate to use this form as the result variable in a
205 find_library() command.
206
207 Xxx_INCLUDE_DIR
208 When the module provides only a single library, this variable
209 can be used to specify where to find headers for using the li‐
210 brary (or more accurately, the path that consumers of the li‐
211 brary should add to their header search path). It would be ap‐
212 propriate to use this as the result variable in a find_path()
213 command.
214
215 Xxx_Yy_INCLUDE_DIR
216 If the module provides more than one library or where other mod‐
217 ules may also provide a library of the same name, this form is
218 recommended for specifying where to find headers for using li‐
219 brary Yy provided by the module. Again, it would be appropriate
220 to use this as the result variable in a find_path() command.
221
222 To prevent users being overwhelmed with settings to configure, try to
223 keep as many options as possible out of the cache, leaving at least one
224 option which can be used to disable use of the module, or locate a
225 not-found library (e.g. Xxx_ROOT_DIR). For the same reason, mark most
226 cache options as advanced. For packages which provide both debug and
227 release binaries, it is common to create cache variables with a _LI‐
228 BRARY_<CONFIG> suffix, such as Foo_LIBRARY_RELEASE and Foo_LIBRARY_DE‐
229 BUG. The SelectLibraryConfigurations module can be helpful for such
230 cases.
231
232 While these are the standard variable names, you should provide back‐
233 wards compatibility for any old names that were actually in use. Make
234 sure you comment them as deprecated, so that no-one starts using them.
235
236 A Sample Find Module
237 We will describe how to create a simple find module for a library Foo.
238
239 The top of the module should begin with a license notice, followed by a
240 blank line, and then followed by a Bracket Comment. The comment should
241 begin with .rst: to indicate that the rest of its content is reStruc‐
242 turedText-format documentation. For example:
243
244 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
245 # file Copyright.txt or https://cmake.org/licensing for details.
246
247 #[=======================================================================[.rst:
248 FindFoo
249 -------
250
251 Finds the Foo library.
252
253 Imported Targets
254 ^^^^^^^^^^^^^^^^
255
256 This module provides the following imported targets, if found:
257
258 ``Foo::Foo``
259 The Foo library
260
261 Result Variables
262 ^^^^^^^^^^^^^^^^
263
264 This will define the following variables:
265
266 ``Foo_FOUND``
267 True if the system has the Foo library.
268 ``Foo_VERSION``
269 The version of the Foo library which was found.
270 ``Foo_INCLUDE_DIRS``
271 Include directories needed to use Foo.
272 ``Foo_LIBRARIES``
273 Libraries needed to link to Foo.
274
275 Cache Variables
276 ^^^^^^^^^^^^^^^
277
278 The following cache variables may also be set:
279
280 ``Foo_INCLUDE_DIR``
281 The directory containing ``foo.h``.
282 ``Foo_LIBRARY``
283 The path to the Foo library.
284
285 #]=======================================================================]
286
287 The module documentation consists of:
288
289 • An underlined heading specifying the module name.
290
291 • A simple description of what the module finds. More description may
292 be required for some packages. If there are caveats or other details
293 users of the module should be aware of, specify them here.
294
295 • A section listing imported targets provided by the module, if any.
296
297 • A section listing result variables provided by the module.
298
299 • Optionally a section listing cache variables used by the module, if
300 any.
301
302 If the package provides any macros or functions, they should be listed
303 in an additional section, but can be documented by additional .rst:
304 comment blocks immediately above where those macros or functions are
305 defined.
306
307 The find module implementation may begin below the documentation block.
308 Now the actual libraries and so on have to be found. The code here
309 will obviously vary from module to module (dealing with that, after
310 all, is the point of find modules), but there tends to be a common pat‐
311 tern for libraries.
312
313 First, we try to use pkg-config to find the library. Note that we can‐
314 not rely on this, as it may not be available, but it provides a good
315 starting point.
316
317 find_package(PkgConfig)
318 pkg_check_modules(PC_Foo QUIET Foo)
319
320 This should define some variables starting PC_Foo_ that contain the in‐
321 formation from the Foo.pc file.
322
323 Now we need to find the libraries and include files; we use the infor‐
324 mation from pkg-config to provide hints to CMake about where to look.
325
326 find_path(Foo_INCLUDE_DIR
327 NAMES foo.h
328 PATHS ${PC_Foo_INCLUDE_DIRS}
329 PATH_SUFFIXES Foo
330 )
331 find_library(Foo_LIBRARY
332 NAMES foo
333 PATHS ${PC_Foo_LIBRARY_DIRS}
334 )
335
336 Alternatively, if the library is available with multiple configura‐
337 tions, you can use SelectLibraryConfigurations to automatically set the
338 Foo_LIBRARY variable instead:
339
340 find_library(Foo_LIBRARY_RELEASE
341 NAMES foo
342 PATHS ${PC_Foo_LIBRARY_DIRS}/Release
343 )
344 find_library(Foo_LIBRARY_DEBUG
345 NAMES foo
346 PATHS ${PC_Foo_LIBRARY_DIRS}/Debug
347 )
348
349 include(SelectLibraryConfigurations)
350 select_library_configurations(Foo)
351
352 If you have a good way of getting the version (from a header file, for
353 example), you can use that information to set Foo_VERSION (although
354 note that find modules have traditionally used Foo_VERSION_STRING, so
355 you may want to set both). Otherwise, attempt to use the information
356 from pkg-config
357
358 set(Foo_VERSION ${PC_Foo_VERSION})
359
360 Now we can use FindPackageHandleStandardArgs to do most of the rest of
361 the work for us
362
363 include(FindPackageHandleStandardArgs)
364 find_package_handle_standard_args(Foo
365 FOUND_VAR Foo_FOUND
366 REQUIRED_VARS
367 Foo_LIBRARY
368 Foo_INCLUDE_DIR
369 VERSION_VAR Foo_VERSION
370 )
371
372 This will check that the REQUIRED_VARS contain values (that do not end
373 in -NOTFOUND) and set Foo_FOUND appropriately. It will also cache
374 those values. If Foo_VERSION is set, and a required version was passed
375 to find_package(), it will check the requested version against the one
376 in Foo_VERSION. It will also print messages as appropriate; note that
377 if the package was found, it will print the contents of the first re‐
378 quired variable to indicate where it was found.
379
380 At this point, we have to provide a way for users of the find module to
381 link to the library or libraries that were found. There are two ap‐
382 proaches, as discussed in the Find Modules section above. The tradi‐
383 tional variable approach looks like
384
385 if(Foo_FOUND)
386 set(Foo_LIBRARIES ${Foo_LIBRARY})
387 set(Foo_INCLUDE_DIRS ${Foo_INCLUDE_DIR})
388 set(Foo_DEFINITIONS ${PC_Foo_CFLAGS_OTHER})
389 endif()
390
391 If more than one library was found, all of them should be included in
392 these variables (see the Standard Variable Names section for more in‐
393 formation).
394
395 When providing imported targets, these should be namespaced (hence the
396 Foo:: prefix); CMake will recognize that values passed to tar‐
397 get_link_libraries() that contain :: in their name are supposed to be
398 imported targets (rather than just library names), and will produce ap‐
399 propriate diagnostic messages if that target does not exist (see policy
400 CMP0028).
401
402 if(Foo_FOUND AND NOT TARGET Foo::Foo)
403 add_library(Foo::Foo UNKNOWN IMPORTED)
404 set_target_properties(Foo::Foo PROPERTIES
405 IMPORTED_LOCATION "${Foo_LIBRARY}"
406 INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
407 INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
408 )
409 endif()
410
411 One thing to note about this is that the INTERFACE_INCLUDE_DIRECTORIES
412 and similar properties should only contain information about the target
413 itself, and not any of its dependencies. Instead, those dependencies
414 should also be targets, and CMake should be told that they are depen‐
415 dencies of this target. CMake will then combine all the necessary in‐
416 formation automatically.
417
418 The type of the IMPORTED target created in the add_library() command
419 can always be specified as UNKNOWN type. This simplifies the code in
420 cases where static or shared variants may be found, and CMake will de‐
421 termine the type by inspecting the files.
422
423 If the library is available with multiple configurations, the IM‐
424 PORTED_CONFIGURATIONS target property should also be populated:
425
426 if(Foo_FOUND)
427 if (NOT TARGET Foo::Foo)
428 add_library(Foo::Foo UNKNOWN IMPORTED)
429 endif()
430 if (Foo_LIBRARY_RELEASE)
431 set_property(TARGET Foo::Foo APPEND PROPERTY
432 IMPORTED_CONFIGURATIONS RELEASE
433 )
434 set_target_properties(Foo::Foo PROPERTIES
435 IMPORTED_LOCATION_RELEASE "${Foo_LIBRARY_RELEASE}"
436 )
437 endif()
438 if (Foo_LIBRARY_DEBUG)
439 set_property(TARGET Foo::Foo APPEND PROPERTY
440 IMPORTED_CONFIGURATIONS DEBUG
441 )
442 set_target_properties(Foo::Foo PROPERTIES
443 IMPORTED_LOCATION_DEBUG "${Foo_LIBRARY_DEBUG}"
444 )
445 endif()
446 set_target_properties(Foo::Foo PROPERTIES
447 INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
448 INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
449 )
450 endif()
451
452 The RELEASE variant should be listed first in the property so that the
453 variant is chosen if the user uses a configuration which is not an ex‐
454 act match for any listed IMPORTED_CONFIGURATIONS.
455
456 Most of the cache variables should be hidden in the ccmake interface
457 unless the user explicitly asks to edit them.
458
459 mark_as_advanced(
460 Foo_INCLUDE_DIR
461 Foo_LIBRARY
462 )
463
464 If this module replaces an older version, you should set compatibility
465 variables to cause the least disruption possible.
466
467 # compatibility variables
468 set(Foo_VERSION_STRING ${Foo_VERSION})
469
471 2000-2022 Kitware, Inc. and Contributors
472
473
474
475
4763.22.2 Jan 25, 2022 CMAKE-DEVELOPER(7)