Linux内核完全注释(基于Linux0.11)_笔记_linux/Makefile

作用

Makefile文件相当于程序编译过程中的批处理文件,是GNU make的的输入文件,其作用是能够自动地编译工程文件
linux/Makefile文件的主要作用是配合独立编译链接的linux/tools/build.c将所有内核编译代码链接和合并成一个可运行的内核映像文件Image

执行流程

示意图

解析

代码解析

变量定义

# 定义RAM盘,若定义了则需要指定参数DRAMDISK块大小
RAMDISK =  #-DRAMDISK=512

# 8086汇编语言的汇编器与链接器
# -0表示生成8086目标程序
AS86	=as86 -0 -a
LD86	=ld86 -0

# GNU编译器与链接器
AS	=as
LD	=ld

# GNU链接器LD的参数
# -m elf_i386表示以elf格式输出i386的32位代码
# -Ttext 0表示代码在0处加载/对齐
# -e startup_32表示指定startup_32为入口点
LDFLAGS	=-m elf_i386 -Ttext 0 -e startup_32

# 指定gcc-3.4编译时使用-i386的指令集
CC	=gcc-3.4 -march=i386 $(RAMDISK)

# -m32表示在64位编译器上生成32位的代码
# -g表示生成调试文件(用于GDB调试)
# -Wall表示打印警告信息
# -O2表示使用二级优化
# -fomit-frame-pointer表示对无需帧指针的函数不把帧指针保留在寄存器中
CFLAGS	=-m32 -g -Wall -O2 -fomit-frame-pointer

# 指定头文件的搜索目录为当前目录下的include(即不使用系统/usr/include目录下的头文件)
CPP	=cpp -nostdinc -Iinclude

# 指定在build程序创建内核映像文件(Image)时所使用的默认根文件系统所在的设备(未指定时,使用默认值/dev/hd6)
ROOT_DEV= #FLOPPY

# 定义kernel目录、mm目录和fs目录的目标代码文件
ARCHIVES=kernel/kernel.o mm/mm.o fs/fs.o

# 制作静态库文件(主要是与硬件交互的驱动程序)
DRIVERS =kernel/blk_drv/blk_drv.a kernel/chr_drv/chr_drv.a
MATH	=kernel/math/math.a
LIBS	=lib/lib.a

Image生成规则编写

# 定义3条了make的隐式规则
# 当源文件是.c文件,目标文件是.s文件,则使用第1条规则
# 当源文件是.s文件,目标文件是.o文件,则使用第2条规则
# 当源文件是.c文件,目标文件是.o文件,则使用第3条规则
.c.s:
	$(CC) $(CFLAGS) \
	-nostdinc -Iinclude -S -o $*.s $<
.s.o:
	$(AS)  -o $*.o $<
.c.o:
	$(CC) $(CFLAGS) \
	-nostdinc -Iinclude -c -o $*.o $<

# all为当前MAkefile文件的最终目标,其依赖于Image(make基本知识)
all:	Image

# 用于生成Image映像文件,其依赖于4个目标文件
# 当要素齐全时,执行命令生成Image映像文件
Image: boot/bootsect boot/setup tools/system tools/build
	cp -f tools/system system.tmp
	strip system.tmp
	objcopy -O binary -R .note -R .comment system.tmp tools/kernel
	tools/build boot/bootsect boot/setup tools/kernel $(ROOT_DEV) > Image
	rm system.tmp
	rm tools/kernel -f
	sync

# 将映像文件Image制作为系统启动盘
disk: Image
	dd bs=8192 if=Image of=/dev/fd0

# 仅使用bootsect模块与setup模块制作映像文件
BootImage: boot/bootsect boot/setup tools/build
	tools/build boot/bootsect boot/setup none $(ROOT_DEV) > Image
	sync

# 编译链接build.c程序
tools/build: tools/build.c
	gcc $(CFLAGS) \
	-o tools/build tools/build.c

# 汇编链接head.s程序
boot/head.o: boot/head.s
	gcc-3.4 -m32 -g -I./include -traditional -c boot/head.s
	mv head.o boot/

# 链接生成system
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 
	nm tools/system | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aU] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)'| sort > System.map 

# 制作数学函数库文件
kernel/math/math.a: FORCE
	(cd kernel/math; make)

# 制作块设备函数库文件
kernel/blk_drv/blk_drv.a: FORCE
	(cd kernel/blk_drv; make)

# 制作字符设备函数库文件
kernel/chr_drv/chr_drv.a: FORCE
	(cd kernel/chr_drv; make)

# 编译生成内核目标文件
kernel/kernel.o: FORCE
	(cd kernel; make)

# 编译生成内存管理目标文件
mm/mm.o: FORCE
	(cd mm; make)

# 编译生成文件系统目标文件 
fs/fs.o: FORCE
	(cd fs; make)

# 制作静态库函数
lib/lib.a: FORCE
	(cd lib; make)

# 使用86的汇编器和链接器编译生成setup文件
boot/setup: boot/setup.s
	$(AS86) -o boot/setup.o boot/setup.s
	$(LD86) -s -o boot/setup boot/setup.o

# 同上,但这里制作的是引导程序(加载操作系统并移交控制权的程序)
boot/bootsect:	boot/bootsect.s
	$(AS86) -o boot/bootsect.o boot/bootsect.s
	$(LD86) -s -o boot/bootsect boot/bootsect.o

# 用于在boot/bootsect.s的头部添加system模块的文件长度信息,现在system模块的文件长度信息直接在boot/bootsect.s的头部定义了,这一步骤已经被弃用
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

常用规则

# 用于清除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)

# 先调用clean清除中间文件,然后对整个操作系统代码进行压缩生成.Z文件
backup: clean
	(cd .. ; tar cf - linux | compress16 - > backup.Z)
	sync

# 用于产生文件之间的依赖关系
dep:
	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)

# 
FORCE:

# 利用隐式规则生成main.o
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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值