blob: c669ca6b87704f1916c9b379b2e0fcbbe469c470 [file] [log] [blame]
Punit Vara2f28bf52017-06-08 14:40:48 +05301#!/usr/bin/env python3
Sonia Leon Bautista616e9622016-06-14 17:55:24 -05002
Ulf Magnussonac7f2232018-05-02 04:52:53 +02003# NOTE: This script has not been updated for Kconfiglib 2 and will not run
4
Sonia Leon Bautista616e9622016-06-14 17:55:24 -05005"""Zephyr Check kconfigs Definitions
6
7Check all CONFIG_* in the code are defined into a Kconfig file
8
9usage: checkconfig.py [-h] [-k KCONFIG] [-c] [-e EXCLUDE]
10
11"""
12
13import sys
14import os
15import re
16import argparse
17from argparse import SUPPRESS
18
19help_text="""
20Checkconfig script scans all '*.c', '*.h' and '*.S' looking for all kconfig
21items used in the Zephyr Code and validates if they are defined in any
22'*.kconfig*' file.
23
24To run script use command:
25
26checkconfig.py [-h|--help] [-c|--complete-log] [-k|--kconfig <kconfig>]
27 [-e|--exclude <dir_to_exclude>]
28
29It 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
49zephyrbase = os.environ.get('ZEPHYR_BASE')
50
51if zephyrbase == None:
52 print ("env. variable ZEPHYR_BASE is not set, "
53 "ensure you have source zephyr-env.sh")
54 exit(1)
55elif 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
60os.environ['srctree'] = os.path.join(zephyrbase + os.path.sep)
61sys.path.append(os.path.join(zephyrbase,'doc','scripts','genrest'))
62
63import kconfiglib
64
65def separate_location_lines(dirs):
66 for dir in dirs:
Punit Vara2f28bf52017-06-08 14:40:48 +053067 print(" ", dir)
Sonia Leon Bautista616e9622016-06-14 17:55:24 -050068
69def 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 Vara2f28bf52017-06-08 14:40:48 +053089 print(completelog)
Sonia Leon Bautista616e9622016-06-14 17:55:24 -050090 if (completelog == True):
91 separate_location_lines(item.get_location())
92 findConfig = True
93 break
94 return findConfig
95
96def 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 Vara2f28bf52017-06-08 14:40:48 +0530105 with open(os.path.join(dirName, fname), "r", encoding="utf-8", errors="ignore") as f:
Sonia Leon Bautista616e9622016-06-14 17:55:24 -0500106 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 Vara2f28bf52017-06-08 14:40:48 +0530113 print('\n' + configName.group(2) + ' at '
Sonia Leon Bautista616e9622016-06-14 17:55:24 -0500114 + os.path.join(dirName, fname))
Punit Vara2f28bf52017-06-08 14:40:48 +0530115 print('line: ' + line.rstrip())
Sonia Leon Bautista616e9622016-06-14 17:55:24 -0500116 find = search_kconfig_items(items, configName.group(2),
117 completelog)
118 if (find == False):
Punit Vara2f28bf52017-06-08 14:40:48 +0530119 print('\n' + configName.group(2) + ' at '
Sonia Leon Bautista616e9622016-06-14 17:55:24 -0500120 + os.path.join(dirName, fname)
121 + ' IS NOT DEFINED')
Punit Vara2f28bf52017-06-08 14:40:48 +0530122 print('line: ' + line.rstrip())
Sonia Leon Bautista616e9622016-06-14 17:55:24 -0500123 notdefConfig = notdefConfig + 1
124 if (completelog == True):
Punit Vara2f28bf52017-06-08 14:40:48 +0530125 print("\n{} Kconfigs evaluated".format(configs))
126 print("{} Kconfigs not defined".format(notdefConfig))
Sonia Leon Bautista616e9622016-06-14 17:55:24 -0500127
128parser = argparse.ArgumentParser(description = help_text,
129 usage = SUPPRESS,
130 formatter_class = argparse.RawTextHelpFormatter)
131parser.add_argument('-s', '--subdir', action='store', dest='subdir',
132 default="",
133 help='sub directory to be scanned')
134parser.add_argument('-c', '--complete-log', action='store_true',
135 dest='completelog', default=False,
136 help='Prints all the kconfigs found')
137parser.add_argument('-e', '--exclude', action='append', dest='exclude',
138 default=["doc", "sanity-out", "outdir"],
139 help='Dirs to be excluded for verification')
140
141args= parser.parse_args()
142if args.completelog:
Punit Vara2f28bf52017-06-08 14:40:48 +0530143 print('sub dir = ', os.path.join(zephyrbase + args.subdir))
144 print('complete-log = ', args.completelog)
145 print('exclude dirs = ', args.exclude)
Sonia Leon Bautista616e9622016-06-14 17:55:24 -0500146
147conf = kconfiglib.Config(os.path.join(zephyrbase,'Kconfig'))
148search_config_in_file(os.path.join(zephyrbase + os.path.sep + args.subdir),
149 conf.get_top_level_items(), args.completelog, args.exclude)