blob: 627d11a480ef834e2077701bf9aed07655070a63 [file] [log] [blame]
Anas Nashif027b69e2016-02-08 21:32:24 -05001VERSION_MAJOR = 1
Anas Nashifa70bbac2016-12-02 23:09:34 -05002VERSION_MINOR = 6
Anas Nashif0a3946a2016-10-24 08:38:13 -04003PATCHLEVEL = 99
Juan Manuel Cruz6de34002015-02-15 21:46:45 -06004VERSION_RESERVED = 0
Anas Nashif5ca45ba2016-08-26 17:35:49 -04005EXTRAVERSION =
Anas Nashifac47c452015-06-19 21:21:48 -04006NAME = Zephyr Kernel
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06007
Anas Nashif3315b1f2015-12-27 16:35:28 -05008export SOURCE_DIR PROJECT MDEF_FILE
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -06009
Anas Nashifa77c7d82016-02-02 07:04:49 -050010ifeq ($(MAKECMDGOALS),)
Anas Nashif7d348b02016-01-31 08:22:18 -050011$(error Invoking make from top-level kernel directory is not supported)
12endif
Anas Nashif7d348b02016-01-31 08:22:18 -050013
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -060014# *DOCUMENTATION*
15# To see a list of typical targets execute "make help"
16# More info can be located in ./README
17# Comments in this file are targeted only to the developer, do not
18# expect to learn how to build the kernel reading this file.
Anas Nashife3ac0902015-08-23 11:22:40 -040019#
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -060020
Anas Nashife3ac0902015-08-23 11:22:40 -040021# o Do not use make's built-in rules and variables
22# (this increases performance and avoids hard-to-debug behaviour);
23# o Look for make include files relative to root of kernel src
24MAKEFLAGS += -rR --include-dir=$(CURDIR)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -060025
Louise Mendoza0c3f05b2015-12-30 16:17:36 -060026UNAME := $(shell uname)
27ifeq (MINGW, $(findstring MINGW, $(UNAME)))
28HOST_OS=MINGW
29PWD_OPT=-W
Anas Nashif3cf1d292016-02-01 13:29:43 -050030DISABLE_TRYRUN=y
Juan Manuel Cruz5516bca2016-04-27 10:15:14 -050031CPATH ?= $(MIGW_DIR)/include
32LIBRARY_PATH ?= $(MINGW_DIR)/lib
33export CPATH LIBRARY_PATH
Louise Mendoza0c3f05b2015-12-30 16:17:36 -060034else ifeq (Linux, $(findstring Linux, $(UNAME)))
35HOST_OS=Linux
36else ifeq (Darwin, $(findstring Darwin, $(UNAME)))
37HOST_OS=Darwin
38endif
39export HOST_OS
40
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -060041# Avoid funny character set dependencies
42unexport LC_ALL
43LC_COLLATE=C
44LC_NUMERIC=C
45export LC_COLLATE LC_NUMERIC
46
47# Avoid interference with shell env settings
48unexport GREP_OPTIONS
49
Anas Nashif3852c8c2015-08-22 18:14:42 -040050DQUOTE = "
51#This comment line is to fix the highlighting of some editors due the quote effect."
Maureen Helm280eadf2016-06-11 09:33:31 -050052export DQUOTE
Anas Nashif3852c8c2015-08-22 18:14:42 -040053
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -060054# We are using a recursive build, so we need to do a little thinking
55# to get the ordering right.
56#
57# Most importantly: sub-Makefiles should only ever modify files in
58# their own directory. If in some directory we have a dependency on
59# a file in another dir (which doesn't happen often, but it's often
60# unavoidable when linking the built-in.o targets which finally
Anas Nashif65230942015-06-05 22:41:29 -040061# turn into the kernel binary), we will call a sub make in that other
62# dir, and after that we are sure that everything which is in that
63# other dir is now up to date.
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -060064#
65# The only cases where we need to modify files which have global
66# effects are thus separated out and done before the recursive
67# descending is started. They are now explicitly listed as the
68# prepare rule.
69
70# Beautify output
71# ---------------------------------------------------------------------------
72#
73# Normally, we echo the whole command before executing it. By making
74# that echo $($(quiet)$(cmd)), we now have the possibility to set
75# $(quiet) to choose other forms of output instead, e.g.
76#
77# quiet_cmd_cc_o_c = Compiling $(RELDIR)/$@
78# cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
79#
80# If $(quiet) is empty, the whole command will be printed.
81# If it is set to "quiet_", only the short version will be printed.
82# If it is set to "silent_", nothing will be printed at all, since
83# the variable $(silent_cmd_cc_o_c) doesn't exist.
84#
85# A simple variant is to prefix commands with $(Q) - that's useful
86# for commands that shall be hidden in non-verbose mode.
87#
88# $(Q)ln $@ :<
89#
90# If KBUILD_VERBOSE equals 0 then the above command will be hidden.
91# If KBUILD_VERBOSE equals 1 then the above command is displayed.
92#
93# To put more focus on warnings, be less verbose as default
94# Use 'make V=1' to see the full commands
95
96ifeq ("$(origin V)", "command line")
97 KBUILD_VERBOSE = $(V)
98endif
99ifndef KBUILD_VERBOSE
100 KBUILD_VERBOSE = 0
101endif
102
103ifeq ($(KBUILD_VERBOSE),1)
104 quiet =
105 Q =
Andrew Boie0d03e322016-07-26 11:36:31 -0700106 GENIDT_EXTRA_ARGS = -d
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600107else
108 quiet=quiet_
109 Q = @
Andrew Boie0d03e322016-07-26 11:36:31 -0700110 GENIDT_EXTRA_ARGS =
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600111endif
112
113# If the user is running make -s (silent mode), suppress echoing of
114# commands
115
116ifneq ($(filter 4.%,$(MAKE_VERSION)),) # make-4
117ifneq ($(filter %s ,$(firstword x$(MAKEFLAGS))),)
118 quiet=silent_
119endif
120else # make-3.8x
121ifneq ($(filter s% -s%,$(MAKEFLAGS)),)
122 quiet=silent_
123endif
124endif
125
126export quiet Q KBUILD_VERBOSE
127
128# kbuild supports saving output files in a separate directory.
129# To locate output files in a separate directory two syntaxes are supported.
130# In both cases the working directory must be the root of the kernel src.
131# 1) O=
132# Use "make O=dir/to/store/output/files/"
133#
134# 2) Set KBUILD_OUTPUT
135# Set the environment variable KBUILD_OUTPUT to point to the directory
136# where the output files shall be placed.
137# export KBUILD_OUTPUT=dir/to/store/output/files/
138# make
139#
140# The O= assignment takes precedence over the KBUILD_OUTPUT environment
141# variable.
142
143# KBUILD_SRC is set on invocation of make in OBJ directory
144# KBUILD_SRC is not intended to be used by the regular user (for now)
145ifeq ($(KBUILD_SRC),)
146
147# OK, Make called in directory where kernel src resides
148# Do we want to locate output files in a separate directory?
149ifeq ("$(origin O)", "command line")
150 KBUILD_OUTPUT := $(O)
151endif
152
153# That's our default target when none is given on the command line
154PHONY := _all
155_all:
156
157# Cancel implicit rules on top Makefile
158$(CURDIR)/Makefile Makefile: ;
159
160ifneq ($(KBUILD_OUTPUT),)
161# Invoke a second make in the output directory, passing relevant variables
162# check that the output directory actually exists
163saved-output := $(KBUILD_OUTPUT)
164KBUILD_OUTPUT := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) \
Louise Mendoza0c3f05b2015-12-30 16:17:36 -0600165 && pwd $(PWD_OPT))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600166$(if $(KBUILD_OUTPUT),, \
167 $(error failed to create output directory "$(saved-output)"))
168
169PHONY += $(MAKECMDGOALS) sub-make
170
171$(filter-out _all sub-make $(CURDIR)/Makefile, $(MAKECMDGOALS)) _all: sub-make
172 @:
173
174sub-make: FORCE
175 $(Q)$(MAKE) -C $(KBUILD_OUTPUT) KBUILD_SRC=$(CURDIR) \
176 -f $(CURDIR)/Makefile $(filter-out _all sub-make,$(MAKECMDGOALS))
177
178# Leave processing to above invocation of make
179skip-makefile := 1
180endif # ifneq ($(KBUILD_OUTPUT),)
181endif # ifeq ($(KBUILD_SRC),)
182
183# We process the rest of the Makefile if this is the final invocation of make
184ifeq ($(skip-makefile),)
185
186# Do not print "Entering directory ...",
187# but we want to display it when entering to the output directory
188# so that IDEs/editors are able to understand relative filenames.
189MAKEFLAGS += --no-print-directory
190
191# Call a source code checker (by default, "sparse") as part of the
192# C compilation.
193#
194# Use 'make C=1' to enable checking of only re-compiled files.
195# Use 'make C=2' to enable checking of *all* source files, regardless
196# of whether they are re-compiled or not.
197#
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600198ifeq ("$(origin C)", "command line")
199 KBUILD_CHECKSRC = $(C)
200endif
201ifndef KBUILD_CHECKSRC
202 KBUILD_CHECKSRC = 0
203endif
204
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600205PHONY += all
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600206_all: all
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600207
208ifeq ($(KBUILD_SRC),)
209 # building in the source tree
210 srctree := .
211else
212 ifeq ($(KBUILD_SRC)/,$(dir $(CURDIR)))
213 # building in a subdirectory of the source tree
214 srctree := ..
215 else
216 srctree := $(KBUILD_SRC)
217 endif
218endif
219objtree := .
220src := $(srctree)
221obj := $(objtree)
222
Kumar Gala23af1e92016-06-03 09:51:56 -0500223VPATH := $(SOURCE_DIR_PARENT) $(srctree)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600224
225export srctree objtree VPATH
226
227
228# SUBARCH tells the usermode build what the underlying arch is. That is set
229# first, and if a usermode build is happening, the "ARCH=um" on the command
230# line overrides the setting of ARCH below. If a native build is happening,
231# then ARCH is assigned, getting whatever value it gets normally, and
232# SUBARCH is subsequently ignored.
233
234SUBARCH := $(shell uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ \
235 -e s/sun4u/sparc64/ \
236 -e s/arm.*/arm/ -e s/sa110/arm/ \
237 -e s/s390x/s390/ -e s/parisc64/parisc/ \
238 -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \
239 -e s/sh[234].*/sh/ -e s/aarch64.*/arm64/ )
240
241# Cross compiling and selecting different set of gcc/bin-utils
242# ---------------------------------------------------------------------------
243#
244# When performing cross compilation for other architectures ARCH shall be set
245# to the target architecture. (See arch/* for the possibilities).
246# ARCH can be set during invocation of make:
Anas Nashif67fc2062015-04-24 18:29:24 -0400247# make ARCH=x86
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600248# Another way is to have ARCH set in the environment.
249# The default ARCH is the host where make is executed.
250
251# CROSS_COMPILE specify the prefix used for all executables used
252# during compilation. Only gcc and related bin-utils executables
253# are prefixed with $(CROSS_COMPILE).
254# CROSS_COMPILE can be set on the command line
Anas Nashif67fc2062015-04-24 18:29:24 -0400255# make CROSS_COMPILE=i586-pc-elf
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600256# Alternatively CROSS_COMPILE can be set in the environment.
257# A third alternative is to store a setting in .config so that plain
258# "make" in the configured kernel build directory always uses that.
259# Default value for CROSS_COMPILE is not to prefix executables
260# Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile
261ARCH ?= $(SUBARCH)
262CROSS_COMPILE ?= $(CONFIG_CROSS_COMPILE:"%"=%)
263
Anas Nashif27603f42015-06-18 10:46:52 -0400264KCONFIG_CONFIG ?= .config
265export KCONFIG_CONFIG
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600266
267# SHELL used by kbuild
268CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
269 else if [ -x /bin/bash ]; then echo /bin/bash; \
270 else echo sh; fi ; fi)
271
Vinicius Costa Gomes28b79192015-12-18 18:40:30 -0200272HOSTCC ?= gcc
273HOSTCXX ?= g++
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600274HOSTCFLAGS = -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 -fomit-frame-pointer -std=gnu89
275HOSTCXXFLAGS = -O2
276
277ifeq ($(shell $(HOSTCC) -v 2>&1 | grep -c "clang version"), 1)
278HOSTCFLAGS += -Wno-unused-value -Wno-unused-parameter \
279 -Wno-missing-field-initializers -fno-delete-null-pointer-checks
280endif
281
282# Decide whether to build built-in, modular, or both.
283# Normally, just do built-in.
284
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600285KBUILD_BUILTIN := 1
286
Anas Nashiface0e222015-12-30 19:01:20 -0500287export KBUILD_BUILTIN
Anas Nashifd6ebd182015-05-28 12:54:03 -0400288export KBUILD_CHECKSRC KBUILD_SRC
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600289
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600290# Look for make include files relative to root of kernel src
291MAKEFLAGS += --include-dir=$(srctree)
292
293# We need some generic definitions (do not try to remake the file).
294$(srctree)/scripts/Kbuild.include: ;
295include $(srctree)/scripts/Kbuild.include
Andrew Boie285c0542015-07-27 11:04:24 -0700296ifeq ($(USE_CCACHE),1)
297CCACHE := ccache
298endif
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600299
300# Make variables (CC, etc...)
301AS = $(CROSS_COMPILE)as
302LD = $(CROSS_COMPILE)ld
Fabien Chereau6d1ee7e2015-08-19 15:34:02 +0200303ifeq ($(USE_CCACHE),1)
Andrew Boie285c0542015-07-27 11:04:24 -0700304CC = $(CCACHE) $(CROSS_COMPILE)gcc
Peter Mitsis8e35cc82016-01-13 13:02:56 -0500305CXX = $(CCACHE) $(CROSS_COMPILE)g++
Fabien Chereau6d1ee7e2015-08-19 15:34:02 +0200306else
Juan Manuel Cruz164ecd02016-04-27 10:25:23 -0500307CC ?= $(CROSS_COMPILE)gcc
308CXX ?= $(CROSS_COMPILE)g++
Fabien Chereau6d1ee7e2015-08-19 15:34:02 +0200309endif
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600310CPP = $(CC) -E
311AR = $(CROSS_COMPILE)ar
312NM = $(CROSS_COMPILE)nm
313STRIP = $(CROSS_COMPILE)strip
314OBJCOPY = $(CROSS_COMPILE)objcopy
315OBJDUMP = $(CROSS_COMPILE)objdump
Anas Nashifc0c15562016-02-01 19:54:32 -0500316GDB = $(CROSS_COMPILE)gdb
Anas Nashif6361be22016-05-22 12:34:06 -0400317READELF = $(CROSS_COMPILE)readelf
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600318AWK = awk
Anas Nashifff23cb52016-09-03 15:43:26 -0400319ifeq ($(PREBUILT_HOST_TOOLS),)
Anas Nashif27603f42015-06-18 10:46:52 -0400320GENIDT = scripts/gen_idt/gen_idt
Anas Nashifbd227482015-08-25 10:04:04 -0400321GENOFFSET_H = scripts/gen_offset_header/gen_offset_header
Anas Nashifff23cb52016-09-03 15:43:26 -0400322FIXDEP = scripts/basic/fixdep
323else
324GENIDT = $(PREBUILT_HOST_TOOLS)/gen_idt
325GENOFFSET_H = $(PREBUILT_HOST_TOOLS)/gen_offset_header
326FIXDEP = $(PREBUILT_HOST_TOOLS)/fixdep
327endif
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600328PERL = perl
329PYTHON = python
330CHECK = sparse
331
Anas Nashif6afc4852015-04-24 18:30:36 -0400332CHECKFLAGS := -Wbitwise -Wno-return-void $(CF)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600333CFLAGS_GCOV = -fprofile-arcs -ftest-coverage
334
Anas Nashif1b702152015-08-23 22:01:07 -0400335ifeq ($(COMPILER),clang)
336ifneq ($(CROSS_COMPILE),)
337CLANG_TARGET := -target $(notdir $(CROSS_COMPILE:%-=%))
338GCC_TOOLCHAIN := $(dir $(CROSS_COMPILE))
339endif
340ifneq ($(GCC_TOOLCHAIN),)
341CLANG_GCC_TC := -gcc-toolchain $(GCC_TOOLCHAIN)
342endif
343ifneq ($(IA),1)
344CLANG_IA_FLAG = -no-integrated-as
345endif
346CLANG_FLAGS := $(CLANG_TARGET) $(CLANG_GCC_TC) $(CLANG_IA_FLAG)
347endif
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600348
349# Use USERINCLUDE when you must reference the UAPI directories only.
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600350USERINCLUDE := -include $(CURDIR)/include/generated/autoconf.h
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600351
Anas Nashif3a87dae2015-12-06 09:51:33 -0500352SOC_NAME = $(subst $(DQUOTE),,$(CONFIG_SOC))
Anas Nashifd2f1c762016-04-03 09:37:43 -0400353SOC_SERIES = $(subst $(DQUOTE),,$(CONFIG_SOC_SERIES))
Anas Nashifa02c34e2016-03-31 08:07:42 -0400354SOC_FAMILY = $(subst $(DQUOTE),,$(CONFIG_SOC_FAMILY))
Anas Nashifd2f1c762016-04-03 09:37:43 -0400355
Anas Nashiffe039db2016-01-22 13:51:50 -0500356override ARCH = $(subst $(DQUOTE),,$(CONFIG_ARCH))
Anas Nashif2d762242015-12-07 20:20:25 -0500357BOARD_NAME = $(subst $(DQUOTE),,$(CONFIG_BOARD))
Anas Nashif3a87dae2015-12-06 09:51:33 -0500358KERNEL_NAME = $(subst $(DQUOTE),,$(CONFIG_KERNEL_BIN_NAME))
359KERNEL_ELF_NAME = $(KERNEL_NAME).elf
Anas Nashif06e78de2015-11-01 11:59:09 -0500360KERNEL_BIN_NAME = $(KERNEL_NAME).bin
Kumar Galabbf736c2016-10-24 10:57:46 -0500361KERNEL_HEX_NAME = $(KERNEL_NAME).hex
Anas Nashif6361be22016-05-22 12:34:06 -0400362KERNEL_STAT_NAME = $(KERNEL_NAME).stat
Anas Nashif3a87dae2015-12-06 09:51:33 -0500363
Anas Nashifd2f1c762016-04-03 09:37:43 -0400364export SOC_FAMILY SOC_SERIES SOC_PATH SOC_NAME BOARD_NAME
Kumar Galabbf736c2016-10-24 10:57:46 -0500365export ARCH KERNEL_NAME KERNEL_ELF_NAME KERNEL_BIN_NAME KERNEL_HEX_NAME
Anas Nashifb8823772015-06-05 22:46:00 -0400366# Use ZEPHYRINCLUDE when you must reference the include/ directory.
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600367# Needed to be compatible with the O= option
Anas Nashifdf73d322015-11-02 20:42:35 -0500368ZEPHYRINCLUDE = \
Anas Nashifd687a952016-12-17 17:48:47 -0500369 -I$(srctree)/kernel/include \
Anas Nashif3a87dae2015-12-06 09:51:33 -0500370 -I$(srctree)/arch/$(ARCH)/include \
Anas Nashifd2f1c762016-04-03 09:37:43 -0400371 -I$(srctree)/arch/$(ARCH)/soc/$(SOC_PATH) \
Kumar Gala8d357602016-10-19 15:13:41 -0500372 -I$(srctree)/boards/$(ARCH)/$(BOARD_NAME) \
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600373 $(if $(KBUILD_SRC), -I$(srctree)/include) \
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600374 -I$(srctree)/include \
375 -I$(CURDIR)/include/generated \
Anas Nashif8b45f4e2015-10-21 07:24:39 -0400376 -I$(CURDIR)/misc/generated/sysgen \
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600377 $(USERINCLUDE) \
378 $(STDINCLUDE)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600379
Flavio Santesbe1dd332016-11-21 16:51:08 -0600380KBUILD_CPPFLAGS := -DKERNEL -D__ZEPHYR__=1
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600381
Juan Manuel Cruz068ebc02015-05-06 10:41:08 -0500382KBUILD_CFLAGS := -c -g -std=c99 \
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600383 -fno-asynchronous-unwind-tables \
Anas Nashif1b702152015-08-23 22:01:07 -0400384 -Wall \
Anas Nashifce9cb262016-11-28 16:28:49 -0500385 -Wformat \
386 -Wformat-security \
387 -D_FORTIFY_SOURCE=2 \
Andrew Boie5bb06312015-08-11 12:46:06 -0700388 -Wno-format-zero-length \
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600389 -Wno-main -ffreestanding
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600390
Peter Mitsis8e35cc82016-01-13 13:02:56 -0500391KBUILD_CXXFLAGS := -c -g -std=c++11 \
392 -fno-reorder-functions \
393 -fno-asynchronous-unwind-tables \
Peter Mitsis8e35cc82016-01-13 13:02:56 -0500394 -fcheck-new \
395 -fno-defer-pop -Wall \
396 -Wno-unused-but-set-variable \
397 -Wno-format-zero-length \
398 -Wno-main -ffreestanding \
399 -ffunction-sections -fdata-sections \
400 -fno-rtti -fno-exceptions
401
Benjamin Walshee659ae2017-01-22 18:06:10 -0500402KBUILD_AFLAGS := -c -g -xassembler-with-cpp -D_ASMLANGUAGE
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600403
Peter Mitsis61eff492015-06-01 11:27:55 -0400404LDFLAGS += $(call ld-option,-nostartfiles)
405LDFLAGS += $(call ld-option,-nodefaultlibs)
406LDFLAGS += $(call ld-option,-nostdlib)
407LDFLAGS += $(call ld-option,-static)
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600408
Anas Nashif32bff692015-06-23 21:11:04 -0400409KERNELVERSION = $(VERSION_MAJOR)$(if $(VERSION_MINOR),.$(VERSION_MINOR)$(if $(PATCHLEVEL),.$(PATCHLEVEL)))$(EXTRAVERSION)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600410
Daniel Thompson90f6ae12016-09-22 15:00:21 +0100411exports += VERSION_MAJOR VERSION_MINOR PATCHLEVEL VERSION_RESERVED EXTRAVERSION
412exports += KERNELRELEASE KERNELVERSION
413exports += ARCH CONFIG_SHELL HOSTCC HOSTCFLAGS CROSS_COMPILE AS LD CC CXX
414exports += CPP AR NM STRIP OBJCOPY OBJDUMP GDB
415exports += MAKE AWK INSTALLKERNEL PERL PYTHON GENIDT GENOFFSET_H
416exports += HOSTCXX HOSTCXXFLAGS CHECK CHECKFLAGS
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600417
Daniel Thompson90f6ae12016-09-22 15:00:21 +0100418exports += KBUILD_CPPFLAGS NOSTDINC_FLAGS ZEPHYRINCLUDE OBJCOPYFLAGS LDFLAGS
419exports += KBUILD_CFLAGS KBUILD_CXXFLAGS CFLAGS_GCOV KBUILD_AFLAGS AFLAGS_KERNEL
Anas Nashifff23cb52016-09-03 15:43:26 -0400420exports += KBUILD_ARFLAGS FIXDEP
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600421
Daniel Thompson90f6ae12016-09-22 15:00:21 +0100422# Push the exports to sub-processes
423export $(exports)
424
425# Make the exports available to third-party build systems
426PHONY += outputexports
427outputexports: Makefile.export
428Makefile.export: include/config/auto.conf FORCE
429 $(call filechk,Makefile.export)
430
431define filechk_Makefile.export
432 (echo "# file is auto-generated, do not modify !"; \
433 echo "BOARD=$(BOARD)"; \
434 echo; \
Daniel Thompson4f0c6f42016-11-08 22:17:36 +0000435 $(foreach e,$(exports),echo $(e)=$($(e));) echo; \
436 echo "include $(O)/include/config/auto.conf";)
Daniel Thompson90f6ae12016-09-22 15:00:21 +0100437endef
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600438
439# Files to ignore in find ... statements
440
441export RCS_FIND_IGNORE := \( -name SCCS -o -name BitKeeper -o -name .svn -o \
442 -name CVS -o -name .pc -o -name .hg -o -name .git \) \
443 -prune -o
444export RCS_TAR_IGNORE := --exclude SCCS --exclude BitKeeper --exclude .svn \
445 --exclude CVS --exclude .pc --exclude .hg --exclude .git
446
447# ===========================================================================
448# Rules shared between *config targets and build targets
449
450# Basic helpers built in scripts/
451PHONY += scripts_basic
Anas Nashifff23cb52016-09-03 15:43:26 -0400452ifeq ($(PREBUILT_HOST_TOOLS),)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600453scripts_basic:
Anas Nashif27603f42015-06-18 10:46:52 -0400454 $(Q)$(MAKE) $(build)=scripts/basic
455 $(Q)$(MAKE) $(build)=scripts/gen_idt
Anas Nashifbd227482015-08-25 10:04:04 -0400456 $(Q)$(MAKE) $(build)=scripts/gen_offset_header
Anas Nashifff23cb52016-09-03 15:43:26 -0400457else
458scripts_basic:
459endif
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600460
461# To avoid any implicit rule to kick in, define an empty command.
462scripts/basic/%: scripts_basic ;
463
464PHONY += outputmakefile
465# outputmakefile generates a Makefile in the output directory, if using a
466# separate output directory. This allows convenient use of make in the
467# output directory.
468outputmakefile:
469ifneq ($(KBUILD_SRC),)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600470 $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkmakefile \
Anas Nashiffde80d72015-08-22 14:40:43 -0400471 $(srctree) $(objtree) $(VERSION_MAJOR) $(VERSION_MINOR)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600472endif
473
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600474# To make sure we do not include .config for any of the *config targets
475# catch them early, and hand them over to scripts/kconfig/Makefile
476# It is allowed to specify more targets when calling make, including
477# mixing *config targets and build targets.
478# For example 'make oldconfig all'.
479# Detect when mixed targets is specified, and make a second invocation
480# of make so .config is not included in this case either (for *config).
481
Juan Manuel Cruz6de34002015-02-15 21:46:45 -0600482version_h := include/generated/version.h
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600483
Anas Nashifa77c7d82016-02-02 07:04:49 -0500484no-dot-config-targets := pristine distclean clean mrproper help kconfig-help \
Anas Nashifb2147172015-11-01 17:55:27 -0500485 cscope gtags TAGS tags help% %docs check% \
486 $(version_h) headers_% kernelversion %src-pkg
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600487
488config-targets := 0
489mixed-targets := 0
490dot-config := 1
491
492ifneq ($(filter $(no-dot-config-targets), $(MAKECMDGOALS)),)
493 ifeq ($(filter-out $(no-dot-config-targets), $(MAKECMDGOALS)),)
494 dot-config := 0
495 endif
496endif
497
Anas Nashifd6ebd182015-05-28 12:54:03 -0400498ifneq ($(filter config %config,$(MAKECMDGOALS)),)
499 config-targets := 1
500 ifneq ($(filter-out config %config,$(MAKECMDGOALS)),)
501 mixed-targets := 1
502 endif
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600503endif
504
505ifeq ($(mixed-targets),1)
506# ===========================================================================
507# We're called with mixed targets (*config and build targets).
508# Handle them one by one.
509
510PHONY += $(MAKECMDGOALS) __build_one_by_one
511
512$(filter-out __build_one_by_one, $(MAKECMDGOALS)): __build_one_by_one
513 @:
514
515__build_one_by_one:
516 $(Q)set -e; \
517 for i in $(MAKECMDGOALS); do \
518 $(MAKE) -f $(srctree)/Makefile $$i; \
519 done
520
521else
522ifeq ($(config-targets),1)
523# ===========================================================================
524# *config targets only - make sure prerequisites are updated, and descend
525# in scripts/kconfig to make the *config target
526
527# Read arch specific Makefile to set KBUILD_DEFCONFIG as needed.
528# KBUILD_DEFCONFIG may point out an alternative default configuration
529# used for 'make defconfig'
Anas Nashifdf73d322015-11-02 20:42:35 -0500530include $(srctree)/arch/$(subst $(DQUOTE),,$(CONFIG_ARCH))/Makefile
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600531export KBUILD_DEFCONFIG KBUILD_KCONFIG
532
533config: scripts_basic outputmakefile FORCE
Anas Nashif27603f42015-06-18 10:46:52 -0400534 $(Q)$(MAKE) $(build)=scripts/kconfig $@
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600535
536%config: scripts_basic outputmakefile FORCE
Anas Nashif27603f42015-06-18 10:46:52 -0400537 $(Q)$(MAKE) $(build)=scripts/kconfig $@
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600538
539else
540# ===========================================================================
Anas Nashif65230942015-06-05 22:41:29 -0400541# Build targets only - this includes zephyr, arch specific targets, clean
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600542# targets and others. In general all targets except *config targets.
543
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600544# Additional helpers built in scripts/
545# Carefully list dependencies so we do not try to build scripts twice
546# in parallel
547PHONY += scripts
Dirk Brandewie991e9d32015-06-01 09:48:10 -0700548scripts: scripts_basic include/config/auto.conf include/config/tristate.conf
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600549 $(Q)$(MAKE) $(build)=$(@)
550
Andrew Boied9a84c52016-07-26 14:45:14 -0700551# arch/ must be last here so that .gnu.linkonce magic for interrupts/exceptions
552# works as expected
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600553
Benjamin Walsh76c76a12016-02-10 14:36:39 -0500554ifneq ($(strip $(MAKEFILE_APP_DIR)),)
555MAKEFILE_APP := $(realpath $(MAKEFILE_APP_DIR)/Makefile.app)
556endif
557
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600558ifneq ($(strip $(PROJECT)),)
Benjamin Walsh76c76a12016-02-10 14:36:39 -0500559ifneq ($(strip $(MAKEFILE_APP)), )
560include $(MAKEFILE_APP)
561else
Anas Nashifa8259af2015-08-10 10:01:15 -0400562-include $(PROJECT)/Makefile.app
Benjamin Walsh76c76a12016-02-10 14:36:39 -0500563endif
Anas Nashifa8259af2015-08-10 10:01:15 -0400564ifneq ($(strip $(KBUILD_ZEPHYR_APP)),)
565export KBUILD_ZEPHYR_APP
566endif
Anas Nashif0c395172015-10-25 09:42:58 -0400567app-y := $(SOURCE_DIR)
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600568endif
569
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600570
571ifeq ($(dot-config),1)
572# Read in config
573-include include/config/auto.conf
574
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600575# Read in dependencies to all Kconfig* files, make sure to run
576# oldconfig if changes are detected.
577-include include/config/auto.conf.cmd
578
579# To avoid any implicit rule to kick in, define an empty command
580$(KCONFIG_CONFIG) include/config/auto.conf.cmd: ;
581
582# If .config is newer than include/config/auto.conf, someone tinkered
583# with it and forgot to run make oldconfig.
584# if auto.conf.cmd is missing then we are probably in a cleaned tree so
585# we execute the config step to be sure to catch updated Kconfig files
586include/config/%.conf: $(KCONFIG_CONFIG) include/config/auto.conf.cmd
587 $(Q)$(MAKE) -f $(srctree)/Makefile silentoldconfig
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600588
589else
590# Dummy target needed, because used as prerequisite
591include/config/auto.conf: ;
592endif # $(dot-config)
593
Andrew Boie625acd52016-11-07 10:27:45 -0800594# kernel objects are built as a static library
Anas Nashifd687a952016-12-17 17:48:47 -0500595libs-y := kernel/
Jukka Rissanen3eaec412016-11-11 23:46:28 +0200596core-y := lib/ misc/ boards/ ext/ subsys/ tests/ arch/
Dmitriy Korovkin19f28842016-10-03 14:58:18 -0400597drivers-y := drivers/
Dmitriy Korovkin9558d472016-09-09 13:59:17 -0400598
Anas Nashifdf73d322015-11-02 20:42:35 -0500599ARCH = $(subst $(DQUOTE),,$(CONFIG_ARCH))
600export ARCH
Juro Bystrickyf25ac092016-07-26 09:44:16 -0700601
Allan Stephens74b98c12015-06-08 11:43:13 -0400602ifdef ZEPHYR_GCC_VARIANT
603include $(srctree)/scripts/Makefile.toolchain.$(ZEPHYR_GCC_VARIANT)
Anas Nashif83d4ea92015-06-18 18:59:37 -0400604else
Anas Nashif47be9a52017-01-15 18:19:59 -0500605ifneq ($(MAKECMDGOALS),htmldocs)
Anas Nashif83d4ea92015-06-18 18:59:37 -0400606$(if $(CROSS_COMPILE),, \
607 $(error ZEPHYR_GCC_VARIANT is not set. ))
Anas Nashifaca245d2015-05-13 00:44:22 -0400608endif
Anas Nashif47be9a52017-01-15 18:19:59 -0500609endif
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600610
Anas Nashif37b1d1e2016-05-25 09:03:33 -0400611-include $(srctree)/ext/Makefile
Anas Nashif1aaa11b2016-06-13 18:35:02 -0400612-include $(srctree)/lib/Makefile
Anas Nashif37b1d1e2016-05-25 09:03:33 -0400613
Juan Manuel Cruz2170ca72016-04-05 16:34:54 -0500614ifneq ($(CC),)
615ifeq ($(shell $(CC) -v 2>&1 | grep -Ec "clang version|icx version"), 1)
616COMPILER := clang
617else
618COMPILER := gcc
619endif
620export COMPILER
621endif
622
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600623# The all: target is the default when no target is given on the
624# command line.
625# This allow a user to issue only 'make' to build a kernel including modules
Anas Nashif65230942015-06-05 22:41:29 -0400626# Defaults to zephyr, but the arch makefile usually adds further targets
627all: zephyr
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600628
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600629ifdef CONFIG_READABLE_ASM
630# Disable optimizations that make assembler listings hard to read.
631# reorder blocks reorders the control in the function
632# ipa clone creates specialized cloned functions
633# partial inlining inlines only parts of functions
634KBUILD_CFLAGS += $(call cc-option,-fno-reorder-blocks,) \
635 $(call cc-option,-fno-ipa-cp-clone,) \
636 $(call cc-option,-fno-partial-inlining)
637endif
638
Anas Nashifeb1fdf52015-07-29 09:33:32 -0400639ifeq ($(CONFIG_DEBUG),y)
Andrew Boie11693c12016-07-22 12:15:40 -0700640KBUILD_CFLAGS += -Og
Anas Nashifeb1fdf52015-07-29 09:33:32 -0400641else
642KBUILD_CFLAGS += -Os
643endif
644
Andre Guedes1622e4f2016-01-07 14:21:22 -0200645ifeq ($(CONFIG_STACK_CANARIES),y)
646KBUILD_CFLAGS += $(call cc-option,-fstack-protector-all,)
647else
648KBUILD_CFLAGS += $(call cc-option,-fno-stack-protector,)
649endif
650
Andrew Boie63cea242016-06-14 10:55:57 -0700651ifeq ($(CONFIG_OMIT_FRAME_POINTER),y)
652fp_arg := $(call cc-option,-fomit-frame-pointer,)
653else
654fp_arg := $(call cc-option,-fno-omit-frame-pointer,)
655endif
656KBUILD_CFLAGS += $(fp_arg)
657KBUILD_CXXFLAGS += $(fp_arg)
658
Anas Nashif3852c8c2015-08-22 18:14:42 -0400659KBUILD_CFLAGS += $(subst $(DQUOTE),,$(CONFIG_COMPILER_OPT))
660
Anas Nashif06e78de2015-11-01 11:59:09 -0500661export LDFLAG_LINKERCMD
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600662
Anas Nashifd2f1c762016-04-03 09:37:43 -0400663ifeq ($(SOC_SERIES),)
664SOC_PATH = $(SOC_NAME)
665else
666SOC_PATH = $(SOC_FAMILY)/$(SOC_SERIES)
667endif
Anas Nashif3a87dae2015-12-06 09:51:33 -0500668include arch/$(ARCH)/Makefile
Anas Nashifd9af71b2015-09-23 15:54:28 -0400669
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600670KBUILD_CFLAGS += $(CFLAGS)
Peter Mitsis8e35cc82016-01-13 13:02:56 -0500671KBUILD_CXXFLAGS += $(CXXFLAGS)
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600672KBUILD_AFLAGS += $(CFLAGS)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600673
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600674
Anas Nashif1b702152015-08-23 22:01:07 -0400675ifeq ($(COMPILER),clang)
Anas Nashif1b702152015-08-23 22:01:07 -0400676KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,)
677KBUILD_CFLAGS += $(call cc-disable-warning, unused-variable)
678KBUILD_CFLAGS += $(call cc-disable-warning, format-invalid-specifier)
679KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
680# Quiet clang warning: comparison of unsigned expression < 0 is always false
681KBUILD_CFLAGS += $(call cc-disable-warning, tautological-compare)
682else
683
684# This warning generated too much noise in a regular build.
685# Use make W=1 to enable this warning (see scripts/Makefile.build)
686KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
687KBUILD_CFLAGS += $(call cc-option,-fno-reorder-functions)
688KBUILD_CFLAGS += $(call cc-option,-fno-defer-pop)
689endif
690
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600691# We trigger additional mismatches with less inlining
692ifdef CONFIG_DEBUG_SECTION_MISMATCH
693KBUILD_CFLAGS += $(call cc-option, -fno-inline-functions-called-once)
694endif
695
696# arch Makefile may override CC so keep this after arch Makefile is included
697NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
Anas Nashifef2fc592016-04-17 11:18:39 +0800698NOSTDINC_FLAGS += -isystem $(shell $(CC) -print-file-name=include-fixed)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600699CHECKFLAGS += $(NOSTDINC_FLAGS)
700
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600701# disable pointer signed / unsigned warnings in gcc 4.0
702KBUILD_CFLAGS += $(call cc-disable-warning, pointer-sign)
703
704# disable invalid "can't wrap" optimizations for signed / pointers
705KBUILD_CFLAGS += $(call cc-option,-fno-strict-overflow)
706
Anas Nashif20439b42015-10-14 11:03:41 -0400707# generate an extra file that specifies the maximum amount of stack used,
708# on a per-function basis.
Anas Nashif9fe306b2015-12-05 09:36:38 -0500709ifdef CONFIG_STACK_USAGE
Anas Nashif20439b42015-10-14 11:03:41 -0400710KBUILD_CFLAGS += $(call cc-option,-fstack-usage)
Anas Nashif9fe306b2015-12-05 09:36:38 -0500711endif
Anas Nashif20439b42015-10-14 11:03:41 -0400712
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600713
714# disallow errors like 'EXPORT_GPL(foo);' with missing header
715KBUILD_CFLAGS += $(call cc-option,-Werror=implicit-int)
716
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600717# Prohibit date/time macros, which would make the build non-deterministic
Anas Nashif47f80882015-06-22 19:44:37 -0400718# KBUILD_CFLAGS += $(call cc-option,-Werror=date-time)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600719
720# use the deterministic mode of AR if available
721KBUILD_ARFLAGS := $(call ar-option,D)
722
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600723include $(srctree)/scripts/Makefile.extrawarn
724
725# Add user supplied CPPFLAGS, AFLAGS and CFLAGS as the last assignments
726KBUILD_CPPFLAGS += $(KCPPFLAGS)
727KBUILD_AFLAGS += $(KAFLAGS)
728KBUILD_CFLAGS += $(KCFLAGS)
729
730# Use --build-id when available.
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600731
Anas Nashif74326ec2016-05-01 17:44:15 -0400732LINKFLAGPREFIX ?= -Wl,
Anas Nashif204b66d2015-12-31 12:08:23 -0500733LDFLAGS_zephyr += $(LDFLAGS)
Anas Nashif74326ec2016-05-01 17:44:15 -0400734LDFLAGS_zephyr += $(call cc-ldoption,$(LINKFLAGPREFIX)-X)
735LDFLAGS_zephyr += $(call cc-ldoption,$(LINKFLAGPREFIX)-N)
736LDFLAGS_zephyr += $(call cc-ldoption,$(LINKFLAGPREFIX)--gc-sections)
737LDFLAGS_zephyr += $(call cc-ldoption,$(LINKFLAGPREFIX)--build-id=none)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600738
Juan Manuel Cruz0d01f5b2015-02-22 14:29:18 -0600739LD_TOOLCHAIN ?= -D__GCC_LINKER_CMD__
Juan Manuel Cruz1c8f5a72015-04-30 17:19:04 -0500740
Anas Nashif6c5509f2016-01-05 08:42:23 -0500741ifdef CONFIG_HAVE_CUSTOM_LINKER_SCRIPT
Benjamin Walshec89a102016-08-15 17:24:28 -0400742KBUILD_LDS := $(subst $(DQUOTE),,$(PROJECT_BASE)/$(CONFIG_CUSTOM_LINKER_SCRIPT))
743ifeq ($(wildcard $(KBUILD_LDS)),)
744KBUILD_LDS := $(subst $(DQUOTE),,$(CONFIG_CUSTOM_LINKER_SCRIPT))
745endif
Anas Nashif6c5509f2016-01-05 08:42:23 -0500746else
747# Try a board specific linker file
Kumar Gala8d357602016-10-19 15:13:41 -0500748KBUILD_LDS := $(srctree)/boards/$(ARCH)/$(BOARD_NAME)/linker.ld
Anas Nashif6c5509f2016-01-05 08:42:23 -0500749
750# If not available, try an SoC specific linker file
751ifeq ($(wildcard $(KBUILD_LDS)),)
Andrew Boieff872dc2016-05-04 13:13:56 -0700752KBUILD_LDS := $(srctree)/arch/$(ARCH)/soc/$(SOC_PATH)/linker.ld
Anas Nashif6c5509f2016-01-05 08:42:23 -0500753endif
754endif
755
756export LD_TOOLCHAIN KBUILD_LDS
Juan Manuel Cruz0d01f5b2015-02-22 14:29:18 -0600757
Juro Bystrickyf25ac092016-07-26 09:44:16 -0700758ifdef MAKEFILE_TOOLCHAIN_DO_PASS2
759# KBUILD_CFLAGS known at this point.
760# We can now determine which multilib libraries to use
761include $(srctree)/scripts/Makefile.toolchain.$(ZEPHYR_GCC_VARIANT)
762endif
763
Daniel Thompson389ed152016-09-11 20:51:22 +0100764QEMU = $(addsuffix /,$(QEMU_BIN_PATH))$(QEMU_$(ARCH))
Juro Bystrickyf25ac092016-07-26 09:44:16 -0700765
Anas Nashif06e78de2015-11-01 11:59:09 -0500766# The all: target is the default when no target is given on the
767# command line.
768# This allow a user to issue only 'make' to build a kernel including modules
769# Defaults to zephyr, but the arch makefile usually adds further targets
Anas Nashif6361be22016-05-22 12:34:06 -0400770all: $(KERNEL_BIN_NAME) $(KERNEL_STAT_NAME)
Anas Nashif06e78de2015-11-01 11:59:09 -0500771
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600772# Default kernel image to build when no specific target is given.
773# KBUILD_IMAGE may be overruled on the command line or
774# set in the environment
775# Also any assignments in arch/$(ARCH)/Makefile take precedence over
776# this default value
Anas Nashif65230942015-06-05 22:41:29 -0400777export KBUILD_IMAGE ?= zephyr
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600778
Anas Nashifc0078cf2015-10-25 14:02:19 -0400779zephyr-dirs := $(patsubst %/,%,$(filter %/, $(core-y) $(drivers-y) \
Kumar Galaff694e92016-06-16 16:16:46 -0500780 $(libs-y)))
Juan Manuel Cruz941059c2016-08-26 17:24:13 -0500781
782# Workaround for some make notdir implementations that require
783# the paramenter not to end in "/".
784zephyr-app-dir-root-name := $(patsubst %/,%, $(SOURCE_DIR))
785
Juan Manuel Cruz5bd398d2016-08-23 15:06:38 -0500786zephyr-app-dir-root := $(abspath $(patsubst %, %/.., $(SOURCE_DIR)))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600787
Juan Manuel Cruz5bd398d2016-08-23 15:06:38 -0500788zephyr-alldirs := $(sort $(zephyr-dirs) $(SOURCE_DIR) $(patsubst %/,%,$(filter %/, \
Anas Nashifce5dc6a2015-12-06 20:02:33 -0500789 $(core-) $(drivers-) $(libs-) $(app-))))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600790
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600791core-y := $(patsubst %/, %/built-in.o, $(core-y))
Juan Manuel Cruz941059c2016-08-26 17:24:13 -0500792app-y := $(patsubst %, %/built-in.o, $(notdir $(zephyr-app-dir-root-name)))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600793drivers-y := $(patsubst %/, %/built-in.o, $(drivers-y))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600794libs-y1 := $(patsubst %/, %/lib.a, $(libs-y))
795libs-y2 := $(patsubst %/, %/built-in.o, $(libs-y))
796libs-y := $(libs-y1) $(libs-y2)
797
Andrew Boied9a84c52016-07-26 14:45:14 -0700798# core-y must be last here. several arches use .gnu.linkonce magic
799# to register interrupt or exception handlers, and defaults under
800# arch/ (part of core-y) must be linked after drivers or libs.
Kumar Galaff694e92016-06-16 16:16:46 -0500801export KBUILD_ZEPHYR_MAIN := $(drivers-y) $(libs-y) $(core-y)
Anas Nashif65230942015-06-05 22:41:29 -0400802export LDFLAGS_zephyr
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600803
Anas Nashif1dd49602016-11-16 09:08:48 -0500804zephyr-deps := $(KBUILD_LDS) $(KBUILD_ZEPHYR_MAIN) $(app-y)
805
Anas Nashif369cf082015-05-17 08:55:21 -0400806ALL_LIBS += $(TOOLCHAIN_LIBS)
807export ALL_LIBS
808
Anas Nashif06e78de2015-11-01 11:59:09 -0500809LINK_LIBS := $(foreach l,$(ALL_LIBS), -l$(l))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600810
Anas Nashif06e78de2015-11-01 11:59:09 -0500811OUTPUT_FORMAT ?= elf32-i386
812OUTPUT_ARCH ?= i386
813
Kumar Gala44b712d2016-06-03 10:52:12 -0500814quiet_cmd_ar_target = AR $@
Dmitriy Korovkin19f28842016-10-03 14:58:18 -0400815# Do not put lib.a into libzephyr.a. lib.a files are to be linked separately to
816# the final image
Dmitriy Korovkin9558d472016-09-09 13:59:17 -0400817cmd_ar_target = rm -f $@; $(AR) rcT$(KBUILD_ARFLAGS) $@ \
Dmitriy Korovkin19f28842016-10-03 14:58:18 -0400818 $(filter-out %/lib.a, $(KBUILD_ZEPHYR_MAIN))
Anas Nashiff4ed1082015-12-06 19:27:22 -0500819libzephyr.a: $(zephyr-deps)
Kumar Gala44b712d2016-06-03 10:52:12 -0500820 $(call cmd,ar_target)
Anas Nashiff4ed1082015-12-06 19:27:22 -0500821
Anas Nashif06e78de2015-11-01 11:59:09 -0500822quiet_cmd_create-lnk = LINK $@
823 cmd_create-lnk = \
824( \
825 echo $(LDFLAGS_zephyr); \
Anas Nashifd53c2372016-06-13 18:31:00 -0400826 echo "$(LINKFLAGPREFIX)-Map=$(KERNEL_NAME).map"; \
Anas Nashif06e78de2015-11-01 11:59:09 -0500827 echo "-L $(objtree)/include/generated"; \
828 echo "-u _OffsetAbsSyms -u _ConfigAbsSyms"; \
829 echo "-e __start"; \
Anas Nashif74326ec2016-05-01 17:44:15 -0400830 echo "$(LINKFLAGPREFIX)--start-group"; \
831 echo "$(LINKFLAGPREFIX)--whole-archive"; \
832 echo "$(KBUILD_ZEPHYR_APP)"; \
Kumar Galaff694e92016-06-16 16:16:46 -0500833 echo "$(app-y)"; \
Anas Nashiff4ed1082015-12-06 19:27:22 -0500834 echo "libzephyr.a"; \
Anas Nashif74326ec2016-05-01 17:44:15 -0400835 echo "$(LINKFLAGPREFIX)--no-whole-archive"; \
Dmitriy Korovkin19f28842016-10-03 14:58:18 -0400836 echo "$(filter %/lib.a, $(KBUILD_ZEPHYR_MAIN))"; \
Anas Nashif06e78de2015-11-01 11:59:09 -0500837 echo "$(objtree)/arch/$(ARCH)/core/offsets/offsets.o"; \
Anas Nashif74326ec2016-05-01 17:44:15 -0400838 echo "$(LINKFLAGPREFIX)--end-group"; \
Anas Nashif06e78de2015-11-01 11:59:09 -0500839 echo "$(LIB_INCLUDE_DIR) $(LINK_LIBS)"; \
840) > $@
841
Anas Nashifa0cf03b2016-05-14 21:56:35 -0400842$(KERNEL_NAME).lnk: $(zephyr-deps)
Anas Nashif06e78de2015-11-01 11:59:09 -0500843 $(call cmd,create-lnk)
844
Anas Nashif872676c2016-02-02 07:58:26 -0500845linker.cmd: $(zephyr-deps)
Anas Nashif06e78de2015-11-01 11:59:09 -0500846 $(Q)$(CC) -x assembler-with-cpp -nostdinc -undef -E -P \
847 $(LDFLAG_LINKERCMD) $(LD_TOOLCHAIN) -I$(srctree)/include \
Andrew Boie82f11bf2016-11-21 14:16:11 -0800848 -I$(SOURCE_DIR) \
Anas Nashif20fe6782016-01-31 04:46:37 -0500849 -I$(objtree)/include/generated $(EXTRA_LINKER_CMD_OPT) $(KBUILD_LDS) -o $@
Anas Nashif06e78de2015-11-01 11:59:09 -0500850
Andy Ross018b8312016-08-25 10:18:09 -0700851PREBUILT_KERNEL = $(KERNEL_NAME)_prebuilt.elf
Anas Nashif06e78de2015-11-01 11:59:09 -0500852
Andy Ross018b8312016-08-25 10:18:09 -0700853$(PREBUILT_KERNEL): $(zephyr-deps) libzephyr.a $(KBUILD_ZEPHYR_APP) $(app-y) linker.cmd $(KERNEL_NAME).lnk
Anas Nashif74326ec2016-05-01 17:44:15 -0400854 $(Q)$(CC) -T linker.cmd @$(KERNEL_NAME).lnk -o $@
Anas Nashif06e78de2015-11-01 11:59:09 -0500855
856quiet_cmd_gen_idt = SIDT $@
Andrew Boie2ee9aca2016-01-27 12:19:15 -0800857 cmd_gen_idt = \
858( \
859 $(OBJCOPY) -I $(OUTPUT_FORMAT) -O binary -j intList $< isrList.bin && \
860 $(GENIDT) -i isrList.bin -n $(CONFIG_IDT_NUM_VECTORS) -o staticIdt.bin \
Andrew Boie238fe1f2016-10-21 10:30:21 -0700861 -m irq_int_vector_map.bin \
Andrew Boie0d03e322016-07-26 11:36:31 -0700862 -l $(CONFIG_MAX_IRQ_LINES) $(GENIDT_EXTRA_ARGS) && \
Andrew Boie2ee9aca2016-01-27 12:19:15 -0800863 $(OBJCOPY) -I binary -B $(OUTPUT_ARCH) -O $(OUTPUT_FORMAT) \
864 --rename-section .data=staticIdt staticIdt.bin staticIdt.o && \
865 $(OBJCOPY) -I binary -B $(OUTPUT_ARCH) -O $(OUTPUT_FORMAT) \
Andrew Boie2ee9aca2016-01-27 12:19:15 -0800866 --rename-section .data=irq_int_vector_map irq_int_vector_map.bin \
867 irq_int_vector_map.o && \
Andrew Boie238fe1f2016-10-21 10:30:21 -0700868 rm staticIdt.bin irq_int_vector_map.bin isrList.bin \
Anas Nashif06e78de2015-11-01 11:59:09 -0500869)
870
Andy Ross018b8312016-08-25 10:18:09 -0700871staticIdt.o: $(PREBUILT_KERNEL)
Anas Nashif06e78de2015-11-01 11:59:09 -0500872 $(call cmd,gen_idt)
873
874quiet_cmd_lnk_elf = LINK $@
875 cmd_lnk_elf = \
876( \
Andrew Boie238fe1f2016-10-21 10:30:21 -0700877 $(CC) -T linker.cmd @$(KERNEL_NAME).lnk staticIdt.o \
878 irq_int_vector_map.o -o $@; \
Ido Yariva8c48ea2016-01-04 14:59:09 -0500879 ${OBJCOPY} --change-section-address intList=${CONFIG_PHYS_LOAD_ADDR} $@ elf.tmp;\
Anas Nashif06e78de2015-11-01 11:59:09 -0500880 $(OBJCOPY) -R intList elf.tmp $@; \
881 rm elf.tmp \
882)
883
Benjamin Walsh0107c802016-05-18 16:24:59 -0400884ASSERT_WARNING_STR := \
885 "\n ------------------------------------------------------------" \
886 "\n --- WARNING: __ASSERT() statements are globally ENABLED ---" \
887 "\n --- The kernel will run more slowly and uses more memory ---" \
888 "\n ------------------------------------------------------------\n\n" \
889
890WARN_ABOUT_ASSERT := $(if $(CONFIG_ASSERT),echo -e -n $(ASSERT_WARNING_STR),true)
891
Andrew Boiea5ae8892016-10-19 13:05:35 -0700892
893DEPRECATION_WARNING_STR := \
894 "\n WARNING: The board '$(BOARD)' is deprecated and will be" \
895 "\n removed in version $(CONFIG_BOARD_DEPRECATED)\n\n"
896
897WARN_ABOUT_DEPRECATION := $(if $(CONFIG_BOARD_DEPRECATED),echo -e \
898 -n $(DEPRECATION_WARNING_STR),true)
899
Anas Nashif06e78de2015-11-01 11:59:09 -0500900ifeq ($(ARCH),x86)
Andrew Boiecaf19b22016-10-21 10:39:43 -0700901$(KERNEL_ELF_NAME): staticIdt.o linker.cmd
Anas Nashif06e78de2015-11-01 11:59:09 -0500902 $(call cmd,lnk_elf)
Andy Ross4cc228b2016-10-03 12:20:39 -0700903 @$(srctree)/scripts/check_link_map.py $(KERNEL_NAME).map
Benjamin Walsh0107c802016-05-18 16:24:59 -0400904 @$(WARN_ABOUT_ASSERT)
Andrew Boiea5ae8892016-10-19 13:05:35 -0700905 @$(WARN_ABOUT_DEPRECATION)
Anas Nashif06e78de2015-11-01 11:59:09 -0500906else
Andy Ross018b8312016-08-25 10:18:09 -0700907$(KERNEL_ELF_NAME): $(PREBUILT_KERNEL)
908 @cp $(PREBUILT_KERNEL) $(KERNEL_ELF_NAME)
Benjamin Walsh0107c802016-05-18 16:24:59 -0400909 @$(WARN_ABOUT_ASSERT)
Andrew Boiea5ae8892016-10-19 13:05:35 -0700910 @$(WARN_ABOUT_DEPRECATION)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600911endif
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600912
Anas Nashif06e78de2015-11-01 11:59:09 -0500913
914quiet_cmd_gen_bin = BIN $@
915 cmd_gen_bin = \
916( \
917 $(OBJDUMP) -S $< > $(KERNEL_NAME).lst; \
918 $(OBJCOPY) -S -O binary -R .note -R .comment -R COMMON -R .eh_frame $< $@; \
919 $(STRIP) -s -o $(KERNEL_NAME).strip $<; \
920)
921
922$(KERNEL_BIN_NAME): $(KERNEL_ELF_NAME)
923 $(call cmd,gen_bin)
924
Kumar Galabbf736c2016-10-24 10:57:46 -0500925quiet_cmd_gen_hex = HEX $@
926 cmd_gen_hex = \
927( \
928 $(OBJCOPY) -S -O ihex -R .note -R .comment -R COMMON -R .eh_frame $< $@; \
929)
930
931$(KERNEL_HEX_NAME): $(KERNEL_ELF_NAME)
932 $(call cmd,gen_hex)
933
Anas Nashif6361be22016-05-22 12:34:06 -0400934$(KERNEL_STAT_NAME): $(KERNEL_BIN_NAME) $(KERNEL_ELF_NAME)
935 @$(READELF) -e $(KERNEL_ELF_NAME) > $@
936
937ram_report: $(KERNEL_STAT_NAME)
938 @$(srctree)/scripts/size_report -r -o $(O)
939rom_report: $(KERNEL_STAT_NAME)
940 @$(srctree)/scripts/size_report -F -o $(O)
941
Anas Nashif06e78de2015-11-01 11:59:09 -0500942zephyr: $(zephyr-deps) $(KERNEL_BIN_NAME)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600943
944# The actual objects are generated when descending,
945# make sure no implicit rule kicks in
Juan Manuel Cruz5bd398d2016-08-23 15:06:38 -0500946$(sort $(zephyr-deps)): $(zephyr-dirs) zephyr-app-dir ;
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600947
Anas Nashif65230942015-06-05 22:41:29 -0400948# Handle descending into subdirectories listed in $(zephyr-dirs)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600949# Preset locale variables to speed up the build process. Limit locale
950# tweaks to this spot to avoid wrong language settings when running
951# make menuconfig etc.
952# Error messages still appears in the original language
953
Anas Nashif65230942015-06-05 22:41:29 -0400954PHONY += $(zephyr-dirs)
955$(zephyr-dirs): prepare scripts
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600956 $(Q)$(MAKE) $(build)=$@
957
Juan Manuel Cruz5bd398d2016-08-23 15:06:38 -0500958PHONY += zephyr-app-dir
959zephyr-app-dir: prepare scripts
Juan Manuel Cruz941059c2016-08-26 17:24:13 -0500960 $(Q)$(MAKE) $(build)=$(notdir $(zephyr-app-dir-root-name)) \
961 srctree=$(zephyr-app-dir-root)
Kumar Galaff694e92016-06-16 16:16:46 -0500962
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600963# Things we need to do before we recursively start building the kernel
964# or the modules are listed in "prepare".
965# A multi level approach is used. prepareN is processed before prepareN-1.
966# archprepare is used in arch Makefiles and when processed asm symlink,
967# version.h and scripts_basic is processed / created.
968
969# Listed in dependency order
Juan Manuel Cruz0167ee42015-06-05 13:08:53 -0500970PHONY += prepare prepare1 prepare2 prepare3
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600971
972# prepare3 is used to check if we are building in a separate output directory,
973# and if so do:
974# 1) Check that make has not been executed in the kernel src $(srctree)
Juan Manuel Cruz8b16a172015-05-22 18:38:45 -0500975prepare3:
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600976ifneq ($(KBUILD_SRC),)
977 @$(kecho) ' Using $(srctree) as source for kernel'
Juan Manuel Cruzce3533a2015-02-23 16:48:02 -0600978 $(Q)if [ -f $(srctree)/.config ]; then \
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600979 echo >&2 " $(srctree) is not clean, please run 'make mrproper'"; \
980 echo >&2 " in the '$(srctree)' directory.";\
981 /bin/false; \
982 fi;
983endif
984
985# prepare2 creates a makefile if using a separate output directory
Juan Manuel Cruz8b16a172015-05-22 18:38:45 -0500986prepare2: prepare3 outputmakefile
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600987
Juan Manuel Cruz8b16a172015-05-22 18:38:45 -0500988prepare1: prepare2 $(version_h) \
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600989 include/config/auto.conf
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600990
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600991archprepare_common = $(strip \
Anas Nashifb2147172015-11-01 17:55:27 -0500992 prepare1 scripts_basic \
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600993 )
994
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600995
Juan Manuel Cruz0167ee42015-06-05 13:08:53 -0500996archprepare = $(strip \
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600997 $(archprepare_common) \
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600998 )
999
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001000# All the preparing..
Juan Manuel Cruz0167ee42015-06-05 13:08:53 -05001001prepare: $(archprepare) FORCE
1002 $(Q)$(MAKE) $(build)=.
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001003
1004# Generate some files
1005# ---------------------------------------------------------------------------
1006
1007# KERNELRELEASE can change from a few different places, meaning version.h
1008# needs to be updated, so this check is forced on all builds
1009
Anas Nashif71bed172015-06-23 19:10:06 -04001010VERSION_MAJOR_HEX=$(shell printf '%02x\n' ${VERSION_MAJOR})
1011VERSION_MINOR_HEX=$(shell printf '%02x\n' ${VERSION_MINOR})
Anas Nashif4cf6c312015-06-23 19:19:45 -04001012PATCHLEVEL_HEX=$(shell printf '%02x\n' ${PATCHLEVEL})
Anas Nashif8ad48782015-06-23 10:29:17 -04001013VERSION_RESERVED_HEX=00
Anas Nashif4cf6c312015-06-23 19:19:45 -04001014KERNEL_VERSION_HEX=0x$(VERSION_MAJOR_HEX)$(VERSION_MINOR_HEX)$(PATCHLEVEL_HEX)
Juan Manuel Cruz6de34002015-02-15 21:46:45 -06001015
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001016define filechk_version.h
Juan Manuel Cruz6de34002015-02-15 21:46:45 -06001017 (echo "#ifndef _KERNEL_VERSION_H_"; \
1018 echo "#define _KERNEL_VERSION_H_"; \
1019 echo ;\
Anas Nashif4cf6c312015-06-23 19:19:45 -04001020 (echo \#define ZEPHYR_VERSION_CODE $(shell \
1021 expr $(VERSION_MAJOR) \* 65536 + 0$(VERSION_MINOR) \* 256 + 0$(PATCHLEVEL)); \
1022 echo '#define ZEPHYR_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))';); \
1023 echo ;\
Anas Nashif9c72cf52015-08-25 10:44:41 -04001024 echo "#define KERNELVERSION \\"; \
Anas Nashif71bed172015-06-23 19:10:06 -04001025 echo "$(KERNEL_VERSION_HEX)$(VERSION_RESERVED_HEX)"; \
Juan Manuel Cruz6de34002015-02-15 21:46:45 -06001026 echo "#define KERNEL_VERSION_NUMBER $(KERNEL_VERSION_HEX)"; \
Juan Manuel Cruz6de34002015-02-15 21:46:45 -06001027 echo "#define KERNEL_VERSION_MAJOR $(VERSION_MAJOR)"; \
1028 echo "#define KERNEL_VERSION_MINOR $(VERSION_MINOR)"; \
Anas Nashif4cf6c312015-06-23 19:19:45 -04001029 echo "#define KERNEL_PATCHLEVEL $(PATCHLEVEL)"; \
Juan Manuel Cruz6de34002015-02-15 21:46:45 -06001030 echo "#define KERNEL_VERSION_STRING \"$(KERNELVERSION)\""; \
1031 echo; \
1032 echo "#endif /* _KERNEL_VERSION_H_ */";)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001033endef
1034
1035$(version_h): $(srctree)/Makefile FORCE
1036 $(call filechk,version.h)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001037
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001038PHONY += headerdep
1039headerdep:
1040 $(Q)find $(srctree)/include/ -name '*.h' | xargs --max-args 1 \
1041 $(srctree)/scripts/headerdep.pl -I$(srctree)/include
1042
1043# ---------------------------------------------------------------------------
1044
1045PHONY += depend dep
1046depend dep:
1047 @echo '*** Warning: make $@ is unnecessary now.'
1048
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001049###
1050# Cleaning is done on three levels.
1051# make clean Delete most generated files
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001052# make mrproper Delete the current configuration, and all generated files
1053# make distclean Remove editor backup files, patch leftover files and the like
1054
1055# Directories & files removed with 'make clean'
1056CLEAN_DIRS += $(MODVERDIR)
1057
Anas Nashif8e9d8eb2015-05-29 11:48:25 -04001058CLEAN_FILES += misc/generated/sysgen/kernel_main.c \
Anas Nashif8b45f4e2015-10-21 07:24:39 -04001059 misc/generated/sysgen/sysgen.h \
Anas Nashif06e78de2015-11-01 11:59:09 -05001060 misc/generated/sysgen/prj.mdef \
Kumar Gala62a9f9c2016-07-09 09:30:52 -05001061 misc/generated/sysgen/micro_private_types.h \
1062 misc/generated/sysgen/kernel_main.h \
Anas Nashif06e78de2015-11-01 11:59:09 -05001063 .old_version .tmp_System.map .tmp_version \
1064 .tmp_* System.map *.lnk *.map *.elf *.lst \
Kumar Galabdd77642016-10-24 11:00:32 -05001065 *.bin *.hex *.stat *.strip staticIdt.o linker.cmd
Juan Manuel Cruzce3533a2015-02-23 16:48:02 -06001066
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001067# Directories & files removed with 'make mrproper'
1068MRPROPER_DIRS += include/config usr/include include/generated \
1069 arch/*/include/generated .tmp_objdiff
Anas Nashif1e8dc5a2015-04-24 18:32:55 -04001070MRPROPER_FILES += .config .config.old .version $(version_h) \
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001071 Module.symvers tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS \
1072 signing_key.priv signing_key.x509 x509.genkey \
1073 extra_certificates signing_key.x509.keyid \
1074 signing_key.x509.signer
1075
Anas Nashiff05ee8f2015-06-05 21:38:07 -04001076# clean - Delete most
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001077#
1078clean: rm-dirs := $(CLEAN_DIRS)
1079clean: rm-files := $(CLEAN_FILES)
Anas Nashif65230942015-06-05 22:41:29 -04001080clean-dirs := $(addprefix _clean_, . $(zephyr-alldirs) )
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001081
Juan Manuel Cruz5bd398d2016-08-23 15:06:38 -05001082PHONY += clean-dirs-target clean archclean zephyrclean
1083clean-dirs-target:
1084 $(Q)$(MAKE) $(clean)=$(patsubst _clean_%,%,$(clean-dirs))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001085
Anas Nashif06e78de2015-11-01 11:59:09 -05001086clean: archclean
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001087
1088# mrproper - Delete all generated files, including .config
1089#
1090mrproper: rm-dirs := $(wildcard $(MRPROPER_DIRS))
1091mrproper: rm-files := $(wildcard $(MRPROPER_FILES))
Juan Manuel Cruzce3533a2015-02-23 16:48:02 -06001092mrproper-dirs := $(addprefix _mrproper_,scripts)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001093
1094PHONY += $(mrproper-dirs) mrproper archmrproper
1095$(mrproper-dirs):
1096 $(Q)$(MAKE) $(clean)=$(patsubst _mrproper_%,%,$@)
1097
1098mrproper: clean archmrproper $(mrproper-dirs)
1099 $(call cmd,rmdirs)
1100 $(call cmd,rmfiles)
1101
1102# distclean
1103#
1104PHONY += distclean
1105
1106distclean: mrproper
1107 @find $(srctree) $(RCS_FIND_IGNORE) \
1108 \( -name '*.orig' -o -name '*.rej' -o -name '*~' \
1109 -o -name '*.bak' -o -name '#*#' -o -name '.*.orig' \
1110 -o -name '.*.rej' -o -name '*%' -o -name 'core' \) \
1111 -type f -print | xargs rm -f
1112
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001113# Brief documentation of the typical targets used
1114# ---------------------------------------------------------------------------
1115
Kumar Gala8d357602016-10-19 15:13:41 -05001116boards := $(wildcard $(srctree)/boards/*/*/*_defconfig)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001117boards := $(sort $(notdir $(boards)))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001118
Anas Nashifa77c7d82016-02-02 07:04:49 -05001119kconfig-help:
1120 @echo 'Configuration targets:'
1121 @echo ''
1122 @$(MAKE) -f $(srctree)/scripts/kconfig/Makefile help
1123 @echo ''
1124
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001125help:
1126 @echo 'Cleaning targets:'
Juan Manuel Cruza5a6ec32015-10-02 13:07:55 -05001127 @echo ' clean - Remove most generated files but keep configuration and backup files'
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001128 @echo ' mrproper - Remove all generated files + config + various backup files'
1129 @echo ' distclean - mrproper + remove editor backup and patch files'
Juan Manuel Cruza5a6ec32015-10-02 13:07:55 -05001130 @echo ' pristine - Remove the output directory with all generated files'
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001131 @echo ''
1132 @echo 'Configuration targets:'
Anas Nashifa77c7d82016-02-02 07:04:49 -05001133 @echo ''
1134 @echo ' run <make kconfig-help>'
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001135 @echo ''
1136 @echo 'Other generic targets:'
1137 @echo ' all - Build all targets marked with [*]'
Anas Nashif7d348b02016-01-31 08:22:18 -05001138 @echo '* zephyr - Build a zephyr application'
Anas Nashif2bc9d692017-01-07 16:31:34 -05001139 @echo ' run - Build a zephyr application and run it if board supports emulation'
1140 @echo ' qemu - Build a zephyr application and run it in qemu [deprecated]'
1141 @echo ' qemugdb - Same as 'qemu' but start a GDB server on port 1234 [deprecated]'
Anas Nashif7d348b02016-01-31 08:22:18 -05001142 @echo ' flash - Build and flash an application'
Anas Nashifa77c7d82016-02-02 07:04:49 -05001143 @echo ' debug - Build and debug an application using GDB'
Anas Nashif6d9ed992017-01-07 13:22:21 -05001144 @echo ' debugserver - Build and start a GDB server (port 1234 for Qemu targets)'
Anas Nashif1b4742f2016-09-24 15:07:28 -04001145 @echo ' ram_report - Build and create RAM usage report'
1146 @echo ' rom_report - Build and create ROM usage report'
Juan Manuel Cruza5a6ec32015-10-02 13:07:55 -05001147 @echo ''
Anas Nashif10bb38c2015-12-17 08:54:35 -05001148 @echo 'Supported Boards:'
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001149 @echo ''
Anas Nashif7d348b02016-01-31 08:22:18 -05001150 @echo ' To build an image for one of the supported boards below, run:'
1151 @echo ''
1152 @echo ' make BOARD=<BOARD NAME>'
1153 @echo ' in the application directory.'
Anas Nashifa77c7d82016-02-02 07:04:49 -05001154 @echo ''
Anas Nashif7d348b02016-01-31 08:22:18 -05001155 @echo ' To flash the image (if supported), run:'
1156 @echo ''
1157 @echo ' make BOARD=<BOARD NAME> flash'
1158 @echo ''
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001159 @$(if $(boards), \
1160 $(foreach b, $(boards), \
Anas Nashifa77c7d82016-02-02 07:04:49 -05001161 printf " make BOARD=%-24s - Build for %s\\n" $(subst _defconfig,,$(b)) $(subst _defconfig,,$(b));) \
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001162 echo '')
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001163
Anas Nashifa77c7d82016-02-02 07:04:49 -05001164 @echo ''
1165 @echo 'Build flags:'
1166 @echo ''
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001167 @echo ' make V=0|1 [targets] 0 => quiet build (default), 1 => verbose build'
1168 @echo ' make V=2 [targets] 2 => give reason for rebuild of target'
1169 @echo ' make O=dir [targets] Locate all output files in "dir", including .config'
1170 @echo ' make C=1 [targets] Check all c source with $$CHECK (sparse by default)'
1171 @echo ' make C=2 [targets] Force check of all c source with $$CHECK'
1172 @echo ' make RECORDMCOUNT_WARN=1 [targets] Warn about ignored mcount sections'
1173 @echo ' make W=n [targets] Enable extra gcc checks, n=1,2,3 where'
1174 @echo ' 1: warnings which may be relevant and do not occur too often'
1175 @echo ' 2: warnings which occur quite often but may still be relevant'
1176 @echo ' 3: more obscure warnings, can most likely be ignored'
1177 @echo ' Multiple levels can be combined with W=12 or W=123'
1178 @echo ''
1179 @echo 'Execute "make" or "make all" to build all targets marked with [*] '
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001180
1181
1182help-board-dirs := $(addprefix help-,$(board-dirs))
1183
1184help-boards: $(help-board-dirs)
1185
Kumar Gala8d357602016-10-19 15:13:41 -05001186boards-per-dir = $(sort $(notdir $(wildcard $(srctree)/boards/*/$*/*_defconfig)))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001187
1188$(help-board-dirs): help-%:
Anas Nashif3a87dae2015-12-06 09:51:33 -05001189 @echo 'Architecture specific targets ($(ARCH) $*):'
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001190 @$(if $(boards-per-dir), \
1191 $(foreach b, $(boards-per-dir), \
1192 printf " %-24s - Build for %s\\n" $*/$(b) $(subst _defconfig,,$(b));) \
1193 echo '')
1194
1195
1196# Documentation targets
1197# ---------------------------------------------------------------------------
Anas Nashif486a2a12015-07-30 07:06:40 -04001198%docs: FORCE
1199 $(Q)$(MAKE) -C doc htmldocs
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001200
Juan Manuel Cruz5bd398d2016-08-23 15:06:38 -05001201clean: clean-dirs-target
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001202 $(call cmd,rmdirs)
1203 $(call cmd,rmfiles)
1204 @find $(if $(KBUILD_EXTMOD), $(KBUILD_EXTMOD), .) $(RCS_FIND_IGNORE) \
Anas Nashif801874d2015-12-06 09:09:03 -05001205 \( -name '*.[oas]' -o -name '.*.cmd' \
1206 -o -name '*.dwo' -o -name '.*.d' -o -name '.*.tmp' \
Anas Nashif3bdae612015-12-06 19:38:53 -05001207 -o -name '.tmp_*.o.*' -o -name '*.gcno' \) -type f \
1208 -print | xargs rm -f
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001209
1210# Generate tags for editors
1211# ---------------------------------------------------------------------------
1212quiet_cmd_tags = GEN $@
1213 cmd_tags = $(CONFIG_SHELL) $(srctree)/scripts/tags.sh $@
1214
1215tags TAGS cscope gtags: FORCE
1216 $(call cmd,tags)
1217
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001218endif #ifeq ($(config-targets),1)
1219endif #ifeq ($(mixed-targets),1)
1220
Juan Manuel Cruzef07b962015-06-05 12:58:50 -05001221PHONY += checkstack kernelversion image_name
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001222
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001223CHECKSTACK_ARCH := $(ARCH)
Anas Nashif3a87dae2015-12-06 09:51:33 -05001224
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001225checkstack:
Anas Nashif3a87dae2015-12-06 09:51:33 -05001226 $(OBJDUMP) -d $(O)/$(KERNEL_ELF_NAME) | \
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001227 $(PERL) $(src)/scripts/checkstack.pl $(CHECKSTACK_ARCH)
1228
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001229kernelversion:
1230 @echo $(KERNELVERSION)
1231
1232image_name:
1233 @echo $(KBUILD_IMAGE)
1234
1235# Clear a bunch of variables before executing the submake
1236tools/: FORCE
1237 $(Q)mkdir -p $(objtree)/tools
1238 $(Q)$(MAKE) LDFLAGS= MAKEFLAGS="$(filter --j% -j,$(MAKEFLAGS))" O=$(objtree) subdir=tools -C $(src)/tools/
1239
1240tools/%: FORCE
1241 $(Q)mkdir -p $(objtree)/tools
1242 $(Q)$(MAKE) LDFLAGS= MAKEFLAGS="$(filter --j% -j,$(MAKEFLAGS))" O=$(objtree) subdir=tools -C $(src)/tools/ $*
1243
Kumar Gala8d357602016-10-19 15:13:41 -05001244-include $(srctree)/boards/$(ARCH)/$(BOARD_NAME)/Makefile.board
Anas Nashif2bc9d692017-01-07 16:31:34 -05001245
1246ifneq ($(EMU_PLATFORM),)
1247-include $(srctree)/scripts/Makefile.$(EMU_PLATFORM)
1248else
1249run:
1250 @echo ===================================================
1251 @echo Emulation/Simulation not supported with this board.
1252 @echo ===================================================
1253endif
1254
Anas Nashif78e1b0e2016-02-01 23:30:37 -05001255ifneq ($(FLASH_SCRIPT),)
Anas Nashif51be9a52016-01-15 12:18:53 -05001256flash: zephyr
1257 @echo "Flashing $(BOARD_NAME)"
1258 $(Q)$(CONFIG_SHELL) $(srctree)/scripts/support/$(FLASH_SCRIPT) flash
Anas Nashif78e1b0e2016-02-01 23:30:37 -05001259else
1260flash: FORCE
1261 @echo Flashing not supported with this board.
1262 @echo Please check the documentation for alternate instructions.
Anas Nashif6d9ed992017-01-07 13:22:21 -05001263endif
Anas Nashif78e1b0e2016-02-01 23:30:37 -05001264
Anas Nashif6d9ed992017-01-07 13:22:21 -05001265ifneq ($(DEBUG_SCRIPT),)
1266debug: zephyr
1267 $(Q)$(CONFIG_SHELL) $(srctree)/scripts/support/$(DEBUG_SCRIPT) debug
1268
1269else
Anas Nashif78e1b0e2016-02-01 23:30:37 -05001270debug: FORCE
1271 @echo Debugging not supported with this board.
1272 @echo Please check the documentation for alternate instructions.
1273endif
Anas Nashifa49762c2016-02-01 22:24:21 -05001274
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001275# Single targets
1276# ---------------------------------------------------------------------------
1277# Single targets are compatible with:
1278# - build with mixed source and output
1279# - build with separate output dir 'make O=...'
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001280#
1281# target-dir => where to store outputfile
1282# build-dir => directory in kernel source tree to use
1283
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001284 build-dir = $(patsubst %/,%,$(dir $@))
1285 target-dir = $(dir $@)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001286
1287%.s: %.c prepare scripts FORCE
1288 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1289%.i: %.c prepare scripts FORCE
1290 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1291%.o: %.c prepare scripts FORCE
1292 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
Peter Mitsis8e35cc82016-01-13 13:02:56 -05001293%.o: %.cpp prepare scripts FORCE
1294 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1295%.o: %.cxx prepare scripts FORCE
1296 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001297%.lst: %.c prepare scripts FORCE
1298 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1299%.s: %.S prepare scripts FORCE
1300 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1301%.o: %.S prepare scripts FORCE
1302 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1303%.symtypes: %.c prepare scripts FORCE
1304 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1305
1306# Modules
1307/: prepare scripts FORCE
Anas Nashiff05ee8f2015-06-05 21:38:07 -04001308 $(Q)$(MAKE) $(build)=$(build-dir)
Anas Nashif7b9455b2015-08-22 11:41:34 -04001309
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001310%/: prepare scripts FORCE
Anas Nashiff05ee8f2015-06-05 21:38:07 -04001311 $(Q)$(MAKE) $(build)=$(build-dir)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001312
1313# FIXME Should go into a make.lib or something
1314# ===========================================================================
1315
1316quiet_cmd_rmdirs = $(if $(wildcard $(rm-dirs)),CLEAN $(wildcard $(rm-dirs)))
1317 cmd_rmdirs = rm -rf $(rm-dirs)
1318
1319quiet_cmd_rmfiles = $(if $(wildcard $(rm-files)),CLEAN $(wildcard $(rm-files)))
1320 cmd_rmfiles = rm -f $(rm-files)
1321
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001322# read all saved command lines
1323
1324targets := $(wildcard $(sort $(targets)))
1325cmd_files := $(wildcard .*.cmd $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
1326
1327ifneq ($(cmd_files),)
1328 $(cmd_files): ; # Do not try to update included dependency files
1329 include $(cmd_files)
1330endif
1331
1332endif # skip-makefile
1333
1334PHONY += FORCE
1335FORCE:
1336
1337# Declare the contents of the .PHONY variable as phony. We keep that
1338# information in a variable so we can use it in if_changed and friends.
1339.PHONY: $(PHONY)