4 # ***** BEGIN GPL LICENSE BLOCK *****
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # Contributor(s): Campbell Barton
22 # ***** END GPL LICENSE BLOCK *****
26 from cmake_consistency_check_config import IGNORE, UTF8_CHECK, SOURCE_DIR
29 from os.path import join, dirname, normpath, splitext
31 print("Scanning:", SOURCE_DIR)
38 def replace_line(f, i, text, keep_indent=True):
39 file_handle = open(f, 'r')
40 data = file_handle.readlines()
44 ws = l[:len(l) - len(l.lstrip())]
46 data[i] = "%s%s\n" % (ws, text)
48 file_handle = open(f, 'w')
49 file_handle.writelines(data)
53 def source_list(path, filename_check=None):
54 for dirpath, dirnames, filenames in os.walk(path):
57 if dirpath.startswith("."):
60 for filename in filenames:
61 if filename_check is None or filename_check(filename):
62 yield os.path.join(dirpath, filename)
66 def is_cmake(filename):
67 ext = splitext(filename)[1]
68 return (ext == ".cmake") or (filename == "CMakeLists.txt")
71 def is_c_header(filename):
72 ext = splitext(filename)[1]
73 return (ext in (".h", ".hpp", ".hxx"))
77 ext = splitext(filename)[1]
78 return (ext in (".c", ".cpp", ".cxx", ".m", ".mm", ".rc"))
81 def is_c_any(filename):
82 return is_c(filename) or is_c_header(filename)
90 filen = open(f, "r", encoding="utf8")
96 def is_definition(l, f, i, name):
97 if ('set(%s' % name) in l or ('set(' in l and l.endswith(name)):
98 if len(l.split()) > 1:
99 raise Exception("strict formatting not kept 'set(%s*' %s:%d" % (name, f, i))
102 if ("list(APPEND %s" % name) in l or ('list(APPEND ' in l and l.endswith(name)):
104 raise Exception("strict formatting not kept 'list(APPEND %s...)' on 1 line %s:%d" % (name, f, i))
107 while it is not None:
109 while it is not None:
113 except StopIteration:
117 if not l.startswith("#"):
118 found = is_definition(l, f, i, "SRC")
122 found = is_definition(l, f, i, "INC")
128 cmake_base = dirname(f)
130 while it is not None:
134 except StopIteration:
140 if not l.startswith("#"):
144 raise Exception("strict formatting not kept '*)' %s:%d" % (f, i))
148 l = l.replace("${CMAKE_CURRENT_SOURCE_DIR}", cmake_base)
152 elif l.startswith("$"):
153 if context_name == "SRC":
154 # assume if it ends with context_name we know about it
155 if not l.split("}")[0].endswith(context_name):
156 print("Can't use var '%s' %s:%d" % (l, f, i))
157 elif len(l.split()) > 1:
158 raise Exception("Multi-line define '%s' %s:%d" % (l, f, i))
160 new_file = normpath(join(cmake_base, l))
162 if context_name == "SRC":
163 if is_c_header(new_file):
164 sources_h.append(new_file)
165 global_refs.setdefault(new_file, []).append((f, i))
167 sources_c.append(new_file)
168 global_refs.setdefault(new_file, []).append((f, i))
169 elif l in ("PARENT_SCOPE", ):
172 elif new_file.endswith(".list"):
174 elif new_file.endswith(".def"):
177 raise Exception("unknown file type - not c or h %s -> %s" % (f, new_file))
179 elif context_name == "INC":
180 if os.path.isdir(new_file):
181 new_path_rel = os.path.relpath(new_file, cmake_base)
183 if new_path_rel != l:
184 print("overly relative path:\n %s:%d\n %s\n %s" % (f, i, l, new_path_rel))
186 ## Save time. just replace the line
187 # replace_line(f, i - 1, new_path_rel)
190 raise Exception("non existant include %s:%d -> %s" % (f, i, new_file))
194 global_h.update(set(sources_h))
195 global_c.update(set(sources_c))
197 if not sources_h and not sources_c:
198 raise Exception("No sources %s" % f)
200 sources_h_fs = list(source_list(cmake_base, is_c_header))
201 sources_c_fs = list(source_list(cmake_base, is_c))
203 # find missing C files:
205 for ff in sources_c_fs:
206 if ff not in sources_c:
207 print(" missing: " + ff)
217 for cmake in source_list(SOURCE_DIR, is_cmake):
228 # First do stupid check, do these files exist?
229 print("\nChecking for missing references:")
232 for f in (global_h | global_c):
233 if f.endswith("dna.c"):
236 if not os.path.exists(f):
237 refs = global_refs[f]
242 raise Exception("CMake referenecs missing, internal error, aborting!")
248 print("%s:%d" % (cf, i))
249 # Write a 'sed' script, useful if we get a lot of these
250 # print("sed '%dd' '%s' > '%s.tmp' ; mv '%s.tmp' '%s'" % (i, cf, cf, cf, cf))
254 raise Exception("CMake referenecs missing files, aborting!")
258 # now check on files not accounted for.
259 print("\nC/C++ Files CMake doesnt know about...")
260 for cf in sorted(source_list(SOURCE_DIR, is_c)):
261 if not is_ignore(cf):
262 if cf not in global_c:
263 print("missing_c: ", cf)
265 # check if automake builds a corrasponding .o file.
268 out1 = os.path.splitext(cf)[0] + ".o"
269 out2 = os.path.splitext(cf)[0] + ".Po"
270 out2_dir, out2_file = out2 = os.path.split(out2)
271 out2 = os.path.join(out2_dir, ".deps", out2_file)
272 if not os.path.exists(out1) and not os.path.exists(out2):
276 print("\nC/C++ Headers CMake doesnt know about...")
277 for hf in sorted(source_list(SOURCE_DIR, is_c_header)):
278 if not is_ignore(hf):
279 if hf not in global_h:
280 print("missing_h: ", hf)
285 for files in (global_c, global_h):
286 for f in sorted(files):
287 if os.path.exists(f):
288 # ignore outside of our source tree
289 if "extern" not in f:
292 for l in open(f, "r", encoding="utf8"):
295 print("Non utf8: %s:%d" % (f, i))
297 traceback.print_exc()