How to properly configure KBuild Makefiles to create subfolders inside the kernel module

I have a kernel module (often compiled using CONFIG_MYMODULE = m) that is configured as follows:

mymodule/Makefile
../foo/Makefile
../foo/component1/Makefile
../foo/component2/Makefile

Currently used:

MyModule / Makefile:

mymodule-y += mod1file.o mod2file.o mod3file.o #etc
include ../foo/Makefile
mymodule-y += $(FOO_FILES)
obj-$(CONFIG_MYMODULE) += mymodule.o

../Foo/Makefile:

include component1/Makefile
include component2/Makefile

and inside each component folder I have:

../Foo/component1/Makefile

FOO_FILES += foo1file.o foo2file.o foo3file.o #etc

This is definitely not suitable for this, since everything is included directly in the mymodule / Makefile and therefore cannot set gcc flags for a specific folder.

What is the right way to organize this while still creating everything in one kernel module ? I read the kbuild / modules.txt documentation, but I did not see anything directly related, and I cannot figure out how to do this or if it is really possible.

thank


, :

"ld: foo:

MyModule/Makefile:

mymodule-y += mod1file.o mod2file.o mod3file.o #etc
mymodule-y += ../foo/
obj-$(CONFIG_MYMODULE) += mymodule.o

../Foo/Makefile

ccflags-y := -I$(src)/component1/ -I$(src)/component2/
foo-y := foo1file.o foo2file.o foo3file.o
foo-y += component1
foo-y += component2

../Foo/1/Makefile

component1-y := component1file.o component1file.o

../Foo/Component2/Makefile

component2-y := component2file.o component2file.o

, obj-y + =../foo, mymodule-y + =../foo, , , , , , , .

+4
2

.

MyModule/Makefile:

...
include ../foo/Makefile
...
# whatever rules use the folder-specific flags:
foo:
    gcc blah blah $(FOLDERFLAGS) blah

../Foo/component1/Makefile:

FOLDER_FILES := foo1file.o foo2file.o foo3file.o #etc
$(FOLDER_FILES): FOLDER_FLAGS=folder_1_flag
FOO_FILES += FOLDER_FILES

../Foo/component2/Makefile:

FOLDER_FILES := foo20file.o foo21file.o foo22file.o #etc
$(FOLDER_FILES): FOLDER_FLAGS=folder_2_flag
FOO_FILES += FOLDER_FILES

?

+1

, subdir- extra-y, ccflags . , mymodule.

mymodule/Makefile, , (, ).

obj-m ( : hello.ko, hello.o, hello.ko)

hello-objs get hello.ko, : hello.

Makefile (. CONFIG_HELLO_COMPONENT1)

, , Makefile, include, hello-objs ccflags-y ( , obj-*).

CFLAGS_$*.o (. ) cflags, , .

mymodule/Makefile:

ifeq ($(CONFIG_HELLO_COMPONENT1), y)
 ccflags-y += -I$(src)/foo/component1 -DCONFIG_HELLO_COMPONENT1
endif
ccflags-y += -I$(src)/foo/component2

obj-m += hello.o

CFLAGS_component2.o += -DTEST_CFLAGS

hello-objs = hello-1.o
ifeq ($(CONFIG_HELLO_COMPONENT1), y)
 hello-objs += foo/component1/component1.o
endif
hello-objs += foo/component2/component2.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

make make CONFIG_HELLO_COMPONENT1=y.

mymodule/hello-1.c:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#ifdef CONFIG_HELLO_COMPONENT1
#include <component1.h>
#endif
#include <component2.h>
static int __init init_hello(void) {
#ifdef CONFIG_HELLO_COMPONENT1
    component1();
#endif
    component2();
    printk(KERN_INFO "Hello world 1.\n");
    return 0;
}
static void __exit cleanup_hello(void) {
    printk(KERN_INFO "Goodbye world 1.\n");
}
module_init(init_hello);
module_exit(cleanup_hello);

mymodule/foo/component2/component2.h:

#ifndef COMPONENT2_H
#define COMPONENT2_H
void component2(void);
#endif

mymodule/foo/component2/component2.c:

#include <linux/kernel.h>
void component2(void) {
    printk(KERN_INFO "component2\n");
}

mymodule/foo/component1/component1.h:

#ifndef COMPONENT1_H
#define COMPONENT1_H
void component1(void);
#endif

mymodule/foo/component1/component1.c:

#include <linux/kernel.h>
void component1(void) {
    printk(KERN_INFO "component1\n");
}
0

Source: https://habr.com/ru/post/1657539/


All Articles