编译完的内核如何快速删除没有编译的.c 文件?目的是我只需要关注被编译的 code 就可以。这样不会受多余的代码干扰。放到代码阅读软件中比如 source insight 也简洁很多。当然如果能删除一些.h 文件,就更好了。但我感觉不一定可以。

虽然我猜肯定有专门的工具干这事情,但是编译一轮之后单纯靠 atime 应该就可以排除没被使用的文件。

不过 Linux 内核的目录结构都很清晰,看代码也不太容易被干扰啊。

一种办法是是使用 objdump 生成带文件路径信息反汇编代码,然后从里面提取文件路径,去掉行号,去重。

不过 Linux 内核的目录结构都很清晰,看代码也不太容易被干扰啊。
对新手或者不了解的人还是很有好处的。另外同一个目录下,也有一些 C 文件可能不会编译

#!/bin/bash
set -e
#set -x

#脚本说明
#该脚本用于在编译后的 Linux Kernel 目录中找出实际编译的全部.c 文件及其依赖的.h 文件,
#并将结果保存在 files.list 中,可直接导入到 sourceinsight 。
#执行方法:将脚本放到 Linux Kernel 编译目录直接执行即可。

TMPDIR=mktemp -d
MASTERDIR="$PWD"
OCMDFILE=$TMPDIR/file.ocmd
TFILE=$TMPDIR/file.t
AFILE=$TMPDIR/file.a
BFILE=$TMPDIR/file.b
CFILE=$TMPDIR/file.c
DFILE=$TMPDIR/file.d
HFILE=$TMPDIR/file.h

function get_header_file_from_ocmd()
{
local prefix=$(echo $MASTERDIR/ | sed 's///\//g')
oc=$1 #name of input file,input file is mixed content of all .o.cmd file
out=$2 #name of output file

echo "handle ocmd file $oc for header file"

grep -E ".h)?$" $oc > $out
sed -i -e 's/.h)$/.h/g' -e "s/^\([^\/]\)/${prefix}\1/g" $out

echo "handle ocmd file $oc for header file done"
}

function merge_result()
{
cat [email protected] > files.list

echo All .c and .h files are listed in files.list, now you can import them to sourceinsight project.
}

function get_defination_from_ocmd()
{

oc=$1 #name of input file,input file is mixed content of all .o.cmd file
out=$2 #name of output file

echo "handle ocmd file $oc for defination"
rm -fv $out

sort -u $oc > $out
cp -v $out $oc

grep "-\bD" $oc |awk -F' ' '{for(i=1;i < NF;i++)printf("%s\n", $i);}' |grep "-\bD"|sed -e "s/-D/\#define\ /g" -e "s/=/\ /g" -e '/KBUILD_/'d | sort -u > $out

echo "handle ocmd file $oc for defination done"
}

find -name ".o.cmd" | sed -e '/.mod./'d -e '/.built-in./d' > $OCMDFILE #no "..modmake -j24.cmd" and ".built-in.o.cmd"
cat $OCMDFILE | awk -F '.' '{printf("%s%s%s.c\n", "'$MASTERDIR'", $2, $3)}' > $TFILE #add $MASTERDIR to the start of line
sort -u $TFILE > $CFILE
echo "System:List of C file is in $CFILE"

echo "Mix content of all .o.cmd file"
while read line; do
cat $line
done < $OCMDFILE >> $AFILE
echo "Mix content of all .o.cmd file,done,$counts files has been handled"

echo "Genrate defination file"
cp -v $AFILE $BFILE

get_defination_from_ocmd $BFILE $DFILE
echo "Genrate defination file,done"
echo "System:List of D file is in $DFILE"

echo "Put all unit of information to single line mode"
sed 's/\ /\n/g' $AFILE > $TFILE
echo "Put all unit of information to single line mode,done"

echo "Sort and Uniq all unit of information"
sort -u $TFILE > $AFILE
echo "Sort and Uniq all unit of information,done"

echo "Get all name of header file"
get_header_file_from_ocmd $AFILE $TFILE
echo "Get all name of header file,done"

echo "Sort and Uniq all name of header file"
sort -u $TFILE > $AFILE
echo "Sort and Uniq all name of header file,done"

echo "Delete the name of header file of host"
sed '/^/usr/d' $AFILE > $HFILE #delete line which start with "/usr",it's host's header file
echo "Delete the name of header file of host,done"

echo "System:List of H file is in $HFILE"

merge_result $CFILE $HFILE

rm -fr $TMPDIR

补充:如果不删 TMPDIR,还可以在 file.d 文件中查看通过命令行 /Makefile 传递进去的宏定义。

其实必要性不大,,,