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

Gil*_*il' 9 makefile linux-kernel kbuild

我正在面对Linux内核构建系统(Kbuild,内核≥2.6.28)以及更大项目的目录结构和构建系统.我们的项目包含一个树外的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
Run Code Online (Sandbox Code Playgroud)

构建过程不得修改任何内容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
Run Code Online (Sandbox Code Playgroud)

这会将目标文件存储在与Kbuild生命不同的目录中.现在我如何指定foo_common.o需要编译…/checkout/src/common/foo_common.cfoo_linux.o从中编译…/checkout/src/linux-driver/foo_linux.c

Aut*_*act 6

这是一个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
Run Code Online (Sandbox Code Playgroud)

注意:您仍然需要一个Kbuild文件...

obj-m += my_driver.o
Run Code Online (Sandbox Code Playgroud)


Joe*_*sen 1

虽然您还没有提到到目前为止您已经尝试过的内容(或者您是否已经找到了解决方案),但看起来您只需要继续深入到module.txt文件中 —第 4.3 节

--- 4.3 Several Subdirectories

kbuild can handle files that are spread over several directories.
Consider the following example:

.
|__ src
|   |__ complex_main.c
|   |__ hal
|   |__ hardwareif.c
|   |__ include
|       |__ hardwareif.h
|__ include
    |__ complex.h

To build the module complex.ko, we then need the following
kbuild file:

    --> filename: Kbuild
    obj-m := complex.o
    complex-y := src/complex_main.o
    complex-y += src/hal/hardwareif.o

    ccflags-y := -I$(src)/include
    ccflags-y += -I$(src)/src/hal/include

As you can see, kbuild knows how to handle object files located
in other directories. The trick is to specify the directory
relative to the kbuild file's location. That being said, this
is NOT recommended practice.

For the header files, kbuild must be explicitly told where to
look. When kbuild executes, the current directory is always the
root of the kernel tree (the argument to "-C") and therefore an
absolute path is needed. $(src) provides the absolute path by
pointing to the directory where the currently executing kbuild
file is located.
Run Code Online (Sandbox Code Playgroud)

  • 我读过那部分,但我不知道这对我有什么帮助。在该示例中,“complex_main.c”和“complex_main.o”位于同一目录中。在我的构建树中,源代码和构建产品是完全分开的。 (4认同)