2

I have a kernel module I'd like to build with any of make, make debug, make test, where the only difference between each one is a -D option to the compiler. This is essentially the same question as Creating a debug target in Linux 2.6 driver module makefile, but that one was marked as answered, and my question remains, after trying a few other things as well.

I've tried the deprecated EXTRA_CFLAGS option in my makefile:

debug:
    $(MAKE) -C $(KDIR) M=$(PWD) EXTRA_CFLAGS="-DDEBUG" modules

as well as the newer ccflags-y option (doesn't seem to work even outside of the debug target):

ccflags-y := -DDEBUG
debug:
    $(MAKE) -C $(KDIR) M=$(PWD) modules

and setting CFLAGS just before the debug target:

debug: CFLAGS_main.o=-DDEBUG
debug:
    $(MAKE) -C $(KDIR) M=$(PWD) modules

but the only way I've found to accomplish what I want is with a separate build script:

#!/bin/sh

case "$1" in
    debug)
        make CFLAGS_main.o=-DDEBUG
        ;;  
    *)  
        make
        ;;  
esac

Is there no way to do this directly in the makefile when building a kernel module??

Roger Dueck
  • 615
  • 7
  • 16

1 Answers1

4

You should be able to use your original version that used EXTRA_CFLAGS, but just replace EXTRA_CFLAGS with ccflags-y:

debug:
        $(MAKE) -C $(KDIR) M=$(PWD) ccflags-y="-DDEBUG" modules

or replace it with CFLAGS_main.o to apply the CFLAGS to a single object:

debug:
        $(MAKE) -C $(KDIR) M=$(PWD) CFLAGS_main.o="-DDEBUG" modules

EDIT

As mentioned by the OP Roger Dueck, setting variables on the make command line has a global effect. It overrides any setting of the same variables within the makefiles which may be undesirable, especially for a globally used variable such as ccflags-y. To avoid this, use your own makefile variable. In the "normal" part of the Makefile that invokes $(MAKE) on the "KBuild" part, change the debug: target to the following, using a custom variable of your choice (I used FOO_CFLAGS here):

debug:
    $(MAKE) -C $(KDIR) M=$(PWD) FOO_CFLAGS="-DDEBUG" modules

In the "KBuild" part of the Makefile invoked by the above rule, use the following to append the custom CFLAGS from FOO_CFLAGS to ccflags-y:

ccflags-y += $(FOO_CFLAGS)
Ian Abbott
  • 15,083
  • 19
  • 33
  • The first solution with `ccflags-y` is something I had tried, but it doesn't work for me - does it for you? To test, I use `$(MAKE) -n ...` and then run `make debug 2>&1 | grep DEBUG`. – Roger Dueck Jan 10 '19 at 21:29
  • The second works! Two things I'm not satisfied with, though: (1) I can't apply this globally to all sources without using `CFLAGS` (which may already have other options), and have to explicitly specify `main.o`, and (2) this clobbers existing options in `CFLAGS_main.o` (I also tried `CFLAGS_main.o+="-DDEBUG"`, but it still overwrites instead of appending). I'll wait a bit to see if there's a better answer out there before accepting this one. – Roger Dueck Jan 10 '19 at 21:34
  • 1
    I tried `ccflags-y` on several kernels from 3.16 onwards and it worked, but as you point out above, any changes to variables set on the `make` command line will be ignored (unless the makefile uses the `override` command to change it). Please see my edited answer for a method using a custom variable of your choosing to pass the extra CFLAGS. – Ian Abbott Jan 11 '19 at 13:06
  • Digging into the "KBuild" makefile isn't so simple (the one I have doesn't include `ccflags-y` or `EXTRA_CFLAGS` anywhere), but looks like the only way to make everything work perfectly. Happily, I can carry on with your first answer, with `CFLAGS_main.o="-DDEBUG"` for now. Thanks! – Roger Dueck Jan 17 '19 at 16:27