I'm new in writing Linux device driver, and I'm wondering how the kernel Makefile
magically knows what to compile. To illustrate what I don't understand, consider the following case:
I did a #include <linux/irq.h>
in my driver code and I'm able to find the header file irq.h
in the kernel directory KDIR/include/linux
. However, this is only the header file, so I thought the irq.c
source code must be out there somewhere. Hence, I looked into the KDIR/arch/arm
searching for irq.c
(since I'm using the ARM architecture). My confusion begins here when I found really many irq.c
inside KDIR/arch/arm
. To simply list a few, I got:
In my Makefile
, I have a line like this:
$(MAKE) -C $(KDIR) M=$(PWD) CROSS_COMPILE=arm-none-linux-gnueabi- ARCH=arm modules
So I understand that the kernel Makefile
knows that I'm using the ARM architecture, but under KDIR/arch/arm/
, there are so many irq.c
with the same name. I'm guessing that the mach-davinci/irq.c
is compiled since davinci is the cpu name I'm using. But then, how can the kernel Makefile
knows this is the one to compile? Or if I would like to have a look for the irq.c
that I'm actually using, which one should I refer to?
I believe there must be a way to know this besides reading the long kernel Makefile
. Thanks for any help!
Beyond the ARCH variable, you can also choose the system type (mach) from the configuration menu (there is actually a sub-menu called "System type" when you type make menuconfig
for instance). This selection will include and compile all files under linux2.6/arch/$ARCH/mach-$MACH, and in your case this is how only one irq.c gets compiled.
That aside, it is interesting to understand how the kernel chooses which files to compile. The mechanism behind this is called Kconfig, and it is what allows you to precisely configure your kernel using make menuconfig
and others, to compile a module from the outside like you are doing, and to select the right files to compile from simple Makefiles. While it is simple to use, its internals are rather complex - but this article, although rather old, explains it rather well: