Skip to content

Commit a0f97e0

Browse files
author
Sam Ravnborg
committed
kbuild: enable 'make CFLAGS=...' to add additional options to CC
The variable CFLAGS is a wellknown variable and the usage by kbuild may result in unexpected behaviour. On top of that several people over time has asked for a way to pass in additional flags to gcc. This patch replace use of CFLAGS with KBUILD_CFLAGS all over the tree and enabling one to use: make CFLAGS=... to specify additional gcc commandline options. One usecase is when trying to find gcc bugs but other use cases has been requested too. Patch was tested on following architectures: alpha, arm, i386, x86_64, mips, sparc, sparc64, ia64, m68k Test was simple to do a defconfig build, apply the patch and check that nothing got rebuild. Signed-off-by: Sam Ravnborg <[email protected]>
1 parent 9a39e27 commit a0f97e0

File tree

46 files changed

+137
-136
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+137
-136
lines changed

Documentation/kbuild/makefiles.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ more details, with real examples.
294294

295295

296296
This variable is necessary because the top Makefile owns the
297-
variable $(CFLAGS) and uses it for compilation flags for the
297+
variable $(KBUILD_CFLAGS) and uses it for compilation flags for the
298298
entire tree.
299299

300300
$(EXTRA_AFLAGS) is a similar string for per-directory options
@@ -437,6 +437,7 @@ more details, with real examples.
437437
-march=pentium-mmx if supported by $(CC), otherwise -march=i586.
438438
The second argument to cc-option is optional, and if omitted,
439439
cflags-y will be assigned no value if first option is not supported.
440+
Note: cc-option uses KBUILD_CFLAGS for $(CC) options
440441

441442
cc-option-yn
442443
cc-option-yn is used to check if gcc supports a given option
@@ -452,6 +453,7 @@ more details, with real examples.
452453
option. When $(biarch) equals 'y', the expanded variables $(aflags-y)
453454
and $(cflags-y) will be assigned the values -a32 and -m32,
454455
respectively.
456+
Note: cc-option-yn uses KBUILD_CFLAGS for $(CC) options
455457

456458
cc-option-align
457459
gcc versions >= 3.0 changed the type of options used to specify
@@ -463,10 +465,11 @@ more details, with real examples.
463465
cc-option-align = -falign
464466

465467
Example:
466-
CFLAGS += $(cc-option-align)-functions=4
468+
KBUILD_CFLAGS += $(cc-option-align)-functions=4
467469

468470
In the above example, the option -falign-functions=4 is used for
469471
gcc >= 3.00. For gcc < 3.00, -malign-functions=4 is used.
472+
Note: cc-option-align uses KBUILD_CFLAGS for $(CC) options
470473

471474
cc-version
472475
cc-version returns a numerical version of the $(CC) compiler version.
@@ -825,17 +828,17 @@ When kbuild executes, the following steps are followed (roughly):
825828
#arch/sparc64/Makefile
826829
AFLAGS += -m64 -mcpu=ultrasparc
827830

828-
CFLAGS $(CC) compiler flags
831+
KBUILD_CFLAGS $(CC) compiler flags
829832

830833
Default value - see top level Makefile
831834
Append or modify as required per architecture.
832835

833-
Often, the CFLAGS variable depends on the configuration.
836+
Often, the KBUILD_CFLAGS variable depends on the configuration.
834837

835838
Example:
836839
#arch/i386/Makefile
837840
cflags-$(CONFIG_M386) += -march=i386
838-
CFLAGS += $(cflags-y)
841+
KBUILD_CFLAGS += $(cflags-y)
839842

840843
Many arch Makefiles dynamically run the target C compiler to
841844
probe supported options:
@@ -847,7 +850,7 @@ When kbuild executes, the following steps are followed (roughly):
847850
-march=pentium2,-march=i686)
848851
...
849852
# Disable unit-at-a-time mode ...
850-
CFLAGS += $(call cc-option,-fno-unit-at-a-time)
853+
KBUILD_CFLAGS += $(call cc-option,-fno-unit-at-a-time)
851854
...
852855

853856

Makefile

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ LINUXINCLUDE := -Iinclude \
320320

321321
CPPFLAGS := -D__KERNEL__ $(LINUXINCLUDE)
322322

323-
CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
323+
KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
324324
-fno-strict-aliasing -fno-common \
325325
-Werror-implicit-function-declaration
326326
AFLAGS := -D__ASSEMBLY__
@@ -335,7 +335,7 @@ export CPP AR NM STRIP OBJCOPY OBJDUMP MAKE AWK GENKSYMS PERL UTS_MACHINE
335335
export HOSTCXX HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS
336336

337337
export CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS LDFLAGS
338-
export CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
338+
export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
339339
export AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
340340

341341
# When compiling out-of-tree modules, put MODVERDIR in the module
@@ -492,36 +492,36 @@ endif # $(dot-config)
492492
all: vmlinux
493493

494494
ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
495-
CFLAGS += -Os
495+
KBUILD_CFLAGS += -Os
496496
else
497-
CFLAGS += -O2
497+
KBUILD_CFLAGS += -O2
498498
endif
499499

500500
include $(srctree)/arch/$(ARCH)/Makefile
501501

502502
ifdef CONFIG_FRAME_POINTER
503-
CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
503+
KBUILD_CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
504504
else
505-
CFLAGS += -fomit-frame-pointer
505+
KBUILD_CFLAGS += -fomit-frame-pointer
506506
endif
507507

508508
ifdef CONFIG_DEBUG_INFO
509-
CFLAGS += -g
509+
KBUILD_CFLAGS += -g
510510
AFLAGS += -gdwarf-2
511511
endif
512512

513513
# Force gcc to behave correct even for buggy distributions
514-
CFLAGS += $(call cc-option, -fno-stack-protector)
514+
KBUILD_CFLAGS += $(call cc-option, -fno-stack-protector)
515515

516516
# arch Makefile may override CC so keep this after arch Makefile is included
517517
NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
518518
CHECKFLAGS += $(NOSTDINC_FLAGS)
519519

520520
# warn about C99 declaration after statement
521-
CFLAGS += $(call cc-option,-Wdeclaration-after-statement,)
521+
KBUILD_CFLAGS += $(call cc-option,-Wdeclaration-after-statement,)
522522

523523
# disable pointer signed / unsigned warnings in gcc 4.0
524-
CFLAGS += $(call cc-option,-Wno-pointer-sign,)
524+
KBUILD_CFLAGS += $(call cc-option,-Wno-pointer-sign,)
525525

526526
# Use --build-id when available.
527527
LDFLAGS_BUILD_ID = $(patsubst -Wl$(comma)%,%,\

arch/alpha/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ endif
8484
# For TSUNAMI, we must have the assembler not emulate our instructions.
8585
# The same is true for IRONGATE, POLARIS, PYXIS.
8686
# BWX is most important, but we don't really want any emulation ever.
87-
CFLAGS += $(cflags-y) -Wa,-mev6
87+
KBUILD_CFLAGS += $(cflags-y) -Wa,-mev6
8888

8989
head-y := arch/alpha/kernel/head.o
9090

arch/alpha/kernel/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44

55
extra-y := head.o vmlinux.lds
6-
EXTRA_AFLAGS := $(CFLAGS)
6+
EXTRA_AFLAGS := $(KBUILD_CFLAGS)
77
EXTRA_CFLAGS := -Werror -Wno-sign-compare
88

99
obj-y := entry.o traps.o process.o init_task.o osf_sys.o irq.o \

arch/alpha/lib/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Makefile for alpha-specific library files..
33
#
44

5-
EXTRA_AFLAGS := $(CFLAGS)
5+
EXTRA_AFLAGS := $(KBUILD_CFLAGS)
66
EXTRA_CFLAGS := -Werror
77

88
# Many of these routines have implementations tuned for ev6.

arch/arm/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ LDFLAGS_vmlinux :=-p --no-undefined -X
1414
CPPFLAGS_vmlinux.lds = -DTEXT_OFFSET=$(TEXT_OFFSET)
1515
OBJCOPYFLAGS :=-O binary -R .note -R .comment -S
1616
GZFLAGS :=-9
17-
#CFLAGS +=-pipe
17+
#KBUILD_CFLAGS +=-pipe
1818
# Explicitly specifiy 32-bit ARM ISA since toolchain default can be -mthumb:
19-
CFLAGS +=$(call cc-option,-marm,)
19+
KBUILD_CFLAGS +=$(call cc-option,-marm,)
2020

2121
# Do not use arch/arm/defconfig - it's always outdated.
2222
# Select a platform tht is kept up-to-date
@@ -28,7 +28,7 @@ MMUEXT := -nommu
2828
endif
2929

3030
ifeq ($(CONFIG_FRAME_POINTER),y)
31-
CFLAGS +=-fno-omit-frame-pointer -mapcs -mno-sched-prolog
31+
KBUILD_CFLAGS +=-fno-omit-frame-pointer -mapcs -mno-sched-prolog
3232
endif
3333

3434
ifeq ($(CONFIG_CPU_BIG_ENDIAN),y)
@@ -85,7 +85,7 @@ CFLAGS_ABI :=$(call cc-option,-mapcs-32,-mabi=apcs-gnu) $(call cc-option,-mno-th
8585
endif
8686

8787
# Need -Uarm for gcc < 3.x
88-
CFLAGS +=$(CFLAGS_ABI) $(arch-y) $(tune-y) $(call cc-option,-mshort-load-bytes,$(call cc-option,-malignment-traps,)) -msoft-float -Uarm
88+
KBUILD_CFLAGS +=$(CFLAGS_ABI) $(arch-y) $(tune-y) $(call cc-option,-mshort-load-bytes,$(call cc-option,-malignment-traps,)) -msoft-float -Uarm
8989
AFLAGS +=$(CFLAGS_ABI) $(arch-y) $(tune-y) -msoft-float
9090

9191
CHECKFLAGS += -D__arm__

arch/arm/boot/compressed/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ ifneq ($(PARAMS_PHYS),)
8787
LDFLAGS_vmlinux += --defsym params_phys=$(PARAMS_PHYS)
8888
endif
8989
LDFLAGS_vmlinux += -p --no-undefined -X \
90-
$(shell $(CC) $(CFLAGS) --print-libgcc-file-name) -T
90+
$(shell $(CC) $(KBUILD_CFLAGS) --print-libgcc-file-name) -T
9191

9292
# Don't allow any static data in misc.o, which
9393
# would otherwise mess up our GOT table

arch/avr32/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ all: uImage vmlinux.elf
1111

1212
KBUILD_DEFCONFIG := atstk1002_defconfig
1313

14-
CFLAGS += -pipe -fno-builtin -mno-pic
14+
KBUILD_CFLAGS += -pipe -fno-builtin -mno-pic
1515
AFLAGS += -mrelax -mno-pic
1616
CFLAGS_MODULE += -mno-relax
1717
LDFLAGS_vmlinux += --relax
1818

1919
cpuflags-$(CONFIG_CPU_AT32AP7000) += -mcpu=ap7000
2020

21-
CFLAGS += $(cpuflags-y)
21+
KBUILD_CFLAGS += $(cpuflags-y)
2222
AFLAGS += $(cpuflags-y)
2323

2424
CHECKFLAGS += -D__avr32__ -D__BIG_ENDIAN

arch/blackfin/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ rev-$(CONFIG_BF_REV_0_5) := 0.5
5353
rev-$(CONFIG_BF_REV_NONE) := none
5454
rev-$(CONFIG_BF_REV_ANY) := any
5555

56-
CFLAGS += -mcpu=$(cpu-y)-$(rev-y)
56+
KBUILD_CFLAGS += -mcpu=$(cpu-y)-$(rev-y)
5757
AFLAGS += -mcpu=$(cpu-y)-$(rev-y)
5858

5959
head-y := arch/$(ARCH)/mach-$(MACHINE)/head.o arch/$(ARCH)/kernel/init_task.o

arch/cris/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ OBJCOPYFLAGS := -O binary -R .note -R .comment -S
3131
CPPFLAGS_vmlinux.lds = -DDRAM_VIRTUAL_BASE=0x$(CONFIG_ETRAX_DRAM_VIRTUAL_BASE)
3232
AFLAGS += -mlinux
3333

34-
CFLAGS := $(CFLAGS) -mlinux -march=$(arch-y) -pipe
34+
KBUILD_CFLAGS += -mlinux -march=$(arch-y) -pipe
3535

3636
ifdef CONFIG_FRAME_POINTER
37-
CFLAGS := $(subst -fomit-frame-pointer,,$(CFLAGS)) -g
38-
CFLAGS += -fno-omit-frame-pointer
37+
KBUILD_CFLAGS := $(subst -fomit-frame-pointer,,$(KBUILD_CFLAGS)) -g
38+
KBUILD_CFLAGS += -fno-omit-frame-pointer
3939
endif
4040

4141
head-y := arch/$(ARCH)/$(SARCH)/kernel/head.o
4242

43-
LIBGCC = $(shell $(CC) $(CFLAGS) -print-file-name=libgcc.a)
43+
LIBGCC = $(shell $(CC) $(KBUILD_CFLAGS) -print-file-name=libgcc.a)
4444

4545
core-y += arch/$(ARCH)/kernel/ arch/$(ARCH)/mm/
4646
core-y += arch/$(ARCH)/$(SARCH)/kernel/ arch/$(ARCH)/$(SARCH)/mm/

arch/frv/Makefile

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,39 @@ endif
3939
ARCHMODFLAGS += -G0 -mlong-calls
4040

4141
ifdef CONFIG_GPREL_DATA_8
42-
CFLAGS += -G8
42+
KBUILD_CFLAGS += -G8
4343
else
4444
ifdef CONFIG_GPREL_DATA_4
45-
CFLAGS += -G4
45+
KBUILD_CFLAGS += -G4
4646
else
4747
ifdef CONFIG_GPREL_DATA_NONE
48-
CFLAGS += -G0
48+
KBUILD_CFLAGS += -G0
4949
endif
5050
endif
5151
endif
5252

5353
#LDFLAGS_vmlinux := -Map linkmap.txt
5454

5555
ifdef CONFIG_GC_SECTIONS
56-
CFLAGS += -ffunction-sections -fdata-sections
56+
KBUILD_CFLAGS += -ffunction-sections -fdata-sections
5757
LINKFLAGS += --gc-sections
5858
endif
5959

6060
ifndef CONFIG_FRAME_POINTER
61-
CFLAGS += -mno-linked-fp
61+
KBUILD_CFLAGS += -mno-linked-fp
6262
endif
6363

6464
ifdef CONFIG_CPU_FR451_COMPILE
65-
CFLAGS += -mcpu=fr450
65+
KBUILD_CFLAGS += -mcpu=fr450
6666
AFLAGS += -mcpu=fr450
6767
ASFLAGS += -mcpu=fr450
6868
else
6969
ifdef CONFIG_CPU_FR551_COMPILE
70-
CFLAGS += -mcpu=fr550
70+
KBUILD_CFLAGS += -mcpu=fr550
7171
AFLAGS += -mcpu=fr550
7272
ASFLAGS += -mcpu=fr550
7373
else
74-
CFLAGS += -mcpu=fr400
74+
KBUILD_CFLAGS += -mcpu=fr400
7575
AFLAGS += -mcpu=fr400
7676
ASFLAGS += -mcpu=fr400
7777
endif
@@ -80,15 +80,15 @@ endif
8080
# pretend the kernel is going to run on an FR400 with no media-fp unit
8181
# - reserve CC3 for use with atomic ops
8282
# - all the extra registers are dealt with only at context switch time
83-
CFLAGS += -mno-fdpic -mgpr-32 -msoft-float -mno-media
84-
CFLAGS += -ffixed-fcc3 -ffixed-cc3 -ffixed-gr15 -ffixed-icc2
83+
KBUILD_CFLAGS += -mno-fdpic -mgpr-32 -msoft-float -mno-media
84+
KBUILD_CFLAGS += -ffixed-fcc3 -ffixed-cc3 -ffixed-gr15 -ffixed-icc2
8585
AFLAGS += -mno-fdpic
8686
ASFLAGS += -mno-fdpic
8787

8888
# make sure the .S files get compiled with debug info
8989
# and disable optimisations that are unhelpful whilst debugging
9090
ifdef CONFIG_DEBUG_INFO
91-
#CFLAGS += -O1
91+
#KBUILD_CFLAGS += -O1
9292
AFLAGS += -Wa,--gdwarf2
9393
ASFLAGS += -Wa,--gdwarf2
9494
endif

arch/h8300/Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ ldflags-$(CONFIG_CPU_H8300H) := -mh8300helf
3030
cflags-$(CONFIG_CPU_H8S) := -ms
3131
ldflags-$(CONFIG_CPU_H8S) := -mh8300self
3232

33-
CFLAGS += $(cflags-y)
34-
CFLAGS += -mint32 -fno-builtin
35-
CFLAGS += -g
36-
CFLAGS += -D__linux__
37-
CFLAGS += -DUTS_SYSNAME=\"uClinux\"
33+
KBUILD_CFLAGS += $(cflags-y)
34+
KBUILD_CFLAGS += -mint32 -fno-builtin
35+
KBUILD_CFLAGS += -g
36+
KBUILD_CFLAGS += -D__linux__
37+
KBUILD_CFLAGS += -DUTS_SYSNAME=\"uClinux\"
3838
AFLAGS += -DPLATFORM=$(PLATFORM) -DMODEL=$(MODEL) $(cflags-y)
3939
LDFLAGS += $(ldflags-y)
4040

4141
CROSS_COMPILE = h8300-elf-
42-
LIBGCC := $(shell $(CROSS-COMPILE)$(CC) $(CFLAGS) -print-libgcc-file-name)
42+
LIBGCC := $(shell $(CROSS-COMPILE)$(CC) $(KBUILD_CFLAGS) -print-libgcc-file-name)
4343

4444
head-y := arch/$(ARCH)/platform/$(PLATFORM)/$(BOARD)/crt0_$(MODEL).o
4545

arch/i386/Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ LDFLAGS_vmlinux := --emit-relocs
3434
endif
3535
CHECKFLAGS += -D__i386__
3636

37-
CFLAGS += -pipe -msoft-float -mregparm=3 -freg-struct-return
37+
KBUILD_CFLAGS += -pipe -msoft-float -mregparm=3 -freg-struct-return
3838

3939
# prevent gcc from keeping the stack 16 byte aligned
40-
CFLAGS += $(call cc-option,-mpreferred-stack-boundary=2)
40+
KBUILD_CFLAGS += $(call cc-option,-mpreferred-stack-boundary=2)
4141

4242
# CPU-specific tuning. Anything which can be shared with UML should go here.
4343
include $(srctree)/arch/i386/Makefile.cpu
@@ -51,7 +51,7 @@ cflags-y += -maccumulate-outgoing-args
5151

5252
# Disable unit-at-a-time mode on pre-gcc-4.0 compilers, it makes gcc use
5353
# a lot more stack due to the lack of sharing of stacklots:
54-
CFLAGS += $(shell if [ $(call cc-version) -lt 0400 ] ; then echo $(call cc-option,-fno-unit-at-a-time); fi ;)
54+
KBUILD_CFLAGS += $(shell if [ $(call cc-version) -lt 0400 ] ; then echo $(call cc-option,-fno-unit-at-a-time); fi ;)
5555

5656
# do binutils support CFI?
5757
cflags-y += $(call as-instr,.cfi_startproc\n.cfi_rel_offset esp${comma}0\n.cfi_endproc,-DCONFIG_AS_CFI=1,)
@@ -61,7 +61,7 @@ AFLAGS += $(call as-instr,.cfi_startproc\n.cfi_rel_offset esp${comma}0\n.cfi_end
6161
cflags-y += $(call as-instr,.cfi_startproc\n.cfi_signal_frame\n.cfi_endproc,-DCONFIG_AS_CFI_SIGNAL_FRAME=1,)
6262
AFLAGS += $(call as-instr,.cfi_startproc\n.cfi_signal_frame\n.cfi_endproc,-DCONFIG_AS_CFI_SIGNAL_FRAME=1,)
6363

64-
CFLAGS += $(cflags-y)
64+
KBUILD_CFLAGS += $(cflags-y)
6565

6666
# Default subarch .c files
6767
mcore-y := arch/x86/mach-default
@@ -116,7 +116,7 @@ drivers-$(CONFIG_OPROFILE) += arch/x86/oprofile/
116116
drivers-$(CONFIG_PM) += arch/x86/power/
117117
drivers-$(CONFIG_FB) += arch/x86/video/
118118

119-
CFLAGS += $(mflags-y)
119+
KBUILD_CFLAGS += $(mflags-y)
120120
AFLAGS += $(mflags-y)
121121

122122
boot := arch/x86/boot

arch/ia64/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ ifeq ($(call cc-version),0304)
4444
cflags-$(CONFIG_MCKINLEY) += -mtune=mckinley
4545
endif
4646

47-
CFLAGS += $(cflags-y)
47+
KBUILD_CFLAGS += $(cflags-y)
4848
head-y := arch/ia64/kernel/head.o arch/ia64/kernel/init_task.o
4949

5050
libs-y += arch/ia64/lib/

0 commit comments

Comments
 (0)