meson/mesonbuild/scripts/clangtidy.py

58 lines
2.1 KiB
Python
Raw Normal View History

2019-09-30 04:17:02 +08:00
# Copyright 2019 The Meson development team
# 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 pathlib
import subprocess
2019-10-20 03:18:28 +08:00
import shutil
import os
import re
2019-09-30 04:17:02 +08:00
from concurrent.futures import ThreadPoolExecutor
2020-08-30 03:23:43 +08:00
import typing as T
2019-09-30 04:17:02 +08:00
from ..compilers import lang_suffixes
def manual_clangtidy(srcdir_name: str, builddir_name: str) -> int:
2019-09-30 04:17:02 +08:00
srcdir = pathlib.Path(srcdir_name)
suffixes = set(lang_suffixes['c']).union(set(lang_suffixes['cpp']))
suffixes.add('h')
futures = []
2019-10-20 03:18:28 +08:00
returncode = 0
2019-09-30 04:17:02 +08:00
with ThreadPoolExecutor() as e:
for f in (x for suff in suffixes for x in srcdir.glob('**/*.' + suff)):
if f.is_dir():
continue
2019-09-30 04:17:02 +08:00
strf = str(f)
if strf.startswith(builddir_name):
continue
2019-10-20 03:18:28 +08:00
futures.append(e.submit(subprocess.run, ['clang-tidy', '-p', builddir_name, strf]))
returncode = max([x.result().returncode for x in futures])
2019-10-20 03:18:28 +08:00
return returncode
def clangtidy(srcdir_name: str, builddir_name: str) -> int:
2019-10-20 03:18:28 +08:00
run_clang_tidy = None
for rct in ('run-clang-tidy', 'run-clang-tidy.py'):
if shutil.which(rct):
run_clang_tidy = rct
break
if run_clang_tidy:
return subprocess.run([run_clang_tidy, '-p', builddir_name, '^(?!' + re.escape(builddir_name + os.path.sep) +').*$']).returncode
2019-10-20 03:18:28 +08:00
else:
print('Could not find run-clang-tidy, running checks manually.')
return manual_clangtidy(srcdir_name, builddir_name)
2019-09-30 04:17:02 +08:00
2020-08-30 03:23:43 +08:00
def run(args: T.List[str]) -> int:
2019-09-30 04:17:02 +08:00
srcdir_name = args[0]
builddir_name = args[1]
return clangtidy(srcdir_name, builddir_name)