|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Enforce Halide's CMake coding standards from doc/CodeStyleCMake.md.""" |
| 3 | + |
| 4 | +import re |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +# fmt: off |
| 9 | + |
| 10 | +PROHIBITED_COMMANDS: dict[str, str] = { |
| 11 | + "add_compile_definitions": "use target_compile_definitions", |
| 12 | + "add_compile_options": "use target_compile_options", |
| 13 | + "add_definitions": "use target_compile_definitions", |
| 14 | + "add_link_options": "use target_link_options", |
| 15 | + "aux_source_directory": "list source files explicitly", |
| 16 | + "build_command": "use CMAKE_CTEST_COMMAND", |
| 17 | + "cmake_host_system_information": "inspect toolchain variables instead", |
| 18 | + "create_test_sourcelist": "use Halide's own testing solution", |
| 19 | + "define_property": "use a cache variable", |
| 20 | + "enable_language": "Halide is C/C++ only", |
| 21 | + "fltk_wrap_ui": "Halide does not use FLTK", |
| 22 | + "include_directories": "use target_include_directories", |
| 23 | + "include_external_msproject": "write a CMake package config file", |
| 24 | + "include_guard": "use functions, not recursive inclusion", |
| 25 | + "include_regular_expression": "changes default dependency checking", |
| 26 | + "link_directories": "use target_link_libraries", |
| 27 | + "link_libraries": "use target_link_libraries", |
| 28 | + "load_cache": "write a vcpkg port instead", |
| 29 | + "macro": "use function() instead", |
| 30 | + "remove_definitions": "use target_compile_definitions with genexes", |
| 31 | + "set_directory_properties": "use cache variables or target properties", |
| 32 | + "site_name": "privacy: do not leak host name", |
| 33 | + "variable_watch": "debugging helper, not for production", |
| 34 | +} |
| 35 | + |
| 36 | +# fmt: on |
| 37 | + |
| 38 | +# Patterns that need special handling beyond simple command detection. |
| 39 | +SPECIAL_PATTERNS: list[tuple[re.Pattern[str], str]] = [ |
| 40 | + ( |
| 41 | + re.compile(r"\bcmake_policy\s*\(.*\bOLD\b", re.IGNORECASE), |
| 42 | + "cmake_policy(... OLD) is deprecated; fix code for new policy", |
| 43 | + ), |
| 44 | + ( |
| 45 | + re.compile(r"\bfile\s*\(\s*GLOB", re.IGNORECASE), |
| 46 | + "file(GLOB ...) interacts poorly with incremental builds; list files explicitly", |
| 47 | + ), |
| 48 | + ( |
| 49 | + re.compile(r"\bset_property\s*\(\s*DIRECTORY\b", re.IGNORECASE), |
| 50 | + "set_property(DIRECTORY) is prohibited; use cache variables or target properties", |
| 51 | + ), |
| 52 | + ( |
| 53 | + re.compile( |
| 54 | + r"\btarget_link_libraries\s*\(\s*\S+\s+(?!PRIVATE|PUBLIC|INTERFACE)\S", |
| 55 | + re.IGNORECASE, |
| 56 | + ), |
| 57 | + "target_link_libraries without visibility specifier; add PRIVATE, PUBLIC, or INTERFACE", |
| 58 | + ), |
| 59 | +] |
| 60 | + |
| 61 | +# Build a single regex for all prohibited commands: matches the command name |
| 62 | +# followed by '(' with optional whitespace, case-insensitive. |
| 63 | +_CMD_PATTERN = re.compile( |
| 64 | + r"\b(" + "|".join(re.escape(c) for c in PROHIBITED_COMMANDS) + r")\s*\(", |
| 65 | + re.IGNORECASE, |
| 66 | +) |
| 67 | + |
| 68 | +_COMMENT_RE = re.compile(r"^\s*#") |
| 69 | + |
| 70 | + |
| 71 | +def check_file(path: Path): |
| 72 | + text = path.read_text() |
| 73 | + |
| 74 | + for lineno, line in enumerate(text.splitlines(), start=1): |
| 75 | + if _COMMENT_RE.match(line): |
| 76 | + continue |
| 77 | + |
| 78 | + if "#" in line: |
| 79 | + code, comment = line.split("#", 1) |
| 80 | + else: |
| 81 | + code, comment = line, "" |
| 82 | + |
| 83 | + if "nolint" in comment: |
| 84 | + continue |
| 85 | + |
| 86 | + for m in _CMD_PATTERN.finditer(code): |
| 87 | + cmd = m.group(1).lower() |
| 88 | + reason = PROHIBITED_COMMANDS[cmd] |
| 89 | + yield f"{path}:{lineno}: {cmd}() is prohibited; {reason}" |
| 90 | + |
| 91 | + for pattern, message in SPECIAL_PATTERNS: |
| 92 | + if pattern.search(code): |
| 93 | + yield f"{path}:{lineno}: {message}" |
| 94 | + |
| 95 | + |
| 96 | +def main(): |
| 97 | + status = 0 |
| 98 | + for arg in sys.argv[1:]: |
| 99 | + for error in check_file(Path(arg)): |
| 100 | + print(error, file=sys.stderr) |
| 101 | + status = 1 |
| 102 | + return status |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + sys.exit(main()) |
0 commit comments