summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/sections/coding_guidelines.md33
-rw-r--r--scripts/py/setup_hooks.py96
2 files changed, 120 insertions, 9 deletions
diff --git a/docs/sections/coding_guidelines.md b/docs/sections/coding_guidelines.md
index 0306430..43a71d1 100644
--- a/docs/sections/coding_guidelines.md
+++ b/docs/sections/coding_guidelines.md
@@ -2,6 +2,7 @@
- [Coding standards and guidelines](./coding_guidelines.md#coding-standards-and-guidelines)
- [Introduction](./coding_guidelines.md#introduction)
+ - [Static Analysis](./coding_guidelines.md#static-analysis)
- [Language version](./coding_guidelines.md#language-version)
- [File naming](./coding_guidelines.md#file-naming)
- [File layout](./coding_guidelines.md#file-layout)
@@ -25,23 +26,37 @@ Layer (HAL). Both of these languages follow different naming conventions within
However, because we also issue function calls to third-party APIs, and they are not guaranteed to follow these
conventions, the intended outcome could be different for every case.
-## Pre-commit formatting
-To help with the adherence of the coding guidelines, we have provided a clang-format file. When commiting, please run
-the following command, post-staging but pre-commit.
+## Static Analysis
+To help with the adherence of the coding guidelines, we have provided a setup script to add a static analysis,
+pre-push Git hook. This will use the clang-format tool along with the clang-format file in the root of the directory to
+check for formatting errors. It will also us the Cppcheck static analysis tool to minimize the risk of bugs
+appearing in your code.
- ```Git
- git-clang-format
+Before starting any work in the repo, please ensure that you have the static analysis Git hook,
+clang-format tool and cppcheck tool installed by running the following commands:
+
+ ```
+ sudo apt install cppcheck
+ sudo apt install clang-format
+ python scripts/py/setup_hooks.py /path/to/git/hooks/directory
```
-This will modify the staged changes, to adhere to the guidelines as described in the
-.clang-format file in the root of the repo. Please note that the clang-format tool must be installed to run this
-step and can be installed using the following command on Ubuntu:
+This will ensure that the modified files in your commit adhere to the coding guidelines and minimizes the risk of
+inefficient/problematic code.
+
+If the Git hook finds formatting errors, the ```git push``` command will fail.
+You can run the following command on the problematic file:
```
- sudo apt install clang-format
+ clang-format -style=file -i path/to/file
```
+This will modify the problematic file to adhere to the guidelines as described in the
+.clang-format file in the root of the repo.
+
+If the Cppcheck tool finds issues, what needs to be addressed will be
+described in the failed ```git push``` output.
## Language version
diff --git a/scripts/py/setup_hooks.py b/scripts/py/setup_hooks.py
new file mode 100644
index 0000000..1f5c01a
--- /dev/null
+++ b/scripts/py/setup_hooks.py
@@ -0,0 +1,96 @@
+#!/usr/bin/env python3
+# Copyright (c) 2022 Arm Limited. All rights reserved.
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import os
+import sys
+import argparse
+import subprocess
+import stat
+
+def set_hooks_dir(hooks_dir):
+ command = 'git config core.hooksPath {}'.format(hooks_dir)
+ subprocess.Popen(command.split(), stdout=subprocess.PIPE)
+
+def add_pre_push_hooks(hooks_dir):
+ pre_push = "pre-push"
+ file_path = os.path.join(hooks_dir, pre_push)
+ file_exists = os.path.exists(file_path)
+ if file_exists:
+ os.remove(file_path)
+ f = open(file_path, "a")
+ f.write(
+'''#!/bin/sh
+# Copyright (c) 2022 Arm Limited. All rights reserved.
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Called by "git push" with no arguments. The hook should
+# exit with non-zero status after issuing an appropriate message if
+# it wants to stop the push.
+
+while read local_ref local_sha remote_ref remote_sha
+do
+ # We should pass only added or modified C/C++ source files to cppcheck.
+ changed_files=$(git diff --name-only HEAD~1 HEAD | grep -E "*\.(c|cpp|cc|cxx)" | cut -f 2)
+ if [ -n "$changed_files" ]; then
+ clang-format -style=file --dry-run --Werror $changed_files
+
+ exitcode1=$?
+ if [ $exitcode1 -ne 0 ]; then
+ echo "Formatting errors found in file: $changed_files.
+ \nPlease run:\n\ \"clang-format -style=file -i $changed_files\"
+ \nto correct these errors"
+ exit $exitcode1
+ fi
+
+ cppcheck --enable=performance,portability --error-exitcode=1 $changed_files
+ exitcode2=$?
+ if [ $exitcode2 -ne 0 ]; then
+ exit $exitcode2
+ fi
+ fi
+ exit 0
+done
+
+exit 0'''
+)
+
+ f.close()
+ s = os.stat(file_path)
+ os.chmod(file_path, s.st_mode | stat.S_IEXEC)
+
+parser = argparse.ArgumentParser()
+parser.add_argument("git_hooks_path")
+args = parser.parse_args()
+
+dir_exists = os.path.exists(args.git_hooks_path)
+if not dir_exists:
+ print('Error! The Git hooks directory you supplied does not exist.')
+ sys.exit()
+
+add_pre_push_hooks(args.git_hooks_path)
+set_hooks_dir(args.git_hooks_path)