77
88import argparse
99import json
10- from pathlib import Path
10+ import os
11+ import subprocess
1112from subprocess import CalledProcessError
1213
1314import utils
14- import os
15- import subprocess
1615
1716# Currently we are not checking all directories as they might need to run compilation first for Qt UIC
1817CHECK_DIRECTORIES = [
1918 "src/core" ,
2019 "src/misc" ,
21- "src/input"
20+ "src/input" ,
21+ "src/sound" ,
22+ "src/plugins" ,
23+ "src/xswiftbus" ,
2224]
2325
2426
2527def _get_all_files (build_path : str ) -> list [str ]:
28+ """Get all files inside the compile commands."""
2629 src_path = utils .get_swift_source_path ()
2730 os .chdir (src_path )
2831
2932 with open (os .path .join (build_path , "compile_commands.json" ), 'r' ) as f :
3033 commands = json .load (f )
3134 commands = set ([os .path .relpath (entry ["file" ], utils .get_swift_source_path ()) for entry in commands ])
3235
33- commands = [command for command in commands if not command .startswith ("third_party" ) and not command .startswith (build_path ) and command .startswith ("src/misc/network/role.cpp" )]
36+ commands = [command for command in commands if
37+ not command .startswith ("third_party" ) and not command .startswith (build_path )]
3438 return commands
3539
40+
3641def _get_all_files_ci (build_path : str ) -> list [str ]:
42+ """Get all files in the compile commands and which will be checked in CI runs."""
3743 src_path = utils .get_swift_source_path ()
3844 os .chdir (src_path )
3945
@@ -44,7 +50,9 @@ def _get_all_files_ci(build_path: str) -> list[str]:
4450 commands = [command for command in commands if command .startswith (tuple (CHECK_DIRECTORIES ))]
4551 return commands
4652
53+
4754def _get_changed_files_ci (build_path : str ) -> set [str ]:
55+ """Get all files in the compile commands which has changed since the last main commit and are in the included directories."""
4856 src_path = utils .get_swift_source_path ()
4957 os .chdir (src_path )
5058
@@ -63,6 +71,21 @@ def _get_changed_files_ci(build_path: str) -> set[str]:
6371 return files & commands
6472
6573
74+ def _has_changed_files () -> bool :
75+ src_path = utils .get_swift_source_path ()
76+ os .chdir (src_path )
77+
78+ result = subprocess .run (
79+ ['git' , 'diff' , '--name-only' , 'origin/main' ],
80+ check = True ,
81+ stdout = subprocess .PIPE ,
82+ text = True
83+ )
84+
85+ return len ([line for line in result .stdout .splitlines () if
86+ (line .endswith ('.cpp' ) or line .endswith ('.h' )) and line .startswith (tuple (CHECK_DIRECTORIES ))]) > 0
87+
88+
6689def run_clang_tidy (build_path : str , changed_source_files : set [str ]):
6790 print (f"Run clang-tidy on files: { changed_source_files } " )
6891 nproc = 10
@@ -76,7 +99,6 @@ def run_clang_tidy(build_path: str, changed_source_files: set[str]):
7699 '-p' , build_path ,
77100 '--warnings-as-errors' , '*' ,
78101 '--quiet' ,
79- #'--header-filter', '*',
80102 '--header-filter' , f'{ utils .get_swift_source_path ()} /src/' ,
81103 ], input = '\n ' .join (changed_source_files ), text = True , check = True )
82104 except CalledProcessError :
@@ -103,11 +125,17 @@ def run_clazy(build_path: str, changed_source_files: set[str]):
103125
104126def main ():
105127 parser = argparse .ArgumentParser (prog = "swift clang-tidy helper" )
106- parser .add_argument ("--build-path" , required = True , help = 'Path to build folder' )
107- parser .add_argument ("--all-files" , action = "store_true" ,
108- help = "Run check on all files in build database" )
109- parser .add_argument ("--files-ci" , action = "store_true" ,
110- help = "Run check on specific files in build database which are also used in CI" )
128+ parser .add_argument ("--build-path" , help = 'Path to build folder' )
129+
130+ check_mode = parser .add_mutually_exclusive_group (required = True )
131+ check_mode .add_argument ("--all-files" , action = "store_true" ,
132+ help = "Run check on all files in the compile commands" )
133+ check_mode .add_argument ("--all-files-ci" , action = "store_true" ,
134+ help = "Run check on all files in the compile commands and which will be checked in CI runs." )
135+ check_mode .add_argument ("--changed-files-ci" , action = "store_true" ,
136+ help = "Run check on all files in the compile commands which are changed since the last main commit and are in the included directories (no Qt UIC files)." )
137+ check_mode .add_argument ("--check-changed-files" , action = "store_true" ,
138+ help = "Check if files have changed for evaluation. Program exits with 0 if no files changed; with 1 otherwise" )
111139
112140 parser .add_argument ("--clang-tidy" , action = "store_true" ,
113141 help = "Run clang-tidy checks" )
@@ -117,10 +145,12 @@ def main():
117145
118146 if args .all_files :
119147 source_files = _get_all_files (args .build_path )
120- elif args .files_ci :
148+ elif args .all_files_ci :
121149 source_files = _get_all_files_ci (args .build_path )
122- else :
150+ elif args . changed_files_ci :
123151 source_files = _get_changed_files_ci (args .build_path )
152+ else :
153+ exit (1 if _has_changed_files () else 0 )
124154
125155 if args .clang_tidy :
126156 run_clang_tidy (args .build_path , source_files )
0 commit comments