Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | # |
| 4 | # Copyright (C) Google LLC, 2018 |
| 5 | # |
| 6 | # Author: Tom Roeder <tmroeder@google.com> |
| 7 | # |
| 8 | """A tool for generating compile_commands.json in the Linux kernel.""" |
| 9 | |
| 10 | import argparse |
| 11 | import json |
| 12 | import logging |
| 13 | import os |
| 14 | import re |
| 15 | |
| 16 | _DEFAULT_OUTPUT = 'compile_commands.json' |
| 17 | _DEFAULT_LOG_LEVEL = 'WARNING' |
| 18 | |
| 19 | _FILENAME_PATTERN = r'^\..*\.cmd$' |
| 20 | _LINE_PATTERN = r'^cmd_[^ ]*\.o := (.* )([^ ]*\.c)$' |
| 21 | _VALID_LOG_LEVELS = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] |
| 22 | |
| 23 | # A kernel build generally has over 2000 entries in its compile_commands.json |
Masahiro Yamada | cb36955 | 2019-07-27 12:01:10 +0900 | [diff] [blame] | 24 | # database. If this code finds 300 or fewer, then warn the user that they might |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 25 | # not have all the .cmd files, and they might need to compile the kernel. |
Masahiro Yamada | cb36955 | 2019-07-27 12:01:10 +0900 | [diff] [blame] | 26 | _LOW_COUNT_THRESHOLD = 300 |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 27 | |
| 28 | |
| 29 | def parse_arguments(): |
| 30 | """Sets up and parses command-line arguments. |
| 31 | |
| 32 | Returns: |
| 33 | log_level: A logging level to filter log output. |
Masahiro Yamada | 0a7d376 | 2020-08-22 23:56:12 +0900 | [diff] [blame] | 34 | directory: The work directory where the objects were built. |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 35 | output: Where to write the compile-commands JSON file. |
Masahiro Yamada | fc2cb22 | 2020-08-22 23:56:14 +0900 | [diff] [blame^] | 36 | paths: The list of directories to handle to find .cmd files. |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 37 | """ |
| 38 | usage = 'Creates a compile_commands.json database from kernel .cmd files' |
| 39 | parser = argparse.ArgumentParser(description=usage) |
| 40 | |
Masahiro Yamada | 0a7d376 | 2020-08-22 23:56:12 +0900 | [diff] [blame] | 41 | directory_help = ('specify the output directory used for the kernel build ' |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 42 | '(defaults to the working directory)') |
Masahiro Yamada | 6fca36f | 2020-08-22 23:56:13 +0900 | [diff] [blame] | 43 | parser.add_argument('-d', '--directory', type=str, default='.', |
| 44 | help=directory_help) |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 45 | |
Masahiro Yamada | 6fca36f | 2020-08-22 23:56:13 +0900 | [diff] [blame] | 46 | output_help = ('path to the output command database (defaults to ' + |
| 47 | _DEFAULT_OUTPUT + ')') |
| 48 | parser.add_argument('-o', '--output', type=str, default=_DEFAULT_OUTPUT, |
| 49 | help=output_help) |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 50 | |
Masahiro Yamada | ea6cedc | 2020-08-22 23:56:10 +0900 | [diff] [blame] | 51 | log_level_help = ('the level of log messages to produce (defaults to ' + |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 52 | _DEFAULT_LOG_LEVEL + ')') |
Masahiro Yamada | ea6cedc | 2020-08-22 23:56:10 +0900 | [diff] [blame] | 53 | parser.add_argument('--log_level', choices=_VALID_LOG_LEVELS, |
| 54 | default=_DEFAULT_LOG_LEVEL, help=log_level_help) |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 55 | |
| 56 | args = parser.parse_args() |
| 57 | |
Masahiro Yamada | 6fca36f | 2020-08-22 23:56:13 +0900 | [diff] [blame] | 58 | return (args.log_level, |
| 59 | os.path.abspath(args.directory), |
Masahiro Yamada | fc2cb22 | 2020-08-22 23:56:14 +0900 | [diff] [blame^] | 60 | args.output, |
| 61 | [args.directory]) |
| 62 | |
| 63 | |
| 64 | def cmdfiles_in_dir(directory): |
| 65 | """Generate the iterator of .cmd files found under the directory. |
| 66 | |
| 67 | Walk under the given directory, and yield every .cmd file found. |
| 68 | |
| 69 | Args: |
| 70 | directory: The directory to search for .cmd files. |
| 71 | |
| 72 | Yields: |
| 73 | The path to a .cmd file. |
| 74 | """ |
| 75 | |
| 76 | filename_matcher = re.compile(_FILENAME_PATTERN) |
| 77 | |
| 78 | for dirpath, _, filenames in os.walk(directory): |
| 79 | for filename in filenames: |
| 80 | if filename_matcher.match(filename): |
| 81 | yield os.path.join(dirpath, filename) |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 82 | |
| 83 | |
Masahiro Yamada | 6ca4c6d | 2020-08-22 23:56:11 +0900 | [diff] [blame] | 84 | def process_line(root_directory, command_prefix, file_path): |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 85 | """Extracts information from a .cmd line and creates an entry from it. |
| 86 | |
| 87 | Args: |
| 88 | root_directory: The directory that was searched for .cmd files. Usually |
| 89 | used directly in the "directory" entry in compile_commands.json. |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 90 | command_prefix: The extracted command line, up to the last element. |
Masahiro Yamada | 6ca4c6d | 2020-08-22 23:56:11 +0900 | [diff] [blame] | 91 | file_path: The .c file from the end of the extracted command. |
| 92 | Usually relative to root_directory, but sometimes absolute. |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 93 | |
| 94 | Returns: |
| 95 | An entry to append to compile_commands. |
| 96 | |
| 97 | Raises: |
Masahiro Yamada | 6ca4c6d | 2020-08-22 23:56:11 +0900 | [diff] [blame] | 98 | ValueError: Could not find the extracted file based on file_path and |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 99 | root_directory or file_directory. |
| 100 | """ |
| 101 | # The .cmd files are intended to be included directly by Make, so they |
| 102 | # escape the pound sign '#', either as '\#' or '$(pound)' (depending on the |
| 103 | # kernel version). The compile_commands.json file is not interepreted |
| 104 | # by Make, so this code replaces the escaped version with '#'. |
| 105 | prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#') |
| 106 | |
Masahiro Yamada | 6ca4c6d | 2020-08-22 23:56:11 +0900 | [diff] [blame] | 107 | # Use os.path.abspath() to normalize the path resolving '.' and '..' . |
| 108 | abs_path = os.path.abspath(os.path.join(root_directory, file_path)) |
| 109 | if not os.path.exists(abs_path): |
| 110 | raise ValueError('File %s not found' % abs_path) |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 111 | return { |
Masahiro Yamada | 6ca4c6d | 2020-08-22 23:56:11 +0900 | [diff] [blame] | 112 | 'directory': root_directory, |
| 113 | 'file': abs_path, |
| 114 | 'command': prefix + file_path, |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
| 118 | def main(): |
| 119 | """Walks through the directory and finds and parses .cmd files.""" |
Masahiro Yamada | fc2cb22 | 2020-08-22 23:56:14 +0900 | [diff] [blame^] | 120 | log_level, directory, output, paths = parse_arguments() |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 121 | |
| 122 | level = getattr(logging, log_level) |
| 123 | logging.basicConfig(format='%(levelname)s: %(message)s', level=level) |
| 124 | |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 125 | line_matcher = re.compile(_LINE_PATTERN) |
| 126 | |
| 127 | compile_commands = [] |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 128 | |
Masahiro Yamada | fc2cb22 | 2020-08-22 23:56:14 +0900 | [diff] [blame^] | 129 | for path in paths: |
| 130 | cmdfiles = cmdfiles_in_dir(path) |
| 131 | |
| 132 | for cmdfile in cmdfiles: |
| 133 | with open(cmdfile, 'rt') as f: |
Masahiro Yamada | 8a685db | 2020-08-22 23:56:09 +0900 | [diff] [blame] | 134 | result = line_matcher.match(f.readline()) |
| 135 | if result: |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 136 | try: |
Masahiro Yamada | fc2cb22 | 2020-08-22 23:56:14 +0900 | [diff] [blame^] | 137 | entry = process_line(directory, result.group(1), |
| 138 | result.group(2)) |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 139 | compile_commands.append(entry) |
| 140 | except ValueError as err: |
| 141 | logging.info('Could not add line from %s: %s', |
Masahiro Yamada | fc2cb22 | 2020-08-22 23:56:14 +0900 | [diff] [blame^] | 142 | cmdfile, err) |
Tom Roeder | b302046 | 2018-12-18 14:49:07 -0800 | [diff] [blame] | 143 | |
| 144 | with open(output, 'wt') as f: |
| 145 | json.dump(compile_commands, f, indent=2, sort_keys=True) |
| 146 | |
| 147 | count = len(compile_commands) |
| 148 | if count < _LOW_COUNT_THRESHOLD: |
| 149 | logging.warning( |
| 150 | 'Found %s entries. Have you compiled the kernel?', count) |
| 151 | |
| 152 | |
| 153 | if __name__ == '__main__': |
| 154 | main() |