1# 2# (C) Copyright 2000-2013 3# Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4# 5# SPDX-License-Identifier: GPL-2.0+ 6# 7######################################################################### 8 9# Set shell to bash if possible, otherwise fall back to sh 10SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \ 11 else if [ -x /bin/bash ]; then echo /bin/bash; \ 12 else echo sh; fi; fi) 13 14export SHELL 15 16ifeq ($(CURDIR),$(SRCTREE)) 17dir := 18else 19dir := $(subst $(SRCTREE)/,,$(CURDIR)) 20endif 21 22ifneq ($(OBJTREE),$(SRCTREE)) 23# Create object files for SPL in a separate directory 24ifeq ($(CONFIG_SPL_BUILD),y) 25obj := $(if $(dir),$(SPLTREE)/$(dir)/,$(SPLTREE)/) 26else 27obj := $(if $(dir),$(OBJTREE)/$(dir)/,$(OBJTREE)/) 28endif 29src := $(if $(dir),$(SRCTREE)/$(dir)/,$(SRCTREE)/) 30 31$(shell mkdir -p $(obj)) 32else 33# Create object files for SPL in a separate directory 34ifeq ($(CONFIG_SPL_BUILD),y) 35obj := $(if $(dir),$(SPLTREE)/$(dir)/,$(SPLTREE)/) 36 37$(shell mkdir -p $(obj)) 38else 39obj := 40endif 41src := 42endif 43 44# clean the slate ... 45PLATFORM_RELFLAGS = 46PLATFORM_CPPFLAGS = 47PLATFORM_LDFLAGS = 48 49######################################################################### 50 51HOSTCFLAGS = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer \ 52 $(HOSTCPPFLAGS) 53HOSTSTRIP = strip 54 55# 56# Mac OS X / Darwin's C preprocessor is Apple specific. It 57# generates numerous errors and warnings. We want to bypass it 58# and use GNU C's cpp. To do this we pass the -traditional-cpp 59# option to the compiler. Note that the -traditional-cpp flag 60# DOES NOT have the same semantics as GNU C's flag, all it does 61# is invoke the GNU preprocessor in stock ANSI/ISO C fashion. 62# 63# Apple's linker is similar, thanks to the new 2 stage linking 64# multiple symbol definitions are treated as errors, hence the 65# -multiply_defined suppress option to turn off this error. 66# 67 68ifeq ($(HOSTOS),darwin) 69# get major and minor product version (e.g. '10' and '6' for Snow Leopard) 70DARWIN_MAJOR_VERSION = $(shell sw_vers -productVersion | cut -f 1 -d '.') 71DARWIN_MINOR_VERSION = $(shell sw_vers -productVersion | cut -f 2 -d '.') 72 73os_x_before = $(shell if [ $(DARWIN_MAJOR_VERSION) -le $(1) -a \ 74 $(DARWIN_MINOR_VERSION) -le $(2) ] ; then echo "$(3)"; else echo "$(4)"; fi ;) 75 76# Snow Leopards build environment has no longer restrictions as described above 77HOSTCC = $(call os_x_before, 10, 5, "cc", "gcc") 78HOSTCFLAGS += $(call os_x_before, 10, 4, "-traditional-cpp") 79HOSTLDFLAGS += $(call os_x_before, 10, 5, "-multiply_defined suppress") 80else 81HOSTCC = gcc 82endif 83 84ifeq ($(HOSTOS),cygwin) 85HOSTCFLAGS += -ansi 86endif 87 88# We build some files with extra pedantic flags to try to minimize things 89# that won't build on some weird host compiler -- though there are lots of 90# exceptions for files that aren't complaint. 91 92HOSTCFLAGS_NOPED = $(filter-out -pedantic,$(HOSTCFLAGS)) 93HOSTCFLAGS += -pedantic 94 95######################################################################### 96# 97# Option checker, gcc version (courtesy linux kernel) to ensure 98# only supported compiler options are used 99# 100CC_OPTIONS_CACHE_FILE := $(OBJTREE)/include/generated/cc_options.mk 101CC_TEST_OFILE := $(OBJTREE)/include/generated/cc_test_file.o 102 103-include $(CC_OPTIONS_CACHE_FILE) 104 105cc-option-sys = $(shell mkdir -p $(dir $(CC_TEST_OFILE)); \ 106 if $(CC) $(CFLAGS) $(1) -S -xc /dev/null -o $(CC_TEST_OFILE) \ 107 > /dev/null 2>&1; then \ 108 echo 'CC_OPTIONS += $(strip $1)' >> $(CC_OPTIONS_CACHE_FILE); \ 109 echo "$(1)"; fi) 110 111ifeq ($(CONFIG_CC_OPT_CACHE_DISABLE),y) 112cc-option = $(strip $(if $(call cc-option-sys,$1),$1,$2)) 113else 114cc-option = $(strip $(if $(findstring $1,$(CC_OPTIONS)),$1,\ 115 $(if $(call cc-option-sys,$1),$1,$2))) 116endif 117 118# cc-version 119# Usage gcc-ver := $(call cc-version) 120cc-version = $(shell $(SHELL) $(SRCTREE)/tools/gcc-version.sh $(CC)) 121binutils-version = $(shell $(SHELL) $(SRCTREE)/tools/binutils-version.sh $(AS)) 122dtc-version = $(shell $(SHELL) $(SRCTREE)/tools/dtc-version.sh $(DTC)) 123 124# 125# Include the make variables (CC, etc...) 126# 127AS = $(CROSS_COMPILE)as 128 129# Always use GNU ld 130LD = $(shell if $(CROSS_COMPILE)ld.bfd -v > /dev/null 2>&1; \ 131 then echo "$(CROSS_COMPILE)ld.bfd"; else echo "$(CROSS_COMPILE)ld"; fi;) 132 133CC = $(CROSS_COMPILE)gcc 134CPP = $(CC) -E 135AR = $(CROSS_COMPILE)ar 136NM = $(CROSS_COMPILE)nm 137LDR = $(CROSS_COMPILE)ldr 138STRIP = $(CROSS_COMPILE)strip 139OBJCOPY = $(CROSS_COMPILE)objcopy 140OBJDUMP = $(CROSS_COMPILE)objdump 141RANLIB = $(CROSS_COMPILE)RANLIB 142DTC = dtc 143CHECK = sparse 144 145######################################################################### 146 147# Load generated board configuration 148ifeq ($(CONFIG_SPL_BUILD),y) 149# Include SPL autoconf 150sinclude $(OBJTREE)/include/spl-autoconf.mk 151else 152# Include normal autoconf 153sinclude $(OBJTREE)/include/autoconf.mk 154endif 155sinclude $(OBJTREE)/include/config.mk 156 157# Some architecture config.mk files need to know what CPUDIR is set to, 158# so calculate CPUDIR before including ARCH/SOC/CPU config.mk files. 159# Check if arch/$ARCH/cpu/$CPU exists, otherwise assume arch/$ARCH/cpu contains 160# CPU-specific code. 161CPUDIR=arch/$(ARCH)/cpu/$(CPU) 162ifneq ($(SRCTREE)/$(CPUDIR),$(wildcard $(SRCTREE)/$(CPUDIR))) 163CPUDIR=arch/$(ARCH)/cpu 164endif 165 166sinclude $(TOPDIR)/arch/$(ARCH)/config.mk # include architecture dependend rules 167sinclude $(TOPDIR)/$(CPUDIR)/config.mk # include CPU specific rules 168 169ifdef SOC 170sinclude $(TOPDIR)/$(CPUDIR)/$(SOC)/config.mk # include SoC specific rules 171endif 172ifdef VENDOR 173BOARDDIR = $(VENDOR)/$(BOARD) 174else 175BOARDDIR = $(BOARD) 176endif 177ifdef BOARD 178sinclude $(TOPDIR)/board/$(BOARDDIR)/config.mk # include board specific rules 179endif 180 181######################################################################### 182 183# We don't actually use $(ARFLAGS) anywhere anymore, so catch people 184# who are porting old code to latest mainline but not updating $(AR). 185ARFLAGS = $(error update your Makefile to use cmd_link_o_target and not AR) 186RELFLAGS= $(PLATFORM_RELFLAGS) 187DBGFLAGS= -g # -DDEBUG 188OPTFLAGS= -Os #-fomit-frame-pointer 189 190OBJCFLAGS += --gap-fill=0xff 191 192gccincdir := $(shell $(CC) -print-file-name=include) 193 194CPPFLAGS := $(DBGFLAGS) $(OPTFLAGS) $(RELFLAGS) \ 195 -D__KERNEL__ 196 197# Enable garbage collection of un-used sections for SPL 198ifeq ($(CONFIG_SPL_BUILD),y) 199CPPFLAGS += -ffunction-sections -fdata-sections 200LDFLAGS_FINAL += --gc-sections 201endif 202 203# TODO(sjg@chromium.org): Is this correct on Mac OS? 204ifdef CONFIG_FIT_SIGNATURE 205HOSTLIBS += -lssl -lcrypto 206 207# This affects include/image.h, but including the board config file 208# is tricky, so manually define this options here. 209HOSTCFLAGS += -DCONFIG_FIT_SIGNATURE 210endif 211 212ifneq ($(CONFIG_SYS_TEXT_BASE),) 213CPPFLAGS += -DCONFIG_SYS_TEXT_BASE=$(CONFIG_SYS_TEXT_BASE) 214endif 215 216ifneq ($(CONFIG_SPL_TEXT_BASE),) 217CPPFLAGS += -DCONFIG_SPL_TEXT_BASE=$(CONFIG_SPL_TEXT_BASE) 218endif 219 220ifneq ($(CONFIG_SPL_PAD_TO),) 221CPPFLAGS += -DCONFIG_SPL_PAD_TO=$(CONFIG_SPL_PAD_TO) 222endif 223 224ifneq ($(CONFIG_UBOOT_PAD_TO),) 225CPPFLAGS += -DCONFIG_UBOOT_PAD_TO=$(CONFIG_UBOOT_PAD_TO) 226endif 227 228ifeq ($(CONFIG_SPL_BUILD),y) 229CPPFLAGS += -DCONFIG_SPL_BUILD 230endif 231 232# Does this architecture support generic board init? 233ifeq ($(__HAVE_ARCH_GENERIC_BOARD),) 234ifneq ($(CONFIG_SYS_GENERIC_BOARD),) 235CHECK_GENERIC_BOARD = $(error Your architecture does not support generic board. \ 236Please undefined CONFIG_SYS_GENERIC_BOARD in your board config file) 237endif 238endif 239 240ifneq ($(RESET_VECTOR_ADDRESS),) 241CPPFLAGS += -DRESET_VECTOR_ADDRESS=$(RESET_VECTOR_ADDRESS) 242endif 243 244ifneq ($(OBJTREE),$(SRCTREE)) 245CPPFLAGS += -I$(OBJTREE)/include2 -I$(OBJTREE)/include 246endif 247 248CPPFLAGS += -I$(TOPDIR)/include 249CPPFLAGS += -fno-builtin -ffreestanding -nostdinc \ 250 -isystem $(gccincdir) -pipe $(PLATFORM_CPPFLAGS) 251 252CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes 253 254ifdef BUILD_TAG 255CFLAGS += -DBUILD_TAG='"$(BUILD_TAG)"' 256endif 257 258CFLAGS_SSP := $(call cc-option,-fno-stack-protector) 259CFLAGS += $(CFLAGS_SSP) 260# Some toolchains enable security related warning flags by default, 261# but they don't make much sense in the u-boot world, so disable them. 262CFLAGS_WARN := $(call cc-option,-Wno-format-nonliteral) \ 263 $(call cc-option,-Wno-format-security) 264CFLAGS += $(CFLAGS_WARN) 265 266# Report stack usage if supported 267CFLAGS_STACK := $(call cc-option,-fstack-usage) 268CFLAGS += $(CFLAGS_STACK) 269 270BCURDIR = $(subst $(SRCTREE)/,,$(CURDIR:$(obj)%=%)) 271 272ifeq ($(findstring examples/,$(BCURDIR)),) 273ifeq ($(CONFIG_SPL_BUILD),) 274ifdef FTRACE 275CFLAGS += -finstrument-functions -DFTRACE 276endif 277endif 278endif 279 280# $(CPPFLAGS) sets -g, which causes gcc to pass a suitable -g<format> 281# option to the assembler. 282AFLAGS_DEBUG := 283 284# turn jbsr into jsr for m68k 285ifeq ($(ARCH),m68k) 286ifeq ($(findstring 3.4,$(shell $(CC) --version)),3.4) 287AFLAGS_DEBUG := -Wa,-gstabs,-S 288endif 289endif 290 291AFLAGS := $(AFLAGS_DEBUG) -D__ASSEMBLY__ $(CPPFLAGS) 292 293LDFLAGS += $(PLATFORM_LDFLAGS) 294LDFLAGS_FINAL += -Bstatic 295 296LDFLAGS_u-boot += -T $(obj)u-boot.lds $(LDFLAGS_FINAL) 297ifneq ($(CONFIG_SYS_TEXT_BASE),) 298LDFLAGS_u-boot += -Ttext $(CONFIG_SYS_TEXT_BASE) 299endif 300 301LDFLAGS_u-boot-spl += -T $(obj)u-boot-spl.lds $(LDFLAGS_FINAL) 302ifneq ($(CONFIG_SPL_TEXT_BASE),) 303LDFLAGS_u-boot-spl += -Ttext $(CONFIG_SPL_TEXT_BASE) 304endif 305 306# Linus' kernel sanity checking tool 307CHECKFLAGS := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ \ 308 -Wbitwise -Wno-return-void -D__CHECK_ENDIAN__ $(CF) 309 310# Location of a usable BFD library, where we define "usable" as 311# "built for ${HOST}, supports ${TARGET}". Sensible values are 312# - When cross-compiling: the root of the cross-environment 313# - Linux/ppc (native): /usr 314# - NetBSD/ppc (native): you lose ... (must extract these from the 315# binutils build directory, plus the native and U-Boot include 316# files don't like each other) 317# 318# So far, this is used only by tools/gdb/Makefile. 319 320ifeq ($(HOSTOS),darwin) 321BFD_ROOT_DIR = /usr/local/tools 322else 323ifeq ($(HOSTARCH),$(ARCH)) 324# native 325BFD_ROOT_DIR = /usr 326else 327#BFD_ROOT_DIR = /LinuxPPC/CDK # Linux/i386 328#BFD_ROOT_DIR = /usr/pkg/cross # NetBSD/i386 329BFD_ROOT_DIR = /opt/powerpc 330endif 331endif 332 333######################################################################### 334 335export HOSTCC HOSTCFLAGS HOSTLDFLAGS PEDCFLAGS HOSTSTRIP CROSS_COMPILE \ 336 AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP MAKE 337export CONFIG_SYS_TEXT_BASE PLATFORM_CPPFLAGS PLATFORM_RELFLAGS CPPFLAGS CFLAGS AFLAGS 338 339######################################################################### 340 341# Allow boards to use custom optimize flags on a per dir/file basis 342ALL_AFLAGS = $(AFLAGS) $(AFLAGS_$(BCURDIR)/$(@F)) $(AFLAGS_$(BCURDIR)) 343ALL_CFLAGS = $(CFLAGS) $(CFLAGS_$(BCURDIR)/$(@F)) $(CFLAGS_$(BCURDIR)) 344EXTRA_CPPFLAGS = $(CPPFLAGS_$(BCURDIR)/$(@F)) $(CPPFLAGS_$(BCURDIR)) 345ALL_CFLAGS += $(EXTRA_CPPFLAGS) 346 347# The _DEP version uses the $< file target (for dependency generation) 348# See rules.mk 349EXTRA_CPPFLAGS_DEP = $(CPPFLAGS_$(BCURDIR)/$(addsuffix .o,$(basename $<))) \ 350 $(CPPFLAGS_$(BCURDIR)) 351$(obj)%.s: %.S 352 $(CPP) $(ALL_AFLAGS) -o $@ $< 353$(obj)%.o: %.S 354 $(CC) $(ALL_AFLAGS) -o $@ $< -c 355$(obj)%.o: %.c 356ifneq ($(CHECKSRC),0) 357 $(CHECK) $(CHECKFLAGS) $(ALL_CFLAGS) $< 358endif 359 $(CC) $(ALL_CFLAGS) -o $@ $< -c 360$(obj)%.i: %.c 361 $(CPP) $(ALL_CFLAGS) -o $@ $< -c 362$(obj)%.s: %.c 363 $(CC) $(ALL_CFLAGS) -o $@ $< -c -S 364 365######################################################################### 366 367# If the list of objects to link is empty, just create an empty built-in.o 368cmd_link_o_target = $(if $(strip $1),\ 369 $(LD) $(LDFLAGS) -r -o $@ $1,\ 370 rm -f $@; $(AR) rcs $@ ) 371 372######################################################################### 373