2012-11-26 14:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
2023-06-14 15:06:58 +02:00
|
|
|
# SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
|
|
#
|
2022-02-10 23:07:11 +01:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2011-09-16 08:58:20 +02:00
|
|
|
|
2011-09-16 10:20:21 +02:00
|
|
|
import project_source_info
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import os
|
2021-10-04 04:12:36 +02:00
|
|
|
import tempfile
|
2011-09-16 10:20:21 +02:00
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
from typing import (
|
|
|
|
Any,
|
|
|
|
List,
|
2022-08-23 02:51:58 +02:00
|
|
|
Tuple,
|
2021-04-13 13:12:17 +02:00
|
|
|
)
|
|
|
|
|
2022-08-23 02:51:58 +02:00
|
|
|
USE_VERBOSE = (os.environ.get("VERBOSE", None) is not None)
|
|
|
|
# Could make configurable.
|
|
|
|
USE_VERBOSE_PROGRESS = True
|
2021-04-13 13:12:17 +02:00
|
|
|
|
2022-08-23 02:51:58 +02:00
|
|
|
CHECKER_BIN = "cppcheck"
|
2012-11-07 02:00:27 +01:00
|
|
|
|
2011-09-16 08:58:20 +02:00
|
|
|
CHECKER_IGNORE_PREFIX = [
|
|
|
|
"extern",
|
2016-07-30 10:15:16 +02:00
|
|
|
]
|
2011-09-16 08:58:20 +02:00
|
|
|
|
2023-06-29 02:53:18 +02:00
|
|
|
# To add files use a relative path.
|
2022-08-23 02:51:58 +02:00
|
|
|
CHECKER_EXCLUDE_SOURCE_FILES = set(os.path.join(*f.split("/")) for f in (
|
2023-06-29 02:53:18 +02:00
|
|
|
# "source/blender/example.c",
|
2022-08-23 02:51:58 +02:00
|
|
|
))
|
2011-09-16 08:58:20 +02:00
|
|
|
|
|
|
|
CHECKER_ARGS = [
|
2022-08-23 02:51:58 +02:00
|
|
|
# Speed up execution.
|
|
|
|
# As Blender has many defines, the total number of configurations is large making execution unreasonably slow.
|
|
|
|
# This could be increased but do so with care.
|
|
|
|
"--max-configs=1",
|
|
|
|
|
|
|
|
# Enable this when includes are missing.
|
|
|
|
# "--check-config",
|
|
|
|
|
|
|
|
# Shows many pedantic issues, some are quite useful.
|
|
|
|
"--enable=all",
|
|
|
|
|
|
|
|
# Also shows useful messages, even if some are false-positives.
|
|
|
|
"--inconclusive",
|
2021-10-04 04:12:36 +02:00
|
|
|
|
2021-10-04 04:12:37 +02:00
|
|
|
# Quiet output, otherwise all defines/includes are printed (overly verbose).
|
|
|
|
# Only enable this for troubleshooting (if defines are not set as expected for example).
|
2022-08-23 02:51:58 +02:00
|
|
|
*(() if USE_VERBOSE else ("--quiet",))
|
2021-10-04 04:12:37 +02:00
|
|
|
|
2021-10-04 04:12:36 +02:00
|
|
|
# NOTE: `--cppcheck-build-dir=<dir>` is added later as a temporary directory.
|
2016-07-30 10:15:16 +02:00
|
|
|
]
|
2011-09-16 08:58:20 +02:00
|
|
|
|
2022-08-23 02:51:58 +02:00
|
|
|
|
|
|
|
def source_info_filter(
|
|
|
|
source_info: List[Tuple[str, List[str], List[str]]],
|
|
|
|
) -> List[Tuple[str, List[str], List[str]]]:
|
|
|
|
source_dir = project_source_info.SOURCE_DIR
|
|
|
|
if not source_dir.endswith(os.sep):
|
|
|
|
source_dir += os.sep
|
|
|
|
source_info_result = []
|
|
|
|
for i, item in enumerate(source_info):
|
|
|
|
c = item[0]
|
|
|
|
if c.startswith(source_dir):
|
|
|
|
c_relative = c[len(source_dir):]
|
|
|
|
if c_relative in CHECKER_EXCLUDE_SOURCE_FILES:
|
|
|
|
CHECKER_EXCLUDE_SOURCE_FILES.remove(c_relative)
|
|
|
|
continue
|
|
|
|
source_info_result.append(item)
|
|
|
|
if CHECKER_EXCLUDE_SOURCE_FILES:
|
|
|
|
sys.stderr.write("Error: exclude file(s) are missing: %r\n" % list(sorted(CHECKER_EXCLUDE_SOURCE_FILES)))
|
|
|
|
sys.exit(1)
|
|
|
|
return source_info_result
|
2012-12-09 15:17:33 +01:00
|
|
|
|
2011-09-20 20:29:19 +02:00
|
|
|
|
2021-10-04 04:12:36 +02:00
|
|
|
def cppcheck() -> None:
|
2011-09-16 08:58:20 +02:00
|
|
|
source_info = project_source_info.build_info(ignore_prefix_list=CHECKER_IGNORE_PREFIX)
|
2013-08-11 01:27:42 +02:00
|
|
|
source_defines = project_source_info.build_defines_as_args()
|
2011-09-16 08:58:20 +02:00
|
|
|
|
2022-08-23 02:51:58 +02:00
|
|
|
# Apply exclusion.
|
|
|
|
source_info = source_info_filter(source_info)
|
|
|
|
|
2011-09-16 08:58:20 +02:00
|
|
|
check_commands = []
|
|
|
|
for c, inc_dirs, defs in source_info:
|
2021-04-13 13:12:17 +02:00
|
|
|
cmd = (
|
|
|
|
[CHECKER_BIN] +
|
|
|
|
CHECKER_ARGS +
|
|
|
|
[c] +
|
|
|
|
[("-I%s" % i) for i in inc_dirs] +
|
|
|
|
[("-D%s" % d) for d in defs] +
|
|
|
|
source_defines
|
|
|
|
)
|
2011-09-16 08:58:20 +02:00
|
|
|
|
|
|
|
check_commands.append((c, cmd))
|
|
|
|
|
2011-09-22 06:41:12 +02:00
|
|
|
process_functions = []
|
2011-09-26 17:39:15 +02:00
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
def my_process(i: int, c: str, cmd: List[str]) -> subprocess.Popen[Any]:
|
2022-08-23 02:51:58 +02:00
|
|
|
if USE_VERBOSE_PROGRESS:
|
2014-03-29 01:25:44 +01:00
|
|
|
percent = 100.0 * (i / len(check_commands))
|
2012-11-07 02:00:27 +01:00
|
|
|
percent_str = "[" + ("%.2f]" % percent).rjust(7) + " %:"
|
2011-09-20 20:29:19 +02:00
|
|
|
|
2012-11-07 02:00:27 +01:00
|
|
|
sys.stdout.flush()
|
2021-10-04 04:12:37 +02:00
|
|
|
sys.stdout.write("%s %s\n" % (
|
|
|
|
percent_str,
|
|
|
|
os.path.relpath(c, project_source_info.SOURCE_DIR)
|
|
|
|
))
|
2011-09-16 08:58:20 +02:00
|
|
|
|
2011-09-22 06:41:12 +02:00
|
|
|
return subprocess.Popen(cmd)
|
|
|
|
|
|
|
|
for i, (c, cmd) in enumerate(check_commands):
|
|
|
|
process_functions.append((my_process, (i, c, cmd)))
|
|
|
|
|
|
|
|
project_source_info.queue_processes(process_functions)
|
2011-09-16 08:58:20 +02:00
|
|
|
|
2012-12-09 15:17:33 +01:00
|
|
|
print("Finished!")
|
|
|
|
|
2011-09-16 08:58:20 +02:00
|
|
|
|
2021-10-04 04:12:36 +02:00
|
|
|
def main() -> None:
|
|
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
CHECKER_ARGS.append("--cppcheck-build-dir=" + temp_dir)
|
|
|
|
cppcheck()
|
|
|
|
|
|
|
|
|
2011-09-16 08:58:20 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|