diff options
author | Aseda Aboagye <aaboagye@google.com> | 2015-06-24 12:13:40 -0700 |
---|---|---|
committer | ChromeOS Commit Bot <chromeos-commit-bot@chromium.org> | 2015-06-27 19:29:29 +0000 |
commit | 0ec956ea9d585f350a47db162e275037008d75cc (patch) | |
tree | 821f9c66fa95c5cf6079c096042b0d7cbd0efb81 | |
parent | ed3f7121014bd823c3c4ace762b71be6c9ff7838 (diff) | |
download | chrome-ec-0ec956ea9d585f350a47db162e275037008d75cc.tar.gz |
util: Add config option checker.
There are several cases in the EC code base where a CONFIG_* option is
used somewhere, but not defined within the include/config.h file. This
script aims to fix that. Eventually, it will become a presubmit hook to
actively prevent future offenses.
BUG=chrome-os-partner:26304
BRANCH=none
TEST=cros lint --debug util/config_option_check.py
TEST=Ran script and found offending config options.
TEST=make -j buildall tests
Change-Id: I999d32ebacc636b3fff9e857f3cc46feee475e80
Signed-off-by: Aseda Aboagye <aaboagye@google.com>
Reviewed-on: https://chromium-review.googlesource.com/281626
Tested-by: Aseda Aboagye <aaboagye@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Commit-Queue: Aseda Aboagye <aaboagye@chromium.org>
Trybot-Ready: Aseda Aboagye <aaboagye@chromium.org>
-rwxr-xr-x | util/config_option_check.py | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/util/config_option_check.py b/util/config_option_check.py new file mode 100755 index 0000000000..149f256fb6 --- /dev/null +++ b/util/config_option_check.py @@ -0,0 +1,103 @@ +#!/usr/bin/python2 +# Copyright 2015 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. +"""Configuration Option Checker. + +Script to ensure that all configuration options for the Chrome EC are defined +in config.h. +""" +from __future__ import print_function +import re +import os +import argparse + +def find_files_to_check(args): + """Returns a list of files to check.""" + file_list = [] + if args.all_files: + cwd = os.getcwd() + for (dirpath, dirnames, filenames) in os.walk(cwd, topdown=True): + # Ignore the build and private directories (taken from .gitignore) + if 'build' in dirnames: + dirnames.remove('build') + if 'private' in dirnames: + dirnames.remove('private') + for f in filenames: + # Only consider C source and assembler files. + if f.endswith(('.c', '.h', '.inc', '.S')): + file_list.append(os.path.join(dirpath, f)) + else: + # Form list from presubmit environment variable. + file_list = os.environ['PRESUBMIT_FILES'].split() + return file_list + +def obtain_current_config_options(): + """Obtains current config options from include/config.h""" + config_options = [] + config_option_re = re.compile(r'\s+(CONFIG_[a-zA-Z0-9_]*)\s*') + with open('include/config.h', 'r') as config_file: + for line in config_file: + match = re.search(config_option_re, line) + if match: + if match.group(1) not in config_options: + config_options.append(match.group(1)) + return config_options + +def print_missing_config_options(file_list, config_options): + """Searches through all files in file_list for missing config options.""" + missing_config_option = False + print_banner = True + # Determine longest CONFIG_* length to be used for formatting. + max_option_length = max(len(option) for option in config_options) + config_option_re = re.compile(r'\s+(CONFIG_[a-zA-Z0-9_]*)\s*') + for f in file_list: + with open(f, 'r') as cur_file: + line_num = 0 + for line in cur_file: + line_num += 1 + match = re.search(config_option_re, line) + if match: + if match.group(1) not in config_options: + missing_config_option = True + # Print the banner once. + if print_banner: + print('Please add all new config options to include/config.h' \ + ' along with a description of the option.\n\n' \ + 'The following config options were found to be missing ' \ + 'from include/config.h.\n') + print_banner = False + # Print the misssing config option. + print('> %-*s %s:%s' % (max_option_length, match.group(1), f, + line_num)) + return missing_config_option + +def main(): + """Searches through specified source files for missing config options. + + Checks through specified C source and assembler file (not in the build/ and + private/ directories) for CONFIG_* options. Then checks to make sure that + all CONFIG_* options are defined in include/config.h. Finally, reports any + missing config options. By default, it will check just the files in the CL. + To check all files in EC code base, run with the flag --all_files. + """ + # Create argument options to specify checking either all the files in the EC + # code base or just the files in the CL. + parser = argparse.ArgumentParser(description='configuration option checker.') + parser.add_argument('--all_files', help='check all files in EC code base', + action='store_true') + args = parser.parse_args() + + # Create list of files to search. + file_list = find_files_to_check(args) + # Obtain config options from include/config.h. + config_options = obtain_current_config_options() + # Find any missing config options from file list and print them. + missing_opts = print_missing_config_options(file_list, config_options) + + if missing_opts: + print('\nIt may also be possible that you have a typo.') + os.sys.exit(1) + +if __name__ == '__main__': + main() |