1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
# BlueBrain HPC Team C++ Development Guidelines
This document describes both C++ development guidelines adopted by
HPC team, and the tools and processes required to
ensure they are properly followed over time.
## Documentation
Development Guidelines are split in the following sections:
* [Tooling](./cpp/Tooling.md)
* [Development Lifecycle](./cpp/DevelopmentLifecycle.md)
* [Code Formatting](./cpp/formatting/README.md)
* [Naming Conventions](./cpp/NamingConventions.md)
* [Code Documentation](./cpp/Documentation.md)
* Best Practices
* Python bindings
## Status
This project in currently under development, and shall not provide the features
its documentation pretends. Here is a raw summary of the status:
| Feature | Definition | Documentation | Integration | Adoption (>10 proj) |
| --------------------- | ------------------ | ------------------ | ------------------ | ------------------- |
| ClangFormat | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | WIP |
| ClangTidy | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | WIP |
| Naming Conventions | :heavy_check_mark: | :heavy_check_mark: | :no_entry_sign: N/A | :no_entry_sign: N/A |
| Writing Documentation | :heavy_check_mark: | :heavy_check_mark: | | |
| Project template | WIP | | | |
| Good Practices | | | | |
| Memory Check | | | | |
| UT Code Coverage | | | | |
## CMake Project
This directory provides a CMake project that allows one to use the tools and the processes
described in this document. This project requires CMake version 3.10 or higher.
### Requirements
This CMake project expects for the following utilities to be available:
* [Python 3.5 or higher](https://python.org)
* [ClangFormat 9](https://releases.llvm.org/9.0.0/tools/clang/docs/ClangFormat.html)
* [ClangTidy 7](https://releases.llvm.org/7.0.0/tools/clang/tools/extra/docs/clang-tidy/index.html)
* [cmake-format](https://github.com/cheshirekow/cmake_format) Python package version 0.6
* [pre-commit](https://pre-commit.com/) Python package
* [git](https://git-scm.com/) version control system 2.17 or higher.
Optionally, it will also look for:
* [valgrind](http://valgrind.org/) memory checker
* code coverage utilities like gcov, lcov, or gcov
### Installation
You can import this CMake project into your Git repository using a git submodule:
```
git submodule add https://github.com/BlueBrain/hpc-coding-conventions.git
git submodule update --init --recursive
```
Note: if your project already has a `cmake` sub-directory, it is recommended to create the
submodule in this directory instead of top-level.
Then simply add the following line in the top `CMakeLists.txt`, after your project
declaration:
```
project(mylib CXX)
# [...]
add_subdirectory(hpc-coding-conventions/cpp)
```
After cloning or updating this git submodule, run CMake to take benefits of the latest changes.
This will setup or update git [pre-commit](https://pre-commit.com) hooks of this repository.
CMake variables defined by this project are prefixed by `${PROJECT_NAME}` by default.
The CMake variable `CODING_CONV_PREFIX` allows to specify another prefix. It must be defined
before including this CMake project, for instance:
```cmake
project(mylib CXX)
# [...]
set(CODING_CONV_PREFIX Foo)
add_subdirectory(hpc-coding-conventions/cpp)
```
### Usage
#### Code Formatting
Create file `.bbp-project.yaml` at the root of your project and enable the formatting tools,
you want to enable, for instance:
```yaml
tools:
ClangFormat:
enable: True
CMakeFormat:
enable: True
```
You can then use the `bin/format` utility.
#### Version deduction
`bin/format` relies on `PATH` environment variable to locate the proper tools.
You can override the default required versions specified in `bbp-project.yaml`:
```yaml
tools:
ClangFormat:
enable: True
version: ~=15.0.0
```
It is also possible to override the path to a tool in the YAML config file:
```yaml
tools:
CMakeFormat:
path: /path/to/bin/cmake-format
```
##### Usage
* To format the entire codebase: `/path/to/hpc-coding-conventions/bin/format`
* To check the formatting: `/path/to/hpc-coding-conventions/bin/format -n`
* To format the CMake files only: `/path/to/hpc-coding-conventions/bin/format --lang CMake`
* To check the formatting of C++ files in a specific locations:
`/path/to/hpc-coding-conventions/bin/format -n --lang c++ src/path1/ /src/path2/foo.cpp`
##### Advanced configuration
You can override the default file filters of a tool, for instance:
```yaml
tools:
ClangFormat:
include:
match: &cpp_patterns
- src/steps/.*\.((h)|(cpp)|(hpp))
- test/unit/.*\.((h)|(cpp)|(hpp))
path: /foo/bin/clang-format
ClangTidy:
include:
match: *cpp_patterns
```
##### Custom ClangFormat configuration
These coding conventions come with a predefined configuration of ClangFormat that
is by default copied in the top directory of the project.
It is recommended to not add this `.clang-format` to git so that it is fully
driven by this project. It will get updated along with the evolution of these
guidelines. Thus it is recommended to inform git to ignore this file by adding
it to the top `.gitignore`.
A project can however override the predefined configuration of ClangFormat
in two ways:
1. Create a `.clang-format.changes` containing only the required modifications.
1. Add `.clang-format` to the git repository. This project will never try
to modify it.
##### Custom CMakeFormat configuration
Like ClangFormat, these coding conventions already provide a CMakeFormat
configuration that the user can customize with a file named
`.cmake-format.changes.yaml` placed at the project's root directory.
This file can be used to specify the signature of owned CMake functions
and macros, for instance:
```yaml
additional_commands:
add_mpi_test:
kwargs:
NAME: 1
NUM_PROCS: 1
COMMAND: '*'
```
will allow CMakeFormat to properly format functions calls like below:
```cmake
add_mpi_test(
NAME OpSplitOmega_h_2D
NUM_PROCS 2
COMMAND $<TARGET_FILE:OpSplitOmega_h> --num-iterations 100 square.msh)
```
##### Continuous Integration
Define `${PROJECT}_TEST_FORMATTING:BOOL` CMake variable to enforce formatting during
the `test` make target.
#### Static Analysis
To activate static analysis of C++ files with clang-tidy within CMake, enable
the CMake variable `${PROJECT}_STATIC_ANALYSIS` where `${PROJECT}` is the name
given to the CMake `project` function.
For instance, given a project `foo`:
`cmake -DFoo_STATIC_ANALYSIS:BOOL=ON <path>`
Whenever a C++ file is compiled by CMake, clang-tidy will be called.
You can also use utility: `bin/clang-tidy`
##### Usage
This will provide a `clang-tidy` *make* target that will perform static analysis
of all C++ files. Target fails as soon as one defect is detected among the files.
It will also activate static analysis report during the compilation phase.
##### Advanced configuration
The following CMake cache variables can be used to customize the static analysis
of the code:
* `${PROJECT}_ClangTidy_DEPENDENCIES`: list of CMake targets to build before
check C/C++ code. Default value is `""`
These variables are meant to be overridden inside your CMake project.
They are CMake _CACHE_ variables whose value must be forced
**before including this CMake project**.
##### Custom ClangTidy configuration
These coding conventions come with a `.clang-tidy` file providing a predefined
list of ClangTidy checks. By default, this file is copied in the top directory
of the project.
It is recommended to not add this `.clang-tidy` to git so that it is fully
driven by this project. It will get updated along with the evolution of these
guidelines. Thus it is recommended to inform git to ignore this file by adding
it to the top `.gitignore`.
A project can however override the predefined configuration of ClangFormat
in two ways:
1. Create a `.clang-tidy.changes` containing only the required modifications.
1. Add `.clang-tidy` to the git repository. This project will never try
to modify it.
#### Pre-Commit utility
Enable CMake option `${PROJECT}_GIT_HOOKS` to enable automatic checks
before committing or pushing with git. the git operation will fail if
one of the registered checks fails.
The following checks are available:
* `check-clang-format`: check C++ formatting
* `check-cmake-format`: check CMake formatting
* `clang-tidy`: execute static-analysis
* `courtesy-msg`: print a courtesy message to the console.
This check never fails. The default message is a reminder to test and
format the changes when pushing a contribution with git.
A project can overwrite the message displayed by adding the CMake template
named `.git-push-message.cmake.in` at the root of the project directory.
To enable these checks, use CMake variables `${PROJECT}_GIT_COMMIT_HOOKS` and
`${PROJECT}_GIT_PUSH_HOOKS` to specify which checks should be executed for
each specific git operation. For instance:
`cmake -Dfoo_GIT_COMMIT_HOOKS=clang-tidy \
-Dfoo_GIT_PUSH_HOOKS=check-clang-format,courtesy-msg <path>`
This feature requires the `pre-commit` utility.
#### Bob
`bob.cmake` is a CMake utility file part of hpc-coding-conventions that provides
a set of convenient macros and functions used to:
* specify your project options and dependencies
* specify the proper compilation flags
* install the proper CMake export flags so that your project can be
loaded by another project with the `find_package` CMake function.
##### Compilation flags
By default, CMake relies on the `CMAKE_BUILD_TYPE` variable to set the proper
compilation flags. Because _bob_ is now taking care of it, you must configure
your project with `CMAKE_BUILD_TYPE` empty.
_bob_ sets the compilation flags according to a set of CMake variables:
* `${PROJECT_NAME}_CXX_OPTIMIZE:BOOL`: Compile C++ with optimization (default is ON)
* `${PROJECT_NAME}_CXX_SYMBOLS:BOOL`: Compile C++ with debug symbols (default is ON)
* `${PROJECT_NAME}_CXX_WARNINGS:BOOL`: Compile C++ with warnings" (default is ON)
* `${PROJECT_NAME}_EXTRA_CXX_FLAGS:STRING`: Additional C++ compilation flags
* `${PROJECT_NAME}_CXX_FLAGS:STRING`: bypass variables above and use the specified
compilation flags. `CMAKE_BUILD_TYPE` is ignored.
* `${PROJECT_NAME}_NORMAL_CXX_FLAGS:BOOL`: Allow `CMAKE_CXX_FLAGS` to follow _normal_ CMake behavior
and bypass all variables above.
Default `CMAKE_CXX_FLAGS` variable value is taken into account.
##### Integration
The top-level CMakelists.txt of your project may look like:
```cmake
cmake_minimum_required(VERSION 3.10)
project(HelloWorld VERSION 1.0.0 LANGUAGES CXX)
add_subdirectory(hpc-coding-conventions/cpp)
bob_begin_package()
bob_begin_cxx_flags()
bob_cxx17_flags()
# specify custom compilation flags
find_package(OpenMP)
if(OpenMP_FOUND)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_FLAGS "${CMAKE_FLAGS} ${OpenMP_FLAGS}")
else()
message(WARNING "OpenMP support is disabled because it could not be found.")
endif()
bob_end_cxx_flags()
# specify your targets:
add_library(...)
add_executable(...)
bob_end_package()
```
#### Embedded third parties
External libraries required to build or test your C++ project can be either
directly added to the git repository or as a git submodule. The standard
root location for this kind of files is the `3rdparty/` directory but can be
overriden with the `${PROJECT_NAME}_3RDPARTY_DIR` CMake variable.
Adding single-file/header-only C++ libraries directly to the git repository
of your project is acceptable in general, like catch2 or the JSON library
of Niels Lohmann for instance.
More significant dependencies should be considered as pure external
dependencies. But it can also be very convenient to have them as git
submodules, and be able to switch between the two.
This project provides helper functions to deal with these dependencies:
###### cpp_cc_git_submodule
````cmake
#
# cpp_cc_git_submodule(source_dir
# [DISABLED]
# SUBDIR path
# [BUILD] [<arguments>]
# [PACKAGE] [<arguments>]
# GIT_ARGS [<arguments>])
#
# Add a CMake option in the cache to control whether the
# submodule is used or not (default ON). The option is named after the source
# directory passed in first argument, for instance:
# cpp_cc_git_submodule(src/eigen)
# adds the following CMake cached option:
# ${PROJECT_NAME}_3RDPARTY_USE_SRC_EIGEN:BOOL=ON
#
# If enabled, then the submodule is fetched if missing in the working copy.
#
# If the DISABLED argument is provided, then the default value for the CMake
# option is OFF.
#
# If the BUILD argument is provided then the directory is added to the build
# through the add_subdirectory CMake function. Arguments following the BUILD
# arguments are passed to the add_subdirectory function call.
#
# The optional SUBDIR argument is used by the BUILD argument to determine
# the path to the directory added to the build. The path specified is relative
# to the path to the git submodule.
#
# If the PACKAGE argument is provided and the CMake option to determine whether
# the git submodule should be used or not is FALSE, then a call to the find_package
# function is made with the arguments specified to the PACKAGE option.
#
# Default options passed to the `git submodule update` command are
# `--init --recursive --depth 1` to perform a shallow clone of the submodule.
# If the GIT_ARGS argument is provided, then its value supersedes the default options.
#
````
## Contributing
Should you want to contribute to the naming conventions,
please refer to the dedicated [contributing document](./cpp/formatting/CONTRIBUTING.md) first.
## Funding & Acknowledgment
The development of this software was supported by funding to the Blue Brain Project, a research center of the École polytechnique fédérale de Lausanne (EPFL), from the Swiss government's ETH Board of the Swiss Federal Institutes of Technology.
Copyright © 2019-2022 Blue Brain Project/EPFL
|