0

I want to write a step compiler Linux kernel module driver method. But I don't know how to write the Makefile. I want to implement Makefile functionality like this:

create files: driver.i driver.S

Because I want to know how the kernel macros unfold.

driver.c

#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>

static int hello_init(void)
{
    int cnt = 0, i = 1;
    printk(KERN_ALERT "init\r\n" );
    while (cnt != i)
        cpu_relax();
    return 0;
}

static void hello_exit(void)
{
    printk(KERN_ALERT ": exit\n" );
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("Dual BSD/GPL");

Makefile

#BIN := app
WHILE_TEST := while_test_driver
LOG_FILE :=log


obj-m := $(WHILE_TEST).o
$(WHILE_TEST)-objs := driver.o

MAKEOPT := ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
export CROSS=arm-none-linux-gnueabi-

ARM_LINUX_KERNEL := /home/zhangbh/linux-4.14.139

PWD = $(shell pwd)
ccflags-y :=-g

all:
    $(info "while test driver")
    $(MAKE) $(MAKEOPT) -C $(ARM_LINUX_KERNEL) M=$(PWD) modules

objdump:
    $(CROSS)objdump -S $(WHILE_TEST).o > $(LOG_FILE).txt
    $(CROSS)objdump -DfhS $(WHILE_TEST).o > $(LOG_FILE)1.txt
    $(CROSS)objdump --debugging $(WHILE_TEST).o > debugging.txt

.PHONY: clean

clean:
    rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions Module.symvers .Makefile.swp modules.order
    rm -rf $(BIN)
    rm -rf $(LOG_FILE).txt $(LOG_FILE)1.txt
0andriy
  • 4,183
  • 1
  • 24
  • 37
sky
  • 1
  • So, you are able to build the `.ko` file, but doesn't know how to build `.i` and `.S` files for your module? You could build your module with additional `V=1` passed to "inner" `make` (which is called with `-C`, `M=` and other options). That way you could see exact command line which compiles your source file. Enter this command line but modify it to create required files instead of regular `.o`. – Tsyvarev Jun 29 '22 at 00:19
  • Yes,I was already able to buid the driver.ko, but I don't know how to buid .i by -E parameters. So how to edit the Makefile? – sky Jun 29 '22 at 07:59

0 Answers0