/scripts/checkkconfigsymbols.sh

来源:互联网 发布:linux学了有什么用 编辑:程序博客网 时间:2024/05/16 19:24

#!/bin/sh
# Find Kconfig variables used in source code but never defined in Kconfig
# Copyright (C) 2007, Paolo 'Blaisorblade' Giarrusso <
blaisorblade@yahoo.it>

# Tested with dash.
#与$*不同,$@指所有位置参数被分别表示为双引号中的N个字符串
paths="$@"
#如果没有指定路径,则默认为当前目录
[ -z "$paths" ] && paths=.

# Doing this once at the beginning saves a lot of time, on a cache-hot tree.
#在当前目录下查找所有名为Kconfig或以Kconfig开头的非备份文件
Kconfigs="`find . -name 'Kconfig' -o -name 'Kconfig*[^~]'`"

echo -e "File list /tundefined symbol used"
#在$paths目录下查找.c, .h, .S文件或Makefile以及以Makefile开头的非备份文件,将文件路径传递到while循环中
find $paths -name '*.[chS]' -o -name 'Makefile' -o -name 'Makefile*[^~]'| while read i
do
 # Output the bare Kconfig variable and the filename; the _MODULE part at
 # the end is not removed here (would need perl an not-hungry regexp for that).
 #查找UML_CONFIG_xxx, 将其替换为xxx $i,其中xxx为变量名,$i为文件路径名
 sed -ne 's!^.*/</(UML_/)/?CONFIG_/([0-9A-Z_]/+/).*!/2 '$i'!p' < $i
done | /
# Smart "sort|uniq" implemented in awk and tuned to collect the names of all
# files which use a given symbol
#创建数组map,记录变量名以及出现的次数等信息
awk '{map[$1, count[$1]++] = $2; }
END {
 for (combIdx in map) {
 #SUBSEP默认为逗号,所以会创建数组separate,其中separate[1]=$1
  split(combIdx, separate, SUBSEP);
  # The value may have been removed.
  #确认该数组项是否已被删除
  if (! ( (separate[1], separate[2]) in map ) )
   continue;
  #变量名
  symb=separate[1];
  #输出变量名
  printf "%s ", symb;
  #Use gawk extension to delete the names vector
  delete names;
  #Portably delete the names vector
  #split("", names);
  for (i=0; i < count[symb]; i++) {
   names[map[symb, i]] = 1;#建立数组names,索引为变量名所在的文件路径
   # Unfortunately, we may still encounter symb, i in the
   # outside iteration.
   delete map[symb, i];
  }
  i=0;
  #输出文件路径名
  for (name in names) {
   if (i > 0)
    printf ", %s", name;
   else
    printf "%s", name;
   i++;
  }
  printf "/n";
 }
}' |
while read symb files; do
 # Remove the _MODULE suffix when checking the variable name. This should
 # be done only on tristate symbols, actually, but Kconfig parsing is
 # beyond the purpose of this script.
 #删除后缀_MODULE
 symb_bare=`echo $symb | sed -e 's/_MODULE//'`
 #如果在$Kconfigs文件中没有找到变量名,就输出文件路径和变量名
 if ! grep -q "/<$symb_bare/>" $Kconfigs; then
  echo -e "$files: /t$symb"
 fi
done|sort