Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kbuild: document howto build external modules using several directories

Update modules.txt with info how to build external modules
with files in several directories.
The question popped up on lkml often enough to warrant this,
let's see if people read this stuff - or google hits it.

Signed-off-by: Sam Ravnborg <sam@ravnborg.org>

+40
+40
Documentation/kbuild/modules.txt
··· 18 18 === 5. Include files 19 19 --- 5.1 How to include files from the kernel include dir 20 20 --- 5.2 External modules using an include/ dir 21 + --- 5.3 External modules using several directories 21 22 === 6. Module installation 22 23 --- 6.1 INSTALL_MOD_PATH 23 24 --- 6.2 INSTALL_MOD_DIR ··· 345 344 Note that in the assignment there is no space between -I and the path. 346 345 This is a kbuild limitation: there must be no space present. 347 346 347 + --- 5.3 External modules using several directories 348 + 349 + If an external module does not follow the usual kernel style but 350 + decide to spread files over several directories then kbuild can 351 + support this too. 352 + 353 + Consider the following example: 354 + 355 + | 356 + +- src/complex_main.c 357 + | +- hal/hardwareif.c 358 + | +- hal/include/hardwareif.h 359 + +- include/complex.h 360 + 361 + To build a single module named complex.ko we then need the following 362 + kbuild file: 363 + 364 + Kbuild: 365 + obj-m := complex.o 366 + complex-y := src/complex_main.o 367 + complex-y += src/hal/hardwareif.o 368 + 369 + EXTRA_CFLAGS := -I$(src)/include 370 + EXTRA_CFLAGS += -I$(src)src/hal/include 371 + 372 + 373 + kbuild knows how to handle .o files located in another directory - 374 + although this is NOT reccommended practice. The syntax is to specify 375 + the directory relative to the directory where the Kbuild file is 376 + located. 377 + 378 + To find the .h files we have to explicitly tell kbuild where to look 379 + for the .h files. When kbuild executes current directory is always 380 + the root of the kernel tree (argument to -C) and therefore we have to 381 + tell kbuild how to find the .h files using absolute paths. 382 + $(src) will specify the absolute path to the directory where the 383 + Kbuild file are located when being build as an external module. 384 + Therefore -I$(src)/ is used to point out the directory of the Kbuild 385 + file and any additional path are just appended. 348 386 349 387 === 6. Module installation 350 388