To put in in one sentence: be as careful when writing the CMake files as when you are writing C++ code.

==Indentation==

==Indentation==

Line 44:

Line 48:

<syntaxhighlight lang="cmake">

<syntaxhighlight lang="cmake">

if(FOOVAR)

if(FOOVAR)

−

some_command(...)

+

some_command(...)

else()

else()

−

another_command(...)

+

another_command(...)

endif()

endif()

</syntaxhighlight>

</syntaxhighlight>

Line 53:

Line 57:

<pre>

<pre>

if(BARVAR)

if(BARVAR)

−

some_other_command(...)

+

some_other_command(...)

endif(BARVAR)

endif(BARVAR)

</pre>

</pre>

+

+

=Writing CMake Find-modules=

==(Not) Using pkg-config==

==(Not) Using pkg-config==

Line 66:

Line 72:

* putting something like if(NOT WIN32) around the pkg-config stuff is not necessary (and should be removed if it is somewhere). If pkg-config is not found, e.g. on Windows, the macros simply do nothing.

* putting something like if(NOT WIN32) around the pkg-config stuff is not necessary (and should be removed if it is somewhere). If pkg-config is not found, e.g. on Windows, the macros simply do nothing.

−

==Writing CMake Find-modules==

+

==Follow CMake's readme.txt==

−

* Follow the style guide from CMake when writing some FindFoo.cmake module:

+

Follow the style guide from CMake when writing some FindFoo.cmake module:

* For checking the results inside the Find-module, the macro find_package_handle_standard_args() (coming with CMake) should be used, using the new extended syntax, which supports also version checking.

+

==Use FindPackageHandleStandardArgs.cmake==

+

For checking the results inside the Find-module, the macro find_package_handle_standard_args() (coming with CMake) should be used, using the new extended syntax, which supports also version checking.

−

* Micro-optimizations like

+

==Avoid Micro-Optimizations==

+

Micro-optimizations like

<pre>

<pre>

if(FOO_LIBRARY AND FOO_INCLUDE_DIR)

if(FOO_LIBRARY AND FOO_INCLUDE_DIR)

−

set(FOO_FOUND TRUE)

+

set(FOO_FOUND TRUE)

else()

else()

−

... execute the whole find-logic

+

... execute the whole find-logic

endif()

endif()

</pre>

</pre>

should be removed, the find-logic should be executed always. These shortcuts can cause problems e.g. when the same file is used from multiple directories but e.g. with different required versions or components etc.

should be removed, the find-logic should be executed always. These shortcuts can cause problems e.g. when the same file is used from multiple directories but e.g. with different required versions or components etc.

+

+

Also manually quieting the module should not be done:

+

<pre>

+

if ( SQLITE_INCLUDE_DIR AND SQLITE_LIBRARIES )

+

set(Sqlite_FIND_QUIETLY TRUE)

+

endif ( SQLITE_INCLUDE_DIR AND SQLITE_LIBRARIES )

+

</pre>

+

+

If find_package_handle_standard_args() is used, this is completely unnecessary, since find_package_handle_standard_args() only prints something if the result has changed compared to the previous run. So if, as in this example, sqlite has already been found, find_package_handle_standard_args() will only print this information during the first CMake run, but not on successive runs.

+

+

So, do not add such code. It is unnecessary and makes the modules look more complicated than necessary.

+

+

=Writing FooConfig.cmake files=

+

+

* See http://quickgit.kde.org/?p=kdeexamples.git&a=tree&hb=HEAD&f=buildsystem/HowToInstallALibrary for a fully commented example

should be removed, the find-logic should be executed always. These shortcuts can cause problems e.g. when the same file is used from multiple directories but e.g. with different required versions or components etc.

If find_package_handle_standard_args() is used, this is completely unnecessary, since find_package_handle_standard_args() only prints something if the result has changed compared to the previous run. So if, as in this example, sqlite has already been found, find_package_handle_standard_args() will only print this information during the first CMake run, but not on successive runs.

So, do not add such code. It is unnecessary and makes the modules look more complicated than necessary.