This document introduces the coding style that will be applied in this repository.
This coding style involves all the following files: .c
, .h
, .cpp
, .cmake
, CMakeLists.txt
. To enforce it we rely on two main tools:
clang-format
version18.1.8
.cmake-format
version0.6.13
.
Please note: tools versions are important! Different versions will enforce slightly different changes on the code. For example
clang-format-18
will produce a slightly different output respect toclang-format-17
always respecting the imposed style.
The coding style is expressed through the 2 configuration file that you find in this repo: .clang-format
, .cmake-format.json
.
There are many ways to enforce the style locally, here we will describe two of them:
- Use
pre-commit
framework. - Use the repo
Makefile
.
The pre-commit
framework allows you to automatically install different git-hooks
that will run at every new commit. More precisely, if you use the .pre-commit-config.yaml
in this repo you will install 3 different hooks:
- The
clang-format
hook: this is apre-commit
git hook that runsclang-format
on your staged changes. - The
cmake-format
hook: this is apre-commit
git hook that runscmake-format
on your staged changes. - The
DCO signed-off
hook: this is apre-commit-msg
git hook that adds theDCO
on your commit if not present. This hook is not strictly related to the coding style so we will talk about it in a separate section: Add DCO signed-off to your commits.
Now let's see what we need to use pre-commit
framework.
Install pre-commit
framework following the official documentation.
Please note: you have to follow only the "Installation" section.
Once you have installed pre-commit
, you don't need to install anything else! This is the good point of using a framework like pre-commit
, all the tools necessary to format your code will be directly managed by the framework. But in order to be ready, you need to install the git hooks in your local repo.
This simple command allows you to install the two pre-commit
git hooks, clang-format
and cmake-format
.
pre-commit install --install-hooks --hook-type pre-commit --overwrite
If you want to install also the pre-commit-msg
git hook for the DCO you have to type the following command, but be sure to have configured all you need as said in the dedicated section
pre-commit install --install-hooks --hook-type prepare-commit-msg --overwrite
You have done, at every new commit, this hook will check that your patch respects the coding style of this repo!
If you want to detach the git hooks, you can simply type:
pre-commit uninstall --hook-type prepare-commit-msg
pre-commit uninstall --hook-type pre-commit
In order to use the repo Makefile
, you need to install on your local machine the two aforementioned tools:
clang-format v18.1.8
One of the easiest ways to install clang-format
could be directly downloading its static binary from here.
There are other ways for example you can download the package for your distro or you can also build it from sources.
cmake-format v0.6.13
To install cmake-format
you can follow the official documentation here.
NOTE: Please check the versions of the two tool with
clang-format --version
andcmake-format --version
.
Once you have installed the right versions of the 2 tools, you can simply type make format-all
from the root directory of the project (/libs
) to format all your code according to the coding style.
Remember to do that before submitting a new patch upstream! 😁
Obviously, you can also install the 2 tools locally and enable some extension of your favorite IDE (like VScode
) to format your code every time you save your files!
Another requirement for contributing to the libs
repository, is applying the DCO to every commit you want to push upstream.
Before doing this you have to configure your git user name
and email
if you haven't already done it. To check your actual name
and email
type:
git config --get user.name
git config --get user.email
If they are correct you have done, otherwise, you have to set them:
git config user.name <full-name>
git config user.email <mail-used_with-GitHub-profile>
Please note: If you have problems in doing this you can read the full documentation here.
Now you are ready to sign your commits! You have two main ways to do this:
- Manually with
git
tool. - Use the
pre-commit-msg
hook quoted before.
To do this you just need to remember the -s
while performing your commits:
git commit -s
or with the inline message:
git commit -s -m "my first commit"
Here if you have already added the hook in the previous section, you have to do nothing otherwise you have to simply install the DCO hook with:
pre-commit install --install-hooks --hook-type prepare-commit-msg --overwrite
And you have done! Now you don't have to remember the -s
option every time you commit something, the DCO hook will automatically add the DCO if you forget it! 😄
To know whether a variable belongs to a class
or a function
, we start member variables with m_
.
Example:
public int32_t m_counter;
To know whether the variable is global or not, we start globals with g_
.
Example:
int g_nplugins;
The naming convention is camel-cased "Unix" style, i.e. always lower case. Words are separated by underscores.
Example:
int32_t g_global_bean_counter;
int32_t count_beans();
and not,
int32_t GlobalBeanCounter;
int32_t CountBeans();
Packed structures should use the GCC and MSVC-style supported pragma
:
Example:
#pragma pack(push,1)
struct frame_control
{
struct fields....
};
#pragma pack(pop)
Put an LL
at the end of your 64-bit
constants. Without the LL
, some platform compilers try to interpret the constant on the right-hand side as a long integer
instead of a long long
and this could lead to an error at building time.
Example:
x=0X00FF00000000000LL