makefile - 在单独的对象目录中构建树外 Linux 内核模块

标签 makefile linux-kernel kbuild

我遇到了 Linux 内核构建系统(Kbuild,内核≥2.6.28),它具有更大项目的目录结构和构建系统。我们的项目包含一个 out-of-tree Linux 内核模块,我们的目录结构如下所示(显然是简化了):

checkout/src/common/*.c           source files (common to Linux and other platforms)
checkout/src/linux-driver/*.c     source files (for the Linux kernel driver)
checkout/build/linux/Kbuild       Kbuild
tmp/linux-2.6.xx/                 where the Linux kernel is unpacked and configured
output/linux-arm-debug/           where object files must end up

构建过程不得修改 checkout 下的任何内容,并且构建模块不得修改 tmp/linux-2.6.xx 下的任何内容.所有输出文件必须以 output/linux-arm-debug 结尾。 (或在构建时选择的任何架构和调试变体)。

我已阅读 kbuild/modules.txt ,并开始写我的Kbuild文件:
MOD_OUTPUT_DIR = ../../../output/linux-$(ARCH)-$(DEBUG)
obj-m += $(MOD_OUTPUT_DIR)/foo_mod.o
$(MOD_OUTPUT_DIR)/our_module-objs := $(MOD_OUTPUT_DIR)/foo_common.o $(MOD_OUTPUT_DIR)/foo_linux.o

这处理将目标文件存储在与 Kbuild 不同的目录中。生活。现在如何指定 foo_common.o需要从…/checkout/src/common/foo_common.c编译和 foo_linux.o来自 …/checkout/src/linux-driver/foo_linux.c ?

最佳答案

这是一个Makefile,它为内核树模块之外的源树构建(改编自@Mark的评论)......

KDIR ?= /lib/modules/$(shell uname -r)/build
BUILD_DIR ?= $(PWD)/build
BUILD_DIR_MAKEFILE ?= $(PWD)/build/Makefile

default: $(BUILD_DIR_MAKEFILE)
    make -C $(KDIR) M=$(BUILD_DIR) src=$(PWD) modules

$(BUILD_DIR):
    mkdir -p "$@"

$(BUILD_DIR_MAKEFILE): $(BUILD_DIR)
    touch "$@"

clean:
    make -C $(KDIR) M=$(BUILD_DIR) src=$(PWD) clean

注意:您仍然需要一个 Kbuild 文件...
obj-m += my_driver.o

关于makefile - 在单独的对象目录中构建树外 Linux 内核模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5718899/

相关文章:

c - 编译C代码时Makefile错误

c++ - 自动依赖生成

linux - Linux Makefile 中的目标文件寻址

c - 处理内核模块上的 `Wframe-larger-than` 警告的建议

linux - CMOS_WRITE 无法工作的可能原因是什么?

linux - 如何从 Linux 内核镜像获取 .config?

makefile - 如何编写Makefile以使用GNU Make自动检测并并行化构建?

c - Makefile 快板

c - 为什么我们需要在 block 宏周围加上括号?

c - 错误 : sleeping function called from invalid context at mm/slub. c:1719