Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DAOS-623 ci: avoid exception in extra.py when clang-format is missing #15762

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions site_scons/site_tools/extra/extra.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""
(C) Copyright 2018-2022 Intel Corporation.
(C) Copyright 2025 Hewlett Packard Enterprise Development LP.

SPDX-License-Identifier: BSD-2-Clause-Patent

Expand All @@ -22,38 +23,42 @@
MIN_FORMAT_VERSION = 12


def _supports_custom_format(clang_exe):
def _get_version_string():
clang_exe = WhereIs('clang-format')
if clang_exe is None:
return None
try:
rawbytes = subprocess.check_output([clang_exe, "-version"])
match = re.search(r"version ((\d+)(\.\d+)+)", rawbytes.decode('utf-8'))
return match.group(1)
except subprocess.CalledProcessError:
return None


def _supports_custom_format(version):
"""Checks if the version of clang-format is new enough.

Older versions complain about some of the options used so enforce a minimum version.
"""
try:
rawbytes = subprocess.check_output([clang_exe, "-version"])
output = rawbytes.decode('utf-8')
except subprocess.CalledProcessError:
if version is None:
print("Unsupported clang-format for custom style. Using Mozilla style.")
return False

match = re.search(r"version (\d+)\.", output)
match = re.search(r"(\d+)\.", version)
if match and int(match.group(1)) >= MIN_FORMAT_VERSION:
return True

print(f'Custom .clang-format wants version {MIN_FORMAT_VERSION}+. Using Mozilla style.')
return False


def _supports_correct_style(clang_exe):
def _supports_correct_style(version):
"""Checks if the version of clang-format is 14.0.5 or newer.

Older versions contain bugs so will generate incorrectly formatted code on occasion.
"""
try:
rawbytes = subprocess.check_output([clang_exe, "-version"])
output = rawbytes.decode('utf-8')
except subprocess.CalledProcessError:
return False

match = re.search(r'version ([\d+\.]+)', output)
match = re.search(r'([\d+\.]+)', version)
if match:
parts = match.group(1).split('.')
if int(parts[0]) != 14:
Expand All @@ -72,7 +77,7 @@ def _find_indent():
indent = WhereIs("clang-format")
if indent is None:
return None
if _supports_custom_format(indent):
if _supports_custom_format(_get_version_string()):
style = "file"
else:
style = "Mozilla"
Expand Down Expand Up @@ -102,10 +107,11 @@ def _preprocess_emitter(source, target, env):

def main():
"""Check for a supported version of clang-format"""
supported = _supports_correct_style(WhereIs('clang-format'))
if not supported:
version = _get_version_string()
if (version is None) or (not _supports_correct_style(version)):
print('Install clang-format version 14.0.5 or newer to reformat code')
sys.exit(1)
print(f"Clang-format version {version} installed")
sys.exit(0)


Expand Down
Loading