.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "ECM-DEVELOPER" "7" "Nov 08, 2024" "6.8" "Extra CMake Modules" .SH NAME ecm-developer \- ECM Developer Reference .SH WRITING MODULES .sp The CMake 3 documentation (and \X'tty: link https://www.cmake.org/cmake/help/git-master/manual/cmake-developer.7.html'\fI\%cmake\-developer(7)\fP\X'tty: link' in particular) has a lot of useful information about writing CMake modules, including a large section devoted to find modules. This guide will only highlight things that are particular to the Extra CMake Modules project. .sp Most of these are stylistic points. For example, the license header for a module in ECM should look like: .INDENT 0.0 .INDENT 3.5 .sp .EX # SPDX\-FileCopyrightText: 20XX Your Name # # SPDX\-License\-Identifier: BSD\-3\-Clause .EE .UNINDENT .UNINDENT .sp Documentation is written in reStructuredText format and put inside a bracket comment with a \fB\&.rst:\fP id after the opening bracket: .INDENT 0.0 .INDENT 3.5 .sp .EX #[=======================================================================[.rst: The docs #]=======================================================================] .EE .UNINDENT .UNINDENT .sp (docs/sphinx/ext/ecm.py has code to extract the rst text from a comment with such wrapping) .sp Functions should be used instead of macros unless there is a good reason not to (and that reason should be noted in a comment), and lowercase should be used for macros, functions and commands. .sp 4 spaces is the generally\-recommended indent, although there are several files that use 2 spaces; consistency within a file is more important than consistency across files. .sp If in doubt, look at how other modules in Extra CMake Modules are written, and follow the same pattern. .SS Find Modules .sp A good template for find module documentation is: .INDENT 0.0 .INDENT 3.5 .sp .EX #[=======================================================================[.rst: FindFoo \-\-\-\-\-\-\- Finds the Foo library. This will define the following variables: \(ga\(gaFoo_FOUND\(ga\(ga True if (the requested version of) Foo is available \(ga\(gaFoo_VERSION\(ga\(ga The version of Foo, if it is found \(ga\(gaFoo_LIBRARIES\(ga\(ga This can be passed to target_link_libraries() instead of the \(ga\(gaFoo::Foo\(ga\(ga target \(ga\(gaFoo_INCLUDE_DIRS\(ga\(ga This should be passed to target_include_directories() if the target is not used for linking \(ga\(gaFoo_DEFINITIONS\(ga\(ga This should be passed to target_compile_options() if the target is not used for linking If \(ga\(gaFoo_FOUND\(ga\(ga is TRUE, it will also define the following imported target: \(ga\(gaFoo::Foo\(ga\(ga The Foo library In general we recommend using the imported target, as it is easier to use. Bear in mind, however, that if the target is in the link interface of an exported library, it must be made available by the package config file. #]=======================================================================] .EE .UNINDENT .UNINDENT .sp Note the use of definition lists for the variables. .sp Because of the \fI\%ECMUseFindModules\fP module, projects may easily make local copies of find modules, and may install those copies with their own CMake project config files. For this reason, find modules should include the full BSD 3\-clause license: .INDENT 0.0 .INDENT 3.5 .sp .EX #============================================================================= # Copyright 20XX Your Name # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR \(ga\(gaAS IS\(aq\(aq AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #============================================================================= .EE .UNINDENT .UNINDENT .sp Find modules should always provide imported targets in addition to the traditional variables (like \fBFoo_LIBRARIES\fP, etc). .sp Unlike find modules shipped with CMake, if the module requires a specific CMake version it is not enough to warn when the minimum required version is not high enough: you should also produce an error when the actual CMake version being used is not high enough. This can be done with: .INDENT 0.0 .INDENT 3.5 .sp .EX if(CMAKE_VERSION VERSION_LESS 3.16.0) message(FATAL_ERROR \(dqCMake 3.16.0 is required by FindFoo.cmake\(dq) endif() if(CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 3.16.0) message(AUTHOR_WARNING \(dqYour project should require at least CMake 3.16.0 to use FindFoo.cmake\(dq) endif() .EE .UNINDENT .UNINDENT .sp The \fI\%ECMFindModuleHelpers\fP module has several useful functions and macros. For example, it allows you to replace the above version check with: .INDENT 0.0 .INDENT 3.5 .sp .EX ecm_find_package_version_check(Foo) .EE .UNINDENT .UNINDENT .SS Components .sp Using \fI\%ECMFindModuleHelpers\fP, creating a find module for a library with several inter\-dependent components is reasonably straightforward. After the documentation, you need to include the module and do the usual version check: .INDENT 0.0 .INDENT 3.5 .sp .EX include(ECMFindModuleHelpers) ecm_find_package_version_check(Foo) .EE .UNINDENT .UNINDENT .sp The important macros are \fBecm_find_package_parse_components\fP and \fBecm_find_package_handle_library_components\fP\&. These take a list of components, and query other variables you provide to find out the information they require. The documentation for \fI\%ECMFindModuleHelpers\fP provides more information, but a simple setup might look like: .INDENT 0.0 .INDENT 3.5 .sp .EX set(Foo_known_components Bar Baz) set(Foo_Bar_pkg_config \(dqfoo\-bar\(dq) set(Foo_Bar_lib \(dqbar\(dq) set(Foo_Bar_header \(dqfoo/bar.h\(dq) set(Foo_Bar_pkg_config \(dqfoo\-baz\(dq) set(Foo_Baz_lib \(dqbaz\(dq) set(Foo_Baz_header \(dqfoo/baz.h\(dq) .EE .UNINDENT .UNINDENT .sp If \fBBaz\fP depends on \fBBar\fP, for example, you can specify this with .INDENT 0.0 .INDENT 3.5 .sp .EX set(Foo_Baz_component_deps \(dqBar\(dq) .EE .UNINDENT .UNINDENT .sp Then call the macros: .INDENT 0.0 .INDENT 3.5 .sp .EX ecm_find_package_parse_components(Foo RESULT_VAR Foo_components KNOWN_COMPONENTS ${Foo_known_components} ) ecm_find_package_handle_library_components(Foo COMPONENTS ${Foo_components} ) .EE .UNINDENT .UNINDENT .sp Of course, if your components need unusual handling, you may want to replace \fBecm_find_package_handle_library_components\fP with, for example, a \fBforeach\fP loop over the components (the body of which should implement most of what a normal find module does, including setting \fBFoo__FOUND\fP). .sp At this point, you should set \fBFoo_VERSION\fP using whatever information you have available (such as from parsing header files). Note that \fBecm_find_package_handle_library_components\fP will set it to the version reported by pkg\-config of the first component found, but this depends on the presence of pkg\-config files, and the version of a component may not be the same as the version of the whole package. After that, finish off with .INDENT 0.0 .INDENT 3.5 .sp .EX include(FindPackageHandleStandardArgs) find_package_handle_standard_args(Foo FOUND_VAR Foo_FOUND REQUIRED_VARS Foo_LIBRARIES VERSION_VAR Foo_VERSION HANDLE_COMPONENTS ) include(FeatureSummary) set_package_properties(Foo PROPERTIES URL \(dqhttps://www.foo.example.com/\(dq DESCRIPTION \(dqA library for doing useful things\(dq) .EE .UNINDENT .UNINDENT .SH SUBMITTING MODULES .sp Proposed new modules should be submitted using the \X'tty: link https://git.reviewboard.kde.org/'\fI\%KDE Review Board instance\fP\X'tty: link', and be assigned to the \fBbuildsystem\fP and \fBextracmakemodules\fP groups. You should be able to point to two separate projects that will make use of the module. .sp The mailing list can be found at \X'tty: link https://mail.kde.org/mailman/listinfo/kde-buildsystem'\fI\%https://mail.kde.org/mailman/listinfo/kde\-buildsystem\fP\X'tty: link'\&. .SH COPYRIGHT KDE Developers .\" Generated by docutils manpage writer. .