Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
Harrison Mutai | 8c3afa3 | 2022-02-04 09:33:24 +0000 | [diff] [blame] | 3 | # Copyright (c) 2019-2022, Arm Limited. All rights reserved. |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
| 8 | import argparse |
| 9 | import codecs |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 10 | import collections |
Zelalem | f75daf2 | 2020-08-04 16:29:43 -0500 | [diff] [blame] | 11 | import functools |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 12 | import os |
| 13 | import re |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 14 | import subprocess |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 15 | import sys |
| 16 | import utils |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 17 | import logging |
Harrison Mutai | ec878ce | 2022-05-13 15:18:38 +0100 | [diff] [blame^] | 18 | from pathlib import Path |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 19 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 20 | # File extensions to check |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 21 | VALID_FILE_EXTENSIONS = (".c", ".S", ".h") |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 22 | |
| 23 | |
| 24 | # Paths inside the tree to ignore. Hidden folders and files are always ignored. |
| 25 | # They mustn't end in '/'. |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 26 | IGNORED_FOLDERS = ( |
| 27 | "include/lib/stdlib", |
| 28 | "include/lib/libc", |
| 29 | "include/lib/libfdt", |
| 30 | "lib/libfdt", |
| 31 | "lib/libc", |
| 32 | "lib/stdlib", |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 33 | ) |
| 34 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 35 | # List of ignored files in folders that aren't ignored |
| 36 | IGNORED_FILES = () |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 37 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 38 | INCLUDE_RE = re.compile(r"^\s*#\s*include\s\s*(?P<path>[\"<].+[\">])") |
| 39 | INCLUDE_RE_DIFF = re.compile(r"^\+?\s*#\s*include\s\s*(?P<path>[\"<].+[\">])") |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 40 | |
| 41 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 42 | def include_paths(lines, diff_mode=False): |
| 43 | """List all include paths in a file. Ignore starting `+` in diff mode.""" |
| 44 | pattern = INCLUDE_RE_DIFF if diff_mode else INCLUDE_RE |
| 45 | matches = (pattern.match(line) for line in lines) |
| 46 | return [m.group("path") for m in matches if m] |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 47 | |
| 48 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 49 | def file_include_list(path): |
| 50 | """Return a list of all include paths in a file or None on failure.""" |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 51 | try: |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 52 | with codecs.open(path, encoding="utf-8") as f: |
| 53 | return include_paths(f) |
| 54 | except Exception: |
| 55 | logging.exception(path + ":error while parsing.") |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 56 | return None |
| 57 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 58 | |
Zelalem | f75daf2 | 2020-08-04 16:29:43 -0500 | [diff] [blame] | 59 | @functools.lru_cache() |
| 60 | def dir_include_paths(directory): |
| 61 | """Generate a set that contains all includes from a directory""" |
| 62 | dir_includes = set() |
| 63 | for (root, _dirs, files) in os.walk(directory): |
| 64 | for fname in files: |
| 65 | if fname.endswith(".h"): |
| 66 | names = os.path.join(root, fname).split(os.sep) |
| 67 | for i in range(len(names)): |
| 68 | suffix_path = "/".join(names[i:]) |
| 69 | dir_includes.add(suffix_path) |
| 70 | return dir_includes |
| 71 | |
| 72 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 73 | def inc_order_is_correct(inc_list, path, commit_hash=""): |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 74 | """Returns true if the provided list is in order. If not, output error |
| 75 | messages to stdout.""" |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 76 | |
| 77 | # If there are less than 2 includes there's no need to check. |
| 78 | if len(inc_list) < 2: |
| 79 | return True |
| 80 | |
| 81 | if commit_hash != "": |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 82 | commit_hash = commit_hash + ":" |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 83 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 84 | # First, check if all includes are in the appropriate group. |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 85 | incs = collections.defaultdict(list) |
| 86 | error_msgs = [] |
Zelalem | f75daf2 | 2020-08-04 16:29:43 -0500 | [diff] [blame] | 87 | plat_incs = dir_include_paths("plat") | dir_include_paths("include/plat") |
Yann Gautier | 72d2dbc | 2021-09-14 10:48:24 +0200 | [diff] [blame] | 88 | plat_common_incs = dir_include_paths("include/plat/common") |
| 89 | plat_incs.difference_update(plat_common_incs) |
Zelalem | f75daf2 | 2020-08-04 16:29:43 -0500 | [diff] [blame] | 90 | libc_incs = dir_include_paths("include/lib/libc") |
Harrison Mutai | ec878ce | 2022-05-13 15:18:38 +0100 | [diff] [blame^] | 91 | proj_incs = dir_include_paths("include/") |
Harrison Mutai | 8c3afa3 | 2022-02-04 09:33:24 +0000 | [diff] [blame] | 92 | indices = [] |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 93 | |
| 94 | for inc in inc_list: |
Harrison Mutai | ec878ce | 2022-05-13 15:18:38 +0100 | [diff] [blame^] | 95 | inc_path = inc[1:-1].replace("..", Path(path).parents[1].as_posix()) |
| 96 | inc_group_index = int(inc_path not in libc_incs) |
| 97 | |
| 98 | if inc_group_index: |
| 99 | if inc_path in proj_incs: |
| 100 | inc_group_index = 1 |
| 101 | elif inc_path in plat_incs: |
| 102 | inc_group_index = 2 |
Harrison Mutai | 8c3afa3 | 2022-02-04 09:33:24 +0000 | [diff] [blame] | 103 | |
| 104 | incs[inc_group_index].append(inc_path) |
| 105 | indices.append((inc_group_index, inc)) |
| 106 | |
Harrison Mutai | ec878ce | 2022-05-13 15:18:38 +0100 | [diff] [blame^] | 107 | index_sorted_paths = sorted(indices, key=lambda x: (x[0], x[1][1:-1])) |
Harrison Mutai | 8c3afa3 | 2022-02-04 09:33:24 +0000 | [diff] [blame] | 108 | if indices != index_sorted_paths: |
Harrison Mutai | ec878ce | 2022-05-13 15:18:38 +0100 | [diff] [blame^] | 109 | error_msgs.append("Include ordering error, order should be:") |
| 110 | last_group = index_sorted_paths[0][0] |
| 111 | for inc in index_sorted_paths: |
Harrison Mutai | 8c3afa3 | 2022-02-04 09:33:24 +0000 | [diff] [blame] | 112 | # Right angle brackets are a special entity in html, convert the |
| 113 | # name to an html friendly format. |
Harrison Mutai | ec878ce | 2022-05-13 15:18:38 +0100 | [diff] [blame^] | 114 | path_ = inc[1] if "<" not in inc[1] else f"<{inc[1][1:-1]}>" |
Harrison Mutai | 8c3afa3 | 2022-02-04 09:33:24 +0000 | [diff] [blame] | 115 | |
Harrison Mutai | ec878ce | 2022-05-13 15:18:38 +0100 | [diff] [blame^] | 116 | if last_group != inc[0]: |
| 117 | error_msgs.append("") |
| 118 | last_group = inc[0] |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 119 | |
Harrison Mutai | ec878ce | 2022-05-13 15:18:38 +0100 | [diff] [blame^] | 120 | error_msgs.append(f"\t#include {path_}") |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 121 | |
| 122 | # Output error messages. |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 123 | if error_msgs: |
Harrison Mutai | 8c3afa3 | 2022-02-04 09:33:24 +0000 | [diff] [blame] | 124 | print(f"\n{commit_hash}:{path}:") |
| 125 | print(*error_msgs, sep="\n") |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 126 | return False |
| 127 | else: |
| 128 | return True |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 129 | |
| 130 | |
| 131 | def file_is_correct(path): |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 132 | """Checks whether the order of includes in the file specified in the path |
| 133 | is correct or not.""" |
| 134 | inc_list = file_include_list(path) |
| 135 | return inc_list is not None and inc_order_is_correct(inc_list, path) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 136 | |
| 137 | |
| 138 | def directory_tree_is_correct(): |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 139 | """Checks all tracked files in the current git repository, except the ones |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 140 | explicitly ignored by this script. |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 141 | Returns True if all files are correct.""" |
| 142 | (rc, stdout, stderr) = utils.shell_command(["git", "ls-files"]) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 143 | if rc != 0: |
| 144 | return False |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 145 | all_files_correct = True |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 146 | for f in stdout.splitlines(): |
| 147 | if not utils.file_is_ignored( |
| 148 | f, VALID_FILE_EXTENSIONS, IGNORED_FILES, IGNORED_FOLDERS |
| 149 | ): |
| 150 | all_files_correct &= file_is_correct(f) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 151 | return all_files_correct |
| 152 | |
| 153 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 154 | def group_lines(patchlines, starting_with): |
| 155 | """Generator of (name, lines) almost the same as itertools.groupby |
| 156 | |
| 157 | This function's control flow is non-trivial. In particular, the clearing |
| 158 | of the lines variable, marked with [1], is intentional and must come |
| 159 | after the yield. That's because we must yield the (name, lines) tuple |
| 160 | after we have found the name of the next section but before we assign the |
| 161 | name and start collecting lines. Further, [2] is required to yeild the |
| 162 | last block as there will not be a block start delimeter at the end of |
| 163 | the stream. |
| 164 | """ |
| 165 | lines = [] |
| 166 | name = None |
| 167 | for line in patchlines: |
| 168 | if line.startswith(starting_with): |
| 169 | if name: |
| 170 | yield name, lines |
| 171 | name = line[len(starting_with) :] |
| 172 | lines = [] # [1] |
| 173 | else: |
| 174 | lines.append(line) |
| 175 | yield name, lines # [2] |
| 176 | |
| 177 | |
| 178 | def group_files(commitlines): |
| 179 | """Generator of (commit hash, lines) almost the same as itertools.groupby""" |
| 180 | return group_lines(commitlines, "+++ b/") |
| 181 | |
| 182 | |
| 183 | def group_commits(commitlines): |
| 184 | """Generator of (file name, lines) almost the same as itertools.groupby""" |
| 185 | return group_lines(commitlines, "commit ") |
| 186 | |
| 187 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 188 | def patch_is_correct(base_commit, end_commit): |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 189 | """Get the output of a git diff and analyse each modified file.""" |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 190 | |
| 191 | # Get patches of the affected commits with one line of context. |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 192 | gitlog = subprocess.run( |
| 193 | [ |
| 194 | "git", |
| 195 | "log", |
| 196 | "--unified=1", |
| 197 | "--pretty=commit %h", |
| 198 | base_commit + ".." + end_commit, |
| 199 | ], |
| 200 | stdout=subprocess.PIPE, |
| 201 | ) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 202 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 203 | if gitlog.returncode != 0: |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 204 | return False |
| 205 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 206 | gitlines = gitlog.stdout.decode("utf-8").splitlines() |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 207 | all_files_correct = True |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 208 | for commit, comlines in group_commits(gitlines): |
| 209 | for path, lines in group_files(comlines): |
| 210 | all_files_correct &= inc_order_is_correct( |
| 211 | include_paths(lines, diff_mode=True), path, commit |
| 212 | ) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 213 | return all_files_correct |
| 214 | |
| 215 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 216 | def parse_cmd_line(argv, prog_name): |
| 217 | parser = argparse.ArgumentParser( |
| 218 | prog=prog_name, |
| 219 | formatter_class=argparse.RawTextHelpFormatter, |
| 220 | description="Check alphabetical order of #includes", |
| 221 | epilog=""" |
| 222 | For each source file in the tree, checks that #include's C preprocessor |
| 223 | directives are ordered alphabetically (as mandated by the Trusted |
| 224 | Firmware coding style). System header includes must come before user |
| 225 | header includes. |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 226 | """, |
| 227 | ) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 228 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 229 | parser.add_argument( |
| 230 | "--tree", |
| 231 | "-t", |
| 232 | help="Path to the source tree to check (default: %(default)s)", |
| 233 | default=os.curdir, |
| 234 | ) |
| 235 | parser.add_argument( |
| 236 | "--patch", |
| 237 | "-p", |
| 238 | help=""" |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 239 | Patch mode. |
| 240 | Instead of checking all files in the source tree, the script will consider |
| 241 | only files that are modified by the latest patch(es).""", |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 242 | action="store_true", |
| 243 | ) |
| 244 | parser.add_argument( |
| 245 | "--from-ref", |
| 246 | help="Base commit in patch mode (default: %(default)s)", |
| 247 | default="master", |
| 248 | ) |
| 249 | parser.add_argument( |
| 250 | "--to-ref", |
| 251 | help="Final commit in patch mode (default: %(default)s)", |
| 252 | default="HEAD", |
| 253 | ) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 254 | args = parser.parse_args(argv) |
| 255 | return args |
| 256 | |
| 257 | |
| 258 | if __name__ == "__main__": |
| 259 | args = parse_cmd_line(sys.argv[1:], sys.argv[0]) |
| 260 | |
| 261 | os.chdir(args.tree) |
| 262 | |
| 263 | if args.patch: |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 264 | print( |
| 265 | "Checking files modified between patches " |
| 266 | + args.from_ref |
| 267 | + " and " |
| 268 | + args.to_ref |
| 269 | + "..." |
| 270 | ) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 271 | if not patch_is_correct(args.from_ref, args.to_ref): |
| 272 | sys.exit(1) |
| 273 | else: |
| 274 | print("Checking all files in directory '%s'..." % os.path.abspath(args.tree)) |
| 275 | if not directory_tree_is_correct(): |
| 276 | sys.exit(1) |
| 277 | |
| 278 | # All source code files are correct. |
| 279 | sys.exit(0) |