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