0
点赞
收藏
分享

微信扫一扫

【Linux 0.11】Makefile

赵炯;《Linux 内核完全注释 0.11 修正版 V3.0》

# linux/Makefile
#
# if you want the ram-disk device, define this to be the
# size in blocks.
#
RAMDISK = #-DRAMDISK=512 # 如果需要使用 RAM 盘设备的话就定义块的大小,默认没有定义

AS86 =as86 -0 -a # 8086 汇编器和连接器,-0:生成8086目标程序;-a 生成与gas和ld部分兼容的代码
LD86 =ld86 -0

AS =gas # GNU汇编编译器和连接器
LD =gld
LDFLAGS =-s -x -M # GNU 连接器gld运行时用到的选项,-s: 输出文件中省略所有的符号信息;-x 删除所有局部符号;-M 表示需要在标准输出设备上打印连接映像,是指由连接程序产生的一种内存地址映像其中列出了程序段转入到内存中的位置信息:目标文件及符号信息映射到内存中的位置,公共符号如何防止;连接中包含的所有文件成员及其引用的符号
CC =gcc $(RAMDISK)
CFLAGS =-Wall -O -fstrength-reduce -fomit-frame-pointer \ # -Wall 打印所有警告信息 -O 对代码进行优化 -fstrength-reduce 用于优化循环语句 -fcombine-regs 用于指明编译器在组合编译阶段把复制一个寄存器到另一个寄存器的指令组合在一起。-fomit-frame-pointer 指明对于无需帧指针的函数不要把帧指针保留在寄存器中
-fcombine-regs -mstring-insns
CPP =cpp -nostdinc -Iinclude # 预处理程序 -nostdinc -Iinclude 不要搜索标准头文件目录中的文件,即不用系统 /usr/include/ 目录下的头文件,而是使用 -I 选项指定的目录或者在当前目录里搜索头文件

#
# 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 指定在创建内核映像文件时所使用的默认根文件系统所在的设备,这可以是软盘、... 或者空着,空着时 build 程序就是用默认值 /dev/hd6
ROOT_DEV=/dev/hd6

# 下面是kernel 目录、mm 目录和 fs 目录所产生的目标代码文件。
ARCHIVES=kernel/kernel.o mm/mm.o fs/fs.o

# 块和字符设备库文件。.a 表示该文件是个归档文件(工具库),即包含有许多可执行二进制代码子程序集合的库文件
DRIVERS =kernel/blk_drv/blk_drv.a kernel/chr_drv/chr_drv.a
MATH =kernel/math/math.a # 数学运算库文件
LIBS =lib/lib.a # 由 lib 目录中的文件所编译生成的通用库文件

.c.s: # make 老式的隐式后缀规则,指示make利用下面的命令将所有的.c文件编译生成 .s 汇编程序
$(CC) $(CFLAGS) \
-nostdinc -Iinclude -S -o $*.s $< # 使用 include/ 目录中的头文件,在适当地编译后不进行汇编就停止。$*.s自动目标变量,$< 代表第一个先决条件,即符和条件 *.c 的文件。
# 若目标是.s文件,而源文件是.c文件则使用第一个规则
.s.o:
$(AS) -c -o $*.o $<
# 若目标.o,源文件是.s,则第二个规则
.c.o:
$(CC) $(CFLAGS) \
-nostdinc -Iinclude -c -o $*.o $<
# 第三个规则
all: Image # 创建 Makefile 所知的最顶层的目标 —— Image(bootimage),all: 需要生成的对象,可以有多个
# all: a b 既会生成对象a, 又会生成对象b,如果a和b均是带有main函数的可执行文件,这种方法好用

Image: boot/bootsect boot/setup tools/system tools/build # 由四个元素产生
tools/build boot/bootsect boot/setup tools/system $(ROOT_DEV) > Image # build 工具程序 + 四个元素 + 根文件系统组装成内核映像文件 Image
sync # 同步命令 迫使缓冲块数据立即写盘并更新超级块

disk: Image # disk 目标由 Image 产生,先有 Image 后有 disk
dd bs=8192 if=Image of=/dev/PS0

tools/build: tools/build.c # 由 tools 目录下的 build.c 生成执行程序 build
$(CC) $(CFLAGS) \
-o tools/build tools/build.c

boot/head.o: boot/head.s

tools/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.map

kernel/math/math.a:
(cd kernel/math; make) # shell 命令

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)

boot/setup: boot/setup.s # 使用8086汇编和连接器
$(AS86) -o boot/setup.o boot/setup.s
$(LD86) -s -o boot/setup boot/setup.o # -s:去除目标文件中的符号信息

boot/bootsect: boot/bootsect.s # 磁盘引导块
$(AS86) -o boot/bootsect.o boot/bootsect.s
$(LD86) -s -o boot/bootsect boot/bootsect.o

# 在bootsect.s文本程序开始处添加一行有关system模块文件长度信息,在把system模块加载到内存期间用于指明系统模块的长度。
# 取得system长度的方法是:首先利用命令ls对编译生产的system模块文件进行长列表显示,用grep命令取得列表行上文件字节数字段信息,并定向保存到tmp.s临时文件中,cut命令用于剪切字符串,
# tr 用于取出行尾的回车符。
# 这个规则已不适用,直接在bootsect.s程序开始处给出了system模块的一个最大默认长度值。
tmp.s: boot/bootsect.s tools/system
(echo -n "SYSSIZE = (";ls -l tools/system | grep system \
| cut -c25-31 | tr '\012' ' '; echo "+ 15 ) / 16") > tmp.s
cat boot/bootsect.s >> tmp.s

clean: # 执行make clean时,执行下面命令
rm -f Image System.map tmp_make core boot/bootsect boot/setup
rm -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

dep: # 用于产生各文件之间的依赖关系,为了让make命令用它们来确定是否需要重建一个目标对象。当某个头文件被改动后,make就能通过生成的依赖关系,重新编译与该头文件有关的所有*.c文件
sed '/\#\#\# Dependencies/q' < Makefile > tmp_make
(for i in init/*.c;do echo -n "init/";$(CPP) -M $$i;done) >> tmp_make
cp 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/times.h include/sys/utsname.h \
include/utime.h include/time.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/signal.h include/asm/system.h include/asm/io.h \
include/stddef.h include/stdarg.h include/fcntl.h


举报

相关推荐

0 条评论