Skip to content

Commit

Permalink
[bazel/ci] Update clang_format in CI to point to Lowrisc toolchain
Browse files Browse the repository at this point in the history
Generated files also use clang-format, we can call into bazel to use it
from the lowrisc toolchain

Signed-off-by: Drew Macrae <[email protected]>
  • Loading branch information
Drew Macrae authored and a-will committed Jul 7, 2022
1 parent 644a2f9 commit f2396eb
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 81 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# OpenTitan CI is using clang-format 6.0. The documentation for this
# configuration is here:
# OpenTitan CI is using clang-format from the bazel workspace
# Documentation for this configuration is here:
# https://releases.llvm.org/6.0.0/tools/clang/docs/ClangFormatStyleOptions.html

BasedOnStyle: Google
Expand Down
16 changes: 3 additions & 13 deletions ci/scripts/clang-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,8 @@ merge_base="$(git merge-base origin/$tgt_branch HEAD)" || {
}
echo "Running C/C++ lint checks on files changed since $merge_base"

TMPFILE="$(mktemp)" || {
echo >&2 "Failed to create temporary file"
exit 1
}
trap 'rm -f "$TMPFILE"' EXIT
trap 'echo "code failed clang_format_check fix with ./bazelisk.sh run //:clang_format_fix"' ERR

set -o pipefail
git diff -U0 "$merge_base" -- "*.cpp" "*.cc" "*.c" "*.h" ':!*/vendor/*' | \
clang-format-diff -p1 | \
tee "$TMPFILE"
if [ -s "$TMPFILE" ]; then
echo -n "##vso[task.logissue type=error]"
echo "C/C++ lint failed. Use 'git clang-format' with appropriate options to reformat the changed code."
exit 1
fi
git diff --name-only "$merge_base" -- "*.cpp" "*.cc" "*.c" "*.h" ':!*/vendor/*' | \
xargs ./bazelisk.sh run //:clang_format_check --
2 changes: 1 addition & 1 deletion doc/rm/c_cpp_coding_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,4 @@ git add your_modified_file.c
git clang-format
```

To reformat the whole tree the script `util/run-clang-format.sh` can be used.
To reformat the whole tree the command `./bazelisk.sh run //:clang_format_fix` can be used.
27 changes: 13 additions & 14 deletions util/autogen_testutils/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# SPDX-License-Identifier: Apache-2.0

import logging
import shutil
import subprocess
import sys
from pathlib import Path
Expand All @@ -28,11 +27,6 @@ def gen_testutils(ips_with_difs: List[Ip]) -> None:
Returns:
None
"""
# Check clang-format is installed.
assert (shutil.which("clang-format") and
"ERROR: clang-format must be installed to format "
" autogenerated code to pass OpenTitan CI checks.")

# Sort input so the templates get rendered in the same order every time.
ips_with_difs.sort(key=lambda ip: ip.name_snake)

Expand All @@ -43,6 +37,8 @@ def gen_testutils(ips_with_difs: List[Ip]) -> None:
# Create output directories if needed.
autogen_testutils_dir.mkdir(exist_ok=True)

testutilses = []

# Render templates.
for testutils_template_path in testutils_templates_dir.iterdir():
if testutils_template_path.suffix == ".tpl":
Expand All @@ -56,13 +52,16 @@ def gen_testutils(ips_with_difs: List[Ip]) -> None:
autogen_banner=get_autogen_banner(
"util/autogen_testutils.py",
comment=comment_syntax)))
testutilses += [testutils]

# Format autogenerated file with clang-format.
try:
subprocess.check_call(["clang-format", "-i", testutils])
except subprocess.CalledProcessError:
logging.error(
f"failed to format {testutils} with clang-format.")
sys.exit(1)
# Format autogenerated file with clang-format.
try:
subprocess.check_call(
["./ci/bazelisk.sh", "run", "//:clang_format_fix", "--"] + testutilses
)
except subprocess.CalledProcessError:
logging.error(
f"failed to format {testutilses} with clang-format.")
sys.exit(1)

print(f"testutils successfully written to {str(testutils)}.")
print(f"testutils successfully written to {str(testutilses)}.")
4 changes: 2 additions & 2 deletions util/design/secded_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,8 @@ def format_c_files(c_src_filename, c_h_filename):
try:
# Call clang-format to in-place format generated C code. If there are
# any issues log a warning.
result = subprocess.run(['clang-format', '-i', c_src_filename,
c_h_filename], stderr=subprocess.PIPE,
result = subprocess.run(['./ci/bazelisk.sh', 'run', '//:clang_format_fix', '--',
c_src_filename, c_h_filename], stderr=subprocess.PIPE,
universal_newlines=True)
result.check_returncode()
except Exception as e:
Expand Down
30 changes: 16 additions & 14 deletions util/make_new_dif.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import argparse
import glob
import logging
import shutil
import subprocess
import sys
from pathlib import Path
Expand Down Expand Up @@ -151,6 +150,7 @@ def main():
str(header_out_file)))

if "autogen" in args.only:
out_files = []
# Render all templates
for filetype in [".h", ".c", "_unittest.cc"]:
# Build input/output file names.
Expand All @@ -172,19 +172,21 @@ def main():
"util/make_new_dif.py --mode=regen --only=autogen",
"//")))

# Format autogenerated file with clang-format.
assert (shutil.which("clang-format") and
"ERROR: clang-format must be installed to format "
" autogenerated code to pass OpenTitan CI checks.")
try:
subprocess.check_call(["clang-format", "-i", out_file])
except subprocess.CalledProcessError:
logging.error(
f"failed to format {out_file} with clang-format.")
sys.exit(1)

print("Autogenerated DIF successfully written to {}.".format(
out_file))
# Assemble a list of files to format all at once
out_files += [out_file]

# Format autogenerated file with clang-format.
try:
subprocess.check_call(
["./ci/bazelisk.sh", "run", "//:clang_format_fix", "--"] + out_files
)
except subprocess.CalledProcessError:
logging.error(
f"failed to format {out_file} with clang-format.")
sys.exit(1)

print("Autogenerated DIF successfully written to {}.".format(
out_files))

if "checklist" in args.only:
checklist_template_file = REPO_TOP / "doc/project/sw_checklist.md.tpl"
Expand Down
35 changes: 0 additions & 35 deletions util/run-clang-format.sh

This file was deleted.

0 comments on commit f2396eb

Please sign in to comment.