2 # SPDX-License-Identifier: GPL-2.0-or-later
5 Run this script to check if headers are included multiple times.
7 python3 check_header_duplicate.py ../../
9 Now build the code to find duplicate errors, resolve them manually.
11 Then restore the headers to their original state:
13 python3 check_header_duplicate.py --restore ../../
16 # Use GCC's __INCLUDE_LEVEL__ to find direct duplicate includes
21 def source_filepath_guard(filepath
):
25 #if __INCLUDE_LEVEL__ == 1
26 # ifdef _DOUBLEHEADERGUARD_%d
27 # error "duplicate header!"
31 #if __INCLUDE_LEVEL__ == 1
32 # define _DOUBLEHEADERGUARD_%d
37 with
open(filepath
, 'a', encoding
='utf-8') as f
:
41 def source_filepath_restore(filepath
):
43 os
.system("git co %s" % filepath
)
46 def scan_source_recursive(dirpath
, is_restore
):
48 from os
.path
import join
, splitext
50 # ensure git working dir is ok
53 def source_list(path
, filename_check
=None):
54 for dirpath
, dirnames
, filenames
in os
.walk(path
):
56 dirnames
[:] = [d
for d
in dirnames
if not d
.startswith(".")]
58 for filename
in filenames
:
59 filepath
= join(dirpath
, filename
)
60 if filename_check
is None or filename_check(filepath
):
63 def is_source(filename
):
64 ext
= splitext(filename
)[1]
65 return (ext
in {".hpp", ".hxx", ".h", ".hh"})
67 def is_ignore(filename
):
70 for filepath
in sorted(source_list(dirpath
, is_source
)):
71 print("file:", filepath
)
72 if is_ignore(filepath
):
76 source_filepath_restore(filepath
)
78 source_filepath_guard(filepath
)
83 is_restore
= ("--restore" in sys
.argv
[1:])
84 scan_source_recursive(sys
.argv
[-1], is_restore
)
87 if __name__
== "__main__":