1.背景
在集成KVM的虚拟化平台上,为实现host主机和guest VM之间数据通信需要创建共享内存,其驱动的添加方式可参考Add device driver (uio_ivshmem.ko) on guest。由于才接触makefile,不懂的地方在此记录,如果理解有误,希望大佬指正。
2.编译生成uio_ivshmem.ko的makefile文件
# obj-m is a list of what kernel modules to build. The .o and other
# objects will be automatically built from the corresponding .c file -
# no need to list the source files explicitly.
obj-m := uio_ivshmem.o
# KDIR is the location of the kernel source. The current standard is
# to link to the associated source tree from the directory containing
# the compiled modules.
KDIR := /lib/modules/$(shell uname -r)/build
# PWD is the current working directory and the location of our module
# source files.
PWD := $(shell pwd)
# default is the default make target. The rule here says to run make
# with a working directory of the directory containing the kernel
# source and compile only the modules in the PWD (local) directory.
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
install:
cp uio_ivshmem.ko /lib/modules/$(shell uname -r)/kernel/drivers/uio/
clean:
rm -f *.ko *.o uio_ivshmem.mod.c Module.symvers
3.代码解释
(1)命令行敲入make,读取当前目录下的makefile,执行的target为:default
(2)$(MAKE) -C $(KDIR) M=$(PWD) modules 这行代码里,$(MAKE)相当于make,若去掉参数,整行代码可先看做 make modules,说明这个命令用于编译内核模块。
(3)-C参数是选择路径,加上$(KDIR),就会去执行该目录下的makefile,M 参数是编译外部模块,生成的模块不会直接加载在内核中,M="dir"则是编译的路径。
参考博文:
https://blog.youkuaiyun.com/importantg/article/details/51763551
https://blog.youkuaiyun.com/xiaowulang20082008/article/details/50586985
https://blog.youkuaiyun.com/lgjjeff/article/details/90489331