From: Jean-Jacques Hiblot Date: Wed, 12 Dec 2018 16:17:09 +0100 Subject: [U-Boot] policy regarding unused code In-Reply-To: References: <7c188c93-4713-7ebd-be7d-d87955f4e756@ti.com> <5f248a38-a5bc-cbb0-28f5-5a595c49a64b@ti.com> <20181211163552.GT8702@bill-the-cat> Message-ID: <47fc6f39-10ae-5f9f-8a3a-6bcdc24f8a9f@ti.com> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit To: u-boot@lists.denx.de >> I suspect this splits into three categories: >> - Dead symbols and code to drop. >> - Typos/thinkos >> - Mistake in your grep? I see CONFIG_VIRTIO_SANDBOX is used today for >> example. > what did you run to get that list of CONFIG_* symbols? years ago, i This is small python script that I wrote (see code below). What it does (roughly): - scan all Makefile and*.mk file to create a set of variables starting with CONFIG_ - create the set of all used CONFIG_* variables by scanning moveconfig.db. This can be created with tools/moveconfig.py -b. (you need patch 'tools: moveconfig: Add an option to build a fuller database of options'). - subtract set#2 from set#1 JJ From: Jean-Jacques Hiblot Date: Wed, 12 Dec 2018 16:15:21 +0100 Subject: [PATCH] simple tool to find unused options Signed-off-by: Jean-Jacques Hiblot ---  tools/find_unused_config_options.py | 78 +++++++++++++++++++++++++++++++++++++  1 file changed, 78 insertions(+)  create mode 100755 tools/find_unused_config_options.py diff --git a/tools/find_unused_config_options.py b/tools/find_unused_config_options.py new file mode 100755 index 0000000..85c859a --- /dev/null +++ b/tools/find_unused_config_options.py @@ -0,0 +1,78 @@ +#! /usr/bin/env python + +import re +import os + +def get_var_from_moveconfig_db(): +       options = set() +       search = re.compile("\s*CONFIG_([^=]*)=.*") +       with open("moveconfig.db", "r") as f: +               for l in f.readlines(): +                       m = search.match(l) +                       if m: +                               options.add(m.group(1)) +       return options + +def get_makefiles(start): +       Makefiles = [] +       for (dirpath, dirnames, filenames) in os.walk(start): +                Makefiles.extend([ os.path.join(dirpath,f) for f in filenames if f == "Makefile" or f.endswith(".mk")]) +       return Makefiles + +def get_C_files(start): +       c_files = [] +       for (dirpath, dirnames, filenames) in os.walk(start): +                Makefiles.extend([ os.path.join(dirpath,f) for f in filenames if f.endswith(".c") or f.endswith(".h")]) +       return c_files + +def get_CONFIG_var_from_Makefile(makefile): +       simple_options = set() +       spl_tpl_options = set() + +       search = re.compile("\$\(CONFIG_(.*)\)") +       with open(makefile, "r") as f: +               for l in f.readlines(): +                       m = search.search(l) +                       if m: +                               option = m.group(1) +                               s = set() +                               if option.startswith("$(SPL_)"): + s.add(option.replace("$(SPL_)","")) + s.add(option.replace("$(SPL_)","SPL_")) +                               elif option.startswith("$(SPL_TPL_)"): + s.add(option.replace("$(SPL_TPL_)","")) + s.add(option.replace("$(SPL_TPL_)","SPL_")) + s.add(option.replace("$(SPL_TPL_)","TPL_")) +                               else: + simple_options.add(option.split(')')[0].split(':')[0]) +                               for opt in s: + spl_tpl_options.add(opt.split(')')[0].split(':')[0]) + +       return simple_options, spl_tpl_options + + +makefiles = get_makefiles('./') +simple_options = set() +spl_tpl_options = set() +for f in makefiles: +       a, b = get_CONFIG_var_from_Makefile(f) +       simple_options |= a +       spl_tpl_options |= b + +var_in_makefiles = simple_options +# add the SPL_TPL variables (but filter out those that are never referenced in the code) +for option in spl_tpl_options: +       ### filter out variable that are not reference at all +       if os.system("git grep 'CONFIG_{}' > /dev/null".format(option)) == 0: +               var_in_makefiles.add(option) + +var_in_cfg = get_var_from_moveconfig_db() + +whitelist = set(["SPL_BUILD","TPL_BUILD", "SHELL"]) +suspects = var_in_makefiles - var_in_cfg - whitelist + +print(" -----------------------") +print ("used in makefile but NOT referenced in defconfigs") +for option in suspects: +       print('CONFIG_'+option) + -- 2.7.4 >