Skip to content

Commit

Permalink
feat: format the code with clang-format
Browse files Browse the repository at this point in the history
The code currently mixes tab and space indenting.

Having the code style defined into a file is usually good practice, as
we can use tools to do it automatically.

This change proposes use clang-format to do it. It provides predefined
rules following LLVM/Google/Mozilla/GNU code style that can be tweaked.

I try to create a .clang-format configuration that just fix the
tab/space mix and keep the rest as-is as much I can.

Currently all cpp/hpp/h are formatted except those in src/ext.

The code formatting will be checked Jenkins.
It also can be run with: `cmake .. && make clang-format`

cmake also install automatically a pre-commit hook to format the code.
It can be disabled with `INSTALL_GIT_HOOKS=OFF`

llvm provide some basic integration for editor:

https://clang.llvm.org/docs/ClangFormat.html#vim-integration
https://clang.llvm.org/docs/ClangFormat.html#emacs-integration

But you can also use other tools like:

https://github.com/SavchenkoValeriy/emacs-clang-format-plus
  • Loading branch information
sileht committed Sep 11, 2020
1 parent c72327d commit 07d6bdc
Show file tree
Hide file tree
Showing 162 changed files with 41,264 additions and 32,493 deletions.
135 changes: 135 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
---
Language: Cpp
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
IndentBraces: true
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: All
BreakBeforeBraces: GNU
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 79
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
- Regex: '.*'
Priority: 1
SortPriority: 0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: c++11
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
...
28 changes: 28 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,34 @@ if (BUILD_TOOLS)
add_subdirectory(tools)
endif()

# Get all project files
file(GLOB_RECURSE ALL_SOURCE_FILES src/*.cpp src/*.hpp src/*.h src/*.c tests/*.cc src/*.cc)
list(FILTER ALL_SOURCE_FILES EXCLUDE REGEX "^${CMAKE_SOURCE_DIR}/src/ext/.*$")

add_custom_target(
clang-format
COMMAND clang-format
-style=file
-i
${ALL_SOURCE_FILES}
)
add_custom_target(
clang-format-check
COMMAND clang-format
--dry-run
--Werror
--ferror-limit=10
-style=file
-i
${ALL_SOURCE_FILES}
)

option(INSTALL_GIT_HOOKS "install client side git hook" ON)
if (INSTALL_GIT_HOOKS)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tools/git-pre-commit-hook
${CMAKE_CURRENT_SOURCE_DIR}/.git/hooks/pre-commit @ONLY)
endif()

# status
message(STATUS "Build Tests : ${BUILD_TESTS}")
message(STATUS "Caffe DEBUG : ${USE_CAFFE_DEBUG}")
1 change: 1 addition & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export PATH="/usr/lib/ccache/:$PATH"
mkdir -p build
cd build
cmake .. -DBUILD_TESTS=ON -DUSE_CUDNN=ON -DUSE_SIMSEARCH=ON -DUSE_TSNE=ON -DUSE_XGBOOST=ON -DUSE_TORCH=ON -DUSE_NCNN=ON -DUSE_TENSORRT=ON -DCUDA_ARCH="-gencode arch=compute_61,code=sm_61"
make clang-format-check
make -j24
ccache -s
'''
Expand Down
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ http://www.deepdetect.com/overview/examples/
| | Caffe | Tensorflow | Source | Top-1 Accuracy (ImageNet) |
|--------------------------|-------|------------|---------------|---------------------------|
| AlexNet | Y | N | BVLC | 57.1% |
| SqueezeNet | [Y](https://deepdetect.com/models/squeezenet/squeezenet_v1.1.caffemodel) | N | DeepScale | 59.5% |
| SqueezeNet | [Y](https://deepdetect.com/models/squeezenet/squeezenet_v1.1.caffemodel) | N | DeepScale | 59.5% |
| Inception v1 / GoogleNet | [Y](https://deepdetect.com/models/ggnet/bvlc_googlenet.caffemodel) | [Y](https://deepdetect.com/models/tf/inception_v1.pb) | BVLC / Google | 67.9% |
| Inception v2 | N | [Y](https://deepdetect.com/models/tf/inception_v2.pb) | Google | 72.2% |
| Inception v3 | N | [Y](https://deepdetect.com/models/tf/inception_v3.pb) | Google | 76.9% |
Expand Down Expand Up @@ -263,7 +263,7 @@ Beware of dependencies, typically on Debian/Ubuntu Linux, do:
sudo apt-get install build-essential libgoogle-glog-dev libgflags-dev libeigen3-dev libopencv-dev libcppnetlib-dev libboost-dev libboost-iostreams-dev libcurlpp-dev libcurl4-openssl-dev protobuf-compiler libopenblas-dev libhdf5-dev libprotobuf-dev libleveldb-dev libsnappy-dev liblmdb-dev libutfcpp-dev cmake libgoogle-perftools-dev unzip python-setuptools python-dev libspdlog-dev python-six python-enum34 libarchive-dev
```

#### Choosing interfaces :
#### Choosing interfaces :

DeepDetect can be used:
- directly from command line for caffe models. To build the executable use:
Expand All @@ -274,7 +274,7 @@ cmake .. -DUSE_COMMAND_LINE=ON
```
cmake .. -DUSE_COMMAND_LINE=ON -DUSE_JSON_API=ON
```
- as a REST server (using JSON API). To build the server executable use (`USE_JSON_API` is auto-selected):
- as a REST server (using JSON API). To build the server executable use (`USE_JSON_API` is auto-selected):
```
cmake .. -DUSE_HTTP_SERVER=ON
```
Expand Down Expand Up @@ -383,7 +383,7 @@ Specify the following option via cmake:
```$xslt
cmake .. -DUSE_TENSORRT=ON
```
TensorRT requires GPU and CUDNN, they are automatically switched on.
TensorRT requires GPU and CUDNN, they are automatically switched on.

#### Build with TensorRT support + TRT oss parts
Specify the following option via cmake:
Expand Down Expand Up @@ -442,6 +442,19 @@ Run tests with:
ctest
```

### Code Style Rules

`clang-format` is used to enforce code style.

You can automatically format it with:

```
cmake ..
make clang-format
```

Or use your favorite editor with a clang-format plugin.

### Start the server

```
Expand Down Expand Up @@ -481,7 +494,7 @@ where `sname` is the service name and the JSON is a string without external quot
- service prediction
```
service_predict;JSON string
```
```

### Pure command line JSON API

Expand All @@ -502,7 +515,7 @@ where `<other options>` stands for the command line parameters from the command
-service_train (/train POST call JSON string) type: string default: ""
-service_train_delete (/train DELETE call JSON string) type: string default: ""
-service_train_status (/train GET call JSON string) type: string default: ""
```

The options above can be obtained from running
Expand Down
Loading

0 comments on commit 07d6bdc

Please sign in to comment.