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
22 exists on the system, set the <PackageName>_FOUND variable to reflect
23 this and provide any variables, macros and imported targets required to
24 use the package. A find module is useful in cases where an upstream
25 library 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
29 below. This is what most of the existing find modules provided by
30 CMake 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
62 libraries) should search for the components listed in Foo_FIND_COMPO‐
63 NENTS if it is set , and only set Foo_FOUND to true if for each
64 searched-for component <c> that was not found, Foo_FIND_REQUIRED_<c> is
65 not set to true. The HANDLE_COMPONENTS argument of find_package_han‐
66 dle_standard_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
78 between find modules. Note that all variables start with Xxx_ to make
79 sure they do not interfere with other find modules; the same considera‐
80 tion applies to macros, functions and imported targets.
81
82 Xxx_INCLUDE_DIRS
83 The final set of include directories listed in one variable for
84 use by client code. This should not be a cache entry.
85
86 Xxx_LIBRARIES
87 The libraries to link against to use Xxx. These should include
88 full paths. This should not be a cache entry.
89
90 Xxx_DEFINITIONS
91 Definitions to use when compiling code that uses Xxx. This
92 really shouldn’t include options such as -DHAS_JPEG that a
93 client source-code file uses to decide whether to #include
94 <jpeg.h>
95
96 Xxx_EXECUTABLE
97 Where to find the Xxx tool.
98
99 Xxx_Yyy_EXECUTABLE
100 Where to find the Yyy tool that comes with Xxx.
101
102 Xxx_LIBRARY_DIRS
103 Optionally, the final set of library directories listed in one
104 variable for use by client code. This should not be a cache
105 entry.
106
107 Xxx_ROOT_DIR
108 Where to find the base directory of Xxx.
109
110 Xxx_VERSION_Yy
111 Expect Version Yy if true. Make sure at most one of these is
112 ever true.
113
114 Xxx_WRAP_Yy
115 If False, do not try to use the relevant CMake wrapping command.
116
117 Xxx_Yy_FOUND
118 If False, optional Yy part of Xxx system is not available.
119
120 Xxx_FOUND
121 Set to false, or undefined, if we haven’t found, or don’t want
122 to use Xxx.
123
124 Xxx_NOT_FOUND_MESSAGE
125 Should be set by config-files in the case that it has set
126 Xxx_FOUND to FALSE. The contained message will be printed by
127 the find_package() command and by find_package_handle_stan‐
128 dard_args() to inform the user about the problem.
129
130 Xxx_RUNTIME_LIBRARY_DIRS
131 Optionally, the runtime library search path for use when running
132 an executable linked to shared libraries. The list should be
133 used by user code to create the PATH on windows or
134 LD_LIBRARY_PATH on UNIX. This should not be a cache entry.
135
136 Xxx_VERSION
137 The full version string of the package found, if any. Note that
138 many existing modules provide Xxx_VERSION_STRING instead.
139
140 Xxx_VERSION_MAJOR
141 The major version of the package found, if any.
142
143 Xxx_VERSION_MINOR
144 The minor version of the package found, if any.
145
146 Xxx_VERSION_PATCH
147 The patch version of the package found, if any.
148
149 The following names should not usually be used in CMakeLists.txt files,
150 but are typically cache variables for users to edit and control the be‐
151 haviour of find modules (like entering the path to a library manually)
152
153 Xxx_LIBRARY
154 The path of the Xxx library (as used with find_library(), for
155 example).
156
157 Xxx_Yy_LIBRARY
158 The path of the Yy library that is part of the Xxx system. It
159 may or may not be required to use Xxx.
160
161 Xxx_INCLUDE_DIR
162 Where to find headers for using the Xxx library.
163
164 Xxx_Yy_INCLUDE_DIR
165 Where to find headers for using the Yy library of the Xxx sys‐
166 tem.
167
168 To prevent users being overwhelmed with settings to configure, try to
169 keep as many options as possible out of the cache, leaving at least one
170 option which can be used to disable use of the module, or locate a
171 not-found library (e.g. Xxx_ROOT_DIR). For the same reason, mark most
172 cache options as advanced. For packages which provide both debug and
173 release binaries, it is common to create cache variables with a
174 _LIBRARY_<CONFIG> suffix, such as Foo_LIBRARY_RELEASE and
175 Foo_LIBRARY_DEBUG.
176
177 While these are the standard variable names, you should provide back‐
178 wards compatibility for any old names that were actually in use. Make
179 sure you comment them as deprecated, so that no-one starts using them.
180
181 A Sample Find Module
182 We will describe how to create a simple find module for a library Foo.
183
184 The top of the module should begin with a license notice, followed by a
185 blank line, and then followed by a Bracket Comment. The comment should
186 begin with .rst: to indicate that the rest of its content is reStruc‐
187 turedText-format documentation. For example:
188
189 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
190 # file Copyright.txt or https://cmake.org/licensing for details.
191
192 #[=======================================================================[.rst:
193 FindFoo
194 -------
195
196 Finds the Foo library.
197
198 Imported Targets
199 ^^^^^^^^^^^^^^^^
200
201 This module provides the following imported targets, if found:
202
203 ``Foo::Foo``
204 The Foo library
205
206 Result Variables
207 ^^^^^^^^^^^^^^^^
208
209 This will define the following variables:
210
211 ``Foo_FOUND``
212 True if the system has the Foo library.
213 ``Foo_VERSION``
214 The version of the Foo library which was found.
215 ``Foo_INCLUDE_DIRS``
216 Include directories needed to use Foo.
217 ``Foo_LIBRARIES``
218 Libraries needed to link to Foo.
219
220 Cache Variables
221 ^^^^^^^^^^^^^^^
222
223 The following cache variables may also be set:
224
225 ``Foo_INCLUDE_DIR``
226 The directory containing ``foo.h``.
227 ``Foo_LIBRARY``
228 The path to the Foo library.
229
230 #]=======================================================================]
231
232 The module documentation consists of:
233
234 · An underlined heading specifying the module name.
235
236 · A simple description of what the module finds. More description may
237 be required for some packages. If there are caveats or other details
238 users of the module should be aware of, specify them here.
239
240 · A section listing imported targets provided by the module, if any.
241
242 · A section listing result variables provided by the module.
243
244 · Optionally a section listing cache variables used by the module, if
245 any.
246
247 If the package provides any macros or functions, they should be listed
248 in an additional section, but can be documented by additional .rst:
249 comment blocks immediately above where those macros or functions are
250 defined.
251
252 The find module implementation may begin below the documentation block.
253 Now the actual libraries and so on have to be found. The code here
254 will obviously vary from module to module (dealing with that, after
255 all, is the point of find modules), but there tends to be a common pat‐
256 tern for libraries.
257
258 First, we try to use pkg-config to find the library. Note that we can‐
259 not rely on this, as it may not be available, but it provides a good
260 starting point.
261
262 find_package(PkgConfig)
263 pkg_check_modules(PC_Foo QUIET Foo)
264
265 This should define some variables starting PC_Foo_ that contain the
266 information from the Foo.pc file.
267
268 Now we need to find the libraries and include files; we use the infor‐
269 mation from pkg-config to provide hints to CMake about where to look.
270
271 find_path(Foo_INCLUDE_DIR
272 NAMES foo.h
273 PATHS ${PC_Foo_INCLUDE_DIRS}
274 PATH_SUFFIXES Foo
275 )
276 find_library(Foo_LIBRARY
277 NAMES foo
278 PATHS ${PC_Foo_LIBRARY_DIRS}
279 )
280
281 If you have a good way of getting the version (from a header file, for
282 example), you can use that information to set Foo_VERSION (although
283 note that find modules have traditionally used Foo_VERSION_STRING, so
284 you may want to set both). Otherwise, attempt to use the information
285 from pkg-config
286
287 set(Foo_VERSION ${PC_Foo_VERSION})
288
289 Now we can use FindPackageHandleStandardArgs to do most of the rest of
290 the work for us
291
292 include(FindPackageHandleStandardArgs)
293 find_package_handle_standard_args(Foo
294 FOUND_VAR Foo_FOUND
295 REQUIRED_VARS
296 Foo_LIBRARY
297 Foo_INCLUDE_DIR
298 VERSION_VAR Foo_VERSION
299 )
300
301 This will check that the REQUIRED_VARS contain values (that do not end
302 in -NOTFOUND) and set Foo_FOUND appropriately. It will also cache
303 those values. If Foo_VERSION is set, and a required version was passed
304 to find_package(), it will check the requested version against the one
305 in Foo_VERSION. It will also print messages as appropriate; note that
306 if the package was found, it will print the contents of the first
307 required variable to indicate where it was found.
308
309 At this point, we have to provide a way for users of the find module to
310 link to the library or libraries that were found. There are two
311 approaches, as discussed in the Find Modules section above. The tradi‐
312 tional variable approach looks like
313
314 if(Foo_FOUND)
315 set(Foo_LIBRARIES ${Foo_LIBRARY})
316 set(Foo_INCLUDE_DIRS ${Foo_INCLUDE_DIR})
317 set(Foo_DEFINITIONS ${PC_Foo_CFLAGS_OTHER})
318 endif()
319
320 If more than one library was found, all of them should be included in
321 these variables (see the Standard Variable Names section for more
322 information).
323
324 When providing imported targets, these should be namespaced (hence the
325 Foo:: prefix); CMake will recognize that values passed to tar‐
326 get_link_libraries() that contain :: in their name are supposed to be
327 imported targets (rather than just library names), and will produce
328 appropriate diagnostic messages if that target does not exist (see pol‐
329 icy CMP0028).
330
331 if(Foo_FOUND AND NOT TARGET Foo::Foo)
332 add_library(Foo::Foo UNKNOWN IMPORTED)
333 set_target_properties(Foo::Foo PROPERTIES
334 IMPORTED_LOCATION "${Foo_LIBRARY}"
335 INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
336 INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
337 )
338 endif()
339
340 One thing to note about this is that the INTERFACE_INCLUDE_DIRECTORIES
341 and similar properties should only contain information about the target
342 itself, and not any of its dependencies. Instead, those dependencies
343 should also be targets, and CMake should be told that they are depen‐
344 dencies of this target. CMake will then combine all the necessary
345 information automatically.
346
347 The type of the IMPORTED target created in the add_library() command
348 can always be specified as UNKNOWN type. This simplifies the code in
349 cases where static or shared variants may be found, and CMake will
350 determine the type by inspecting the files.
351
352 If the library is available with multiple configurations, the
353 IMPORTED_CONFIGURATIONS target property should also be populated:
354
355 if(Foo_FOUND)
356 if (NOT TARGET Foo::Foo)
357 add_library(Foo::Foo UNKNOWN IMPORTED)
358 endif()
359 if (Foo_LIBRARY_RELEASE)
360 set_property(TARGET Foo::Foo APPEND PROPERTY
361 IMPORTED_CONFIGURATIONS RELEASE
362 )
363 set_target_properties(Foo::Foo PROPERTIES
364 IMPORTED_LOCATION_RELEASE "${Foo_LIBRARY_RELEASE}"
365 )
366 endif()
367 if (Foo_LIBRARY_DEBUG)
368 set_property(TARGET Foo::Foo APPEND PROPERTY
369 IMPORTED_CONFIGURATIONS DEBUG
370 )
371 set_target_properties(Foo::Foo PROPERTIES
372 IMPORTED_LOCATION_DEBUG "${Foo_LIBRARY_DEBUG}"
373 )
374 endif()
375 set_target_properties(Foo::Foo PROPERTIES
376 INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
377 INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
378 )
379 endif()
380
381 The RELEASE variant should be listed first in the property so that the
382 variant is chosen if the user uses a configuration which is not an
383 exact match for any listed IMPORTED_CONFIGURATIONS.
384
385 Most of the cache variables should be hidden in the ccmake interface
386 unless the user explicitly asks to edit them.
387
388 mark_as_advanced(
389 Foo_INCLUDE_DIR
390 Foo_LIBRARY
391 )
392
393 If this module replaces an older version, you should set compatibility
394 variables to cause the least disruption possible.
395
396 # compatibility variables
397 set(Foo_VERSION_STRING ${Foo_VERSION})
398
400 2000-2019 Kitware, Inc. and Contributors
401
402
403
404
4053.16.1 Dec 14, 2019 CMAKE-DEVELOPER(7)