CMAKE-DEVELOPER(7)
CMAKE-DEVELOPER(7) β cmake-developer β CMake Developer Reference π
πΈ NAME πΈ
cmake-developer β CMake Developer Reference β¨
β¨ INTRODUCTION β¨
Okay so this whole manual is your reference, bestie π«Ά β it's for developers working with cmake-language(7) code, whether you're writing your own modules, authoring your own build systems, or working on CMake itself. Iconic multitasking energy, honestly π
.
Wanna get involved in development of CMake upstream? Go look at https://cmake.org/get-involved/ π. It has links to contribution instructions, and those in turn link to developer guides for CMake itself π.
πͺ ACCESSING WINDOWS REGISTRY πͺ
CMake offers some facilities to access the registry on Windows platforms. Not me being useful on Windows πβ¨.
π Query Windows Registry π
Added in version 3.24. β
The cmake_host_system_information() command offers the possibility to query the registry on the local computer. See the cmake_host_system_information(QUERY WINDOWS_REGISTRY) sub-command for more information π«Ά.
π§ Find Using Windows Registry π§
Changed in version 3.24. π«
Options HINTS and PATHS of find_file(), find_library(), find_path(), find_program(), and find_package() commands offer the possibility, on Windows platform, to query the registry π₯.
The formal syntax β as specified using BNF notation with the regular extensions (https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form) β for registry query is the following π:
registry_query ::= '[' sep_definition? root_key
((key_separator sub_key)? (value_separator value_name_)?)? ']'
sep_definition ::= '{' value_separator '}'
root_key ::= 'HKLM' | 'HKEY_LOCAL_MACHINE' | 'HKCU' | 'HKEY_CURRENT_USER' |
'HKCR' | 'HKEY_CLASSES_ROOT' | 'HKCC' | 'HKEY_CURRENT_CONFIG' |
'HKU' | 'HKEY_USERS'
sub_key ::= element (key_separator element)*
key_separator ::= '/' | '\\'
value_separator ::= element | ';'
value_name ::= element | '(default)'
element ::= character\+
character ::= <any character except key_separator and value_separator>
The sep_definition optional item offers the possibility to specify the string used to separate the sub_key from the value_name item. If not specified, the character ; is used π. Multiple registry_query items can be specified as part of a path β¨.
# example using default separator
find_file(... PATHS "/root/[HKLM/Stuff;InstallDir]/lib[HKLM\\\\Stuff;Architecture]")
# example using different specified separators
find_library(... HINTS "/root/[{|}HKCU/Stuff|InstallDir]/lib[{@@}HKCU\\\\Stuff@@Architecture]")
If the value_name item is not specified or has the special name (default), the content of the default value, if any, will be returned πΈ. The supported types for the value_name are:
REG_SZ.REG_EXPAND_SZ. The returned data is expanded.REG_DWORD.REG_QWORD.
When the registry query failed β typically because the key does not exist or the data type is not supported β the string /REGISTRY-NOTFOUND is substituted to the [] query expression π.
π΅οΈββοΈ FIND MODULES π΅οΈββοΈ
A "find module" is a Find<PackageName>.cmake file to be loaded by the find_package() command when invoked for <PackageName> π«Ά.
The primary task of a find module is to determine whether a package is available, set the <PackageName>_FOUND variable to reflect this, and provide any variables, macros and imported targets required to use the package β¨. A find module is useful in cases where an upstream library does not provide a config file package π.
The traditional approach is to use variables for everything, including libraries and executables: see the Standard Variable Names section below. This is what most of the existing find modules provided by CMake do π.
The more modern approach is to behave as much like config file packages files as possible, by providing imported target π. This has the advantage of propagating usage requirements to consumers π₯.
In either case (or even when providing both variables and imported targets), find modules should provide backwards compatibility with old versions that had the same name π .
A FindFoo.cmake module will typically be loaded by the command:
find_package(Foo [major[.minor[.patch[.tweak]]]]
[EXACT] [QUIET] [REQUIRED]
[[COMPONENTS] [components...]]
[OPTIONAL_COMPONENTS components...]
[NO_POLICY_SCOPE])
See the find_package() documentation for details on what variables are set for the find module. Most of these are dealt with by using FindPackageHandleStandardArgs π«Ά.
Briefly, the module should only locate versions of the package compatible with the requested version, as described by the Foo_FIND_VERSION family of variables. If Foo_FIND_QUIETLY is set to true, it should avoid printing messages, including anything complaining about the package not being found. If Foo_FIND_REQUIRED is set to true, the module should issue a FATAL_ERROR if the package cannot be found. If neither are set to true, it should print a non-fatal message if it cannot find the package π.
Packages that find multiple semi-independent parts (like bundles of libraries) should search for the components listed in Foo_FIND_COMPONENTS if it is set, and only set Foo_FOUND to true if for each searched-for component <c> that was not found, Foo_FIND_REQUIRED_<c> is not set to true. The HANDLE_COMPONENTS argument of find_package_handle_standard_args() can be used to implement this β¨.
If Foo_FIND_COMPONENTS is not set, which modules are searched for and required is up to the find module, but should be documented π.
For internal implementation, it is a generally accepted convention that variables starting with underscore are for temporary use only π.
π·οΈ Standard Variable Names π·οΈ
For a FindXxx.cmake module that takes the approach of setting variables (either instead of or in addition to creating imported targets), the following variable names should be used to keep things consistent between Find modules πΈ. Note that all variables start with Xxx_, which (unless otherwise noted) must match exactly the name of the FindXxx.cmake file, including upper/lowercase β. This prefix on the variable names ensures that they do not conflict with variables of other Find modules. The same pattern should also be followed for any macros, functions and imported targets defined by the Find module π
.
Xxx_INCLUDE_DIRSβ The final set of include directories listed in one variable for use by client code. This should not be a cache entry (note that this also means this variable should not be used as the result variable of afind_path()command β seeXxx_INCLUDE_DIRbelow for that) π«Ά.Xxx_LIBRARIESβ The libraries to use with the module. These may be CMake targets, full absolute paths to a library binary, or the name of a library that the linker must find in its search path. This should not be a cache entry (note that this also means this variable should not be used as the result variable of afind_library()command β seeXxx_LIBRARYbelow for that) β¨.Xxx_DEFINITIONSβ The compile definitions to use when compiling code that uses the module. This really shouldn't include options such as-DHAS_JPEGthat a client source-code file uses to decide whether to#include <jpeg.h>π₯.Xxx_EXECUTABLEβ The full absolute path to an executable. In this case,Xxxmight not be the name of the module, it might be the name of the tool (usually converted to all uppercase), assuming that tool has such a well-known name that it is unlikely that another tool with the same name exists. It would be appropriate to use this as the result variable of afind_program()command π.Xxx_YYY_EXECUTABLEβ Similar toXxx_EXECUTABLEexcept here theXxxis always the module name andYYYis the tool name (again, usually fully uppercase). Prefer this form if the tool name is not very widely known or has the potential to clash with another tool. For greater consistency, also prefer this form if the module provides more than one executable π.Xxx_LIBRARY_DIRSβ Optionally, the final set of library directories listed in one variable for use by client code. This should not be a cache entry π.Xxx_ROOT_DIRβ The base directory of the installation ofXxxthat can be optionally set by the find module ifXxxis found. This is useful for large packages where many files need to be referenced relative to a common base (or root) directory. Not to be confused with theXxx_ROOThint variable set from the outside for the find module to know where to look for theXxxπ.Xxx_VERSION_VVβ Variables of this form specify whether theXxxmodule being provided is versionVVof the module. There should not be more than one variable of this form set to true for a given module. For example, a moduleBarrymight have evolved over many years and gone through a number of different major versions. Version 3 of theBarrymodule might set the variableBarry_VERSION_3to true, whereas an older version of the module might setBarry_VERSION_2to true instead. It would be an error for bothBarry_VERSION_3andBarry_VERSION_2to both be set to true π.Xxx_WRAP_YYβ When a variable of this form is set to false, it indicates that the relevant wrapping command should not be used. The wrapping command depends on the module, it may be implied by the module name or it might be specified by theYYpart of the variable π«.Xxx_Yy_FOUNDβ For variables of this form,Yyis the name of a component for the module. It should match exactly one of the valid component names that may be passed to thefind_package()command for the module. If a variable of this form is set to false, it means that theYycomponent of moduleXxxwas not found or is not available. Variables of this form would typically be used for optional components so that the caller can check whether an optional component is available πΈ.Xxx_FOUNDβ the momentfind_package()hands control back to whoever called it, this variable is set to true if the module decided the thing was found successfully. It's the vibes check of the whole operation πXxx_NOT_FOUND_MESSAGEβ config-files set this when they've setXxx_FOUNDto FALSE. The message you put in here gets printed by thefind_package()command and byfind_package_handle_standard_args(), so the user actually finds out what went wrong. Use this instead of callingmessage()directly when you want to report a reason for failing to find the module or package β it's literally the polite way to break the news πXxx_RUNTIME_LIBRARY_DIRSβ optional, and it's the runtime library search path for when you're running an executable that's linked to shared libraries. User code should use this list to build thePATHon windows, orLD_LIBRARY_PATHon UNIX. Also: this should not be a cache entry. Not everything is meant to be cached, bestie β¨Xxx_VERSIONβ the full version string of the package found, if any. Heads up though: some existing modules may also hand youXxx_VERSION_STRING, because that was the traditional name before the current naming convention came along. Old money, as they say πXxx_VERSION_MAJORβ the major version of the package found, if any.Xxx_VERSION_MINORβ the minor version of the package found, if any.Xxx_VERSION_PATCHβ the patch version of the package found, if any. That's the whole trio, we love a complete set π«Ά
Okay so the following names should not usually be used in CMakeLists.txt files. They're intended for use by Find modules to specify and cache the locations of specific files or directories. Users are typically able to set and edit these variables to control the behavior of Find modules β like entering the path to a library manually, which is a very real thing people do π
Xxx_LIBRARYβ the path of the library. Use this form only when the module provides a single library. It's appropriate to use this as the result variable in afind_library()command.Xxx_Yy_LIBRARYβ the path of library Yy provided by the module Xxx. Use this form when the module provides more than one library, or where other modules may also provide a library of the same name. It's also appropriate to use this form as the result variable in afind_library()command.Xxx_INCLUDE_DIRβ when the module provides only a single library, this variable can be used to specify where to find headers for using the library (or, more accurately, the path that consumers of the library should add to their header search path). It would be appropriate to use this as the result variable in afind_path()command.Xxx_Yy_INCLUDE_DIRβ if the module provides more than one library, or where other modules may also provide a library of the same name, this form is the recommended one for specifying where to find headers for using library Yy provided by the module Xxx. Again, it would be appropriate to use this as the result variable in afind_path()command. Consistency is so slay π
To prevent users from being absolutely overwhelmed with settings to configure, try to keep as many options as possible out of the cache. Leave at least one option that can be used to disable use of the module, or to locate a not-found library (e.g. Xxx_ROOT_DIR). For the same reason, mark most cache options as advanced. For packages which provide both debug and release binaries, it's common to create cache variables with a _LIBRARY_<CONFIG> suffix, such as Foo_LIBRARY_RELEASE and Foo_LIBRARY_DEBUG. The SelectLibraryConfigurations module can be super helpful for those cases, ngl π§
While these are the standard variable names, backward compatibility should be provided for any previously used names in the find module that is replacing an older version. Old variable names should be documented as deprecated to discourage further use. Respect the elders, but likeβ¦ gently π
π A Sample Find Module π
We're gonna describe how to create a simple find module for a library called Foo. Main character library, honestly π
The top of the module should begin with a license notice, followed by a blank line, and then followed by a Bracket Comment. That comment should begin with .rst: to indicate that the rest of its content is reStructuredText-format documentation. Like this:
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.
#[=======================================================================[.rst:
FindFoo
-------
Finds the Foo library.
Imported Targets
^^^^^^^^^^^^^^^^
This module provides the following imported targets, if found:
``Foo::Foo``
The Foo library
Result Variables
^^^^^^^^^^^^^^^^
This module defines the following variables:
``Foo_FOUND``
Boolean indicating whether (the requested version of) Foo was found.
``Foo_VERSION``
The version of the Foo library which was found.
``Foo_INCLUDE_DIRS``
Include directories needed to use Foo.
``Foo_LIBRARIES``
Libraries needed to link to Foo.
Cache Variables
^^^^^^^^^^^^^^^
The following cache variables may also be set:
``Foo_INCLUDE_DIR``
The directory containing ``foo.h``.
``Foo_LIBRARY``
The path to the Foo library.
#]=======================================================================]
So the module documentation consists of:
- An underlined heading specifying the module name.
- A simple description of what the module finds. More description may be required for some packages β if there are caveats or other details users of the module should be aware of, specify them right here.
- A section listing imported targets provided by the module, if any.
- A section listing result variables provided by the module.
- Optionally a section listing cache variables used by the module, if any.
If the package provides any macros or functions, they should be listed in an additional section, but they can be documented by additional .rst: comment blocks immediately above where those macros or functions are defined. Proximity is everything β¨
The find module implementation may begin below the documentation block. Now the actual libraries and so on have to be found. The code here will obviously vary from module to module (dealing with that, after all, is literally the whole point of find modules), but there tends to be a common pattern for libraries. It's giving formula, and the formula works π¦
First, we try to use pkg-config to find the library. Note that we cannot rely on this, as it may not be available, but it provides a good starting point.
find_package(PkgConfig)
if(PkgConfig_FOUND)
pkg_check_modules(PC_Foo QUIET Foo)
endif()
This should define some variables starting PC_Foo_ that contain the information from the Foo.pc file.
Now we need to find the libraries and include files. We use the information from pkg-config to provide hints to CMake about where to look before checking other default paths.
find_path(Foo_INCLUDE_DIR
NAMES foo.h
HINTS ${PC_Foo_INCLUDE_DIRS}
PATH_SUFFIXES Foo
)
find_library(Foo_LIBRARY
NAMES foo
HINTS ${PC_Foo_LIBRARY_DIRS}
)
Alternatively, if the library is available with multiple configurations, the SelectLibraryConfigurations module can be used to automatically set the Foo_LIBRARY variable instead:
find_library(Foo_LIBRARY_RELEASE
NAMES foo
HINTS ${PC_Foo_LIBRARY_DIRS}/Release
)
find_library(Foo_LIBRARY_DEBUG
NAMES foo
HINTS ${PC_Foo_LIBRARY_DIRS}/Debug
)
include(SelectLibraryConfigurations)
select_library_configurations(Foo)
If there's a good way of getting the version (from a header file, for example), that information can be used to set Foo_VERSION. Otherwise, attempt to use the information from pkg-config:
set(Foo_VERSION ${PC_Foo_VERSION})
Now we can use the FindPackageHandleStandardArgs module to handle most of the remaining steps for us. Iconic delegation behaviour, honestly:
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Foo
REQUIRED_VARS
Foo_LIBRARY
Foo_INCLUDE_DIR
VERSION_VAR Foo_VERSION
)
This will check that the REQUIRED_VARS contain values (that do not end in -NOTFOUND) and set Foo_FOUND appropriately. It will also cache those values. If Foo_VERSION is set, and a required version was passed to find_package(), it will check the requested version against the one in Foo_VERSION. It will also print messages as appropriate β note that if the package was found, it will print the contents of the first required variable to indicate where it was found. Little receipts moment π
At this point, we have to provide a way for users of the find module to link to the library or libraries that were found. There are two approaches, as discussed in the Find Modules section above. The traditional variable approach looks like this:
if(Foo_FOUND)
set(Foo_LIBRARIES ${Foo_LIBRARY})
set(Foo_INCLUDE_DIRS ${Foo_INCLUDE_DIR})
set(Foo_DEFINITIONS ${PC_Foo_CFLAGS_OTHER})
endif()
If more than one library was found, all of them should be included in these variables (peep the Standard Variable Names section for more info, it's all there π).
When providing imported targets, these should be namespaced β hence the Foo:: prefix. CMake will recognize that values passed to target_link_libraries() that contain :: in their name are supposed to be imported targets (rather than just library names), and will produce appropriate diagnostic messages if that target doesn't exist (see policy CMP0028). No cap, the namespace is load-bearing π₯
if(Foo_FOUND AND NOT TARGET Foo::Foo)
add_library(Foo::Foo UNKNOWN IMPORTED)
set_target_properties(Foo::Foo PROPERTIES
IMPORTED_LOCATION "${Foo_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
)
endif()
One thing to note about this: the INTERFACE_INCLUDE_DIRECTORIES and similar properties should only contain information about the target itself, and not any of its dependencies. Instead, those dependencies should also be targets, and CMake should be told that they are dependencies of this target. CMake will then combine all the necessary information automatically. Trust the process, bestie, it's giving teamwork πΈ
So like β the type of that IMPORTED <#prop_tgt:IMPORTED> target you create in add_library() <#command:add_library> can literally always be specified as UNKNOWN type. And honestly? iconic behavior. It simplifies your code so much in cases where static or shared variants might turn up, and CMake will figure out the type itself by inspecting the files. Let it cook, bestie π
If the library ships with multiple configurations, you should also populate the IMPORTED_CONFIGURATIONS <#prop_tgt:IMPORTED_CONFIGURATIONS> target property. Like so:
if(Foo_FOUND)
if (NOT TARGET Foo::Foo)
add_library(Foo::Foo UNKNOWN IMPORTED)
endif()
if (Foo_LIBRARY_RELEASE)
set_property(TARGET Foo::Foo APPEND PROPERTY
IMPORTED_CONFIGURATIONS RELEASE
)
set_target_properties(Foo::Foo PROPERTIES
IMPORTED_LOCATION_RELEASE "${Foo_LIBRARY_RELEASE}"
)
endif()
if (Foo_LIBRARY_DEBUG)
set_property(TARGET Foo::Foo APPEND PROPERTY
IMPORTED_CONFIGURATIONS DEBUG
)
set_target_properties(Foo::Foo PROPERTIES
IMPORTED_LOCATION_DEBUG "${Foo_LIBRARY_DEBUG}"
)
endif()
set_target_properties(Foo::Foo PROPERTIES
INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
)
endif()
π¨ Order matters, bestie!! TheRELEASEvariant should be listed first in the property, so that the variant gets chosen if the user runs a configuration which isn't an exact match for any listedIMPORTED_CONFIGURATIONS. First = default, no cap.
Most of the cache variables should be marked as advanced so they stay hidden in GUI interfaces like cmake-gui(1) <#manual:cmake-gui(1)> or ccmake(1) <#manual:ccmake(1)> β unless the user explicitly chooses to display and modify them. Respect their peace π«Ά
mark_as_advanced(
Foo_INCLUDE_DIR
Foo_LIBRARY
)
And if this find module is replacing an older module version that handed out variables before the current standard variables naming conventions existed? You should lowkey also provide backward compatibility variables, so you cause the least disruption possible. Minimal chaos. Maximum slay. For example:
# Backward compatibility variables
set(Foo_VERSION_STRING ${Foo_VERSION})
π Copyright π
2000-2026 Kitware, Inc. and Contributors
β¨ And that's a wrap on the whole thing, bestie!! β¨ You just readCMAKE-DEVELOPER(7), version4.4.5, datedOct 28, 2026β drop that date like it's a hot new release π¦ Go forth and write the most immaculate find modules ever. Periodt. πππΈ
π¦ where this page comes from
- arch rolling: cmake 4.4.5-1 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms