驱动模块(4)——模块编译-Makefile编写

1.Kernel中的Makefile将多个文件编译成一个模块

Example1: drivers/usb/core/Makefile:
usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o usbcore-y += devio.o notify.o generic.o quirks.o devices.o usbcore-y += port.o usbcore-$(CONFIG_OF) += of.o usbcore-$(CONFIG_USB_PCI) += hcd-pci.o usbcore-$(CONFIG_ACPI) += usb-acpi.o obj-$(CONFIG_USB) += usbcore.o obj-$(CONFIG_USB_LEDS_TRIGGER_USBPORT) += ledtrig-usbport.o
Example2:net/rfkill/Makefile
rfkill-y            += core.o
rfkill-$(CONFIG_RFKILL_INPUT)    += input.o
obj-$(CONFIG_RFKILL)        += rfkill.o
obj-$(CONFIG_RFKILL_GPIO)    += rfkill-gpio.o

只有obj-y += xxxx.o 才会被编译成xxxx.ko文件

 

2.使用Yocto内核模块编译方法(多文件)

usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
usbcore-y += devio.o notify.o generic.o quirks.o devices.o
usbcore-y += port.o
usbcore-y += of.o

obj-m += usbcore.o

KDIR := /media/ubuntu/work/Yocto3_4/build/tmp/work/g6m-poky-linux/linux-renesas/4.14.35-r1/build/

all:
    make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=aarch64-poky-linux- ARCH=arm64
    rm -f *.o *.order *.mod.c *.symvers *~

clean:
    rm -f *.o *.order *.mod.c *.symvers *~ *.ko

#补充
clean:
    make -C $(KERN_DIR) M=`pwd` modules clean #可实现自动删除
    rm -rf modules.order

 

3.将单个驱动文件编译成模块

SRC := ehci-hcd
obj-m := $(SRC).o
KDIR := /media/ubuntu/work/Yocto3_4/build/tmp/work/g6m-poky-linux/linux-renesas/4.14.35-r1/build

all:
    #make -C $(KDIR) M=$(PWD) modules  ARCH=arm64(使用时这个删除并且下面给tab键)
    make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=aarch64-poky-linux- ARCH=arm64

clean:
    rm -f *.o *.order *.mod.c *.symvers *~ *.ko

 

4.Ubuntu主机模块编译

obj-m += leds_4412.o

all:
    make -C /lib/modules/`uname -r`/build M=$(PWD)
    rm -rf *.o *.o.cmd *.ko.cmd *.order *.symvers *.mod.c .tmp_versions

clean:
    rm -rf *.ko

 

5. Makefile中使用宏选择模块,和使用内核版本选择模块

ifeq ($(CONFIG_FEATURE_SELECT), y)
ifeq ($(VERSION).$(PATCHLEVEL), 5.4)
obj-y += feature_select_v1.o
endif
else
obj-y += feature_select_v2.o
endif

 

6. Makefile中编译elf测试文件

参考 samples/timers 写自己的elf文件,然后在deconfig文件中选中。

samples$ tree hello
hello
├── hello_world.c
└── Makefile

samples$ cat hello/Makefile 
# SPDX-License-Identifier: GPL-2.0
userprogs-always-y += hello_world
userccflags += -I usr/include

samples$ cat Makefile 
subdir-$(CONFIG_SAMPLE_HELLO_WORLD)    += hello

samples$ cat Kconfig
config SAMPLE_HELLO_WORLD
    bool "hello world"
    help
      only printf hello world.

 

 

 

 

 

参考:
Documentation/kbuild
Documentation/kbuild/modules.txt里面介绍了如何编译成模块。

 

posted on 2018-07-21 14:56  Hello-World3  阅读(450)  评论(0编辑  收藏  举报

导航