驱动学习笔记(零)一篇简单makefile的学习

ldd3中开始有一个makefile文件,英文注释很详细,加上简要中文注释,如下

# To build modules outside of the kernel tree, we run "make"
# in the kernel source tree; the Makefile these then includes this
# Makefile once again.
# This conditional selects whether we are being included from the
# kernel Makefile or not.
ifeq ($(KERNELRELEASE),)         #如果未定义KERNELRELEASE则说明是在内核构造系统环境外

    # Assume the source tree is where the running kernel was built
    # You should set KERNELDIR in the environment if it's elsewhere
    KERNELDIR ?= /usr/src/linux-source-2.6.38/linux-source-2.6.38   #内核路径
    # The current directory is passed to sub-makes as argument
    PWD := $(shell pwd)

modules:
 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
 rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

else
    # called from kernel build system: just declare what our modules are
    obj-m := hello.o hellop.o seq.o jit.o jiq.o sleepy.o complete.o \
             silly.o faulty.o kdatasize.o kdataalign.o

  #有一些模块需要从目标文件hello.o hellop.o seq.o jit.o jiq.o sleepy.o complete.o \
             silly.o faulty.o kdatasize.o kdataalign.o 中构造
endif

其中的PHONY,含义如下

PHONY 目标
  PHONY 目标并非实际的文件名:只是在显式请求时执行命令的名字。有两种理由需要使用PHONY 目标:避免和同名文件冲突,改善性能。
  如果编写一个规则,并不产生目标文件,则其命令在每次make 该目标时都执行。
  例如:
  clean:
  rm *.o temp
  因为"rm"命令并不产生"clean"文件,则每次执行"make clean"的时候,该命令都会执行。如果目录中出现了"clean"文件,则规则失效了:没有依赖文件,文件"clean"始终是最新的,命令永远不会执行;为避免这个问题,可使用".PHONY"指明该目标。如:
  .PHONY : clean
  这样执行"make clean"会无视"clean"文件存在与否。

  已知phony 目标并非是由其它文件生成的实际文件,make 会跳过隐含规则搜索。这就是声明phony 目标会改善性能的原因,即使你并不担心实际文件存在与否。
  完整的例子如下:
  .PHONY : clean
  clean :
  rm *.o temp
  phony 目标不应是真正目标文件的依赖。如果这样,每次make 在更新此文件时,命令都会执行。只要phony 目标不是真正目标的依赖,规则的命令只有在指定此目标时才执行。

  phony 目标可以有依赖关系。当一个目录中有多个程序,将其放在一个makefile 中会更方便。因为缺省目标是makefile 中的第一个目标,通常将这个phony 目标叫做"all",其依赖文件为各个程序:
  all : prog1 prog2 prog3
  .PHONY : all
  prog1 : prog1.o utils.o
           cc -o prog1 prog1.o utils.o
  prog2 : prog2.o
           cc -o prog2 prog2.o
  prog3 : prog3.o sort.o utils.o
           cc -o prog3 prog3.o sort.o utils.o



 

posted @ 2011-08-09 21:10  夏大王  阅读(288)  评论(0编辑  收藏  举报