一、本文说明

  本文为linux内核Makefile整体分析的续篇,是依据Linux内核Makefile体系的主要内容编写一个简要的测试工程。Linux内核Makefile体系就好像一只“大鸟”,而这篇测试算是“麻雀”,主要是为了通过动手实战进一步理解Linux内核Makefile体系的原理和特点。

二、源码结构

1、测试源码包括1个顶层目录以及5个子目录。

2、Makefile体系的构成是顶层1个Makefile文件、scripts目录中的4个脚本文件以及add、sub、main三个目录中的Makefile文件。

3、C程序源码是add/add.c、sub/sub.c、main/main.c

三、Makefile重要源码展示

1、顶层Makefile

PHONY :=
MAKE := make srctree := $(if $(KBUILD_SRC),$(KBUILD_SRC),$(CURDIR))
export srctree AS = $(CROSS_COMPILE)as
LD = $(CROSS_COMPILE)ld
CC = $(CROSS_COMPILE)gcc
CPP = $(CC) -E
AR = $(CROSS_COMPILE)ar
NM = $(CROSS_COMPILE)nm export AS LD CC CPP AR NM include $(srctree)/scripts/Kbuild.include //该文件中定义有build变量 all: target srcdir := add sub main
PHONY += $(srcdir) objdir := $(srcdir)
PHONY += $(objdir) target: $(srcdir) //总目标的依赖是srcdir
gcc -o target add/built-in.o sub/built-in.o main/built-in.o //定义总目标的生成规则 $(srcdir): //通过srcdir依次生成各个子目录中的*/built-in.o
$(MAKE) $(build)=$@ clean-dirs := $(addprefix _clean_,$(objdir)) //clean-dirs其实与objdir中的目录是一样的,只是为了执行不同的命令(clean-dirs要删除目标文件),所以要clean-dirs的前边要加上前缀以与srcdir=objdir的目标区别
PHONY += $(clean-dirs) clean $(clean-dirs):
$(MAKE) $(clean)=$(patsubst _clean_%,%,$@) clean: $(clean-dirs)
rm -f target clean := -f scripts/Makefile.clean obj .PHONY: $(PHONY)

2、srcdirs/Makefile.build

# ==========================================================================
# Building
# ========================================================================== src := $(obj) PHONY := __build
__build: //Makefile.build中的总目标 include scripts/Kbuild.include # The filename Kbuild has precedence over Makefile
kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile)
include $(kbuild-file) //包含子目录中的Makefile include scripts/Makefile.lib ifneq ($(strip $(obj-y)),)
builtin-target := $(obj)/built-in.o
endif __build: $(builtin-target) # If the list of objects to link is empty, just create an empty built-in.o
cmd_link_o_target = $(if $(strip $(obj-y)),\
$(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^)) $(builtin-target): $(obj-y)
$(call if_changed,link_o_target) //调用生成库built-in.o的函数 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< define rule_cc_o_c
$(cmd_cc_o_c)
endef # Built-in and composite module parts
$(obj)/%.o: $(src)/%.c
$(call if_changed_rule,cc_o_c) //调用生成目标文件.o的函数 PHONY += FORCE
FORCE: .PHONY: $(PHONY)

3、srcdirs/Kbuild.include

build := -f scripts/Makefile.build obj

if_changed = $(cmd_$())//定义函数if_changed                                 

# Usage: $(call if_changed_rule,foo)
# Will check if $(cmd_foo) or any of the prerequisites changed,
# and if so will execute $(rule_foo).
if_changed_rule = $(rule_$()) //定义函数if_changed_rule

4、srcdirs/Makefile.lib

c_flags        =        //定义gcc编译器选项

ld_flags       = $(LDFLAGS) $(ldflags-y) //定义ld连接器选项
obj-y := $(patsubst %/, %/built-in.o, $(obj-y)) //对obj-y中的目录添加built-in.o
obj-y := $(addprefix $(obj)/,$(obj-y))//再添加obj前缀

5、srcdirs/Makefile.clean

# ==========================================================================
# Cleaning up
# ========================================================================== src := $(obj) PHONY := __clean //此为Makefile.clean中的总目标
__clean:   
rm -f $(obj)/*.o //删除目标文件.o

6、add/Makefile

obj-y                += add.o

 四、当前源码缺陷

1、没有完成依赖的自动生成

2、没有完成生成目标的目录与源码不在同一目录的功能

附:LinuxKernelMakefileTest.zip

Linux Kernel Makefile Test的更多相关文章

  1. Linux kernel Makefile for ctags

    /********************************************************************** * Linux kernel Makefile for ...

  2. 编译android的linux kernel goldfish

    https://source.android.com/source/building-kernels.html $ export PATH=/home/hzh/oldhome/learn/androi ...

  3. Linux Kernel的Makefile与Kconfig文件的语法

    https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt Introduction ------------ The c ...

  4. Linux kernel config and makefile system

    转载自:http://blog.csdn.net/dreamxu/article/details/6125545 http://www-900.ibm.com/developerWorks/cn/li ...

  5. kernel/Makefile

    ## Makefile for the linux kernel.## Note! Dependencies are done automagically by 'make dep', which a ...

  6. Linux kernel develop -- Hello World

    hello.c: #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h&g ...

  7. Linux内核Makefile文件(翻译自内核手册)

    --译自Linux3.9.5 Kernel Makefiles(内核目录documention/kbuild/makefiles.txt) kbuild(kernel build) 内核编译器 Thi ...

  8. Linux Kernel sys_call_table、Kernel Symbols Export Table Generation Principle、Difference Between System Calls Entrance In 32bit、64bit Linux

    目录 . sys_call_table:系统调用表 . 内核符号导出表:Kernel-Symbol-Table . Linux 32bit.64bit环境下系统调用入口的异同 . Linux 32bi ...

  9. Intel 80x86 Linux Kernel Interrupt(中断)、Interrupt Priority、Interrupt nesting、Prohibit Things Whthin CPU In The Interrupt Off State

    目录 . 引言 . Linux 中断的概念 . 中断处理流程 . Linux 中断相关的源代码分析 . Linux 硬件中断 . Linux 软中断 . 中断优先级 . CPU在关中断状态下编程要注意 ...

随机推荐

  1. Web资源文件的加载

    一.关于页面跳转的一下几种方法: package com.gqx.load; import java.io.IOException; import java.io.PrintWriter; impor ...

  2. CONTROLS: <> TYPE TABLEVIEW USING SCREEN<>.在 ABAP/4 中声明表格 控制

    在 ABAP/4 中声明表格 控制 在屏幕中使 用表格控制 时,必须在 ABAP/4 程序中同时 声明表格控 制结构和表 格控制字段 . 例如: TABLES:   SFLIGHT. CONTROLS ...

  3. ibatis 开发中的经验 (一)ibatis 和hibernate 在开发中的理解

    这个项目的底层是用ibatis框架完毕,这几天也是都在用这个框架写代码,也有了一些简单的理解,把项目拿过来后基本的配置都已经配置好了,比方一些事务.日志控制等,在开发中主要用到的是写SQL语句以及熟悉 ...

  4. 如何判断Android系统的版本

    随着Android版本的增多,在不同的版本中使用不同的设计是必须的,根据程序运行的版本来提供不同的功能.这涉及到如何在程序中判断Android系统的版本. 在Android api中的android. ...

  5. iOS之ASIHttp简单的网络请求实现

    描述: ASIHttpRequest是应用第三方库的方法,利用代码快,减少代码量,提高效率 准备工作: 一.导入第三方库ASIHttpRequest 二.会报很多的错,原因有两个,一个是要导入Xcod ...

  6. Visual Studio 调试技巧 (三) -- 调试第三方组件代码

    上次我们提到,没有源代码而且没有调试符号,我们也可以 Debug.有人可能会问,我什么时候需要在这种情况下调试吗?! 是的.比方说,我们用了某些第三方的组件,这些组件里面难免也会有 Bug.就算是微软 ...

  7. hdu2025java字符题

    查找最大元素 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  8. XC图片浏览器

    XC图片浏览器,这是基于Android4.4开发的一款浏览手机里的图片的浏览器.简单美观实用.欢迎下载. 下载地址:http://download.csdn.net/detail/jczmdevelo ...

  9. linux+apache+mod_Jk+tomcat实现tomcat集群

    最近一段时间一直在研究实现apache + jk_mod + tomcat实现负载均衡,起初负载均衡算是配置蛮顺利的,但是到了配置tomcat集群时所有配置都没有问题,但是tomcat日志中一直提示没 ...

  10. Summary: How to calculate PI? Based on Monte Carlo method

    refer to: http://www.stealthcopter.com/blog/2009/09/python-calculating-pi-using-random-numbers/ Duri ...