Find clang-format with alternative names

This is similar to what we currently do for scan-build except there is
no environment variable to choose a specific clang-format to run. If an
environment variable is needed for better control, we can add it later.
pull/5939/head
Ting-Wei Lan 5 years ago
parent 08ce1fb541
commit 0390b673f1
  1. 3
      mesonbuild/backend/ninjabackend.py
  2. 18
      mesonbuild/environment.py
  3. 13
      mesonbuild/scripts/clangformat.py

@ -2657,9 +2657,8 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
self.create_target_alias('meson-scan-build')
def generate_clangformat(self):
import shutil
target_name = 'clang-format'
if shutil.which('clang-format') is None:
if not environment.detect_clangformat():
return
if not os.path.exists(os.path.join(self.environment.source_dir, '.clang-format')) and \
not os.path.exists(os.path.join(self.environment.source_dir, '_clang-format')):

@ -189,7 +189,7 @@ def get_llvm_tool_names(tool: str) -> typing.List[str]:
names.append(tool + suffix)
return names
def detect_scanbuild():
def detect_scanbuild() -> typing.List[str]:
""" Look for scan-build binary on build platform
First, if a SCANBUILD env variable has been provided, give it precedence
@ -220,6 +220,22 @@ def detect_scanbuild():
return [tool]
return []
def detect_clangformat() -> typing.List[str]:
""" Look for clang-format binary on build platform
Do the same thing as detect_scanbuild to find clang-format except it
currently does not check the environment variable.
Return: a single-element list of the found clang-format binary ready to be
passed to Popen()
"""
tools = get_llvm_tool_names('clang-format')
for tool in tools:
path = shutil.which(tool)
if path is not None:
return [path]
return []
def detect_native_windows_arch():
"""
The architecture of Windows itself: x86, amd64 or arm64

@ -16,9 +16,10 @@ import pathlib
import subprocess
from concurrent.futures import ThreadPoolExecutor
from ..environment import detect_clangformat
from ..compilers import lang_suffixes
def clangformat(srcdir_name, builddir_name):
def clangformat(exelist, srcdir_name, builddir_name):
srcdir = pathlib.Path(srcdir_name)
suffixes = set(lang_suffixes['c']).union(set(lang_suffixes['cpp']))
suffixes.add('h')
@ -28,11 +29,17 @@ def clangformat(srcdir_name, builddir_name):
strf = str(f)
if strf.startswith(builddir_name):
continue
futures.append(e.submit(subprocess.check_call, ['clang-format', '-style=file', '-i', strf]))
futures.append(e.submit(subprocess.check_call, exelist + ['-style=file', '-i', strf]))
[x.result() for x in futures]
return 0
def run(args):
srcdir_name = args[0]
builddir_name = args[1]
return clangformat(srcdir_name, builddir_name)
exelist = detect_clangformat()
if not exelist:
print('Could not execute clang-format "%s"' % ' '.join(exelist))
return 1
return clangformat(exelist, srcdir_name, builddir_name)

Loading…
Cancel
Save