Revert "Revert "Revert "Revert "Break out common compile flags""""

This reverts commit 9d7d7c653bfac91180ec6e4a8047be8dc2e73448

Change-Id: Id4357ee6f22666b47734a8f754da67317265d348
Reviewed-on: https://gerrit.chromium.org/gerrit/11809
Tested-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Commit-Ready: Simon Glass <sjg@chromium.org>
diff --git a/Makefile b/Makefile
index 0d381d3..7652bde 100644
--- a/Makefile
+++ b/Makefile
@@ -5,39 +5,43 @@
 export FIRMWARE_ARCH
 export MOCK_TPM
 
-export CC ?= gcc
-export CXX ?= g++
-export CFLAGS = -Wall -Werror
-
-ifeq (${DEBUG},)
-CFLAGS += -O3
-else
-CFLAGS += -O0 -g
-endif
-
+#
+# Provide default CC and CFLAGS for firmware builds; if you have any -D flags,
+# please add them after this point (e.g., -DVBOOT_DEBUG).
 #
 # TODO(crosbug.com/16808) We hard-code u-boot's compiler flags here just
 # temporarily. As we are still investigating which flags are necessary for
 # maintaining a compatible ABI, etc. between u-boot and vboot_reference.
 #
-# Override CC and CFLAGS for firmware builds; if you have any -D flags, please
-# add them after this point (e.g., -DVBOOT_DEBUG).
+# As a first step, this makes the setting of CC and CFLAGS here optional, to
+# permit a calling script or Makefile to set these.
 #
+# Flag ordering: arch, then -f, then -m, then -W
+DEBUG_FLAGS := $(if ${DEBUG},-g -O0,-Os)
+COMMON_FLAGS := -nostdinc -pipe \
+	-ffreestanding -fno-builtin -fno-stack-protector \
+	-Werror -Wall -Wstrict-prototypes $(DEBUG_FLAGS)
+
 ifeq ($(FIRMWARE_ARCH), arm)
-CC = armv7a-cros-linux-gnueabi-gcc
-CFLAGS = -g -Os -fno-common -ffixed-r8 -msoft-float -fno-builtin \
-	-ffreestanding -nostdinc \
-	-pipe -marm -mabi=aapcs-linux -mno-thumb-interwork -march=armv5 \
-	-Werror -Wall -Wstrict-prototypes -fno-stack-protector
+CC ?= armv7a-cros-linux-gnueabi-gcc
+CFLAGS ?= -march=armv5 \
+	-fno-common -ffixed-r8 \
+	-msoft-float -marm -mabi=aapcs-linux -mno-thumb-interwork \
+	$(COMMON_FLAGS)
 endif
 ifeq ($(FIRMWARE_ARCH), i386)
-CC = i686-pc-linux-gnu-gcc
-CFLAGS = -g -Os -ffunction-sections -fvisibility=hidden -fno-builtin \
-	-ffreestanding -nostdinc -pipe -Wstrict-prototypes -mregparm=3 \
-	-fomit-frame-pointer -fno-stack-protector \
-	-mpreferred-stack-boundary=2 -fno-dwarf2-cfi-asm -Werror -Wall
+CC ?= i686-pc-linux-gnu-gcc
+# Drop -march=i386 to permit use of SSE instructions
+CFLAGS ?= \
+	-ffunction-sections -fvisibility=hidden -fno-strict-aliasing \
+	-fomit-frame-pointer -fno-toplevel-reorder -fno-dwarf2-cfi-asm \
+	-mpreferred-stack-boundary=2 -mregparm=3 \
+	$(COMMON_FLAGS)
 endif
 
+CC ?= gcc
+CXX ?= g++
+
 # Fix compiling directly on host (outside of emake)
 ifeq ($(ARCH),)
 export ARCH=amd64
@@ -55,6 +59,8 @@
 CFLAGS += -DNDEBUG
 endif
 
+export CC CXX CFLAGS
+
 export TOP = $(shell pwd)
 export FWDIR=$(TOP)/firmware
 export HOSTDIR=$(TOP)/host