在单独的对象目录中构建树外 Linux 内核模块 [英] Building an out-of-tree Linux kernel module in a separate object directory

查看:20
本文介绍了在单独的对象目录中构建树外 Linux 内核模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在面对 Linux 内核构建系统(Kbuild,内核 ≥2.6.28)以及更大项目的目录结构和构建系统.我们的项目包含一个 out-of-tree Linux 内核模块,我们的目录结构看起来像这样(明显简化):

I'm confronting the Linux kernel build system (Kbuild, kernel ≥2.6.28) with the directory structure and build system for a larger project. Our project contains an out-of-tree Linux kernel module, and our directory structure looks like this (simplified, obviously):

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(或在构建时选择的任何架构和调试变体)下结束.

The build process must not modify anything under checkout, and building the module must not modify anything under tmp/linux-2.6.xx. All output files must end up under output/linux-arm-debug (or whatever architecture and debug variant was selected at build time).

我已经阅读了 kbuild/modules.txt,并开始编写我的 Kbuild 文件:

I've read kbuild/modules.txt, and started to write my Kbuild file:

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.cfoo_linux.o 编译来自 .../checkout/src/linux-driver/foo_linux.c?

This handles storing the object files in a different directory from where Kbuild lives. Now how can I specify that foo_common.o needs to be compiled from …/checkout/src/common/foo_common.c and foo_linux.o from …/checkout/src/linux-driver/foo_linux.c?

推荐答案

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

Here is a Makefile which does out of source-tree builds for out of kernel-tree modules (adapted from @Mark's comment)...

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 文件...

Note: You still need a Kbuild file...

obj-m += my_driver.o

这篇关于在单独的对象目录中构建树外 Linux 内核模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆