Skip to content

Commit 6d6808f

Browse files
nashifkartben
authored andcommitted
doc: cmake: add cmake style guidelines
Add cmake style guidelines. Signed-off-by: Anas Nashif <[email protected]>
1 parent e453a0c commit 6d6808f

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

doc/build/cmake/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ source file :file:`src/main.c`, behavior that we surely do not want. The
4343
``PUBLIC`` keyword could however be useful when modifying the include
4444
paths of a target library.
4545

46+
When introducing build system code using CMake or adding new CMake files,
47+
please follow the style guidlines outline :ref:`here <cmake-style>`.
48+
4649

4750
Build and Configuration Phases
4851
==============================

doc/build/cmake/style.rst

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
:orphan:
2+
3+
.. _cmake-style:
4+
5+
CMake Style Guidelines
6+
======================
7+
8+
General Formatting
9+
------------------
10+
11+
- **Indentation**: Use **2 spaces** for indentation. Avoid tabs to ensure
12+
consistency across different environments.
13+
- **Line Length**: Limit line length to **100 characters** where possible.
14+
- **Empty Lines**: Use empty lines to separate logically distinct sections
15+
within a CMake file.
16+
- **No Space Before Opening Brackets**: Do not add a space between a command
17+
and the opening parenthesis.
18+
Use ``if(...)`` instead of ``if (...)``.
19+
20+
.. code-block:: cmake
21+
22+
# Good:
23+
if(ENABLE_TESTS)
24+
add_subdirectory(tests)
25+
endif()
26+
27+
# Bad:
28+
if (ENABLE_TESTS)
29+
add_subdirectory(tests)
30+
endif()
31+
32+
Commands and Syntax
33+
-------------------
34+
35+
- **Lowercase Commands**: Always use **lowercase** CMake commands (e.g.,
36+
``add_executable``, ``find_package``). This improves readability and
37+
consistency.
38+
39+
.. code-block:: cmake
40+
41+
# Good:
42+
add_library(my_lib STATIC src/my_lib.cpp)
43+
44+
# Bad:
45+
ADD_LIBRARY(my_lib STATIC src/my_lib.cpp)
46+
47+
- **One File Argument per Line**: Break the file arguments across multiple
48+
lines to make it easier to scan and identify each source file or item.
49+
50+
.. code-block:: cmake
51+
52+
# Good:
53+
target_sources(my_target PRIVATE
54+
src/file1.cpp
55+
src/file2.cpp
56+
)
57+
58+
# Bad:
59+
target_sources(my_target PRIVATE src/file1.cpp src/file2.cpp)
60+
61+
Variable Naming
62+
---------------
63+
64+
- **Use Uppercase for Cache Variables or variables shared across CMake files**:
65+
When defining cache variables using ``option`` or ``set(... CACHE ...)``, use
66+
**UPPERCASE names**.
67+
68+
.. code-block:: cmake
69+
70+
option(ENABLE_TESTS "Enable test suite" ON)
71+
set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard version")
72+
73+
- **Use Lowercase for Local Variables**: For local variables within CMake
74+
files, use **lowercase** or **snake_case**.
75+
76+
.. code-block:: cmake
77+
78+
set(output_dir "${CMAKE_BINARY_DIR}/output")
79+
80+
- **Consistent Prefixing**: Use consistent prefixes for variables to avoid name
81+
conflicts, especially in large projects.
82+
83+
.. code-block:: cmake
84+
85+
set(MYPROJECT_SRC_DIR "${CMAKE_SOURCE_DIR}/src")
86+
87+
Quoting
88+
-------
89+
90+
- **Quote Strings and Variables**: Always quote string literals and variables
91+
to prevent unintended behavior, especially when dealing with paths or
92+
arguments that may contain spaces.
93+
94+
.. code-block:: cmake
95+
96+
# Good:
97+
set(my_path "${CMAKE_SOURCE_DIR}/include")
98+
99+
# Bad:
100+
set(my_path ${CMAKE_SOURCE_DIR}/include)
101+
102+
- **Do Not Quote Boolean Values**: For boolean values (``ON``, ``OFF``,
103+
``TRUE``, ``FALSE``), avoid quoting them.
104+
105+
.. code-block:: cmake
106+
107+
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
108+
109+
Avoid Hardcoding Paths
110+
----------------------
111+
112+
- Use CMake variables (``CMAKE_SOURCE_DIR``, ``CMAKE_BINARY_DIR``,
113+
``CMAKE_CURRENT_SOURCE_DIR``) instead of hardcoding paths.
114+
115+
.. code-block:: cmake
116+
117+
set(output_dir "${CMAKE_BINARY_DIR}/bin")
118+
119+
Conditional Logic
120+
-----------------
121+
122+
- Use ``if``, ``elseif``, and ``else`` with proper indentation, and close with
123+
``endif()``.
124+
125+
.. code-block:: cmake
126+
127+
if(ENABLE_TESTS)
128+
add_subdirectory(tests)
129+
endif()
130+
131+
Documentation
132+
-------------
133+
134+
- Use comments to document complex logic in the CMake files.
135+
136+
.. code-block:: cmake
137+
138+
# Find LlvmLld components required for building with llvm
139+
find_package(LlvmLld 14.0.0 REQUIRED)

0 commit comments

Comments
 (0)