自己动手搭建 Linux 0.12 编译环境 — Makefile

来源:互联网 发布:美工完成工作进度表 编辑:程序博客网 时间:2024/06/16 18:15

自己动手搭建 Linux 0.12 编译环境

——Makefile篇

作者:MTK_蛙蛙鱼

写作时间:2013年11月13日

更新时间:2013年11月15日


:时隔几个月,我又开始了Linux 0.12的征程,现在的我变得更加成熟,掌握了更多的技能。时间在变,环境在变,心情也在变,唯一不变的是我探究你玩弄你的心,我喜欢沉浸在思考的世界里,喜欢揣测你想干什么,这好像已经成为了我生活的另外一个分支,一直一直伴随我的百分之十的生命。


Makefile

## if you want the ram-disk device, define this to be the# size in blocks.#RAMDISK = #-DRAMDISK=512AS86=as86 -0 -aLD86=ld86 -0AS=gasLD=gldLDFLAGS=-s -x -MCC=gcc $(RAMDISK)CFLAGS=-Wall -O -fstrength-reduce -fomit-frame-pointer \-fcombine-regs -mstring-insnsCPP=cpp -nostdinc -Iinclude

第1-16行,有个地方需要注意的,as86和ld86这套汇编器与GNU的那套不同,汇编程序(GNU的汇编叫AT&T,这套暂且叫Minix汇编吧且仅支持Intel 8086、80386哦~)是写法有差异的,boot/bootsect.S和boot/setup.S这两个16位汇编文件就需要用它们来处理,如果你用的是Red Hat系统派生的系统(比如CentOS、Federa),那么简单使用“yum install dev86”命令即可。

第5行,内存盘的支持。

第7行,‘-0’这里是‘零’哦,意思是使用16位代码段,如果指令高于8086则会警告,‘-a’保持部分的兼容性,比如方括号和圆括号(这里应该指的是内存寻址相关)和jmps、calls的特殊语法,这个加上就行,不用过多追究,应该是解决前后版本的兼容问题的。

第8行,‘-0’指出在执行文件头部产生16位的魔数(magic)。

第12行,有3个连接器的options,‘-s’忽略所有的符号信息,‘-x’删除所有的本地符号,‘-M’打印link map到标准输出上,这个link map就是符号地址映射表。

第14行,这个编译器的options真是多,还不容易理解的那种,‘-Wall’警告信息打印出来,‘-O’是大写的‘欧’优化选项(还有O1、O2、O3、O0不优化),大家都知道啊,‘-fstrength-reduce’就是消除一些重复的语句,其实有些时候是有害的,‘-fomit-frame-pointer’禁止帧指针函数,‘-fcombine-regs’编译器的组合编译选项,‘-mstring-insns’和机器字符串操作指令相关的。

第16行,预编译器选项,比较简单啦,‘-nostdinc’指明不用使用标准的头文件搜索路径,结合‘-I’选项来指定头文件搜索路径,这里就是在当前目录的include/目录中找头文件。

## ROOT_DEV specifies the default root-device when making the image.# This can be either FLOPPY, /dev/xxxx or empty, in which case the# default of /dev/hd6 is used by 'build'.#ROOT_DEV=/dev/hd6SWAP_DEV=/dev/hd2ARCHIVES=kernel/kernel.o mm/mm.o fs/fs.oDRIVERS =kernel/blk_drv/blk_drv.a kernel/chr_drv/chr_drv.aMATH=kernel/math/math.aLIBS=lib/lib.a

这段看起来就简单,定义了几个变量而已,关于swap交换设备我会在后期准备一篇文章,其实也不难啦~,另外提醒一下如果不懂/dev/hdx(记住这是在Minix系统上的表示方法,不过现代的Linux是另外的表示方法了,所以记得改动为你自己主机上的设备名称哦~)的表示方法,在去把书翻一翻,hd2表示第一块硬盘第二个分区,hd6表示第二块硬盘的第一个分区。

.c.s:$(CC) $(CFLAGS) \-nostdinc -Iinclude -S -o $*.s $<.s.o:$(AS) -c -o $*.o $<.c.o:$(CC) $(CFLAGS) \-nostdinc -Iinclude -c -o $*.o $<

隐式规则。

Image: boot/bootsect boot/setup tools/system tools/buildtools/build boot/bootsect boot/setup tools/system $(ROOT_DEV) \$(SWAP_DEV) > Imagesyncdisk: Imagedd bs=8192 if=Image of=/dev/PS0tools/build: tools/build.c$(CC) $(CFLAGS) \-o tools/build tools/build.c

制作内核img。

第9行,单独编译build tool,这个工具可以将boot/bootsect、boot/setup和system模块组合成内核img文件Image。

第6行,用dd命令将内核img拷贝到/dev/PS0设备(在Linux系统上就是指/dev/fd0第一块软盘设备,而由于作者是在Minix系统上制作内核img因此设备名称不一样而已)上,单次读写块为8K。

boot/head.o: boot/head.stools/system:boot/head.o init/main.o \$(ARCHIVES) $(DRIVERS) $(MATH) $(LIBS)$(LD) $(LDFLAGS) boot/head.o init/main.o \$(ARCHIVES) \$(DRIVERS) \$(MATH) \$(LIBS) \-o tools/system > System.mapkernel/math/math.a:(cd kernel/math; make)kernel/blk_drv/blk_drv.a:(cd kernel/blk_drv; make)kernel/chr_drv/chr_drv.a:(cd kernel/chr_drv; make)kernel/kernel.o:(cd kernel; make)mm/mm.o:(cd mm; make)fs/fs.o:(cd fs; make)lib/lib.a:(cd lib; make)

这块主要是把system模块给做出来,生成的system应该是没有任何符号信息的因为LDFLAGS里面加了‘-s’。其他的Makefile都通过“cd ...; make”来执行的,这部分就不分析了。最好还把那个link map给输出成了‘System.map’文件好我们来查询与调试用。

boot/setup: boot/setup.s$(AS86) -o boot/setup.o boot/setup.s$(LD86) -s -o boot/setup boot/setup.oboot/setup.s:boot/setup.S include/linux/config.h$(CPP) -traditional boot/setup.S -o boot/setup.sboot/bootsect.s:boot/bootsect.S include/linux/config.h$(CPP) -traditional boot/bootsect.S -o boot/bootsect.sboot/bootsect:boot/bootsect.s$(AS86) -o boot/bootsect.o boot/bootsect.s$(LD86) -s -o boot/bootsect boot/bootsect.o

这个两个文件用到了as86和ld86的软件,上面已经介绍如何安装。由于bootsect.S和setup.S包含了include/linux/config.h头文件,因此需要将它们先预编译一下(cpp居然可以处理汇编程序,牛!)。

clean:rm -f Image System.map tmp_make core boot/bootsect boot/setup \boot/bootsect.s boot/setup.srm -f init/*.o tools/system tools/build boot/*.o(cd mm;make clean)(cd fs;make clean)(cd kernel;make clean)(cd lib;make clean)backup: clean(cd .. ; tar cf - linux | compress - > backup.Z)sync

第10行,很有意思,我第一次看0.12的时候压根儿没注意到(完全忽略),作者还搞了个备份命令,哈哈。“tar cf - linux”把linux目录打包后输出到标准输出上,“compress - > backup.Z”把标准输入的内容压缩到backup.Z。我在想当年难道还没有出现gzip或bzip2压缩软件。

dep:sed '/\#\#\# Dependencies/q' < Makefile > tmp_make(for i in init/*.c;do echo -n "init/";$(CPP) -M $$i;done) >> tmp_makecp tmp_make Makefile(cd fs; make dep)(cd kernel; make dep)(cd mm; make dep)### Dependencies:init/main.o : init/main.c include/unistd.h include/sys/stat.h \  include/sys/types.h include/sys/time.h include/time.h include/sys/times.h \  include/sys/utsname.h include/sys/param.h include/sys/resource.h \  include/utime.h include/linux/tty.h include/termios.h include/linux/sched.h \  include/linux/head.h include/linux/fs.h include/linux/mm.h \  include/linux/kernel.h include/signal.h include/asm/system.h \  include/asm/io.h include/stddef.h include/stdarg.h include/fcntl.h \  include/string.h 

第1行,创建依赖关系,真的很用心很细心,这种自动化创建依赖关系学习了。通过sed文本处理命令和cpp -M来形成依赖信息,sed命令用得很巧妙,将字符串“### Dependencies”添加了转移字符‘\’,这样就不会把自身给踩到了,哈哈,真是好东西。

第2行,将Makefile中的依赖关系先去掉然后输出到tmp_make文件中。

第3行,找到init/目录所有.c文件的依赖关系,记住要加上‘init/’字眼,因为cpp -M不会包含前导目录哦~,最后把这些依赖信息追加到tmp_make中变成一个新的Makefile文件咯。

第10行,你看,这些就可以通过make dep命令来生成啦,真是方便啊。如果你对.c文件中包含的头文件有新增与删除,那么请先dep一下吧。


end