diff options
author | Jussi Pakkanen <jpakkane@gmail.com> | 2018-12-23 16:19:45 +0200 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2018-12-30 00:50:00 +0200 |
commit | 1fca654055d3502d2db9c5aad66a522beaa1df19 (patch) | |
tree | baf97e14e15d57482ed73adba96ab98180ca13d6 /mesonbuild | |
parent | 3b495c397ec1baffe99d63031f2b03301b7f6ba5 (diff) | |
download | meson-1fca654055d3502d2db9c5aad66a522beaa1df19.zip meson-1fca654055d3502d2db9c5aad66a522beaa1df19.tar.gz meson-1fca654055d3502d2db9c5aad66a522beaa1df19.tar.bz2 |
Add a clang-format target.
Diffstat (limited to 'mesonbuild')
-rw-r--r-- | mesonbuild/backend/ninjabackend.py | 26 | ||||
-rw-r--r-- | mesonbuild/scripts/clangformat.py | 37 |
2 files changed, 61 insertions, 2 deletions
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index f49649b..5d59fa9 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -2579,8 +2579,7 @@ rule FORTRAN_DEP_HACK%s # Alias that runs the target defined above self.create_target_alias('meson-dist', outfile) - # For things like scan-build and other helper tools we might have. - def generate_utils(self, outfile): + def generate_scanbuild(self, outfile): cmd = self.environment.get_build_command() + \ ['--internal', 'scanbuild', self.environment.source_dir, self.environment.build_dir] + \ self.environment.get_build_command() + self.get_user_option_args() @@ -2590,6 +2589,29 @@ rule FORTRAN_DEP_HACK%s elem.write(outfile) # Alias that runs the target defined above self.create_target_alias('meson-scan-build', outfile) + + def generate_clangformat(self, outfile): + import shutil + target_name = 'clang-format' + if shutil.which('clang-format') is None: + 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')): + return + if 'target_name' in self.all_outputs: + return + cmd = self.environment.get_build_command() + \ + ['--internal', 'clangformat', self.environment.source_dir, self.environment.build_dir] + elem = NinjaBuildElement(self.all_outputs, 'meson-' + target_name, 'CUSTOM_COMMAND', 'PHONY') + elem.add_item('COMMAND', cmd) + elem.add_item('pool', 'console') + elem.write(outfile) + self.create_target_alias('meson-' + target_name, outfile) + + # For things like scan-build and other helper tools we might have. + def generate_utils(self, outfile): + self.generate_scanbuild(outfile) + self.generate_clangformat(outfile) cmd = self.environment.get_build_command() + ['--internal', 'uninstall'] elem = NinjaBuildElement(self.all_outputs, 'meson-uninstall', 'CUSTOM_COMMAND', 'PHONY') elem.add_item('COMMAND', cmd) diff --git a/mesonbuild/scripts/clangformat.py b/mesonbuild/scripts/clangformat.py new file mode 100644 index 0000000..fcdf5af --- /dev/null +++ b/mesonbuild/scripts/clangformat.py @@ -0,0 +1,37 @@ +# Copyright 2018 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 +from concurrent.futures import ThreadPoolExecutor + +from ..compilers import lang_suffixes + +def clangformat(srcdir_name, builddir_name): + srcdir = pathlib.Path(srcdir_name) + suffixes = set(lang_suffixes['c']).union(set(lang_suffixes['cpp'])) + futures = [] + with ThreadPoolExecutor() as e: + for f in (x for suff in suffixes for x in srcdir.glob('**/*.' + suff)): + strf = str(f) + if strf.startswith(builddir_name): + continue + futures.append(e.submit(subprocess.check_call, ['clang-format', '-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) |