Punit Vara | 2f28bf5 | 2017-06-08 14:40:48 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Sonia Leon Bautista | 616e962 | 2016-06-14 17:55:24 -0500 | [diff] [blame] | 2 | |
Ulf Magnusson | ac7f223 | 2018-05-02 04:52:53 +0200 | [diff] [blame] | 3 | # NOTE: This script has not been updated for Kconfiglib 2 and will not run |
| 4 | |
Sonia Leon Bautista | 616e962 | 2016-06-14 17:55:24 -0500 | [diff] [blame] | 5 | """Zephyr Check kconfigs Definitions |
| 6 | |
| 7 | Check all CONFIG_* in the code are defined into a Kconfig file |
| 8 | |
| 9 | usage: checkconfig.py [-h] [-k KCONFIG] [-c] [-e EXCLUDE] |
| 10 | |
| 11 | """ |
| 12 | |
| 13 | import sys |
| 14 | import os |
| 15 | import re |
| 16 | import argparse |
| 17 | from argparse import SUPPRESS |
| 18 | |
| 19 | help_text=""" |
| 20 | Checkconfig script scans all '*.c', '*.h' and '*.S' looking for all kconfig |
| 21 | items used in the Zephyr Code and validates if they are defined in any |
| 22 | '*.kconfig*' file. |
| 23 | |
| 24 | To run script use command: |
| 25 | |
| 26 | checkconfig.py [-h|--help] [-c|--complete-log] [-k|--kconfig <kconfig>] |
| 27 | [-e|--exclude <dir_to_exclude>] |
| 28 | |
| 29 | It will send to output: |
| 30 | -config name |
| 31 | -location where config is used |
| 32 | -full line where the config is being used |
| 33 | -location were config is being defined (there could be multiple declarations) |
| 34 | |
| 35 | [-c|--complete-log] is an optional parameter. |
| 36 | If it is not given it will print only the kconfigs not defined |
| 37 | If given, it will print all the kconfigs found |
| 38 | |
| 39 | [-s|--subdir <subdir>] is an optional parameter. |
| 40 | When a directory is given it will start scan from that specific directorywith . |
| 41 | By default it scans from zephyr base tree |
| 42 | |
| 43 | [-e|--exclude <subdir_to_exclude>] is an optional parameter. |
| 44 | When a subdirectory is given it will be appended to defaut exclude dirs list. |
| 45 | Default exclude dirs are: "doc", "sanity-out" and "outdir" |
| 46 | """ |
| 47 | |
| 48 | #the genrest dir contains kconfiglib.py |
| 49 | zephyrbase = os.environ.get('ZEPHYR_BASE') |
| 50 | |
| 51 | if zephyrbase == None: |
| 52 | print ("env. variable ZEPHYR_BASE is not set, " |
| 53 | "ensure you have source zephyr-env.sh") |
| 54 | exit(1) |
| 55 | elif not os.path.exists(zephyrbase): |
| 56 | print ("env. variable ZEPHYR_BASE \""+ zephyrbase + |
| 57 | "\" does not exist as a valid zephyr base directory") |
| 58 | exit(1) |
| 59 | |
| 60 | os.environ['srctree'] = os.path.join(zephyrbase + os.path.sep) |
| 61 | sys.path.append(os.path.join(zephyrbase,'doc','scripts','genrest')) |
| 62 | |
| 63 | import kconfiglib |
| 64 | |
| 65 | def separate_location_lines(dirs): |
| 66 | for dir in dirs: |
Punit Vara | 2f28bf5 | 2017-06-08 14:40:48 +0530 | [diff] [blame] | 67 | print(" ", dir) |
Sonia Leon Bautista | 616e962 | 2016-06-14 17:55:24 -0500 | [diff] [blame] | 68 | |
| 69 | def search_kconfig_items(items, name, completelog): |
| 70 | findConfig = False |
| 71 | for item in items: |
| 72 | if item.is_symbol(): |
| 73 | if (item.get_name() == name): |
| 74 | if (completelog == True): |
| 75 | separate_location_lines(item.get_def_locations()) |
| 76 | findConfig = True |
| 77 | break |
| 78 | |
| 79 | elif item.is_menu(): |
| 80 | if search_kconfig_items(item.get_items(), name, completelog): |
| 81 | return True |
| 82 | |
| 83 | elif item.is_choice(): |
| 84 | if search_kconfig_items(item.get_items(), name, completelog): |
| 85 | return True |
| 86 | |
| 87 | elif item.is_comment(): |
| 88 | if (item.get_text() == name): |
Punit Vara | 2f28bf5 | 2017-06-08 14:40:48 +0530 | [diff] [blame] | 89 | print(completelog) |
Sonia Leon Bautista | 616e962 | 2016-06-14 17:55:24 -0500 | [diff] [blame] | 90 | if (completelog == True): |
| 91 | separate_location_lines(item.get_location()) |
| 92 | findConfig = True |
| 93 | break |
| 94 | return findConfig |
| 95 | |
| 96 | def search_config_in_file(tree, items, completelog, exclude): |
| 97 | configs = 0 |
| 98 | notdefConfig = 0 |
| 99 | for dirName, subdirs, files in os.walk(tree, topdown=True): |
| 100 | subdirs[:] = [d for d in subdirs if d not in exclude] |
| 101 | for fname in files: |
| 102 | if (fname.endswith(".c") or |
| 103 | fname.endswith(".h") or |
| 104 | fname.endswith(".S")): |
Punit Vara | 2f28bf5 | 2017-06-08 14:40:48 +0530 | [diff] [blame] | 105 | with open(os.path.join(dirName, fname), "r", encoding="utf-8", errors="ignore") as f: |
Sonia Leon Bautista | 616e962 | 2016-06-14 17:55:24 -0500 | [diff] [blame] | 106 | searchConf = f.readlines() |
| 107 | for i, line in enumerate(searchConf): |
| 108 | if re.search('(^|[\s|(])CONFIG_([a-zA-Z0-9_]+)', line) : |
| 109 | configName = re.search('(^|[\s|(])' |
| 110 | +'CONFIG_([a-zA-Z0-9_]+)', line) |
| 111 | configs = configs + 1 |
| 112 | if (completelog == True): |
Punit Vara | 2f28bf5 | 2017-06-08 14:40:48 +0530 | [diff] [blame] | 113 | print('\n' + configName.group(2) + ' at ' |
Sonia Leon Bautista | 616e962 | 2016-06-14 17:55:24 -0500 | [diff] [blame] | 114 | + os.path.join(dirName, fname)) |
Punit Vara | 2f28bf5 | 2017-06-08 14:40:48 +0530 | [diff] [blame] | 115 | print('line: ' + line.rstrip()) |
Sonia Leon Bautista | 616e962 | 2016-06-14 17:55:24 -0500 | [diff] [blame] | 116 | find = search_kconfig_items(items, configName.group(2), |
| 117 | completelog) |
| 118 | if (find == False): |
Punit Vara | 2f28bf5 | 2017-06-08 14:40:48 +0530 | [diff] [blame] | 119 | print('\n' + configName.group(2) + ' at ' |
Sonia Leon Bautista | 616e962 | 2016-06-14 17:55:24 -0500 | [diff] [blame] | 120 | + os.path.join(dirName, fname) |
| 121 | + ' IS NOT DEFINED') |
Punit Vara | 2f28bf5 | 2017-06-08 14:40:48 +0530 | [diff] [blame] | 122 | print('line: ' + line.rstrip()) |
Sonia Leon Bautista | 616e962 | 2016-06-14 17:55:24 -0500 | [diff] [blame] | 123 | notdefConfig = notdefConfig + 1 |
| 124 | if (completelog == True): |
Punit Vara | 2f28bf5 | 2017-06-08 14:40:48 +0530 | [diff] [blame] | 125 | print("\n{} Kconfigs evaluated".format(configs)) |
| 126 | print("{} Kconfigs not defined".format(notdefConfig)) |
Sonia Leon Bautista | 616e962 | 2016-06-14 17:55:24 -0500 | [diff] [blame] | 127 | |
| 128 | parser = argparse.ArgumentParser(description = help_text, |
| 129 | usage = SUPPRESS, |
| 130 | formatter_class = argparse.RawTextHelpFormatter) |
| 131 | parser.add_argument('-s', '--subdir', action='store', dest='subdir', |
| 132 | default="", |
| 133 | help='sub directory to be scanned') |
| 134 | parser.add_argument('-c', '--complete-log', action='store_true', |
| 135 | dest='completelog', default=False, |
| 136 | help='Prints all the kconfigs found') |
| 137 | parser.add_argument('-e', '--exclude', action='append', dest='exclude', |
| 138 | default=["doc", "sanity-out", "outdir"], |
| 139 | help='Dirs to be excluded for verification') |
| 140 | |
| 141 | args= parser.parse_args() |
| 142 | if args.completelog: |
Punit Vara | 2f28bf5 | 2017-06-08 14:40:48 +0530 | [diff] [blame] | 143 | print('sub dir = ', os.path.join(zephyrbase + args.subdir)) |
| 144 | print('complete-log = ', args.completelog) |
| 145 | print('exclude dirs = ', args.exclude) |
Sonia Leon Bautista | 616e962 | 2016-06-14 17:55:24 -0500 | [diff] [blame] | 146 | |
| 147 | conf = kconfiglib.Config(os.path.join(zephyrbase,'Kconfig')) |
| 148 | search_config_in_file(os.path.join(zephyrbase + os.path.sep + args.subdir), |
| 149 | conf.get_top_level_items(), args.completelog, args.exclude) |