make -C M=

来源:互联网 发布:c语言~(~a^a),a|1 编辑:程序博客网 时间:2024/05/21 19:38
Makefile为,
PWD = $(shell pwd)
KERNEL_SRC = /usr/src/linux-source-2.6.15/
 
obj-m := test.o
module-objs := test.o
 
all:
       $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules
      
clean:
       rm *.ko
       rm *.o
在test.c和Makefile所在的目录下运行make,如果看到类似输出
make -C /usr/src/linux-source-2.6.15/ M=/home/vmeth modules
make[1]: Entering directory `/usr/src/linux-source-2.6.15'
  CC [M]  /home/vmeth/hello.o
  Building modules, stage 2.
  MODPOST
  CC      /home/vmeth/hello.mod.o
  LD [M]  /home/vmeth/hello.ko
make[1]: Leaving directory `/usr/src/linux-source-2.6.15'
 
一般用下面的Makefile,
# Makefile2.6
ifneq ($(KERNELRELEASE),)
#kbuild syntax. dependency relationshsip of files and target modules are listed here.
mymodule-objs := hello.o
obj-m := hello.o  
else
PWD  := $(shell pwd)
KVER ?= $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
       $(MAKE) -C $(KDIR) M=$(PWD)
clean:
       rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions
endif
KERNELRELEASE 是在内核源码的顶层Makefile中定义的一个变量,在第一次读取执行此Makefile时,KERNELRELEASE没有被定义,所以make将读取执行else之后的内容。
当make的目标为all时,-C $(KDIR) 指明跳转到内核源码目录下读取那里的Makefile;M=$(PWD) 表明然后返回到当前目录继续读入、执行当前的Makefile。
当从内核源码目录返回时,KERNELRELEASE已被被定义,kbuild也被启动去解析kbuild语法的语句,make将继续读取else之前的内容。else之前的内容为kbuild语法的语句, 指明模块源码中各文件的依赖关系,以及要生成的目标模块名。

每个内核的名字都包含了它的版本号,这也是 uname -r 命令显示的值。



新的内核模块编程中的make命令里有个M选项,如下: 
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
M=$(PWD) 表明然后返回到当前目录继续读入、执行当前的Makefile。 
请参考:
从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响

这个M是kbuild的东西呢,还是make本来自己就有的东西呢? 
按理说,它是make的一个参数,应该是make的东西,但是make的doc里又找不到, 
如果是kbuild里的东西,它应该怎样来实现呢?

M是makefile脚本中的一个变量(variable) 


# Use make M=dir to specify directory of external module to build
# Old syntax make ... SUBDIRS=$PWD is still supported
# Setting the environment variable KBUILD_EXTMOD take precedence
ifdef SUBDIRS
KBUILD_EXTMOD ?= $(SUBDIRS)
endif
ifdef M //如果没有定义或赋值M,此处M未定义(undefined)

ifeq ("$(origin M)", "command line") //如果定义了,此句用来判断M是否从命令行来

KBUILD_EXTMOD := $(M)
endif
endif



from:http://blog.sina.com.cn/s/blog_89fa41ef0100trjr.html

原创粉丝点击