Skip to content

Commit b1043de

Browse files
nashifkartben
authored andcommitted
doc: contribute: split style guidelines
Split style guidelines into multiple pages to allow expansion and adding more details and levels. Signed-off-by: Anas Nashif <[email protected]>
1 parent 465fb18 commit b1043de

File tree

7 files changed

+161
-164
lines changed

7 files changed

+161
-164
lines changed

doc/contribute/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ General Guidelines
1818
contributor_expectations.rst
1919
reviewer_expectations.rst
2020
coding_guidelines/index.rst
21-
style_guidelines.rst
21+
style/index.rst
2222
proposals_and_rfcs.rst
2323
modifying_contributions.rst
2424

doc/contribute/style/cmake.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
.. _cmake-style:
44

55
CMake Style Guidelines
6-
======================
6+
######################
77

88
General Formatting
9-
------------------
9+
******************
1010

1111
- **Indentation**: Use **2 spaces** for indentation. Avoid tabs to ensure
1212
consistency across different environments.
@@ -30,7 +30,7 @@ General Formatting
3030
endif()
3131
3232
Commands and Syntax
33-
-------------------
33+
*******************
3434

3535
- **Lowercase Commands**: Always use **lowercase** CMake commands (e.g.,
3636
``add_executable``, ``find_package``). This improves readability and
@@ -59,7 +59,7 @@ Commands and Syntax
5959
target_sources(my_target PRIVATE src/file1.cpp src/file2.cpp)
6060
6161
Variable Naming
62-
---------------
62+
***************
6363

6464
- **Use Uppercase for Cache Variables or variables shared across CMake files**:
6565
When defining cache variables using ``option`` or ``set(... CACHE ...)``, use
@@ -85,7 +85,7 @@ Variable Naming
8585
set(MYPROJECT_SRC_DIR "${CMAKE_SOURCE_DIR}/src")
8686
8787
Quoting
88-
-------
88+
*******
8989

9090
- **Quote Strings and Variables**: Always quote string literals and variables
9191
to prevent unintended behavior, especially when dealing with paths or
@@ -107,7 +107,7 @@ Quoting
107107
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
108108
109109
Avoid Hardcoding Paths
110-
----------------------
110+
**********************
111111

112112
- Use CMake variables (``CMAKE_SOURCE_DIR``, ``CMAKE_BINARY_DIR``,
113113
``CMAKE_CURRENT_SOURCE_DIR``) instead of hardcoding paths.
@@ -117,7 +117,7 @@ Avoid Hardcoding Paths
117117
set(output_dir "${CMAKE_BINARY_DIR}/bin")
118118
119119
Conditional Logic
120-
-----------------
120+
*****************
121121

122122
- Use ``if``, ``elseif``, and ``else`` with proper indentation, and close with
123123
``endif()``.
@@ -129,7 +129,7 @@ Conditional Logic
129129
endif()
130130
131131
Documentation
132-
-------------
132+
*************
133133

134134
- Use comments to document complex logic in the CMake files.
135135

doc/contribute/style/code.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.. _general_code_style:
2+
3+
C Code and General Style Guidelines
4+
###################################
5+
6+
Coding style is enforced on any new or modified code, but contributors are
7+
not expected to correct the style on existing code that they are not
8+
modifying.
9+
10+
For style aspects where the guidelines don't offer explicit guidance or
11+
permit multiple valid ways to express something, contributors should follow
12+
the style of existing code in the tree, with higher importance given to
13+
"nearby" code (first look at the function, then the same file, then
14+
subsystem, etc).
15+
16+
In general, follow the `Linux kernel coding style`_, with the following
17+
exceptions and clarifications:
18+
19+
* Use `snake case`_ for code and variables.
20+
* The line length is 100 columns or fewer. In the documentation, longer lines
21+
for URL references are an allowed exception.
22+
* Add braces to every ``if``, ``else``, ``do``, ``while``, ``for`` and
23+
``switch`` body, even for single-line code blocks.
24+
* Use spaces instead of tabs to align comments after declarations, as needed.
25+
* Use C89-style single line comments, ``/* */``. The C99-style single line
26+
comment, ``//``, is not allowed.
27+
* Use ``/** */`` for doxygen comments that need to appear in the documentation.
28+
* Avoid using binary literals (constants starting with ``0b``).
29+
* Avoid using non-ASCII symbols in code, unless it significantly improves
30+
clarity, avoid emojis in any case.
31+
* Use proper capitalization of nouns in code comments (e.g. ``UART`` and not
32+
``uart``, ``CMake`` and not ``cmake``).
33+
34+
.. _Linux kernel coding style:
35+
https://kernel.org/doc/html/latest/process/coding-style.html
36+
37+
.. _snake case:
38+
https://en.wikipedia.org/wiki/Snake_case

doc/contribute/style/devicetree.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.. _devicetree_style:
2+
3+
Devicetree Style Guidelines
4+
###########################
5+
6+
* Indent with tabs.
7+
* Follow the Devicetree specification conventions and rules.
8+
* Use dashes (``-``) as word separators for node and property names.
9+
* Use underscores (``_``) as word separators in node labels.
10+
* Leave a single space on each side of the equal sign (``=``) in property
11+
definitions.
12+
* Don't insert empty lines before a dedenting ``};``.
13+
* Insert a single empty line to separate nodes at the same hierarchy level.

doc/contribute/style/index.rst

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
.. _coding_style:
2+
3+
4+
Coding Style Guidelines
5+
#######################
6+
7+
.. toctree::
8+
:maxdepth: 1
9+
10+
code.rst
11+
cmake.rst
12+
devicetree.rst
13+
kconfig.rst
14+
15+
16+
Style Tools
17+
***********
18+
19+
Checkpatch
20+
==========
21+
22+
The Linux kernel GPL-licensed tool ``checkpatch`` is used to check
23+
coding style conformity.
24+
25+
.. note::
26+
checkpatch does not currently run on Windows.
27+
28+
Checkpatch is available in the scripts directory. To invoke it when committing
29+
code, make the file *$ZEPHYR_BASE/.git/hooks/pre-commit* executable and edit
30+
it to contain:
31+
32+
.. code-block:: bash
33+
34+
#!/bin/sh
35+
set -e exec
36+
exec git diff --cached | ${ZEPHYR_BASE}/scripts/checkpatch.pl -
37+
38+
Instead of running checkpatch at each commit, you may prefer to run it only
39+
before pushing on zephyr repo. To do this, make the file
40+
*$ZEPHYR_BASE/.git/hooks/pre-push* executable and edit it to contain:
41+
42+
.. code-block:: bash
43+
44+
#!/bin/sh
45+
remote="$1"
46+
url="$2"
47+
48+
z40=0000000000000000000000000000000000000000
49+
50+
echo "Run push hook"
51+
52+
while read local_ref local_sha remote_ref remote_sha
53+
do
54+
args="$remote $url $local_ref $local_sha $remote_ref $remote_sha"
55+
exec ${ZEPHYR_BASE}/scripts/series-push-hook.sh $args
56+
done
57+
58+
exit 0
59+
60+
If you want to override checkpatch verdict and push you branch despite reported
61+
issues, you can add option --no-verify to the git push command.
62+
63+
A different way for running ``checkpatch`` is by using :ref:`check_compliance_py`
64+
script, which does additional style and compliance related checks.
65+
66+
clang-format
67+
============
68+
69+
The `clang-format tool <https://clang.llvm.org/docs/ClangFormat.html>`_ can
70+
be helpful to quickly reformat large amounts of new source code to our
71+
`Coding Style Guidelines`_ standards together with the ``.clang-format`` configuration file
72+
provided in the repository. ``clang-format`` is well integrated into most
73+
editors, but you can also run it manually like this:
74+
75+
.. code-block:: bash
76+
77+
clang-format -i my_source_file.c
78+
79+
``clang-format`` is part of LLVM, which can be downloaded from the project
80+
`releases page <https://github.com/llvm/llvm-project/releases>`_. Note that if
81+
you are a Linux user, ``clang-format`` will likely be available as a package in
82+
your distribution repositories.
83+
84+
When there are differences between the `Coding Style Guidelines`_ guidelines and the
85+
formatting generated by code formatting tools, the `Coding Style Guidelines`_ guidelines
86+
take precedence. If there is ambiguity between formatting tools and the
87+
guidelines, maintainers may decide which style should be adopted.

doc/contribute/style/kconfig.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.. _kconfig_style:
2+
3+
Kconfig Style Guidelines
4+
########################
5+
6+
* Line length of 100 columns or fewer.
7+
* Indent with tabs, except for ``help`` entry text which should be placed at
8+
one tab plus two extra spaces.
9+
* Leave a single empty line between option declarations.
10+
* Use Statements like ``select`` carefully, see
11+
:ref:`kconfig_tips_and_tricks` for more information.
12+
* Format comments as ``# Comment`` rather than ``#Comment``
13+
* Insert an empty line before/after each top-level ``if`` and ``endif``
14+
statement.

doc/contribute/style_guidelines.rst

Lines changed: 0 additions & 155 deletions
This file was deleted.

0 commit comments

Comments
 (0)