Makefile

#版本号,将版本号,补丁号,版本写入tools/version.h中
VERSION = 1
#补丁号
PATCHLEVEL = 0
#ALPHA版
ALPHA =

#目标all,依赖于Version zImage
all:    Version zImage

.EXPORT_ALL_VARIABLES:

#从此到编译.c文件为.o文件为止,之间所定义的变量都会被导出
#配置shell环境
CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH;
      else if [ -x /bin/bash ]; then echo /bin/bash;
      else echo sh; fi ; fi)

#
# Make "config" the default target if there is no configuration file or
# "depend" the target if there is no top-level dependency information.
#
#如果目录存在*.config文件,则包含.config,这样会将.config文件内容
#嵌入到makefile文件中
ifeq (.config,$(wildcard .config))
include .config
#如果目录下存在.depend文件,则包含.depend文件,会将.depend文件内容嵌入到
#makefile文件中
ifeq (.depend,$(wildcard .depend))
include .depend
#否则定义变量CONFIGURATION=depend
else
CONFIGURATION = depend
endif
#否则定义变量CONFIGURATION=config
else
CONFIGURATION = config
endif

#如果定义了变量CONFIGURATION则定义变量CONFIGURE = dummy
ifdef CONFIGURATION
CONFIGURE = dummy
endif

#
# ROOT_DEV specifies the default root-device when making the image.
# This can be either FLOPPY, CURRENT, /dev/xxxx or empty, in which case
# the default of FLOPPY is used by 'build'.
#
#当创建image文件时ROOT_DEV指定默认的根设备,可能是软盘,当前,dev/xxx或者空
#在默认的时候软盘被当做默认设备
ROOT_DEV = CURRENT

#
# If you want to preset the SVGA mode, uncomment the next line and
# set SVGA_MODE to whatever number you want.
# Set it to -DSVGA_MODE=NORMAL_VGA if you just want the EGA/VGA mode.
# The number is the same as you would ordinarily press at bootup.
#

#设置SVGA模式
SVGA_MODE=    -DSVGA_MODE=NORMAL_VGA

#
# standard CFLAGS
#

#标准的编译选项
CFLAGS = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -pipe

#如果定义了CONFIG_CPP宏,则CFLAGS增加c++编译选项
ifdef CONFIG_CPP
CFLAGS := $(CFLAGS) -x c++
endif

#如果定义了宏CONFIG_M486,则编译选项增加486芯片否则为386芯片,在配置过程中确定
ifdef CONFIG_M486
CFLAGS := $(CFLAGS) -m486
else
CFLAGS := $(CFLAGS) -m386
endif

#
# if you want the ram-disk device, define this to be the
# size in blocks.
#

#确定RAMDISK的大小,此处不设置RAMDISK
#RAMDISK = -DRAMDISK=512

#定义 as86、 ld86、AS 和 LD选项
AS86    =as86 -0 -a
LD86    =ld86 -0

AS    =as
LD    =ld
HOSTCC    =gcc
CC    =gcc -D__KERNEL__
MAKE    =make
CPP    =$(CC) -E
AR    =ar
STRIP    =strip

#定义变量
ARCHIVES    =kernel/kernel.o mm/mm.o fs/fs.o net/net.o ipc/ipc.o
FILESYSTEMS    =fs/filesystems.a
DRIVERS        =drivers/block/block.a
         drivers/char/char.a
         drivers/net/net.a
         ibcs/ibcs.o
LIBS        =lib/lib.a
#定义子目录
SUBDIRS        =kernel drivers mm fs net ipc ibcs lib

#定义内核头文件路径
KERNELHDRS    =/usr/src/linux/include

#如果用户选择了SCSI设备,则这里就会增加这部分定义
ifdef CONFIG_SCSI
DRIVERS := $(DRIVERS) drivers/scsi/scsi.a
endif

#是否配置了声卡驱动
ifdef CONFIG_SOUND
DRIVERS := $(DRIVERS) drivers/sound/sound.a
endif

#是否配置了数学协处理器
ifdef CONFIG_MATH_EMULATION
DRIVERS := $(DRIVERS) drivers/FPU-emu/math.a
endif

#到此为止所定义的变量都会被导出

#.c文件编译为.s文件
.c.s:
    $(CC) $(CFLAGS) -S -o $*.s $<
#.s文件编译为.o文件
.s.o:
    $(AS) -c -o $*.o $<
#.c文件编译为.o文件
.c.o:
    $(CC) $(CFLAGS) -c -o $*.o $<

#此目标执行,删除tools/version.h文件
Version: dummy
    #删除文件tools/version.h
    rm -f tools/version.h

#执行清除工作之后,会执行配置部分
config:
#  读取config.in文件
#     /bin/sh  Configure < config.in
    $(CONFIG_SHELL) Configure $(OPTS) < config.in
    @if grep -s '^CONFIG_SOUND' .tmpconfig ; then
        $(MAKE) -C drivers/sound config;             
        else : ; fi
    mv .tmpconfig .config
    #用.tmpconfig替代.config

#在已定义好的子目录中搜索子目录
linuxsubdirs: dummy
    set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i; done

#
tools/./version.h: tools/version.h

#生成tools/version.h需要依赖与Makefile
tools/version.h: $(CONFIGURE) Makefile
    @./makever.sh
    @echo #define UTS_RELEASE "$(VERSION).$(PATCHLEVEL)$(ALPHA)" > tools/version.h
    @echo #define UTS_VERSION "#`cat .version` `date`" >> tools/version.h
    @echo #define LINUX_COMPILE_TIME "`date +%T`" >> tools/version.h
    @echo #define LINUX_COMPILE_BY "`whoami`" >> tools/version.h
    @echo #define LINUX_COMPILE_HOST "`hostname`" >> tools/version.h
    @echo #define LINUX_COMPILE_DOMAIN "`domainname`" >> tools/version.h

#编译生成目标文件tools/build
tools/build: tools/build.c $(CONFIGURE)
    $(HOSTCC) $(CFLAGS) -o $@ $<

#编译head.s文件生成boot/head.o
boot/head.o: $(CONFIGURE) boot/head.s

#生成中间汇编文件boot/head.s
boot/head.s: boot/head.S $(CONFIGURE) include/linux/tasks.h
    $(CPP) -traditional $< -o $@

#生成版本
tools/version.o: tools/version.c tools/version.h

#生成main.o
init/main.o: $(CONFIGURE) init/main.c
    $(CC) $(CFLAGS) $(PROFILING) -c -o $*.o $<

#生成system
tools/system:    boot/head.o init/main.o tools/version.o linuxsubdirs
    $(LD) $(LDFLAGS) -Ttext 1000 boot/head.o init/main.o tools/version.o
        $(ARCHIVES)
        $(FILESYSTEMS)
        $(DRIVERS)
        $(LIBS)
        -o tools/system
    nm tools/zSystem | grep -v '(compiled)|(.o$$)|( a )' |
        sort > System.map

#生成setup
boot/setup: boot/setup.o
    $(LD86) -s -o $@ $<
#生成setup.o
boot/setup.o: boot/setup.s
    $(AS86) -o $@ $<
#生成boot/setup.s: boot/setup.S
boot/setup.s: boot/setup.S $(CONFIGURE) include/linux/config.h Makefile
    $(CPP) -traditional $(SVGA_MODE) $(RAMDISK) $< -o $@

#生成bootsect目标
boot/bootsect: boot/bootsect.o
    $(LD86) -s -o $@ $<

boot/bootsect.o: boot/bootsect.s
    $(AS86) -o $@ $<

#生成bootsect.s
boot/bootsect.s: boot/bootsect.S $(CONFIGURE) include/linux/config.h Makefile
    $(CPP) -traditional $(SVGA_MODE) $(RAMDISK) $< -o $@

#生成zSystem目标
zBoot/zSystem: zBoot/*.c zBoot/*.S tools/zSystem
    $(MAKE) -C zBoot

#生成映像文件
zImage: $(CONFIGURE) boot/bootsect boot/setup zBoot/zSystem tools/build
    tools/build boot/bootsect boot/setup zBoot/zSystem $(ROOT_DEV) > zImage
    sync

#将映像文件写入磁盘
zdisk: zImage
    dd bs=8192 if=zImage of=/dev/fd0

#生成zlilo
zlilo: $(CONFIGURE) zImage
    if [ -f /vmlinuz ]; then mv /vmlinuz /vmlinuz.old; fi
    if [ -f /zSystem.map ]; then mv /zSystem.map /zSystem.old; fi
    cat zImage > /vmlinuz
    cp zSystem.map /
    if [ -x /sbin/lilo ]; then /sbin/lilo; else /etc/lilo/install; fi

#创建映像文件
tools/zSystem:    boot/head.o init/main.o tools/version.o linuxsubdirs
    $(LD) $(LDFLAGS) -Ttext 100000 boot/head.o init/main.o tools/version.o
        $(ARCHIVES)
        $(FILESYSTEMS)
        $(DRIVERS)
        $(LIBS)
        -o tools/zSystem
    nm tools/zSystem | grep -v '(compiled)|(.o$$)|( a )' |
        sort > zSystem.map

#查找相应的子目录,进入执行make
fs: dummy
    $(MAKE) linuxsubdirs SUBDIRS=fs
#查找相应的子目录,进入执行make
lib: dummy
    $(MAKE) linuxsubdirs SUBDIRS=lib
#查找相应的子目录,进入执行make
mm: dummy
    $(MAKE) linuxsubdirs SUBDIRS=mm
#查找相应的子目录,进入执行make
ipc: dummy
    $(MAKE) linuxsubdirs SUBDIRS=ipc
#查找相应的子目录,进入执行make
kernel: dummy
    $(MAKE) linuxsubdirs SUBDIRS=kernel
#查找相应的子目录,进入执行make
drivers: dummy
    $(MAKE) linuxsubdirs SUBDIRS=drivers
#查找相应的子目录,进入执行make
net: dummy
    $(MAKE) linuxsubdirs SUBDIRS=net

#清除目标
clean:
    rm -f kernel/ksyms.lst
    rm -f core `find . -name '*.[oas]' -print`
    rm -f core `find . -name 'core' -print`
    rm -f zImage zSystem.map tools/zSystem tools/system
    rm -f Image System.map boot/bootsect boot/setup
    rm -f zBoot/zSystem zBoot/xtract zBoot/piggyback
    rm -f .tmp* drivers/sound/configure
    rm -f init/*.o tools/build boot/*.o tools/*.o
#首先执行make mrproper,会先执行上一步骤清除工作
mrproper: clean
    #在执行以下命令时,会先执行上一步骤
    #删除include/linux/autoconf.h tools/version.h这两个文件,首次执行的时候还未曾生成这两个头文件
    rm -f include/linux/autoconf.h tools/version.h
    #删除drivers/sound/local.h,首次执行的时候还未曾生成这个头文件
    rm -f drivers/sound/local.h
    #删除.version .config* config.old,首次执行的时候还未曾生成这三个头文件
    rm -f .version .config* config.old
    #删除.depend,首次执行的时候还未曾生成这三个头文件
    rm -f .depend `find . -name .depend -print`

#执行distclean,需要依赖mrproper
distclean: mrproper

#备份backup命令,依赖mrproper
backup: mrproper
    cd .. && tar cf - linux | gzip -9 > backup.gz
    sync
#cc -M main.c
depend dep:
    touch tools/version.h
    for i in init/*.c;do echo -n "init/";$(CPP) -M $$i;done > .tmpdepend
    for i in tools/*.c;do echo -n "tools/";$(CPP) -M $$i;done >> .tmpdepend
    set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i dep; done
    rm -f tools/version.h
    mv .tmpdepend .depend

#当我们用“@”字符在命令行前,那么,这个命令将不被make显示出来
#如果定义了CONFIGURATION这个宏,意味着有些文件不存在,需要执行make" $(CONFIGURATION)"生成
#必要的文件
ifdef CONFIGURATION
..$(CONFIGURATION):
    @echo
    @echo "You have a bad or nonexistent" .$(CONFIGURATION) ": running 'make" $(CONFIGURATION)"'"
    @echo
    $(MAKE) $(CONFIGURATION)
    @echo
    @echo "Successful. Try re-making (ignore the error that follows)"
    @echo
    exit 1

#dummy目标依赖..$(CONFIGURATION),正常时应该是空值,否则产生定义的值,则后续执行时会报错
dummy: ..$(CONFIGURATION)

else

dummy:

endif

#
# Leave these dummy entries for now to tell people that they are going away..
#
#不支持以下命令,以相应的压缩命令代替
lilo:
    @echo
    @echo Uncompressed kernel images no longer supported. Use
    @echo "make zlilo" instead.
    @echo
    @exit 1

Image:
    @echo
    @echo Uncompressed kernel images no longer supported. Use
    @echo "make zImage" instead.
    @echo
    @exit 1

disk:
    @echo
    @echo Uncompressed kernel images no longer supported. Use
    @echo "make zdisk" instead.
    @echo
    @exit 1

原文地址:https://www.cnblogs.com/xiaofengwei/p/3741407.html