nocc 0.0.1d发布,去除整个C项目中未用代码
时间:2007-04-08 来源:inside2004
1, nocc 0.0.1d能干什么:
使用C语言编写的软件为了适应各种不同的平台或要求,大多会使用条件编译,这在提高了软件适应性的同时却降低了代码的可读性,因为针对不同平台许多函数有不同实现,当你使用ctags或者SourceInSight等工具来分析代码时很难一下找到真正的目标。
以linux kernel为例,目前版本(2.6.20)已经支持数十个平台,代码也达到了恐怖的700万行以上,但一般来说你需要的只是针对某个特定平台的部分,比如i386/uml/arm/ppc中的一个,如何将你不需要的代码去掉呢?使用nocc可以达到这个目的。
先来看如何针对kernel-2.6.20使用nocc,首先获取并解压代码包:)
$ tar zxf linux-2.6.20.tar.gz
$ cd linux-2.6.20
配置编译,我这里使用的uml平台:
$ make menuconfig ARCH=um
进入配置菜单后直接双击ESC退出,即使用默认配置:
$ make ARCH=um
现在统计一下源码包中的代码数量(*.c/*.h):
$ find . -type f -name "*.[ch]" | xargs wc -l | grep total
191018 total
240112 total
178362 total
183622 total
166939 total
455531 total
466098 total
323675 total
418717 total
740114 total
606878 total
626812 total
572085 total
180344 total
66175 total
64838 total
87619 total
107007 total
62556 total
101182 total
119397 total
269427 total
460149 total
425931 total
--------------
7114588(这行是我加起来的,非上面命令的直接输出)
源码总量是711万多行,应该说是比较恐怖的,接下来使用nocc处理源码:
$ nocc -c "make ARCH=um" -d .
处理完后再编译一次,看是否能通过:
$ make ARCH=um
编译正常完成,现在来统计还有多少代码:
$ find . -type f -name "*.[ch]" | xargs wc -l | grep total
0 total
218 total
0 total
0 total
19642 total
26707 total
8835 total
0 total
187 total
5337 total
0 total
46 total
110152 total
874 total
0 total
6407 total
0 total
0 total
2026 total
29228 total
44996 total
98737 total
--------------
421992
处理后只剩42万行了,这个结果还算不错吧,至少从代码量上说kernel也就是一个中型软件了,如果你现在用ctags或SourceInSight等工具来浏览,相信会容易很多。
总结一下,nocc的作用就是去除项目里C语言源文件(*.c/*.h)中不被编译的部分。
2,nocc 0.0.1d是怎么工作的:
nocc 0.0.1d的工作原理很容易理解:
1,在要处理的代码(*.c/*.h)中合适的位置插入gcc扩展的预编译头#warning。
2,进而编译整个项目获取gcc编译输出并进行分析。
3,根据分析结果确定某段代码是否使用,使用则保留,未使用则去除。
这种工作方式相当于手动用#warning来确定某个条件编译分枝(或者整个文件)是否有效,原理上讲是相当可靠的,不会把你的有效代码删除。但要求你的代码在使用nocc处理之前必须能使用“make...”等方式调用gcc成功编译。
3,如何使用nocc 0.0.1d:
首先是下载nocc 0.0.1d源码并编译,nocc使用GPL方式发布,你可以从http://inside2004.cublog.cn处下载源码文件,获取源码后解压/make/make install 即完成编译安装。安装目录在/usr/local/bin下,可以自己拷贝或者修改Makefile以便放入其他位置,但请确认nocc所在目录在你的执行目录$PATH里。
编译安装完成后就是利用nocc处理你的源文件。特别提醒,请在处理之前对你的代码做备份。
下面分别以stunnel-4.20和kernel-2.6.20为例说明使用方法:
1,stunnel-4.20:
$ tar zxf stunnel-4.20.tar.gz
$ cd stunnel-4.20
$ ./configure
$ make
make通过后即可处理,-d表示处理失败时保存失败信息(在当前目录下的nocc.dbg文件中),'.'表示处理当前目录(即你的源码目录)下的所有源文件。
$ nocc -d .
Backing up all source file...[OK]
Adding "#warning" to each source file...[OK]
Parsing output of the compilation..(省略若干'.')..[OK]
Striping unnecessary CCs...[OK]
再次make确保可修改后正常:
$ make
2,kernel-2.6.20:
$ tar zxf linux-2.6.20.tar.gz
$ cd linux-2.6.20
$ make menuconfig ARCH=um
为了说明nocc的运行时间,这里使用time在编译时进行时间统计。
$ time make ARCH=um
real 4m6.178s
user 3m12.692s
sys 0m30.518s
正常编译可通过后即可使用nocc处理,nocc默认的项目编译命令为"make",但kernel编译时使用的是"make ARCH=um",所以这里需要使用-c选项告诉nocc编译命令,其他与上面编译一致:
$ time nocc -c "make ARCH=um" -d .
real 8m40.420s
user 4m34.353s
sys 2m4.149s
Backing up all source file...[OK]
Adding "#warning" to each source file...[OK]
Parsing output of the compilation..(省略若干'.')..[OK]
Striping unnecessary CCs...[OK]
最后再次make确认修改结果:
$ time make ARCH=um
real 3m58.890s
user 3m11.536s
sys 0m30.392s
OK!一切正常。
我的主机CPU是P4D 2.8G,对nocc的用时估计应该和执行一次完整编译相当,但从上面的结果来看是一次完整编译的2倍,不过也算正常。
4,nocc 0.0.1d与nocc前面版本有何差别:
前面也已发布了了0.0.1/0.0.2等版本,但0.0.1d与之相比有很大差异,主要表现在:
1,0.0.1d针对整个C项目,而非项目中单个的*.c文件,更容易使用。顺便说明一下,0.0.1d中'd'的含义是目录。
2,0.0.1d不仅能处理*.c也能处理*.h。
3,不存在0.0.1/0.0.2版本中提到的局限,对于实际中没有使用的*.c/*.h文件也能正常处理。
5,其他:
nocc 0.0.1d经过了一定的测试,但难免仍然存在bug,希望试用的朋友能将发现的问题发给我([email protected])或者在我的blog(http://inside2004.cublog.cn)留言,谢谢!
下面是对我的主机上所有开源软件进行测试的结果,这里贴出来(绿色表示通过/红色表示有问题):
ethereal-0.99.0
fcitx-3.4.2
js-1.60
libevent-1.2
libpcap-0.9.5
linux-2.6.20
openssl-0.9.8d
openvpn-2.0.9
privoxy-3.0.6-stable
prozilla-1.3.7.4
screen-4.0.2
squid-2.6.STABLE10
ssldump-0.9b3
stardict-2.4.5
stunnel-4.20
tcpdump-3.9.5
thttpd-2.25b
tor-0.1.2.7-alpha
valgrind-3.2.0
vim70
这其中除ethereal在make时会使用类似于YACC的工具新生成C源文件导致nocc未能通过外,其他均可正常处理。关于ethereal这类做法我目前对它的原理不了解,暂时无法处理。
最后,非常欢迎试用nocc的朋友能将自己使用的结果贴上来,能通过的包对我是种鼓励,而处理有误的源码包名将有助与发现解决问题,谢谢。
使用C语言编写的软件为了适应各种不同的平台或要求,大多会使用条件编译,这在提高了软件适应性的同时却降低了代码的可读性,因为针对不同平台许多函数有不同实现,当你使用ctags或者SourceInSight等工具来分析代码时很难一下找到真正的目标。
以linux kernel为例,目前版本(2.6.20)已经支持数十个平台,代码也达到了恐怖的700万行以上,但一般来说你需要的只是针对某个特定平台的部分,比如i386/uml/arm/ppc中的一个,如何将你不需要的代码去掉呢?使用nocc可以达到这个目的。
先来看如何针对kernel-2.6.20使用nocc,首先获取并解压代码包:)
$ tar zxf linux-2.6.20.tar.gz
$ cd linux-2.6.20
配置编译,我这里使用的uml平台:
$ make menuconfig ARCH=um
进入配置菜单后直接双击ESC退出,即使用默认配置:
$ make ARCH=um
现在统计一下源码包中的代码数量(*.c/*.h):
$ find . -type f -name "*.[ch]" | xargs wc -l | grep total
191018 total
240112 total
178362 total
183622 total
166939 total
455531 total
466098 total
323675 total
418717 total
740114 total
606878 total
626812 total
572085 total
180344 total
66175 total
64838 total
87619 total
107007 total
62556 total
101182 total
119397 total
269427 total
460149 total
425931 total
--------------
7114588(这行是我加起来的,非上面命令的直接输出)
源码总量是711万多行,应该说是比较恐怖的,接下来使用nocc处理源码:
$ nocc -c "make ARCH=um" -d .
处理完后再编译一次,看是否能通过:
$ make ARCH=um
编译正常完成,现在来统计还有多少代码:
$ find . -type f -name "*.[ch]" | xargs wc -l | grep total
0 total
218 total
0 total
0 total
19642 total
26707 total
8835 total
0 total
187 total
5337 total
0 total
46 total
110152 total
874 total
0 total
6407 total
0 total
0 total
2026 total
29228 total
44996 total
98737 total
--------------
421992
处理后只剩42万行了,这个结果还算不错吧,至少从代码量上说kernel也就是一个中型软件了,如果你现在用ctags或SourceInSight等工具来浏览,相信会容易很多。
总结一下,nocc的作用就是去除项目里C语言源文件(*.c/*.h)中不被编译的部分。
2,nocc 0.0.1d是怎么工作的:
nocc 0.0.1d的工作原理很容易理解:
1,在要处理的代码(*.c/*.h)中合适的位置插入gcc扩展的预编译头#warning。
2,进而编译整个项目获取gcc编译输出并进行分析。
3,根据分析结果确定某段代码是否使用,使用则保留,未使用则去除。
这种工作方式相当于手动用#warning来确定某个条件编译分枝(或者整个文件)是否有效,原理上讲是相当可靠的,不会把你的有效代码删除。但要求你的代码在使用nocc处理之前必须能使用“make...”等方式调用gcc成功编译。
3,如何使用nocc 0.0.1d:
首先是下载nocc 0.0.1d源码并编译,nocc使用GPL方式发布,你可以从http://inside2004.cublog.cn处下载源码文件,获取源码后解压/make/make install 即完成编译安装。安装目录在/usr/local/bin下,可以自己拷贝或者修改Makefile以便放入其他位置,但请确认nocc所在目录在你的执行目录$PATH里。
编译安装完成后就是利用nocc处理你的源文件。特别提醒,请在处理之前对你的代码做备份。
下面分别以stunnel-4.20和kernel-2.6.20为例说明使用方法:
1,stunnel-4.20:
$ tar zxf stunnel-4.20.tar.gz
$ cd stunnel-4.20
$ ./configure
$ make
make通过后即可处理,-d表示处理失败时保存失败信息(在当前目录下的nocc.dbg文件中),'.'表示处理当前目录(即你的源码目录)下的所有源文件。
$ nocc -d .
Backing up all source file...[OK]
Adding "#warning" to each source file...[OK]
Parsing output of the compilation..(省略若干'.')..[OK]
Striping unnecessary CCs...[OK]
再次make确保可修改后正常:
$ make
2,kernel-2.6.20:
$ tar zxf linux-2.6.20.tar.gz
$ cd linux-2.6.20
$ make menuconfig ARCH=um
为了说明nocc的运行时间,这里使用time在编译时进行时间统计。
$ time make ARCH=um
real 4m6.178s
user 3m12.692s
sys 0m30.518s
正常编译可通过后即可使用nocc处理,nocc默认的项目编译命令为"make",但kernel编译时使用的是"make ARCH=um",所以这里需要使用-c选项告诉nocc编译命令,其他与上面编译一致:
$ time nocc -c "make ARCH=um" -d .
real 8m40.420s
user 4m34.353s
sys 2m4.149s
Backing up all source file...[OK]
Adding "#warning" to each source file...[OK]
Parsing output of the compilation..(省略若干'.')..[OK]
Striping unnecessary CCs...[OK]
最后再次make确认修改结果:
$ time make ARCH=um
real 3m58.890s
user 3m11.536s
sys 0m30.392s
OK!一切正常。
我的主机CPU是P4D 2.8G,对nocc的用时估计应该和执行一次完整编译相当,但从上面的结果来看是一次完整编译的2倍,不过也算正常。
4,nocc 0.0.1d与nocc前面版本有何差别:
前面也已发布了了0.0.1/0.0.2等版本,但0.0.1d与之相比有很大差异,主要表现在:
1,0.0.1d针对整个C项目,而非项目中单个的*.c文件,更容易使用。顺便说明一下,0.0.1d中'd'的含义是目录。
2,0.0.1d不仅能处理*.c也能处理*.h。
3,不存在0.0.1/0.0.2版本中提到的局限,对于实际中没有使用的*.c/*.h文件也能正常处理。
5,其他:
nocc 0.0.1d经过了一定的测试,但难免仍然存在bug,希望试用的朋友能将发现的问题发给我([email protected])或者在我的blog(http://inside2004.cublog.cn)留言,谢谢!
下面是对我的主机上所有开源软件进行测试的结果,这里贴出来(绿色表示通过/红色表示有问题):
ethereal-0.99.0
fcitx-3.4.2
js-1.60
libevent-1.2
libpcap-0.9.5
linux-2.6.20
openssl-0.9.8d
openvpn-2.0.9
privoxy-3.0.6-stable
prozilla-1.3.7.4
screen-4.0.2
squid-2.6.STABLE10
ssldump-0.9b3
stardict-2.4.5
stunnel-4.20
tcpdump-3.9.5
thttpd-2.25b
tor-0.1.2.7-alpha
valgrind-3.2.0
vim70
这其中除ethereal在make时会使用类似于YACC的工具新生成C源文件导致nocc未能通过外,其他均可正常处理。关于ethereal这类做法我目前对它的原理不了解,暂时无法处理。
最后,非常欢迎试用nocc的朋友能将自己使用的结果贴上来,能通过的包对我是种鼓励,而处理有误的源码包名将有助与发现解决问题,谢谢。
|
相关阅读 更多 +