28

Eg: a common device module's Makefile

obj-m:=jc.o

default:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules clean

I wonder if I can set gcc's CFLAGS for this specific Makefile. When I change default section to (note the addition of the -O2):

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

The change does not take effect (i.e., - the -O2 is NOT added to the CFLAGS).

Any help? Thanks a lot.

1
  • 2
    $$PWD can replace $(shell pwd) Commented Aug 28, 2013 at 3:57

3 Answers 3

35

-O2 would be an option to make (or $(MAKE), as you're using it) in what you tried. Obviously, the compiler (probably gcc) needs this flag, not make.

Kbuild understands a make variable named CFLAGS_modulename.o to add specific C flags when compiling this unit. In your case, your module object will be jc.o, so you can specify:

CFLAGS_jc.o := -O2

and it should work. Add V=1 to your $(MAKE) lines to get a verbose output and you should see -O2 when jc.c is being compiled.

You can find more about compiling modules in the official documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

what about the CFLAGS that are (potentially) set by inner "Makefile(s)" and/or the defualt kernel flags configuration/settings? Will it "collide" with the flags set for the speicifc external module?
19

You can also use

ccflags-y := -O2

This will be applied to all of the source files compiled for your module with the Makefile. This is indirectly documented in the link provided by eepp in Section 4.2

4 Comments

This will override ccflags-y setting in some inner Makefiles, and as a result will break .h file lookup.
For me, it helped to use EXTRA_CFLAGS instead. It is referenced in scripts/Makefile.lib as a thing for "backwards compatibility" and it is added to ccflags-y, thus not breaking anything.
ccflags-y += -O2 should work. It was shown in Documentation/kbuild/modules.txt.
Is there a way to do this by buildroot configuration instead of needing to modify Makefiles?
1
default:
    $(MAKE) CFLAGS_MODULE="your c flags" -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.