Skip to content

Commit

Permalink
kbuild: enable 'make CFLAGS=...' to add additional options to CC
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Sam Ravnborg committed Oct 14, 2007
1 parent 9a39e27 commit a0f97e0
Show file tree
Hide file tree
Showing 46 changed files with 137 additions and 136 deletions.
15 changes: 9 additions & 6 deletions Documentation/kbuild/makefiles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ more details, with real examples.


This variable is necessary because the top Makefile owns the
variable $(CFLAGS) and uses it for compilation flags for the
variable $(KBUILD_CFLAGS) and uses it for compilation flags for the
entire tree.

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

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

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

Example:
CFLAGS += $(cc-option-align)-functions=4
KBUILD_CFLAGS += $(cc-option-align)-functions=4

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

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

CFLAGS $(CC) compiler flags
KBUILD_CFLAGS $(CC) compiler flags

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

Often, the CFLAGS variable depends on the configuration.
Often, the KBUILD_CFLAGS variable depends on the configuration.

Example:
#arch/i386/Makefile
cflags-$(CONFIG_M386) += -march=i386
CFLAGS += $(cflags-y)
KBUILD_CFLAGS += $(cflags-y)

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


Expand Down
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ LINUXINCLUDE := -Iinclude \

CPPFLAGS := -D__KERNEL__ $(LINUXINCLUDE)

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

export CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS LDFLAGS
export CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
export AFLAGS AFLAGS_KERNEL AFLAGS_MODULE

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

ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
CFLAGS += -Os
KBUILD_CFLAGS += -Os
else
CFLAGS += -O2
KBUILD_CFLAGS += -O2
endif

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

ifdef CONFIG_FRAME_POINTER
CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
KBUILD_CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
else
CFLAGS += -fomit-frame-pointer
KBUILD_CFLAGS += -fomit-frame-pointer
endif

ifdef CONFIG_DEBUG_INFO
CFLAGS += -g
KBUILD_CFLAGS += -g
AFLAGS += -gdwarf-2
endif

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

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

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

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

# Use --build-id when available.
LDFLAGS_BUILD_ID = $(patsubst -Wl$(comma)%,%,\
Expand Down
2 changes: 1 addition & 1 deletion arch/alpha/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ endif
# For TSUNAMI, we must have the assembler not emulate our instructions.
# The same is true for IRONGATE, POLARIS, PYXIS.
# BWX is most important, but we don't really want any emulation ever.
CFLAGS += $(cflags-y) -Wa,-mev6
KBUILD_CFLAGS += $(cflags-y) -Wa,-mev6

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

Expand Down
2 changes: 1 addition & 1 deletion arch/alpha/kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#

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

obj-y := entry.o traps.o process.o init_task.o osf_sys.o irq.o \
Expand Down
2 changes: 1 addition & 1 deletion arch/alpha/lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Makefile for alpha-specific library files..
#

EXTRA_AFLAGS := $(CFLAGS)
EXTRA_AFLAGS := $(KBUILD_CFLAGS)
EXTRA_CFLAGS := -Werror

# Many of these routines have implementations tuned for ev6.
Expand Down
8 changes: 4 additions & 4 deletions arch/arm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ LDFLAGS_vmlinux :=-p --no-undefined -X
CPPFLAGS_vmlinux.lds = -DTEXT_OFFSET=$(TEXT_OFFSET)
OBJCOPYFLAGS :=-O binary -R .note -R .comment -S
GZFLAGS :=-9
#CFLAGS +=-pipe
#KBUILD_CFLAGS +=-pipe
# Explicitly specifiy 32-bit ARM ISA since toolchain default can be -mthumb:
CFLAGS +=$(call cc-option,-marm,)
KBUILD_CFLAGS +=$(call cc-option,-marm,)

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

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

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

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

CHECKFLAGS += -D__arm__
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/boot/compressed/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ ifneq ($(PARAMS_PHYS),)
LDFLAGS_vmlinux += --defsym params_phys=$(PARAMS_PHYS)
endif
LDFLAGS_vmlinux += -p --no-undefined -X \
$(shell $(CC) $(CFLAGS) --print-libgcc-file-name) -T
$(shell $(CC) $(KBUILD_CFLAGS) --print-libgcc-file-name) -T

# Don't allow any static data in misc.o, which
# would otherwise mess up our GOT table
Expand Down
4 changes: 2 additions & 2 deletions arch/avr32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ all: uImage vmlinux.elf

KBUILD_DEFCONFIG := atstk1002_defconfig

CFLAGS += -pipe -fno-builtin -mno-pic
KBUILD_CFLAGS += -pipe -fno-builtin -mno-pic
AFLAGS += -mrelax -mno-pic
CFLAGS_MODULE += -mno-relax
LDFLAGS_vmlinux += --relax

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

CFLAGS += $(cpuflags-y)
KBUILD_CFLAGS += $(cpuflags-y)
AFLAGS += $(cpuflags-y)

CHECKFLAGS += -D__avr32__ -D__BIG_ENDIAN
Expand Down
2 changes: 1 addition & 1 deletion arch/blackfin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ rev-$(CONFIG_BF_REV_0_5) := 0.5
rev-$(CONFIG_BF_REV_NONE) := none
rev-$(CONFIG_BF_REV_ANY) := any

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

head-y := arch/$(ARCH)/mach-$(MACHINE)/head.o arch/$(ARCH)/kernel/init_task.o
Expand Down
8 changes: 4 additions & 4 deletions arch/cris/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ OBJCOPYFLAGS := -O binary -R .note -R .comment -S
CPPFLAGS_vmlinux.lds = -DDRAM_VIRTUAL_BASE=0x$(CONFIG_ETRAX_DRAM_VIRTUAL_BASE)
AFLAGS += -mlinux

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

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

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

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

core-y += arch/$(ARCH)/kernel/ arch/$(ARCH)/mm/
core-y += arch/$(ARCH)/$(SARCH)/kernel/ arch/$(ARCH)/$(SARCH)/mm/
Expand Down
22 changes: 11 additions & 11 deletions arch/frv/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,39 @@ endif
ARCHMODFLAGS += -G0 -mlong-calls

ifdef CONFIG_GPREL_DATA_8
CFLAGS += -G8
KBUILD_CFLAGS += -G8
else
ifdef CONFIG_GPREL_DATA_4
CFLAGS += -G4
KBUILD_CFLAGS += -G4
else
ifdef CONFIG_GPREL_DATA_NONE
CFLAGS += -G0
KBUILD_CFLAGS += -G0
endif
endif
endif

#LDFLAGS_vmlinux := -Map linkmap.txt

ifdef CONFIG_GC_SECTIONS
CFLAGS += -ffunction-sections -fdata-sections
KBUILD_CFLAGS += -ffunction-sections -fdata-sections
LINKFLAGS += --gc-sections
endif

ifndef CONFIG_FRAME_POINTER
CFLAGS += -mno-linked-fp
KBUILD_CFLAGS += -mno-linked-fp
endif

ifdef CONFIG_CPU_FR451_COMPILE
CFLAGS += -mcpu=fr450
KBUILD_CFLAGS += -mcpu=fr450
AFLAGS += -mcpu=fr450
ASFLAGS += -mcpu=fr450
else
ifdef CONFIG_CPU_FR551_COMPILE
CFLAGS += -mcpu=fr550
KBUILD_CFLAGS += -mcpu=fr550
AFLAGS += -mcpu=fr550
ASFLAGS += -mcpu=fr550
else
CFLAGS += -mcpu=fr400
KBUILD_CFLAGS += -mcpu=fr400
AFLAGS += -mcpu=fr400
ASFLAGS += -mcpu=fr400
endif
Expand All @@ -80,15 +80,15 @@ endif
# pretend the kernel is going to run on an FR400 with no media-fp unit
# - reserve CC3 for use with atomic ops
# - all the extra registers are dealt with only at context switch time
CFLAGS += -mno-fdpic -mgpr-32 -msoft-float -mno-media
CFLAGS += -ffixed-fcc3 -ffixed-cc3 -ffixed-gr15 -ffixed-icc2
KBUILD_CFLAGS += -mno-fdpic -mgpr-32 -msoft-float -mno-media
KBUILD_CFLAGS += -ffixed-fcc3 -ffixed-cc3 -ffixed-gr15 -ffixed-icc2
AFLAGS += -mno-fdpic
ASFLAGS += -mno-fdpic

# make sure the .S files get compiled with debug info
# and disable optimisations that are unhelpful whilst debugging
ifdef CONFIG_DEBUG_INFO
#CFLAGS += -O1
#KBUILD_CFLAGS += -O1
AFLAGS += -Wa,--gdwarf2
ASFLAGS += -Wa,--gdwarf2
endif
Expand Down
12 changes: 6 additions & 6 deletions arch/h8300/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ ldflags-$(CONFIG_CPU_H8300H) := -mh8300helf
cflags-$(CONFIG_CPU_H8S) := -ms
ldflags-$(CONFIG_CPU_H8S) := -mh8300self

CFLAGS += $(cflags-y)
CFLAGS += -mint32 -fno-builtin
CFLAGS += -g
CFLAGS += -D__linux__
CFLAGS += -DUTS_SYSNAME=\"uClinux\"
KBUILD_CFLAGS += $(cflags-y)
KBUILD_CFLAGS += -mint32 -fno-builtin
KBUILD_CFLAGS += -g
KBUILD_CFLAGS += -D__linux__
KBUILD_CFLAGS += -DUTS_SYSNAME=\"uClinux\"
AFLAGS += -DPLATFORM=$(PLATFORM) -DMODEL=$(MODEL) $(cflags-y)
LDFLAGS += $(ldflags-y)

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

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

Expand Down
10 changes: 5 additions & 5 deletions arch/i386/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ LDFLAGS_vmlinux := --emit-relocs
endif
CHECKFLAGS += -D__i386__

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

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

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

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

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

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

# Default subarch .c files
mcore-y := arch/x86/mach-default
Expand Down Expand Up @@ -116,7 +116,7 @@ drivers-$(CONFIG_OPROFILE) += arch/x86/oprofile/
drivers-$(CONFIG_PM) += arch/x86/power/
drivers-$(CONFIG_FB) += arch/x86/video/

CFLAGS += $(mflags-y)
KBUILD_CFLAGS += $(mflags-y)
AFLAGS += $(mflags-y)

boot := arch/x86/boot
Expand Down
2 changes: 1 addition & 1 deletion arch/ia64/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ifeq ($(call cc-version),0304)
cflags-$(CONFIG_MCKINLEY) += -mtune=mckinley
endif

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

libs-y += arch/ia64/lib/
Expand Down
Loading

0 comments on commit a0f97e0

Please sign in to comment.