Linux Makefile modules

V550的makefile

ifneq ($(KERNELRELEASE),)

obj-$(CONFIG_XXX) := base/ resource/

else

CROSS_COMPILE ?= arm-eabi-
DEBUG ?= 0

all: android

android:
$(MAKE) -C $(KDIR) M=$(CURDIR) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) OS=android DEBUG=$(DEBUG) HW=1

clean:
$(MAKE) -C $(KDIR) M=$(CURDIR) HW=1 clean

.PHONY: all android

endif

 

首先分析一下 make -C

make -C 嵌套执行 makefile,相当于cd到subdir 然后再make

cd subdir && $(MAKE) 《--》 $(MAKE) -C subdir

 

然后分析一下 make M=

M是自己指定的一个参数 和make没有关系 在linux顶层makefile中:

# Use make M=dir or set the environment variable KBUILD_EXTMOD to specify the

# directory of external module to build. Setting M= takes precedence.
ifeq ("$(origin M)", "command line")
KBUILD_EXTMOD := $(M)
endif

 

接下来分析编译过程:

结合之前gpio driver模块编译的makefile(V550的makefile需要修改一下)

#######################

KERNEL_DIR := /home/zhang/work/linux-4.14.55
CURRENT_PATH = $(shell pwd)

obj-m = leddts_gpio_driver.o

build:kernelmodule

kernelmodule:
$(MAKE) -C $(KERNEL_DIR) M=$(CURRENT_PATH) modules
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURRENT_PATH) clean

#######################

 

make -C /home/zhang/work/linux-4.14.55 M=当前路径 modules

然后就是跳转到/home/zhang/work/linux-4.14.55下的makefile 目标是modules

else # KBUILD_EXTMOD

modules: descend $(objtree)/Module.symvers
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost

 

descend: $(build-dirs)         #build-dirs := $(KBUILD_EXTMOD)    KBUILD_EXTMOD := $(M)
$(build-dirs): prepare
$(Q)$(MAKE) $(build)=$@ single-build=$(single-build) need-builtin=1 need-modorder=1

==》 make -f scripts/Makefile.build obj=M指定的路径 single-build=$(single-build) need-builtin=1 need-modorder=1

其中:

scripts/Makefile.build 没有指定目标,所以使用默认目标__build

__build:

__build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
$(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
$(subdir-ym) $(always)
@:

__build依赖于obj-m

 

# The filename Kbuild has precedence over Makefile
kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))     #src := $(obj)  obj=M指定的路径
kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile)
include $(kbuild-file)

包含M指定的路径下的Makefile

M指定的路径下的Makefile  obj-m = leddts_gpio_driver.o

 

 

如果模块还包含子文件夹

比如随便找一个模块

obj-$(CONFIG_TEGRA_HOST1X) += host1x/
obj-y += drm/ vga/
obj-$(CONFIG_IMX_IPUV3_CORE) += ipu-v3/

目录下包含host1x等四个文件夹,host1x的makefile,用host1x-y表示host1x.o由哪些组成

host1x-y = \
bus.o \
。。。

obj-$(CONFIG_TEGRA_HOST1X) += host1x.o

 

Linux Makefile modules

上一篇:cache,persist以及checkpoint


下一篇:浅谈Eclipse中maven的搭建