V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
huangya
V2EX  ›  Linux

编译完的内核如何快速删除没有编译的.c 文件

  •  
  •   huangya · 2021-08-18 23:10:23 +08:00 · 1827 次点击
    这是一个创建于 975 天前的主题,其中的信息可能已经有所发展或是发生改变。
    编译完的内核如何快速删除没有编译的.c 文件?目的是我只需要关注被编译的 code 就可以。这样不会受多余的代码干扰。放到代码阅读软件中比如 source insight 也简洁很多。当然如果能删除一些.h 文件,就更好了。但我感觉不一定可以。
    6 条回复    2021-08-25 17:51:32 +08:00
    yanqiyu
        1
    yanqiyu  
       2021-08-19 00:47:11 +08:00 via Android
    虽然我猜肯定有专门的工具干这事情,但是编译一轮之后单纯靠 atime 应该就可以排除没被使用的文件。

    不过 Linux 内核的目录结构都很清晰,看代码也不太容易被干扰啊。
    abutter
        2
    abutter  
       2021-08-19 07:56:18 +08:00
    一种办法是是使用 objdump 生成带文件路径信息反汇编代码,然后从里面提取文件路径,去掉行号,去重。
    huangya
        3
    huangya  
    OP
       2021-08-19 08:34:03 +08:00
    @yanqiyu
    >不过 Linux 内核的目录结构都很清晰,看代码也不太容易被干扰啊。
    对新手或者不了解的人还是很有好处的。另外同一个目录下,也有一些 C 文件可能不会编译
    bfdh
        4
    bfdh  
       2021-08-19 10:39:24 +08:00   ❤️ 1
    #!/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 $@ > 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 传递进去的宏定义。
    nsynet57
        5
    nsynet57  
       2021-08-19 12:24:11 +08:00
    其实必要性不大,,,
    huangya
        6
    huangya  
    OP
       2021-08-25 17:51:32 +08:00
    @bfdh 不好意思,这几天太忙了。现在才回复,初步测试了一下,非常好。source insight 检索速度快很多。非常感谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2647 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 11:33 · PVG 19:33 · LAX 04:33 · JFK 07:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.