Makefile variable substitution is obviously not performed, though: = is used in the declaration

I have a core kernel module that other kernel modules interact with. I structured such modules (conceptually):

main module/ | \drivers/ | |\driver1 |\driver2 \driver3 

Since these are kernel modules, I need to compile them as follows:

 make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules 

However, since the Makefile from the drivers can be called from previous directories, I need to make $(shell pwd) before calling another make (linux make). So, the Makefile now looks like this:

 CURRENT_DIR := $(shell pwd) .PHONY: all all: $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(CURRENT_DIR) modules 

So far, this is great, and it works great. The problem is this: I have a file that drivers need to include, so I have to specify the path to include. I tried it first

 EXTRA_CFLAGS += -I../.. 

and immediately realized why it does not work (the relative path will be in / lib / module / ... not in the current directory). So I changed it to:

 MAIN_MODULE_HOME := $(CURRENT_DIR)/../.. EXTRA_CFLAGS += -I$(MAIN_MODULE_HOME) 

Oddly enough, this does not work! If i write

 EXTRA_CFLAGS += -Ipath/I/get/from/pwd/../.. 

manually, it compiles! Can someone explain what I'm doing wrong? Before calling make, I echo ed $(CURRENT_DIR) and $(MAIN_MODULE_HOME) , and the variables make sense.

I know that EXTRA_CFLAGS not immediately evaluated, but since CURRENT_DIR and MAIN_MODULE_HOME declared with := , I don’t understand how things got messed up.

(If anyone can articulate a better title for the question, do it!)

+4
source share
2 answers

You should pass EXTRA_CFLAGS to do the following:

 $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(CURRENT_DIR) \ EXTRA_CFLAGS="$(EXTRA_CFLAGS)" modules 

Update:

The contents of driver1 / Makefile are read twice: first, when you run make inside the driver1 directory, and secondly, using the Kbuild system.

First, CURRENT_DIR := $(shell pwd) evaluates to /home/users/.../main module/drivers/driver1 . Secondly, Kbuild evaluates CURRENT_DIR := $(shell pwd) somehow like /usr/src/linux-headers-2.6.32-33-generic/

This situation is described in LDD3, ch2, p24

The trick is to write your makefile as follows:

 # If KERNELRELEASE is defined, we've been invoked from the # kernel build system and can use its language. ifneq ($(KERNELRELEASE),) obj-m := hello.o # Otherwise we were called directly from the command # line; invoke the kernel build system. else KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules #endif 
+7
source

This is most likely due to the recursive flavor EXTRA_CFLAGS , which actually expands into a sub-make that does not have access to the MAIN_MODULE_HOME that it refers to.

First try exporting MAIN_MODULE_HOME :

 export MAIN_MODULE_HOME 

I would also try to smooth EXTRA_CFLAGS before using it (however, I'm not sure if this is good practice for Kbuild):

 EXTRA_CFLAGS := $(EXTRA_CFLAGS) 
0
source

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


All Articles