• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3# Copyright 2019 The ANGLE Project Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# apply_clang_format_on_all_sources.py:
8#   Script to apply clang-format recursively on directory,
9#   example usage:
10#       ./scripts/apply_clang_format_on_all_sources.py src
11
12from __future__ import print_function
13
14import os
15import sys
16import platform
17import subprocess
18
19# inplace change and use style from .clang-format
20CLANG_FORMAT_ARGS = ['-i', '-style=file']
21
22
23def main(directory):
24    system = platform.system()
25
26    clang_format_exe = 'clang-format'
27    if system == 'Windows':
28        clang_format_exe += '.bat'
29
30    partial_cmd = [clang_format_exe] + CLANG_FORMAT_ARGS
31
32    for subdir, _, files in os.walk(directory):
33        if 'third_party' in subdir:
34            continue
35
36        for f in files:
37            if f.endswith(('.c', '.h', '.cpp', '.hpp')):
38                f_abspath = os.path.join(subdir, f)
39                print("Applying clang-format on ", f_abspath)
40                subprocess.check_call(partial_cmd + [f_abspath])
41
42
43if __name__ == '__main__':
44    if len(sys.argv) > 2:
45        print('Too mang args', file=sys.stderr)
46
47    elif len(sys.argv) == 2:
48        main(os.path.join(os.getcwd(), sys.argv[1]))
49
50    else:
51        main(os.getcwd())
52