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

clang-format and clang-tidy scripts: More robust algorithm to find correct executable #6041

Merged
merged 9 commits into from
Jan 3, 2024
50 changes: 33 additions & 17 deletions script/clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,43 @@ import sys
import threading



def get_binary(name, version):
clydebarrow marked this conversation as resolved.
Show resolved Hide resolved
clang_binary = f"{name}-{version}"
try:
result = subprocess.check_output([clang_binary, '-version'])
if result.returncode == 0:
return clang_binary
except Exception:
pass
clang_binary = name
try:
result = subprocess.run([clang_binary, '-version'], text=True, capture_output=True)
if result.returncode == 0 and (f"version {version}") in result.stdout:
return clang_binary
except Exception as ex:
pass

print(
f"""
Oops. It looks like {name} is not installed.

Please check you can run "{name} -version" or "{name}-{version} -version"
clydebarrow marked this conversation as resolved.
Show resolved Hide resolved
in your terminal and install
{name} (v{version}) if necessary.

Note you can also upload your code as a pull request on GitHub and see the CI check
output to apply {name}
"""
)
sys.exit()
clydebarrow marked this conversation as resolved.
Show resolved Hide resolved


def run_format(args, queue, lock, failed_files):
"""Takes filenames out of queue and runs clang-format on them."""
while True:
path = queue.get()
invocation = ["clang-format-13"]
invocation = [get_binary("clang-format", 13)]
if args.inplace:
invocation.append("-i")
else:
Expand Down Expand Up @@ -58,22 +90,6 @@ def main():
)
args = parser.parse_args()

try:
get_output("clang-format-13", "-version")
except:
print(
"""
Oops. It looks like clang-format is not installed.

Please check you can run "clang-format-13 -version" in your terminal and install
clang-format (v13) if necessary.

Note you can also upload your code as a pull request on GitHub and see the CI check
output to apply clang-format.
"""
)
return 1

files = []
for path in git_ls_files(["*.cpp", "*.h", "*.tcc"]):
files.append(os.path.relpath(path, os.getcwd()))
Expand Down
51 changes: 34 additions & 17 deletions script/clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,39 @@ import sys
import tempfile
import threading

def get_binary(name, version):
clydebarrow marked this conversation as resolved.
Show resolved Hide resolved
clang_binary = f"{name}-{version}"
try:
result = subprocess.check_output([clang_binary, '-version'])
if result.returncode == 0:
return clang_binary
except Exception:
pass
clang_binary = name
try:
result = subprocess.run([clang_binary, '-version'], text=True, capture_output=True)
if result.returncode == 0 and (f"version {version}") in result.stdout:
return clang_binary
print(f"did not find version {version} in output: {result.stdout}")
except Exception as ex:
print(ex)
pass

print(
f"""
Oops. It looks like {name} is not installed.

Please check you can run "{name} -version" or "{name}-{version} -version"
clydebarrow marked this conversation as resolved.
Show resolved Hide resolved
in your terminal and install
{name} (v{version}) if necessary.

Note you can also upload your code as a pull request on GitHub and see the CI check
output to apply {name}
"""
)
sys.exit()



def clang_options(idedata):
cmd = []
Expand Down Expand Up @@ -113,7 +146,7 @@ def clang_options(idedata):
def run_tidy(args, options, tmpdir, queue, lock, failed_files):
while True:
path = queue.get()
invocation = ["clang-tidy-14"]
invocation = [get_binary("clang-tidy", 14)]

if tmpdir is not None:
invocation.append("--export-fixes")
Expand Down Expand Up @@ -193,22 +226,6 @@ def main():
)
args = parser.parse_args()

try:
get_output("clang-tidy-14", "-version")
except:
print(
"""
Oops. It looks like clang-tidy-14 is not installed.

Please check you can run "clang-tidy-14 -version" in your terminal and install
clang-tidy (v14) if necessary.

Note you can also upload your code as a pull request on GitHub and see the CI check
output to apply clang-tidy.
"""
)
return 1

idedata = load_idedata(args.environment)
options = clang_options(idedata)

Expand Down