'make tries to find include file under a directory specified with -C option in a previous rule. why?

Makefile :

export KDIR:=linux-5.4.188

all:
    make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KDIR) M=$(PWD) modules
    cp axpu_ldd_kc.ko /srv/tftp/.

clean:
    make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KDIR) M=$(PWD) clean
    rm -f axpu_ldd_kc.ko

%: %.c
    $(CROSS_COMPILE)gcc -fmax-errors=2 $^ -o $@

include Makefile.qemu

Makefile.qemu :

q_kernel_g :
    ${QEMU} -machine ab21q,gic-version=max,secure=off,virtualization=true -cpu max -kernel ${LINUX_DIR}/arch/arm64/boot/Image -m 2G -nographic -netdev user,id=vnet,hostfwd=:127.0.0.1:0-:22,tftp=/srv/tftp -device virtio-net-pci,netdev=vnet -machine iommu=smmuv3 --append "root=/dev/ram init=/init nokaslr earlycon ip=dhcp hugepages=16" -s -S

g_kernel :
    pid=`ps aux | grep qemu-system | awk 'NR==1{print $2}'`
    aarch64-none-elf-gdb linux-5.4.188/vmlinux $pid -x gdb_script

When I do make, it gives me this error.

make[1]: Entering directory '/home/ckim/ProjX/QEMU/qemu_test/test_ldd_540/linux-5.4.188'
/home/ckim/ProjX/QEMU/qemu_test/test_ldd_540/Makefile:35: Makefile.qemu: No such file or directory
make[2]: *** No rule to make target 'Makefile.qemu'.  Stop.

I don't know why make tries to find Makefile.qemu under linux-5.4.188 directory. If I copy Makefile.qemu to under linux-5.4.188, everything works fine. To make target all, it changes to directory linux-5.4.188, but why does it expect Makefile.qemu to be under there?



Solution 1:[1]

I believe that Linux kernel Makefile will include your Makefile, and since this is done from a different directory, hence the error. You can separate the part that is only relevant for a regular Makefile call and not for kernel build, as described in documentation:

ifeq ($(KERNELRELEASE),)
include Makefile.qemu
endif

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 raspy