简体   繁体   English

Linux内核模块makefile中的特殊文件编译

[英]Special file compile in Linux kernel module makefile

I have a kernel module in which I would like to include a special file. 我有一个内核模块,其中想包含一个特殊文件。 The file is an ASM file. 该文件是ASM文件。 Nothing special about that, but the file extension is also ".asm" which isn't recognized by gcc. 没什么特别的,但是文件扩展名也是“ .asm”,gcc无法识别。 I would like to avoid any renaming due to portability. 由于可移植性,我想避免任何重命名。 (It's also used in another non-GNU compiler.) (它也在另一个非GNU编译器中使用。)

Usually my makefiles look something like this: 通常我的makefile看起来像这样:

obj-m := chardev.o
chardev-objs := chardev2.o file2.o asm_test.o

all:
    make -C $(uClinux_dist_path) M=$$PWD modules

clean:
    make -C $(uClinux_dist_path) M=$$PWD clean

The asm_test is the offending one, as it's named "asm_test.asm". asm_test是令人讨厌的一项,因为它名为“ asm_test.asm”。 (It would work if named "asm_test.S".) (如果命名为“ asm_test.S”,它将起作用。)

In my user space programs I can solve this by inserting the following rule: 在我的用户空间程序中,我可以通过插入以下规则来解决此问题:

asm_test.o:
    $(CC) -c -x assembler-with-cpp asm_test.asm

But that doesn't work in kernel makefiles it seems. 但这似乎在内核makefile中不起作用。

Anyone knows how to create special file rules in kernel makefiles? 有谁知道如何在内核makefile中创建特殊的文件规则?



Btw, I'm working with a cross compiled Blackfin uClinux dist. 顺便说一句,我正在使用交叉编译的Blackfin uClinux dist。 if it makes a difference. 如果有所作为。

The standard module Makefile for kbuild runs with 2 passes: the first just re-runs make out of the kernel source tree, which uses the complex kbuild suite of Makefiles to build your module. 用于kbuild的标准模块Makefile运行2次:首先从内核源代码树中重新运行make,后者使用复杂的kbuild Makefile套件构建模块。 This second invocation uses the variable definitions out of your Makefile, but all the rules out of the kbuild world. 第二次调用使用Makefile中的变量定义,但所有规则均超出kbuild领域。

Section 3.10 (Special Rules) of the kbuild documentation https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt explains how to create these extra rules. kbuild文档https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt的 3.10节(特殊规则)介绍了如何创建这些附加规则。 kbuild defines a few extra variables for you that you need to use to expand the actual directories used. kbuild为您定义了一些额外的变量,您需要使用这些变量来扩展实际使用的目录。 In your case: 在您的情况下:

$(obj)/asm_test.o:
    $(CC) -c -x assembler-with-cpp -o $(obj)/asm_test.o $(src)/asm_test.asm

Notice that I've added an explicit output filename, to be sure that results go in the correct directory. 请注意,我已经添加了一个明确的输出文件名,以确保结果位于正确的目录中。

You don't need to rename the file, you can just copy or link to it: 您无需重命名文件,只需复制或链接到该文件即可:

asm_test.S: asm_test.asm
    ln -s asm_test.asm asm_test.S

In kernel's Makefile rules, each target added into obj-m is assumed to be a single source kernel module. 在内核的Makefile规则中,假定添加到obj-m中的每个目标都是单个源内核模块。 That is why adding assmebly object into obj-m may fail. 这就是为什么将组装对象添加到obj-m可能会失败的原因。 Kernel build rules treat each file as C source, then can not find your assembly source. 内核构建规则将每个文件视为C源,然后找不到您的程序集源。

We must tell the make system that our module is multiple sources module.In the multiple sources module, the source may be C or assembly. 我们必须告诉make系统我们的模块是多源模块。在多源模块中,源可以是C或程序集。

Here is example that defines an archive object file containing all objects, and adding this archive object file into obj-m. 这是定义包含所有对象的归档对象文件并将此归档对象文件添加到obj-m中的示例。

obj-m += modulename.o
modulename-objs += cfile1.o cfile2.o asmfile.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

This example works fine with Linux 3.6.6 Makefile rules. 此示例可与Linux 3.6.6 Makefile规则配合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM