SourceInsight 精确导入Linux kernel源码的方法 --转载未测试

相信有很多人用 SourceInsight 查看 Linux Kernel 源码,但导入源码时会遇到一些问题。
1、如果把整个源码树都导入进去,查看符号定义的时候,会发现有大量重复定义,很难找到正确的位置
2、如果手动导入只和该硬件平台相关的源码,工作量太大
本帖提供了一个方法,可用脚本生成只和该硬件平台相关的源码的文件列表,然后通过该文件列表,将相应文件导入 SourceInsight 。
以下是步骤,共4步:
1、新建 SourceInsight 项目
     先不要导入文件,停在如下界面:
    SourceInsight 精确导入Linux kernel源码的方法 --转载未测试 

2、完整编译内核,将编译时输出的信息保存到一个文件中
    例如:
   

  1. make ARCH=arm > build_log.txt


    build_log.txt 文件中内容大致如下:
   

  1. CC      init/main.o
  2.   CHK     include/generated/compile.h
  3.   CC      init/do_mounts.o
  4.   HOSTCC  usr/gen_init_cpio
  5.   CC      arch/arm/vfp/vfpmodule.o
  6.   CC      arch/arm/kernel/elf.o
  7.   AS      arch/arm/kernel/entry-armv.o
  8.   AS      arch/arm/vfp/entry.o
  9.   AS      arch/arm/kernel/entry-common.o
  10.   CC      arch/arm/kernel/irq.o
  11.   AS      arch/arm/vfp/vfphw.o
  12.   GEN     usr/initramfs_data.cpio
  13.   CC      arch/arm/kernel/opcodes.o



3、生成文件列表
     下载 帖子 最后面的附件,解压后是 linux shell 脚本。
    编辑脚本两个变量,ARCH 和 MACH,MACH是平台的名字。如果你用的平台对应 arch/arm/mach-at91 的话,就改成 MACH=at91,以此类推。
          SourceInsight 精确导入Linux kernel源码的方法 --转载未测试 
    在内核源码目录下执行:
   

  1. ./sg.sh build_log.txt file_list.txt

复制代码


    这样,SourceInsight 需要的 文件列表 file_list.txt 就生成了。里面的内容大致如下:
      SourceInsight 精确导入Linux kernel源码的方法 --转载未测试 

4、导入文件列表
     回到 SourceInsight,点击窗口右下角 help 按钮上方的 “add from list” 按钮导入生成的 file_list.txt 文件。
      SourceInsight 精确导入Linux kernel源码的方法 --转载未测试 
     到此为止,文件导入完成了。重新打开工程,会提示同步、构建,之后就可以正常的查看源码了。

这样导入的文件和硬件平台是完全对应的。
下面是本帖的核心,就是这个脚本,有兴趣的可以看看:
   

#!/bin/sh
ARCH=arm
MACH=at91
FILE_IN=$1
FILE_OUT=$2

# .c
SOURCE_LIST=""

# generated file list
FILE_LIST=""

# nest depth for function get_includes()
NEST_DTPTH=0

# recursive function, used to get included files from files.
# result is stored in FILE_LIST
# $1 : file list, e.g. "fs/ext4/file.c fs/ext4/fsync.c"
get_includes()
{
        local includes
        local file

        for file in $1
        do
                if [ ! -e ${file} ]; then
                        continue
                fi

                if echo "${FILE_LIST}" | grep -E ${file} > /dev/null; then
                        continue
                fi
                FILE_LIST="${FILE_LIST} ${file}"

                NEST_DTPTH=$((NEST_DTPTH+1))
                echo "<${NEST_DTPTH} : ${file}"

                includes=$(                                                                                \
                        grep -E -H '^#include' ${file} |                                \
                        sed -r \
                                -e '[email protected]^.*<(acpi/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(asm-generic/.*)>@include/\[email protected]'\
                                -e '[email protected]^.*<(config/.*)>@include/\[email protected]'         \
                                -e '[email protected]^.*<(crypto/.*)>@include/\[email protected]'         \
                                -e '[email protected]^.*<(drm/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(generated/.*)>@include/\[email protected]'         \
                                -e '[email protected]^.*<(keys/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(linux/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(math-emu/.*)>@include/\[email protected]'         \
                                -e '[email protected]^.*<(media/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(misc/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(mtd/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(net/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(pcmcia/.*)>@include/\[email protected]'         \
                                -e '[email protected]^.*<(rdma/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(rxrpc/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(scsi/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(sound/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(target/.*)>@include/\[email protected]'         \
                                -e '[email protected]^.*<(trace/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(uapi/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(video/.*)>@include/\[email protected]'                 \
                                -e '[email protected]^.*<(xen/.*)>@include/\[email protected]'                 \
                                -e "[email protected]^.*<(asm/.*)>@arch/${ARCH}/include/\1 arch/${ARCH}/include/generated/\[email protected]"        \
                                -e "[email protected]^.*<(mach/.*)>@arch/${ARCH}/mach-${MACH}/include/\[email protected]"        \
                                -e '[email protected](^.*/)[^/]+\.c.*\"(.*)\"@\1\[email protected]'         \
                                -e '[email protected]/\*.*@@'                                                         \
                                -e '[email protected]^.*\#include.*[email protected]@'                                  \
                                -e '[email protected]^@ @' |                                                        \
                        sort |                                                                                 \
                        uniq |                                                                                \
                        tr -d '\n' |                                                                 \
                        tr -d '\r'                                                                        \
                )

                if [ -n "${includes}" ]; then
                        get_includes "${includes}"
                fi
                echo ">${NEST_DTPTH}) : ${file}"
                NEST_DTPTH=$((NEST_DTPTH-1))
        done
}

# get *.c from kernel build log
SOURCE_LIST=$(                                                \
        grep -E '^\s*CC' ${FILE_IN} |        \
        sed -r                                                         \
                -e 's/^\s*CC\s*/ /'                        \
                -e 's/\.o/\.c/'                        |        \
        tr -d '\n' |                                         \
        tr -d '\r'                                                \
)
echo ${SOURCE_LIST}
get_includes "${SOURCE_LIST}"

FILE_LIST=$(echo "${FILE_LIST}" | sed -r -e 's/\s/\r\n/g' )

echo "${FILE_LIST}" > ${FILE_OUT}