1
2CONFIG_CROSS_COMPILE := arm-ali-aoseabi-
3CONFIG_STRICT_CFLAGS ?= y
4CONFIG_SAVE_TARGET ?= n
5CONFIG_FORCE_WIN_SHELL ?= y
6
7export CONFIG_STRICT_CFLAGS CONFIG_SAVE_TARGET CONFIG_FORCE_WIN_SHELL HAAS_OTA_ENABLED
8
9# ---------------------------------------------------------------------------
10# Platform and shell detection
11
12export WIN_PLAT := n
13ifeq ($(OS),Windows_NT)
14# Detect Cygwin
15ifneq ($(findstring /,$(PWD)),/)
16# also $(findstring /,$(HOME)) ?
17WIN_PLAT := y
18endif
19endif
20
21export WIN_SHELL := n
22ifeq ($(WIN_PLAT),y)
23ifeq ($(CONFIG_FORCE_WIN_SHELL),y)
24WIN_SHELL := y
25else
26# Detect Unix-style shell
27ifeq ($(shell echo $$0),$$0)
28WIN_SHELL := y
29endif
30endif
31endif
32
33BACKSLASH := \ # backslash
34BACKSLASH := $(strip $(BACKSLASH))
35
36TO_UNIX_PATH = $(subst $(BACKSLASH),/,$(1))
37TO_WIN_PATH = $(subst /,$(BACKSLASH),$(1))
38ESC_WIN_PATH = $(subst $(BACKSLASH),$(BACKSLASH)$(BACKSLASH),$(1))
39
40ifeq ($(WIN_PLAT),y)
41ifeq ($(WIN_SHELL),y)
42# make will choose sh.exe as SHELL if it finds sh.exe in the directories of PATH, regardless of
43# the setting in environment or parent (e.g., when git.exe is in the PATH)
44SHELL := cmd.exe
45SHELL_CMD = $(call TO_WIN_PATH,$(1))
46else
47SHELL_CMD = $(call ESC_WIN_PATH,$(call TO_WIN_PATH,$(1)))
48endif
49else
50SHELL_CMD = $(1)
51endif
52
53# The Unix-style path is recognized by compiler toolchain, GNU utilities and windows redirection
54# operators, but not by windows native commands (e.g., mkdir) and applications.
55
56# End of platform and shell detection
57# ---------------------------------------------------------------------------
58
59# Do not use make's built-in rules and variables
60# (this increases performance and avoids hard-to-debug behaviour);
61MAKEFLAGS += -rR
62
63# Avoid funny character set dependencies
64unexport LC_ALL
65LC_COLLATE=C
66LC_NUMERIC=C
67export LC_COLLATE LC_NUMERIC
68
69# Avoid interference with shell env settings
70unexport GREP_OPTIONS
71
72# Check if just to show the help content
73ifeq ($(MAKECMDGOALS),help)
74ifeq ($(T),)
75HELP_TARGET := 1
76endif
77endif
78
79ifneq ($(HELP_TARGET),1)
80# We are using a recursive build, so we need to do a little thinking
81# to get the ordering right.
82#
83# Most importantly: sub-Makefiles should only ever modify files in
84# their own directory. If in some directory we have a dependency on
85# a file in another dir (which doesn't happen often, but it's often
86# unavoidable when linking the built-in.o targets which finally
87# turn into elf file), we will call a sub make in that other dir, and
88# after that we are sure that everything which is in that other dir
89# is now up to date.
90#
91# The only cases where we need to modify files which have global
92# effects are thus separated out and done before the recursive
93# descending is started. They are now explicitly listed as the
94# prepare rule.
95
96# Beautify output
97# ---------------------------------------------------------------------------
98#
99# Normally, we echo the whole command before executing it. By making
100# that echo $($(quiet)$(cmd)), we now have the possibility to set
101# $(quiet) to choose other forms of output instead, e.g.
102#
103#         quiet_cmd_cc_o_c = Compiling $(RELDIR)/$@
104#         cmd_cc_o_c       = $(CC) $(c_flags) -c -o $@ $<
105#
106# If $(quiet) is empty, the whole command will be printed.
107# If it is set to "quiet_", only the short version will be printed.
108# If it is set to "silent_", nothing will be printed at all, since
109# the variable $(silent_cmd_cc_o_c) doesn't exist.
110#
111# A simple variant is to prefix commands with $(Q) - that's useful
112# for commands that shall be hidden in non-verbose mode.
113#
114#	$(Q)ln $@ :<
115#
116# If KBUILD_VERBOSE equals 0 then the above command will be hidden.
117# If KBUILD_VERBOSE equals 1 then the above command is displayed.
118#
119# To put more focus on warnings, be less verbose as default
120# Use 'make V=1' to see the full commands
121
122ifeq ("$(origin V)","command line")
123  KBUILD_VERBOSE = $(V)
124endif
125ifndef KBUILD_VERBOSE
126  KBUILD_VERBOSE = 0
127endif
128
129ifeq ($(KBUILD_VERBOSE),1)
130  quiet :=
131  Q :=
132else
133  quiet := quiet_
134  Q := @
135endif
136
137# If the user is running make -s (silent mode), suppress echoing of
138# commands
139
140ifneq ($(filter 4.%,$(MAKE_VERSION)),) # make-4
141ifneq ($(filter %s ,$(firstword x$(MAKEFLAGS))),)
142  quiet=silent_
143endif
144else                                   # make-3.8x
145ifneq ($(filter s% -s%,$(MAKEFLAGS)),)
146  quiet=silent_
147endif
148endif
149
150export quiet Q KBUILD_VERBOSE
151
152TARGET_CFG_FILE = config/$(T)/target.mk
153TARGET_COMMON_FILE = config/common.mk
154
155# To locate output files in a separate directory two syntaxes are supported.
156# In both cases the working directory must be the root of the kernel src.
157# 1) O=
158# Use "make O=dir/to/store/output/files/"
159#
160# 2) Set KBUILD_OUTPUT
161# Set the environment variable KBUILD_OUTPUT to point to the directory
162# where the output files shall be placed.
163# export KBUILD_OUTPUT=dir/to/store/output/files/
164# make
165#
166# The O= assignment takes precedence over the KBUILD_OUTPUT environment
167# variable.
168
169# KBUILD_SRC is set on invocation of make in OBJ directory
170# KBUILD_SRC is not intended to be used by the regular user (for now)
171ifeq ($(KBUILD_SRC),)
172
173export KBUILD_ROOT := $(CURDIR)
174
175# OK, Make called in directory where kernel src resides
176# Do we want to locate output files in a separate directory?
177
178export KBUILD_OUTPUT := $(CURDIR)/out
179ifeq ("$(origin O)","command line")
180  KBUILD_OUTPUT := $(O)
181endif
182
183# Select target
184ifeq ($(CONFIG_SAVE_TARGET),y)
185ifeq ($(T),)
186-include $(KBUILD_OUTPUT)/.config
187T := $(strip $(T))
188endif
189endif
190ifeq ($(T),)
191$(error Please specify the target in the command line: T=<targetName>)
192endif
193ifeq ($(wildcard $(TARGET_CFG_FILE)),)
194$(error Invalid target: T=$(T))
195endif
196export T
197
198KBUILD_OUTPUT := $(KBUILD_OUTPUT)/$(T)
199
200# That's our default target when none is given on the command line
201PHONY := _all
202_all:
203
204# Cancel implicit rules on the config file
205$(KBUILD_OUTPUT)/.config: ;
206
207ifneq ($(KBUILD_OUTPUT),)
208# Invoke a second make in the output directory, passing relevant variables
209# check that the output directory actually exists
210saved-output := $(KBUILD_OUTPUT)
211ifeq ($(WIN_PLAT),y)
212KBUILD_OUTPUT := $(subst /,\,$(KBUILD_OUTPUT))
213KBUILD_OUTPUT := $(shell ( if not exist $(KBUILD_OUTPUT)\ mkdir $(KBUILD_OUTPUT) ) \
214                         && cd $(KBUILD_OUTPUT) && cd)
215KBUILD_OUTPUT := $(subst \,/,$(KBUILD_OUTPUT))
216else
217KBUILD_OUTPUT := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) \
218                         && /bin/pwd)
219endif
220
221$(if $(KBUILD_OUTPUT),, \
222     $(error failed to create output directory "$(saved-output)"))
223
224ifeq ($(CONFIG_SAVE_TARGET),y)
225ifeq ($(WIN_PLAT),y)
226_dummy := $(shell echo T := $(T)> $(KBUILD_OUTPUT)/../.config)
227else
228_dummy := $(shell echo "T := $(T)" > $(KBUILD_OUTPUT)/../.config)
229endif
230endif
231
232PHONY += $(MAKECMDGOALS) sub-make
233
234$(filter-out _all sub-make $(CURDIR)/Makefile, $(MAKECMDGOALS)) _all: sub-make
235	@:
236
237include $(CURDIR)/scripts/submods_init.mk
238
239ifneq ($(filter allclean,$(MAKECMDGOALS)),)
240ALLCLEAN := 1
241export ALLCLEAN
242endif
243
244# Look for make include files relative to root of kernel src
245MAKEFLAGS += --include-dir=$(CURDIR)
246
247ifeq ($(WIN_PLAT),y)
248START_TIME := $(shell echo %time%)
249START_DATE_TIME := $(shell echo %date% %time%)
250else
251START_TIME := $(shell date +"%s.%N")
252START_DATE_TIME := $(shell date +"%Y-%m-%d %T.%N")
253endif
254
255sub-make: FORCE
256	@echo MAKE START: $(START_DATE_TIME)
257	$(Q)$(MAKE) -C $(KBUILD_OUTPUT) KBUILD_SRC=$(CURDIR) \
258		-f $(CURDIR)/Makefile $(filter-out _all sub-make,$(MAKECMDGOALS))
259ifeq ($(WIN_PLAT),y)
260	@echo MAKE END: %date% %time%
261ifneq ($(wildcard tools/timediff.bat),)
262	@tools/timediff.bat "%time%" "$(START_TIME)"
263endif
264else
265	@echo MAKE END: $$(date +"%Y-%m-%d %T.%N")
266	@printf "MAKE TIME: %.2f seconds\n" $$(echo "$$(date +%s.%N) - $(START_TIME)" | bc)
267endif
268
269# Leave processing to above invocation of make
270skip-makefile := 1
271endif # ifneq ($(KBUILD_OUTPUT),)
272endif # ifeq ($(KBUILD_SRC),)
273
274# We process the rest of the Makefile if this is the final invocation of make
275ifeq ($(skip-makefile),)
276
277# Do not print "Entering directory ...",
278# but we want to display it when entering to the output directory
279# so that IDEs/editors are able to understand relative filenames.
280MAKEFLAGS += --no-print-directory
281
282# If building an external module we do not care about the all: rule
283# but instead _all depend on modules
284PHONY += all
285_all: all
286
287ifeq ($(KBUILD_SRC),)
288        # building in the source tree
289        srctree := .
290else
291        ifeq ($(KBUILD_SRC)/,$(dir $(CURDIR)))
292                # building in a subdirectory of the source tree
293                srctree := ..
294        else
295                ifeq ($(KBUILD_SRC)/,$(dir $(patsubst %/,%,$(dir $(CURDIR)))))
296                        srctree := ../..
297                else
298                        srctree := $(KBUILD_SRC)
299                endif
300        endif
301endif
302objtree		:= .
303src		:= $(srctree)
304obj		:= $(objtree)
305
306VPATH		:= $(srctree)
307
308export srctree objtree VPATH
309
310# Git revision
311ifeq ($(WIN_PLAT),y)
312GIT_REVISION := $(shell (where git >nul 2>&1) && (git rev-parse --short HEAD 2>nul))
313else
314GIT_REVISION := $(shell (which git >/dev/null 2>&1) && (git rev-parse --short HEAD 2>/dev/null))
315endif
316
317ifneq ($(GIT_REVISION),)
318ifeq ($(WIN_PLAT),y)
319GIT_REVISION := $(GIT_REVISION)$(shell (git diff --quiet && git diff --cached --quiet) >nul 2>&1 || echo -dirty)
320else
321GIT_REVISION := $(GIT_REVISION)$(shell (git diff --quiet && git diff --cached --quiet) >/dev/null 2>&1 || echo -dirty)
322endif
323endif
324
325
326# Cross compiling and selecting different set of gcc/bin-utils
327# ---------------------------------------------------------------------------
328#
329# When performing cross compilation for other architectures ARCH shall be set
330# to the target architecture. (See arch/* for the possibilities).
331# ARCH can be set during invocation of make:
332# make ARCH=ia64
333# Another way is to have ARCH set in the environment.
334# The default ARCH is the host where make is executed.
335
336# CROSS_COMPILE specify the prefix used for all executables used
337# during compilation. Only gcc and related bin-utils executables
338# are prefixed with $(CROSS_COMPILE).
339# CROSS_COMPILE can be set on the command line
340# make CROSS_COMPILE=ia64-linux-
341# Alternatively CROSS_COMPILE can be set in the environment.
342# A third alternative is to store a setting in .config so that plain
343# "make" in the configured kernel build directory always uses that.
344# Default value for CROSS_COMPILE is not to prefix executables
345# Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile
346ARCH		?= arm
347CROSS_COMPILE	?= $(CONFIG_CROSS_COMPILE:"%"=%)
348
349# SHELL used by kbuild
350ifneq ($(WIN_PLAT),y)
351CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
352          else if [ -x /bin/bash ]; then echo /bin/bash; \
353          else echo sh; fi ; fi)
354endif
355
356# Make variables (CC, etc...)
357ifeq ($(TOOLCHAIN),armclang)
358CC		= armclang --target=arm-arm-none-eabi
359CPP		= $(CC) -E
360AS		= $(CC)
361C++		= $(CC)
362LD		= $(CC)
363AR		= armar
364NM		= fromelf
365STRIP	= fromelf
366OBJCOPY	= fromelf
367OBJDUMP	= fromelf
368else
369ifeq ($(OS),Windows_NT)
370AS		= $(CROSS_COMPILE)as.exe
371CC		= $(CROSS_COMPILE)gcc.exe
372CPP		= $(CC) -E
373C++		= $(CROSS_COMPILE)g++.exe
374LD		= $(CC)
375#LD		= $(CROSS_COMPILE)ld
376AR		= $(CROSS_COMPILE)ar.exe
377NM		= $(CROSS_COMPILE)nm.exe
378STRIP	= $(CROSS_COMPILE)strip.exe
379OBJCOPY	= $(CROSS_COMPILE)objcopy.exe
380OBJDUMP	= $(CROSS_COMPILE)objdump.exe
381else
382AS		= $(CROSS_COMPILE)as
383CC		= $(CROSS_COMPILE)gcc
384CPP		= $(CC) -E
385C++		= $(CROSS_COMPILE)g++
386LD		= $(CC)
387#LD		= $(CROSS_COMPILE)ld
388AR		= $(CROSS_COMPILE)ar
389NM		= $(CROSS_COMPILE)nm
390STRIP	= $(CROSS_COMPILE)strip
391OBJCOPY	= $(CROSS_COMPILE)objcopy
392OBJDUMP	= $(CROSS_COMPILE)objdump
393endif
394endif
395
396AWK		= awk
397PERL	= perl
398PYTHON	= python
399
400KBUILD_CPPFLAGS :=
401
402KBUILD_CFLAGS	:= -fno-common -fmessage-length=0 -Wall \
403		   -fno-exceptions -ffunction-sections \
404		   -fdata-sections -fomit-frame-pointer
405
406# By default char on ARM platform is unsigned char, but char on x86 platform
407# is signed char. To avoid porting issues, force char to be signed char
408# on ARM platform.
409KBUILD_CFLAGS	+= -fsigned-char
410
411ifneq ($(TOOLCHAIN),armclang)
412# 1) Avoid checking out-of-bound array accesses in a loop
413#    (and unrolling/peeling/exiting the loop based on the check)
414# 2) Avoid detecting paths dereferencing a NULL pointer
415#    (and turning the problematic statement into a trap)
416KBUILD_CFLAGS	+= -fno-aggressive-loop-optimizations \
417		   -fno-isolate-erroneous-paths-dereference
418endif
419
420# Treat floating-point constants as float instead of double
421ifeq ($(TOOLCHAIN),armclang)
422KBUILD_CFLAGS	+= -cl-single-precision-constant -fshort-enums
423else
424KBUILD_CFLAGS	+= -fsingle-precision-constant
425endif
426KBUILD_CFLAGS	+= -Wdouble-promotion -Wfloat-conversion
427
428#C_ONLY_FLAGS	:= -std=gnu89
429#C_ONLY_FLAGS	?= -std=gnu99
430C_ONLY_FLAGS	:= -std=gnu11
431
432#C++_ONLY_FLAGS	?= -std=gnu++98 -fno-rtti
433C++_ONLY_FLAGS	:= -std=gnu++11 -fno-rtti
434
435KBUILD_AFLAGS   := -D__ASSEMBLY__
436
437export ARCH CROSS_COMPILE AS LD CC
438export CPP C++ AR NM STRIP OBJCOPY OBJDUMP
439export MAKE AWK PERL PYTHON
440
441export KBUILD_CPPFLAGS NOSTDINC_FLAGS OBJCOPYFLAGS LDFLAGS
442export KBUILD_CFLAGS
443export KBUILD_AFLAGS
444export KBUILD_ARFLAGS
445export C_ONLY_FLAGS C++_ONLY_FLAGS
446
447# Files to ignore in find ... statements
448
449export RCS_FIND_IGNORE := \( -name SCCS -o -name BitKeeper -o -name .svn -o    \
450			  -name CVS -o -name .pc -o -name .hg -o -name .git \) \
451			  -prune -o
452export RCS_TAR_IGNORE := --exclude SCCS --exclude BitKeeper --exclude .svn \
453			 --exclude CVS --exclude .pc --exclude .hg --exclude .git
454
455# ===========================================================================
456# Build targets only.
457
458# Objects we will link into $(IMAGE_FILE) / subdirs we need to visit
459init-y		:= init/
460core-y		:= main/
461
462LDS_FILE	:= best1000.lds
463
464# Link flags for all LD processes
465LINK_CFLAGS	:=
466export LINK_CFLAGS
467
468# Link flags for image only
469LIB_LDFLAGS		:=
470CFLAGS_IMAGE	:= -static
471ifeq ($(TOOLCHAIN),armclang)
472LDFLAGS_IMAGE	:= --no_locals
473else
474LDFLAGS_IMAGE	:= -X --no-wchar-size-warning
475endif
476
477# Include target definitions
478include $(srctree)/$(TARGET_CFG_FILE)
479include $(srctree)/$(TARGET_COMMON_FILE)
480
481$(srctree)/$(TARGET_CFG_FILE): ;
482$(srctree)/$(TARGET_COMMON_FILE): ;
483
484ifneq ($(filter-out %/,$(init-y) $(core-y)),)
485$(error The object files cannot be linked at top level: $(filter-out %/,$(init-y) $(core-y)))
486endif
487
488ifeq ($(TOOLCHAIN),armclang)
489# Entry objects
490ifeq ($(entry-y),)
491entry-y		+= utils/boot_struct/boot_struct.o
492ifeq ($(ROM_BUILD),1)
493entry-y		+= tests/rom/startup_ARMCM.o
494ifneq ($(filter tests/rom/,$(core-y)),)
495entry-y		+= tests/rom/export_fn_rom.o
496endif
497else # !ROM_BUILD
498entry-y		+= platform/cmsis/retarget_armclang_asm.o
499ifeq ($(PROGRAMMER),1)
500entry-y		+= tests/programmer/sys_api_programmer.o
501else
502entry-y		+= platform/main/startup_main.o
503endif
504endif # !ROM_BUILD
505endif
506ifeq ($(filter %.o,$(entry-y)),)
507$(error Entry objects must be defined in entry-y in target.mk)
508endif
509BAD_ENTRY_OBJS := $(filter-out %.o,$(entry-y))
510ifneq ($(BAD_ENTRY_OBJS),)
511$(error Only objects can be defined in entry-y in target.mk: $(BAD_ENTRY_OBJS))
512endif
513IMAGE_ENTRY := $(entry-y)
514ifeq ($(ROM_BUILD),1)
515CFLAGS_IMAGE	+= -e Reset_Handler
516else ifeq ($(PROGRAMMER),1)
517CFLAGS_IMAGE	+= -e programmer_start
518else
519CFLAGS_IMAGE	+= -e __main
520endif
521endif
522
523ifneq ($(NO_BUILDID),1)
524ifneq ($(TOOLCHAIN),armclang)
525LDFLAGS_IMAGE	+= --build-id
526endif
527endif
528ifeq ($(CROSS_REF),1)
529ifeq ($(TOOLCHAIN),armclang)
530LDFLAGS_IMAGE	+= --xref
531else
532LDFLAGS_IMAGE	+= --cref
533endif
534endif
535
536REAL_LDS_FILE := $(LDS_FILE)
537ifeq ($(TOOLCHAIN),armclang)
538SCATTER_LDS_SUFFIX := _scat
539ifeq ($(filter %$(SCATTER_LDS_SUFFIX),$(LDS_FILE)),)
540REAL_LDS_FILE := $(LDS_FILE)$(SCATTER_LDS_SUFFIX)
541endif
542endif
543
544# Generate REVISION_INFO (might be defined in target)
545ifeq ($(REVISION_INFO),)
546ifeq ($(CUST_TGT_INFO),)
547REVISION_INFO := $(GIT_REVISION):$(T)
548else
549REVISION_INFO := $(GIT_REVISION):$(CUST_TGT_INFO)
550endif
551endif
552
553include $(srctree)/scripts/include.mk
554
555REVISION_INFO := $(subst $(space),-,$(strip $(REVISION_INFO)))
556SOFTWARE_VERSION := $(subst $(space),-,$(strip $(SOFTWARE_VERSION)))
557
558$(info -------------------------------)
559$(info REVISION_INFO: $(REVISION_INFO))
560$(info -------------------------------)
561
562# Build host and user info
563ifeq ($(WIN_PLAT),y)
564export BUILD_HOSTNAME := $(COMPUTERNAME)
565export BUILD_USERNAME := $(USERNAME)
566else
567export BUILD_HOSTNAME := $(shell hostname -s)
568export BUILD_USERNAME := $(shell id -un)
569endif
570
571BUILD_HOSTNAME := $(subst $(space),-,$(strip $(BUILD_HOSTNAME)))
572BUILD_USERNAME := $(subst $(space),-,$(strip $(BUILD_USERNAME)))
573
574# Default kernel image to build when no specific target is given.
575# IMAGE_FILE may be overruled on the command line or
576# set in the environment
577IMAGE_FILE ?= $(notdir $(T)).elf
578
579ifneq ($(filter .map .bin .hex .lst,$(suffix $(IMAGE_FILE))),)
580$(error Invalid IMAGE_FILE (conflicted suffix): $(IMAGE_FILE))
581endif
582
583LST_SECTION_OPTS :=
584LST_SECTION_NAME :=
585ifneq ($(LST_ONLY_SECTION),)
586ifeq ($(TOOLCHAIN),armclang)
587LST_SECTION_OPTS += $(foreach m,$(subst $(comma),$(space),$(LST_ONLY_SECTION)),--only=$m )
588else
589LST_SECTION_OPTS += $(foreach m,$(subst $(comma),$(space),$(LST_ONLY_SECTION)),-j $m )
590endif
591LST_SECTION_NAME := _$(subst *,+,$(subst !,-,$(LST_ONLY_SECTION)))
592endif
593ifneq ($(LST_RM_SECTION),)
594ifeq ($(TOOLCHAIN),armclang)
595LST_SECTION_OPTS += $(foreach m,$(subst $(comma),$(space),$(LST_RM_SECTION)),--ignore_section=$m )
596else
597LST_SECTION_OPTS += $(foreach m,$(subst $(comma),$(space),$(LST_RM_SECTION)),-R $m )
598endif
599LST_SECTION_NAME := $(LST_SECTION_NAME)_no_$(subst *,+,$(subst !,-,$(LST_RM_SECTION)))
600endif
601
602IMAGE_MAP := $(addsuffix .map,$(basename $(IMAGE_FILE)))
603IMAGE_BIN := $(addsuffix .bin,$(basename $(IMAGE_FILE)))
604IMAGE_HEX := $(addsuffix .hex,$(basename $(IMAGE_FILE)))
605ifeq ($(LST_SECTION_OPTS),)
606IMAGE_LST := $(addsuffix .lst,$(basename $(IMAGE_FILE)))
607else
608IMAGE_LST := $(addsuffix $(LST_SECTION_NAME).lst,$(basename $(IMAGE_FILE)))
609IMAGE_SEC := $(addsuffix $(LST_SECTION_NAME)$(suffix $(IMAGE_FILE)),$(basename $(IMAGE_FILE)))
610endif
611
612LDS_TARGET := _$(notdir $(REAL_LDS_FILE))
613
614IMAGE_VER  := build_info.o
615
616targets := $(LDS_TARGET) $(IMAGE_FILE) $(IMAGE_BIN) $(IMAGE_LST) $(IMAGE_VER)
617cmd_files := $(wildcard $(foreach f,$(targets),$(call get_depfile_name,$(f))))
618
619ifneq ($(cmd_files),)
620include $(cmd_files)
621$(cmd_files): ;
622endif
623
624# The all: target is the default when no target is given on the
625# command line.
626# This allow a user to issue only 'make' to build a kernel including modules
627# Defaults to $(IMAGE_BIN)
628all: $(IMAGE_BIN) ;
629
630ifeq ($(TOOLCHAIN),armclang)
631      cmd_gen-IMAGE_BIN = $(OBJCOPY) --bincombined -o $@ $<
632else
633      cmd_gen-IMAGE_BIN = $(OBJCOPY) -O binary $< $@
634endif
635quiet_cmd_gen-IMAGE_BIN = GENBIN  $@
636
637$(IMAGE_BIN): $(IMAGE_FILE)
638ifneq ($(filter 1,$(COMPILE_ONLY) $(NO_BIN)),)
639	@:
640else
641	+$(call if_changed,gen-IMAGE_BIN)
642endif
643
644ifneq ($(TOOLCHAIN),armclang)
645      cmd_gen-IMAGE_HEX = $(OBJCOPY) -O ihex $< $@
646quiet_cmd_gen-IMAGE_HEX = GENHEX  $@
647
648$(IMAGE_HEX): $(IMAGE_FILE)
649ifeq ($(COMPILE_ONLY),1)
650	@:
651else
652	+$(call if_changed,gen-IMAGE_HEX)
653endif
654endif
655
656PHONY += lst lst_only
657lst lst_only: $(IMAGE_LST) ;
658
659ifneq ($(filter lst_only,$(MAKECMDGOALS)),)
660NO_COMPILE := 1
661endif
662
663ifeq ($(TOOLCHAIN),armclang)
664      cmd_gen-IMAGE_LST = $(OBJDUMP) $(LST_SECTION_OPTS) --datasymbols --text -c -d --output=$@ $<
665else
666ifeq ($(LST_SECTION_OPTS),)
667      cmd_gen-IMAGE_LST = $(OBJDUMP) -Sldx $< > $@
668else
669      cmd_gen-IMAGE_LST = $(OBJCOPY) $(LST_SECTION_OPTS) $< $(IMAGE_SEC) && $(OBJDUMP) -Sldx $(IMAGE_SEC) > $@
670endif
671endif
672quiet_cmd_gen-IMAGE_LST = GENLST  $@
673
674$(IMAGE_LST): $(IMAGE_FILE)
675	+$(call if_changed,gen-IMAGE_LST)
676
677
678# Flags
679
680# arch Makefile may override CC so keep this after arch Makefile is included
681#ifeq ($(CONFIG_STRICT_CFLAGS),y)
682#NOSTDINC_FLAGS += -nostdinc
683#endif
684#NOSTDINC_FLAGS += -isystem "$(subst \,/,$(shell $(CC) -print-file-name=include))"
685
686ifeq ($(CONFIG_STRICT_CFLAGS),y)
687# warn about C99 declaration after statement
688#C_ONLY_FLAGS    += -Wdeclaration-after-statement
689
690# disallow errors like 'EXPORT_GPL(foo);' with missing header
691C_ONLY_FLAGS   += -Werror=implicit-int
692
693# require functions to have arguments in prototypes, not empty 'int foo()'
694#C_ONLY_FLAGS    += -Werror=strict-prototypes
695
696C_ONLY_FLAGS    += -Werror-implicit-function-declaration
697
698# Prohibit date/time macros, which would make the build non-deterministic
699KBUILD_CFLAGS   += $(call cc-option,-Werror=date-time)
700
701KBUILD_CFLAGS   += $(call cc-option,-Wlogical-op)
702
703#KBUILD_CFLAGS   += -Wno-address-of-packed-member
704
705KBUILD_CFLAGS	+= -Wno-trigraphs \
706		   -fno-strict-aliasing \
707		   -Wno-format-security
708
709#KBUILD_CFLAGS	+= Wundef
710
711# use the deterministic mode of AR if available
712KBUILD_ARFLAGS := D
713
714include $(srctree)/scripts/extrawarn.mk
715endif # CONFIG_STRICT_CFLAGS
716
717ifeq ($(TOOLCHAIN),armclang)
718KBUILD_CFLAGS += -Wno-typedef-redefinition
719endif
720
721# Add user supplied CPPFLAGS, AFLAGS and CFLAGS as the last assignments
722KBUILD_CPPFLAGS += $(KCPPFLAGS)
723KBUILD_AFLAGS += $(KAFLAGS)
724KBUILD_CFLAGS += $(KCFLAGS)
725
726IMAGE-dirs	:= $(patsubst %/,%,$(filter %/, $(init-y) $(core-y)))
727
728submodgoals =
729ifneq ($(SUBMODS),)
730include $(srctree)/scripts/submods.mk
731
732IMAGE-builddirs	:= $(call get_subdirs,$(IMAGE-dirs),$(SUBMODS))
733ifeq ($(COMPILE_ONLY),1)
734submodgoals = $(call get_submodgoals,$@,$(SUBMODS))
735endif
736else
737IMAGE-builddirs	:= $(IMAGE-dirs)
738endif
739
740IMAGE-alldirs	:= $(sort $(IMAGE-dirs) $(patsubst %/,%,$(filter %/, \
741			$(init-) $(core-) $(extra-))))
742
743init-y		:= $(patsubst %/, %/built-in$(built_in_suffix), $(init-y))
744core-y		:= $(patsubst %/, %/built-in$(built_in_suffix), $(core-y))
745
746IMAGE_INIT := $(init-y)
747IMAGE_MAIN := $(core-y)
748
749ifeq ($(NO_COMPILE),1)
750IMAGE-deps :=
751else
752IMAGE-deps := $(LDS_TARGET) $(IMAGE_INIT) $(IMAGE_MAIN) $(IMAGE_VER)
753endif
754
755BUILD_INFO_FLAGS := \
756	-DREVISION_INFO=$(REVISION_INFO) \
757	-DFLASH_SIZE=$(FLASH_SIZE) \
758	-DOTA_UPGRADE_CRC_LOG_SIZE=$(OTA_UPGRADE_CRC_LOG_SIZE) \
759	-DNV_REC_DEV_VER=$(NV_REC_DEV_VER) \
760	-I$(srctree)/platform/hal
761
762BUILD_INFO_FLAGS += $(LDS_SECTION_FLAGS)
763BUILD_INFO_FLAGS += -DCHIP=$(CHIP)
764
765ifneq ($(CHIP_SUBTYPE),)
766BUILD_INFO_FLAGS += -DCHIP_SUBTYPE=$(CHIP_SUBTYPE)
767endif
768ifneq ($(SOFTWARE_VERSION),)
769BUILD_INFO_FLAGS += -DSOFTWARE_VERSION=$(SOFTWARE_VERSION)
770endif
771ifneq ($(OTA_BOOT_SIZE),)
772BUILD_INFO_FLAGS += -DOTA_BOOT_SIZE=$(OTA_BOOT_SIZE)
773endif
774ifneq ($(OTA_CODE_OFFSET),)
775BUILD_INFO_FLAGS += -DOTA_CODE_OFFSET=$(OTA_CODE_OFFSET)
776endif
777ifneq ($(OTA_REMAP_OFFSET),)
778BUILD_INFO_FLAGS += -DOTA_REMAP_OFFSET=$(OTA_REMAP_OFFSET)
779endif
780ifeq ($(CRC32_OF_IMAGE),1)
781BUILD_INFO_FLAGS += -DCRC32_OF_IMAGE
782endif
783ifeq ($(TRACE_CRLF),1)
784BUILD_INFO_FLAGS += -DTRACE_CRLF
785endif
786
787BUILD_INFO_FLAGS += -DKERNEL=$(KERNEL)
788
789quiet_cmd_image_ver = CC      $(IMAGE_VER)
790      cmd_image_ver = $(CC) $(filter-out -Werror=date-time, \
791                             $(call flags,KBUILD_CPPFLAGS) \
792                             $(call flags,KBUILD_CFLAGS) \
793                             $(call flags,C_ONLY_FLAGS) \
794                             $(NOSTDINC_FLAGS)) \
795                             $(BUILD_INFO_FLAGS) \
796                             -MD -MP -MF $(depfile) -MT $@ \
797                             -c -o $@ $<
798
799IMAGE_VER_SRC := $(src)/utils/build_info/build_info.c
800
801$(IMAGE_VER): $(IMAGE_VER_SRC) $(filter-out $(IMAGE_VER),$(IMAGE-deps)) FORCE
802	$(call if_changed_dep,image_ver)
803
804# Linker scripts preprocessor (.lds.S -> .lds)
805# ---------------------------------------------------------------------------
806quiet_cmd_cpp_lds_S = LDS     $@
807      cmd_cpp_lds_S = $(CPP) $(call flags,KBUILD_CPPFLAGS) \
808                             $(call flags,CPPFLAGS_$(LDS_FILE)) \
809                             -MD -MP -MF $(depfile) -MT $@ \
810                             $(NOSTDINC_FLAGS) \
811                             -P -C -E -x c -o $@ $<
812
813LDS_SRC_STEM := $(src)/scripts/link/$(REAL_LDS_FILE)
814LDS_SRC := $(firstword $(wildcard $(LDS_SRC_STEM).S $(LDS_SRC_STEM).sx) $(LDS_SRC_STEM).S)
815
816$(LDS_TARGET): $(LDS_SRC) FORCE
817	$(call if_changed_dep,cpp_lds_S)
818
819PHONY += lds
820lds: $(LDS_TARGET) ;
821
822
823# Final link of $(IMAGE_FILE)
824# ---------------------------------------------------------------------------
825#
826# 1) Link the archives twice to solve circular references between two or
827#    more archives. Otherwise we should use --start-group and --end-group
828#    options. Normally, an archive is searched only once in the order that
829#    it is specified on the command line.
830# 2) Use --whole-archive option to solve weak symbol overriding issue.
831#    It tells LD to include every object file in the archive in the link,
832#    rather than searching the archive for the required object files.
833#    By default the strong symbols defined in the archive will not override
834#    any weak symbol, for LD only searches the archive if there is a undefined
835#    symbol (and a weak symbol is considered as a defined symbol).
836#
837ifeq ($(TOOLCHAIN),armclang)
838#LDFLAGS_IMAGE += --symbols --list_mapping_symbols
839ifeq ($(KBUILD_VERBOSE),1)
840LDFLAGS_IMAGE += --verbose
841endif
842
843      cmd_link-IMAGE_FILE = $(LD) -o $@ \
844	      $(CFLAGS_IMAGE) \
845	      -Wl,$(subst $(space),$(comma),$(strip \
846	      $(LDFLAGS) $(LDFLAGS_IMAGE) \
847	      --scatter=$(LDS_TARGET) \
848	      --list=$(IMAGE_MAP) \
849	      --info=summarysizes --info=summarystack --info=totals --info=unused \
850	      --map --load_addr_map_info \
851	      --remove --no_autoat \
852	      --emit_debug_overlay_relocs --emit_debug_overlay_section \
853	      --diag_style=gnu --diag_suppress=L6314 --diag_suppress=L6329)) \
854	      $(IMAGE_ENTRY) $(IMAGE_INIT) $(IMAGE_MAIN) $(IMAGE_VER) \
855	      $(LIB_LDFLAGS) $(LIB_LDFLAGS)
856else
857      cmd_link-IMAGE_FILE = $(LD) -o $@ \
858	      -T $(LDS_TARGET) \
859	      $(CFLAGS_IMAGE) \
860	      -Wl,$(subst $(space),$(comma),$(strip \
861	      $(LDFLAGS) $(LDFLAGS_IMAGE) \
862	      -Map=$(IMAGE_MAP) \
863	      --gc-sections \
864	      --whole-archive)) \
865	      $(IMAGE_INIT) $(IMAGE_MAIN) $(IMAGE_VER) \
866	      -Wl,--no-whole-archive $(LIB_LDFLAGS) $(LIB_LDFLAGS)
867endif
868quiet_cmd_link-IMAGE_FILE = LINK    $@
869
870# Include targets which we want to
871# execute if the rest of the kernel build went well.
872$(IMAGE_FILE): $(IMAGE-deps) FORCE
873ifneq ($(filter 1,$(COMPILE_ONLY) $(NO_COMPILE)),)
874	@:
875else
876	+$(call if_changed,link-IMAGE_FILE)
877endif
878
879ifneq ($(IMAGE-deps),)
880# The actual objects are generated when descending,
881# make sure no implicit rule kicks in
882$(sort $(filter %/built-in$(built_in_suffix),$(IMAGE-deps))): $(IMAGE-builddirs) ;
883endif
884
885# Handle descending into subdirectories listed in $(IMAGE-dirs)
886# Preset locale variables to speed up the build process. Limit locale
887# tweaks to this spot to avoid wrong language settings when running
888# make menuconfig etc.
889# Error messages still appears in the original language
890
891PHONY += $(IMAGE-dirs)
892$(IMAGE-dirs): scripts
893	$(Q)$(MAKE) $(build)=$@ $(submodgoals)
894
895# clean - Delete most, but leave enough to build external modules
896#
897clean: rm-dirs  := $(CLEAN_DIRS)
898clean: rm-files := $(CLEAN_FILES)
899ifneq ($(SUBMODS),)
900clean-dirs      := $(addprefix _clean_, $(IMAGE-builddirs))
901else
902clean-dirs      := $(addprefix _clean_, $(IMAGE-alldirs))
903endif
904
905PHONY += $(clean-dirs) clean IMAGE-clean
906$(clean-dirs):
907	$(Q)$(MAKE) $(clean)=$(patsubst _clean_%,%,$@)
908
909IMAGE-clean:
910	$(Q)$(call CMDRMFILE,$(IMAGE_FILE) $(IMAGE_MAP) \
911		$(IMAGE_BIN) $(IMAGE_HEX) $(IMAGE_LST) \
912		$(IMAGE_VER) $(LDS_TARGET))
913
914clean: IMAGE-clean
915
916clean: $(clean-dirs)
917	$(call cmd,rmdirs)
918	$(call cmd,rmfiles)
919ifeq ($(SUBMODS),)
920	$(Q)$(call CMDRMFILER,.,*.o *.a *.s *.d)
921endif
922
923PHONY += allclean
924ifeq ($(KBUILD_OUTPUT),)
925allclean: clean ;
926else
927ifeq ($(SUBMODS),)
928quiet_cmd_clean    = RMDIR   $(KBUILD_OUTPUT)
929      cmd_clean    = $(call CMDRMDIR,$(KBUILD_OUTPUT))
930
931allclean:
932	+$(call cmd,clean)
933else
934allclean: clean ;
935endif
936endif
937
938quiet_cmd_predefined-macros = GEN     $@
939      cmd_predefined-macros = $(CPP) $(filter-out -I% -D% -include%,$(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(C_ONLY_FLAGS)) \
940                                     -x c -E -dM -o $@ $(devnull)
941
942PREDEF_MACRO_FILE := predefined-macros.txt
943
944$(PREDEF_MACRO_FILE): FORCE
945	$(call cmd,predefined-macros)
946
947PHONY += predefined-macros
948predefined-macros: $(PREDEF_MACRO_FILE) ;
949
950# FIXME Should go into a make.lib or something
951# ===========================================================================
952
953quiet_cmd_rmdirs = $(if $(wildcard $(rm-dirs)),CLEAN   $(wildcard $(rm-dirs)))
954      cmd_rmdirs = $(if $(wildcard $(rm-dirs)),$(call CMDRMDIR,$(rm-dirs)))
955
956quiet_cmd_rmfiles = $(if $(wildcard $(rm-files)),CLEAN   $(wildcard $(rm-files)))
957      cmd_rmfiles = $(if $(wildcard $(rm-files)),$(call CMDRMFILE,$(rm-files)))
958
959# Shorthand for $(Q)$(MAKE) -f scripts/clean.mk obj=dir
960# Usage:
961# $(Q)$(MAKE) $(clean)=dir
962clean := -f $(srctree)/scripts/clean.mk obj
963
964ifneq ($(WIN_PLAT),y)
965# Generate tags for editors
966# ---------------------------------------------------------------------------
967quiet_cmd_tags = GEN     $@
968      cmd_tags = $(CONFIG_SHELL) $(srctree)/tools/tags.sh $@
969
970tags TAGS cscope gtags: FORCE
971	$(call cmd,tags)
972endif
973
974HELP_TARGET := 2
975
976endif # ifeq ($(skip-makefile),)
977endif # ifneq ($(HELP_TARGET),1)
978
979# Help target
980ifneq ($(HELP_TARGET),)
981ifeq ($(HELP_TARGET),1)
982include scripts/include.mk
983endif
984
985help: FORCE
986	$(call echo-help,Mandatory options:)
987	$(call echo-help,  T=<targetBoard> - Select a target board configuration in config/)
988	$(call echo-help,)
989	$(call echo-help,Cleaning targets:)
990	$(call echo-help,  clean           - Remove most generated files)
991	$(call echo-help,  allclean        - Remove all generated files and the output directory if possible)
992	$(call echo-help,)
993	$(call echo-help,Generic targets:)
994	$(call echo-help,  all             - Build all targets marked with [*])
995	$(call echo-help,  lst             - Build the mixed source/assembly file of the final image)
996	$(call echo-help,  lds             - Build the linker script file)
997ifeq ($(HELP_TARGET),2)
998	$(call echo-help,* $(IMAGE_FILE))
999	$(call echo-help,                  - Build the final image)
1000endif
1001	$(call echo-help,  dir/            - Build all files in dir and below)
1002	$(call echo-help,  dir/file.[oisS] - Build specified target only)
1003	$(call echo-help,  dir/file.lst    - Build specified mixed source/assembly target only)
1004	$(call echo-help,                    (requires a recent binutils and recent build (System.map)))
1005	$(call echo-help,)
1006	$(call echo-help,  make V=0|1 [targets] 0 => quiet build (default), 1 => verbose build)
1007	$(call echo-help,  make V=2   [targets] 2 => give reason for rebuild of target)
1008	$(call echo-help,  make O=dir [targets] Locate all output files in "dir", including .config)
1009	$(call echo-help,  make W=n   [targets] Enable extra gcc checks, n=1,2,3 where)
1010	$(call echo-help,         1: warnings which may be relevant and do not occur too often)
1011	$(call echo-help,         2: warnings which occur quite often but may still be relevant)
1012	$(call echo-help,         3: more obscure warnings, can most likely be ignored)
1013	$(call echo-help,         Multiple levels can be combined with W=12 or W=123)
1014	$(call echo-help,)
1015	$(call echo-help,Execute "make" or "make all" to build all targets marked with [*])
1016
1017endif # ifneq ($(HELP_TARGET),)
1018
1019# Cancel implicit rules on top Makefile
1020ifeq ($(KBUILD_SRC),)
1021$(CURDIR)/Makefile Makefile: ;
1022else
1023$(KBUILD_SRC)/Makefile Makefile: ;
1024endif
1025
1026PHONY += FORCE
1027FORCE: ;
1028
1029# Declare the contents of the .PHONY variable as phony.  We keep that
1030# information in a variable so we can use it in if_changed and friends.
1031.PHONY: $(PHONY)
1032