blob: 35ae41edbe5af74064dd5550e008ae68bb680817 [file] [log] [blame]
Anas Nashif027b69e2016-02-08 21:32:24 -05001VERSION_MAJOR = 1
Anas Nashif0e6c11a2016-08-25 07:14:47 -04002VERSION_MINOR = 5
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 Nashif27603f42015-06-18 10:46:52 -0400319GENIDT = scripts/gen_idt/gen_idt
Anas Nashifbd227482015-08-25 10:04:04 -0400320GENOFFSET_H = scripts/gen_offset_header/gen_offset_header
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600321PERL = perl
322PYTHON = python
323CHECK = sparse
324
Anas Nashif6afc4852015-04-24 18:30:36 -0400325CHECKFLAGS := -Wbitwise -Wno-return-void $(CF)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600326CFLAGS_GCOV = -fprofile-arcs -ftest-coverage
327
Anas Nashif1b702152015-08-23 22:01:07 -0400328ifeq ($(COMPILER),clang)
329ifneq ($(CROSS_COMPILE),)
330CLANG_TARGET := -target $(notdir $(CROSS_COMPILE:%-=%))
331GCC_TOOLCHAIN := $(dir $(CROSS_COMPILE))
332endif
333ifneq ($(GCC_TOOLCHAIN),)
334CLANG_GCC_TC := -gcc-toolchain $(GCC_TOOLCHAIN)
335endif
336ifneq ($(IA),1)
337CLANG_IA_FLAG = -no-integrated-as
338endif
339CLANG_FLAGS := $(CLANG_TARGET) $(CLANG_GCC_TC) $(CLANG_IA_FLAG)
340endif
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600341
342# Use USERINCLUDE when you must reference the UAPI directories only.
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600343USERINCLUDE := -include $(CURDIR)/include/generated/autoconf.h
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600344
Anas Nashif3a87dae2015-12-06 09:51:33 -0500345SOC_NAME = $(subst $(DQUOTE),,$(CONFIG_SOC))
Anas Nashifd2f1c762016-04-03 09:37:43 -0400346SOC_SERIES = $(subst $(DQUOTE),,$(CONFIG_SOC_SERIES))
Anas Nashifa02c34e2016-03-31 08:07:42 -0400347SOC_FAMILY = $(subst $(DQUOTE),,$(CONFIG_SOC_FAMILY))
Anas Nashifd2f1c762016-04-03 09:37:43 -0400348
Anas Nashiffe039db2016-01-22 13:51:50 -0500349override ARCH = $(subst $(DQUOTE),,$(CONFIG_ARCH))
Anas Nashif2d762242015-12-07 20:20:25 -0500350BOARD_NAME = $(subst $(DQUOTE),,$(CONFIG_BOARD))
Anas Nashif3a87dae2015-12-06 09:51:33 -0500351KERNEL_NAME = $(subst $(DQUOTE),,$(CONFIG_KERNEL_BIN_NAME))
352KERNEL_ELF_NAME = $(KERNEL_NAME).elf
Anas Nashif06e78de2015-11-01 11:59:09 -0500353KERNEL_BIN_NAME = $(KERNEL_NAME).bin
Kumar Galabbf736c2016-10-24 10:57:46 -0500354KERNEL_HEX_NAME = $(KERNEL_NAME).hex
Anas Nashif6361be22016-05-22 12:34:06 -0400355KERNEL_STAT_NAME = $(KERNEL_NAME).stat
Anas Nashif3a87dae2015-12-06 09:51:33 -0500356
Anas Nashifd2f1c762016-04-03 09:37:43 -0400357export SOC_FAMILY SOC_SERIES SOC_PATH SOC_NAME BOARD_NAME
Kumar Galabbf736c2016-10-24 10:57:46 -0500358export ARCH KERNEL_NAME KERNEL_ELF_NAME KERNEL_BIN_NAME KERNEL_HEX_NAME
Anas Nashifb8823772015-06-05 22:46:00 -0400359# Use ZEPHYRINCLUDE when you must reference the include/ directory.
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600360# Needed to be compatible with the O= option
Anas Nashifdf73d322015-11-02 20:42:35 -0500361ZEPHYRINCLUDE = \
Anas Nashif3a87dae2015-12-06 09:51:33 -0500362 -I$(srctree)/arch/$(ARCH)/include \
Anas Nashifd2f1c762016-04-03 09:37:43 -0400363 -I$(srctree)/arch/$(ARCH)/soc/$(SOC_PATH) \
Kumar Gala8d357602016-10-19 15:13:41 -0500364 -I$(srctree)/boards/$(ARCH)/$(BOARD_NAME) \
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600365 $(if $(KBUILD_SRC), -I$(srctree)/include) \
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600366 -I$(srctree)/include \
367 -I$(CURDIR)/include/generated \
Anas Nashif8b45f4e2015-10-21 07:24:39 -0400368 -I$(CURDIR)/misc/generated/sysgen \
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600369 $(USERINCLUDE) \
370 $(STDINCLUDE)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600371
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600372KBUILD_CPPFLAGS := -DKERNEL
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600373
Juan Manuel Cruz068ebc02015-05-06 10:41:08 -0500374KBUILD_CFLAGS := -c -g -std=c99 \
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600375 -fno-asynchronous-unwind-tables \
Anas Nashif1b702152015-08-23 22:01:07 -0400376 -Wall \
Andrew Boie5bb06312015-08-11 12:46:06 -0700377 -Wno-format-zero-length \
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600378 -Wno-main -ffreestanding
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600379
Peter Mitsis8e35cc82016-01-13 13:02:56 -0500380KBUILD_CXXFLAGS := -c -g -std=c++11 \
381 -fno-reorder-functions \
382 -fno-asynchronous-unwind-tables \
Peter Mitsis8e35cc82016-01-13 13:02:56 -0500383 -fcheck-new \
384 -fno-defer-pop -Wall \
385 -Wno-unused-but-set-variable \
386 -Wno-format-zero-length \
387 -Wno-main -ffreestanding \
388 -ffunction-sections -fdata-sections \
389 -fno-rtti -fno-exceptions
390
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600391KBUILD_AFLAGS := -c -g -xassembler-with-cpp
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600392
Peter Mitsis61eff492015-06-01 11:27:55 -0400393LDFLAGS += $(call ld-option,-nostartfiles)
394LDFLAGS += $(call ld-option,-nodefaultlibs)
395LDFLAGS += $(call ld-option,-nostdlib)
396LDFLAGS += $(call ld-option,-static)
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600397
Anas Nashif32bff692015-06-23 21:11:04 -0400398KERNELVERSION = $(VERSION_MAJOR)$(if $(VERSION_MINOR),.$(VERSION_MINOR)$(if $(PATCHLEVEL),.$(PATCHLEVEL)))$(EXTRAVERSION)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600399
Daniel Thompson90f6ae12016-09-22 15:00:21 +0100400exports += VERSION_MAJOR VERSION_MINOR PATCHLEVEL VERSION_RESERVED EXTRAVERSION
401exports += KERNELRELEASE KERNELVERSION
402exports += ARCH CONFIG_SHELL HOSTCC HOSTCFLAGS CROSS_COMPILE AS LD CC CXX
403exports += CPP AR NM STRIP OBJCOPY OBJDUMP GDB
404exports += MAKE AWK INSTALLKERNEL PERL PYTHON GENIDT GENOFFSET_H
405exports += HOSTCXX HOSTCXXFLAGS CHECK CHECKFLAGS
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600406
Daniel Thompson90f6ae12016-09-22 15:00:21 +0100407exports += KBUILD_CPPFLAGS NOSTDINC_FLAGS ZEPHYRINCLUDE OBJCOPYFLAGS LDFLAGS
408exports += KBUILD_CFLAGS KBUILD_CXXFLAGS CFLAGS_GCOV KBUILD_AFLAGS AFLAGS_KERNEL
409exports += KBUILD_ARFLAGS
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600410
Daniel Thompson90f6ae12016-09-22 15:00:21 +0100411# Push the exports to sub-processes
412export $(exports)
413
414# Make the exports available to third-party build systems
415PHONY += outputexports
416outputexports: Makefile.export
417Makefile.export: include/config/auto.conf FORCE
418 $(call filechk,Makefile.export)
419
420define filechk_Makefile.export
421 (echo "# file is auto-generated, do not modify !"; \
422 echo "BOARD=$(BOARD)"; \
423 echo; \
Daniel Thompson4f0c6f42016-11-08 22:17:36 +0000424 $(foreach e,$(exports),echo $(e)=$($(e));) echo; \
425 echo "include $(O)/include/config/auto.conf";)
Daniel Thompson90f6ae12016-09-22 15:00:21 +0100426endef
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600427
428# Files to ignore in find ... statements
429
430export RCS_FIND_IGNORE := \( -name SCCS -o -name BitKeeper -o -name .svn -o \
431 -name CVS -o -name .pc -o -name .hg -o -name .git \) \
432 -prune -o
433export RCS_TAR_IGNORE := --exclude SCCS --exclude BitKeeper --exclude .svn \
434 --exclude CVS --exclude .pc --exclude .hg --exclude .git
435
436# ===========================================================================
437# Rules shared between *config targets and build targets
438
439# Basic helpers built in scripts/
440PHONY += scripts_basic
441scripts_basic:
Anas Nashif27603f42015-06-18 10:46:52 -0400442 $(Q)$(MAKE) $(build)=scripts/basic
443 $(Q)$(MAKE) $(build)=scripts/gen_idt
Anas Nashifbd227482015-08-25 10:04:04 -0400444 $(Q)$(MAKE) $(build)=scripts/gen_offset_header
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600445
446# To avoid any implicit rule to kick in, define an empty command.
447scripts/basic/%: scripts_basic ;
448
449PHONY += outputmakefile
450# outputmakefile generates a Makefile in the output directory, if using a
451# separate output directory. This allows convenient use of make in the
452# output directory.
453outputmakefile:
454ifneq ($(KBUILD_SRC),)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600455 $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkmakefile \
Anas Nashiffde80d72015-08-22 14:40:43 -0400456 $(srctree) $(objtree) $(VERSION_MAJOR) $(VERSION_MINOR)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600457endif
458
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600459# To make sure we do not include .config for any of the *config targets
460# catch them early, and hand them over to scripts/kconfig/Makefile
461# It is allowed to specify more targets when calling make, including
462# mixing *config targets and build targets.
463# For example 'make oldconfig all'.
464# Detect when mixed targets is specified, and make a second invocation
465# of make so .config is not included in this case either (for *config).
466
Juan Manuel Cruz6de34002015-02-15 21:46:45 -0600467version_h := include/generated/version.h
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600468
Anas Nashifa77c7d82016-02-02 07:04:49 -0500469no-dot-config-targets := pristine distclean clean mrproper help kconfig-help \
Anas Nashifb2147172015-11-01 17:55:27 -0500470 cscope gtags TAGS tags help% %docs check% \
471 $(version_h) headers_% kernelversion %src-pkg
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600472
473config-targets := 0
474mixed-targets := 0
475dot-config := 1
476
477ifneq ($(filter $(no-dot-config-targets), $(MAKECMDGOALS)),)
478 ifeq ($(filter-out $(no-dot-config-targets), $(MAKECMDGOALS)),)
479 dot-config := 0
480 endif
481endif
482
Anas Nashifd6ebd182015-05-28 12:54:03 -0400483ifneq ($(filter config %config,$(MAKECMDGOALS)),)
484 config-targets := 1
485 ifneq ($(filter-out config %config,$(MAKECMDGOALS)),)
486 mixed-targets := 1
487 endif
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600488endif
489
490ifeq ($(mixed-targets),1)
491# ===========================================================================
492# We're called with mixed targets (*config and build targets).
493# Handle them one by one.
494
495PHONY += $(MAKECMDGOALS) __build_one_by_one
496
497$(filter-out __build_one_by_one, $(MAKECMDGOALS)): __build_one_by_one
498 @:
499
500__build_one_by_one:
501 $(Q)set -e; \
502 for i in $(MAKECMDGOALS); do \
503 $(MAKE) -f $(srctree)/Makefile $$i; \
504 done
505
506else
507ifeq ($(config-targets),1)
508# ===========================================================================
509# *config targets only - make sure prerequisites are updated, and descend
510# in scripts/kconfig to make the *config target
511
512# Read arch specific Makefile to set KBUILD_DEFCONFIG as needed.
513# KBUILD_DEFCONFIG may point out an alternative default configuration
514# used for 'make defconfig'
Anas Nashifdf73d322015-11-02 20:42:35 -0500515include $(srctree)/arch/$(subst $(DQUOTE),,$(CONFIG_ARCH))/Makefile
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600516export KBUILD_DEFCONFIG KBUILD_KCONFIG
517
518config: scripts_basic outputmakefile FORCE
Anas Nashif27603f42015-06-18 10:46:52 -0400519 $(Q)$(MAKE) $(build)=scripts/kconfig $@
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600520
521%config: scripts_basic outputmakefile FORCE
Anas Nashif27603f42015-06-18 10:46:52 -0400522 $(Q)$(MAKE) $(build)=scripts/kconfig $@
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600523
524else
525# ===========================================================================
Anas Nashif65230942015-06-05 22:41:29 -0400526# Build targets only - this includes zephyr, arch specific targets, clean
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600527# targets and others. In general all targets except *config targets.
528
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600529# Additional helpers built in scripts/
530# Carefully list dependencies so we do not try to build scripts twice
531# in parallel
532PHONY += scripts
Dirk Brandewie991e9d32015-06-01 09:48:10 -0700533scripts: scripts_basic include/config/auto.conf include/config/tristate.conf
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600534 $(Q)$(MAKE) $(build)=$(@)
535
Andrew Boied9a84c52016-07-26 14:45:14 -0700536# arch/ must be last here so that .gnu.linkonce magic for interrupts/exceptions
537# works as expected
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600538
Benjamin Walsh76c76a12016-02-10 14:36:39 -0500539ifneq ($(strip $(MAKEFILE_APP_DIR)),)
540MAKEFILE_APP := $(realpath $(MAKEFILE_APP_DIR)/Makefile.app)
541endif
542
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600543ifneq ($(strip $(PROJECT)),)
Benjamin Walsh76c76a12016-02-10 14:36:39 -0500544ifneq ($(strip $(MAKEFILE_APP)), )
545include $(MAKEFILE_APP)
546else
Anas Nashifa8259af2015-08-10 10:01:15 -0400547-include $(PROJECT)/Makefile.app
Benjamin Walsh76c76a12016-02-10 14:36:39 -0500548endif
Anas Nashifa8259af2015-08-10 10:01:15 -0400549ifneq ($(strip $(KBUILD_ZEPHYR_APP)),)
550export KBUILD_ZEPHYR_APP
551endif
Anas Nashif0c395172015-10-25 09:42:58 -0400552app-y := $(SOURCE_DIR)
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600553endif
554
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600555
556ifeq ($(dot-config),1)
557# Read in config
558-include include/config/auto.conf
559
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600560# Read in dependencies to all Kconfig* files, make sure to run
561# oldconfig if changes are detected.
562-include include/config/auto.conf.cmd
563
564# To avoid any implicit rule to kick in, define an empty command
565$(KCONFIG_CONFIG) include/config/auto.conf.cmd: ;
566
567# If .config is newer than include/config/auto.conf, someone tinkered
568# with it and forgot to run make oldconfig.
569# if auto.conf.cmd is missing then we are probably in a cleaned tree so
570# we execute the config step to be sure to catch updated Kconfig files
571include/config/%.conf: $(KCONFIG_CONFIG) include/config/auto.conf.cmd
572 $(Q)$(MAKE) -f $(srctree)/Makefile silentoldconfig
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600573
574else
575# Dummy target needed, because used as prerequisite
576include/config/auto.conf: ;
577endif # $(dot-config)
578
Andrew Boie625acd52016-11-07 10:27:45 -0800579# kernel objects are built as a static library
Dmitriy Korovkin9558d472016-09-09 13:59:17 -0400580libs-y := kernel/unified/
Anas Nashif4d090e02016-10-30 16:33:25 -0400581core-y := lib/ misc/ net/ boards/ ext/ subsys/ tests/ arch/
Dmitriy Korovkin19f28842016-10-03 14:58:18 -0400582drivers-y := drivers/
Dmitriy Korovkin9558d472016-09-09 13:59:17 -0400583
Anas Nashifdf73d322015-11-02 20:42:35 -0500584ARCH = $(subst $(DQUOTE),,$(CONFIG_ARCH))
585export ARCH
Juro Bystrickyf25ac092016-07-26 09:44:16 -0700586
Allan Stephens74b98c12015-06-08 11:43:13 -0400587ifdef ZEPHYR_GCC_VARIANT
588include $(srctree)/scripts/Makefile.toolchain.$(ZEPHYR_GCC_VARIANT)
Anas Nashif83d4ea92015-06-18 18:59:37 -0400589else
590$(if $(CROSS_COMPILE),, \
591 $(error ZEPHYR_GCC_VARIANT is not set. ))
Anas Nashifaca245d2015-05-13 00:44:22 -0400592endif
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600593
Anas Nashif37b1d1e2016-05-25 09:03:33 -0400594-include $(srctree)/ext/Makefile
Anas Nashif1aaa11b2016-06-13 18:35:02 -0400595-include $(srctree)/lib/Makefile
Anas Nashif37b1d1e2016-05-25 09:03:33 -0400596
Juan Manuel Cruz2170ca72016-04-05 16:34:54 -0500597ifneq ($(CC),)
598ifeq ($(shell $(CC) -v 2>&1 | grep -Ec "clang version|icx version"), 1)
599COMPILER := clang
600else
601COMPILER := gcc
602endif
603export COMPILER
604endif
605
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600606# The all: target is the default when no target is given on the
607# command line.
608# This allow a user to issue only 'make' to build a kernel including modules
Anas Nashif65230942015-06-05 22:41:29 -0400609# Defaults to zephyr, but the arch makefile usually adds further targets
610all: zephyr
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600611
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600612ifdef CONFIG_READABLE_ASM
613# Disable optimizations that make assembler listings hard to read.
614# reorder blocks reorders the control in the function
615# ipa clone creates specialized cloned functions
616# partial inlining inlines only parts of functions
617KBUILD_CFLAGS += $(call cc-option,-fno-reorder-blocks,) \
618 $(call cc-option,-fno-ipa-cp-clone,) \
619 $(call cc-option,-fno-partial-inlining)
620endif
621
Anas Nashifeb1fdf52015-07-29 09:33:32 -0400622ifeq ($(CONFIG_DEBUG),y)
Andrew Boie11693c12016-07-22 12:15:40 -0700623KBUILD_CFLAGS += -Og
Anas Nashifeb1fdf52015-07-29 09:33:32 -0400624else
625KBUILD_CFLAGS += -Os
626endif
627
Andre Guedes1622e4f2016-01-07 14:21:22 -0200628ifeq ($(CONFIG_STACK_CANARIES),y)
629KBUILD_CFLAGS += $(call cc-option,-fstack-protector-all,)
630else
631KBUILD_CFLAGS += $(call cc-option,-fno-stack-protector,)
632endif
633
Andrew Boie63cea242016-06-14 10:55:57 -0700634ifeq ($(CONFIG_OMIT_FRAME_POINTER),y)
635fp_arg := $(call cc-option,-fomit-frame-pointer,)
636else
637fp_arg := $(call cc-option,-fno-omit-frame-pointer,)
638endif
639KBUILD_CFLAGS += $(fp_arg)
640KBUILD_CXXFLAGS += $(fp_arg)
641
Anas Nashif3852c8c2015-08-22 18:14:42 -0400642KBUILD_CFLAGS += $(subst $(DQUOTE),,$(CONFIG_COMPILER_OPT))
643
Anas Nashif06e78de2015-11-01 11:59:09 -0500644export LDFLAG_LINKERCMD
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600645
Anas Nashifd2f1c762016-04-03 09:37:43 -0400646ifeq ($(SOC_SERIES),)
647SOC_PATH = $(SOC_NAME)
648else
649SOC_PATH = $(SOC_FAMILY)/$(SOC_SERIES)
650endif
Anas Nashif3a87dae2015-12-06 09:51:33 -0500651include arch/$(ARCH)/Makefile
Anas Nashifd9af71b2015-09-23 15:54:28 -0400652
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600653KBUILD_CFLAGS += $(CFLAGS)
Peter Mitsis8e35cc82016-01-13 13:02:56 -0500654KBUILD_CXXFLAGS += $(CXXFLAGS)
Juan Manuel Cruza0b54662015-02-17 11:16:47 -0600655KBUILD_AFLAGS += $(CFLAGS)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600656
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600657
Anas Nashif1b702152015-08-23 22:01:07 -0400658ifeq ($(COMPILER),clang)
Anas Nashif1b702152015-08-23 22:01:07 -0400659KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,)
660KBUILD_CFLAGS += $(call cc-disable-warning, unused-variable)
661KBUILD_CFLAGS += $(call cc-disable-warning, format-invalid-specifier)
662KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
663# Quiet clang warning: comparison of unsigned expression < 0 is always false
664KBUILD_CFLAGS += $(call cc-disable-warning, tautological-compare)
665else
666
667# This warning generated too much noise in a regular build.
668# Use make W=1 to enable this warning (see scripts/Makefile.build)
669KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
670KBUILD_CFLAGS += $(call cc-option,-fno-reorder-functions)
671KBUILD_CFLAGS += $(call cc-option,-fno-defer-pop)
672endif
673
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600674# We trigger additional mismatches with less inlining
675ifdef CONFIG_DEBUG_SECTION_MISMATCH
676KBUILD_CFLAGS += $(call cc-option, -fno-inline-functions-called-once)
677endif
678
679# arch Makefile may override CC so keep this after arch Makefile is included
680NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
Anas Nashifef2fc592016-04-17 11:18:39 +0800681NOSTDINC_FLAGS += -isystem $(shell $(CC) -print-file-name=include-fixed)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600682CHECKFLAGS += $(NOSTDINC_FLAGS)
683
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600684# disable pointer signed / unsigned warnings in gcc 4.0
685KBUILD_CFLAGS += $(call cc-disable-warning, pointer-sign)
686
687# disable invalid "can't wrap" optimizations for signed / pointers
688KBUILD_CFLAGS += $(call cc-option,-fno-strict-overflow)
689
Anas Nashif20439b42015-10-14 11:03:41 -0400690# generate an extra file that specifies the maximum amount of stack used,
691# on a per-function basis.
Anas Nashif9fe306b2015-12-05 09:36:38 -0500692ifdef CONFIG_STACK_USAGE
Anas Nashif20439b42015-10-14 11:03:41 -0400693KBUILD_CFLAGS += $(call cc-option,-fstack-usage)
Anas Nashif9fe306b2015-12-05 09:36:38 -0500694endif
Anas Nashif20439b42015-10-14 11:03:41 -0400695
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600696
697# disallow errors like 'EXPORT_GPL(foo);' with missing header
698KBUILD_CFLAGS += $(call cc-option,-Werror=implicit-int)
699
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600700# Prohibit date/time macros, which would make the build non-deterministic
Anas Nashif47f80882015-06-22 19:44:37 -0400701# KBUILD_CFLAGS += $(call cc-option,-Werror=date-time)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600702
703# use the deterministic mode of AR if available
704KBUILD_ARFLAGS := $(call ar-option,D)
705
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600706include $(srctree)/scripts/Makefile.extrawarn
707
708# Add user supplied CPPFLAGS, AFLAGS and CFLAGS as the last assignments
709KBUILD_CPPFLAGS += $(KCPPFLAGS)
710KBUILD_AFLAGS += $(KAFLAGS)
711KBUILD_CFLAGS += $(KCFLAGS)
712
713# Use --build-id when available.
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600714
Anas Nashif74326ec2016-05-01 17:44:15 -0400715LINKFLAGPREFIX ?= -Wl,
Anas Nashif204b66d2015-12-31 12:08:23 -0500716LDFLAGS_zephyr += $(LDFLAGS)
Anas Nashif74326ec2016-05-01 17:44:15 -0400717LDFLAGS_zephyr += $(call cc-ldoption,$(LINKFLAGPREFIX)-X)
718LDFLAGS_zephyr += $(call cc-ldoption,$(LINKFLAGPREFIX)-N)
719LDFLAGS_zephyr += $(call cc-ldoption,$(LINKFLAGPREFIX)--gc-sections)
720LDFLAGS_zephyr += $(call cc-ldoption,$(LINKFLAGPREFIX)--build-id=none)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600721
Juan Manuel Cruz0d01f5b2015-02-22 14:29:18 -0600722LD_TOOLCHAIN ?= -D__GCC_LINKER_CMD__
Juan Manuel Cruz1c8f5a72015-04-30 17:19:04 -0500723
Anas Nashif6c5509f2016-01-05 08:42:23 -0500724ifdef CONFIG_HAVE_CUSTOM_LINKER_SCRIPT
Benjamin Walshec89a102016-08-15 17:24:28 -0400725KBUILD_LDS := $(subst $(DQUOTE),,$(PROJECT_BASE)/$(CONFIG_CUSTOM_LINKER_SCRIPT))
726ifeq ($(wildcard $(KBUILD_LDS)),)
727KBUILD_LDS := $(subst $(DQUOTE),,$(CONFIG_CUSTOM_LINKER_SCRIPT))
728endif
Anas Nashif6c5509f2016-01-05 08:42:23 -0500729else
730# Try a board specific linker file
Kumar Gala8d357602016-10-19 15:13:41 -0500731KBUILD_LDS := $(srctree)/boards/$(ARCH)/$(BOARD_NAME)/linker.ld
Anas Nashif6c5509f2016-01-05 08:42:23 -0500732
733# If not available, try an SoC specific linker file
734ifeq ($(wildcard $(KBUILD_LDS)),)
Andrew Boieff872dc2016-05-04 13:13:56 -0700735KBUILD_LDS := $(srctree)/arch/$(ARCH)/soc/$(SOC_PATH)/linker.ld
Anas Nashif6c5509f2016-01-05 08:42:23 -0500736endif
737endif
738
739export LD_TOOLCHAIN KBUILD_LDS
Juan Manuel Cruz0d01f5b2015-02-22 14:29:18 -0600740
Juro Bystrickyf25ac092016-07-26 09:44:16 -0700741ifdef MAKEFILE_TOOLCHAIN_DO_PASS2
742# KBUILD_CFLAGS known at this point.
743# We can now determine which multilib libraries to use
744include $(srctree)/scripts/Makefile.toolchain.$(ZEPHYR_GCC_VARIANT)
745endif
746
Daniel Thompson389ed152016-09-11 20:51:22 +0100747QEMU = $(addsuffix /,$(QEMU_BIN_PATH))$(QEMU_$(ARCH))
Juro Bystrickyf25ac092016-07-26 09:44:16 -0700748
Anas Nashif06e78de2015-11-01 11:59:09 -0500749# The all: target is the default when no target is given on the
750# command line.
751# This allow a user to issue only 'make' to build a kernel including modules
752# Defaults to zephyr, but the arch makefile usually adds further targets
Anas Nashif6361be22016-05-22 12:34:06 -0400753all: $(KERNEL_BIN_NAME) $(KERNEL_STAT_NAME)
Anas Nashif06e78de2015-11-01 11:59:09 -0500754
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600755# Default kernel image to build when no specific target is given.
756# KBUILD_IMAGE may be overruled on the command line or
757# set in the environment
758# Also any assignments in arch/$(ARCH)/Makefile take precedence over
759# this default value
Anas Nashif65230942015-06-05 22:41:29 -0400760export KBUILD_IMAGE ?= zephyr
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600761
Anas Nashifc0078cf2015-10-25 14:02:19 -0400762zephyr-dirs := $(patsubst %/,%,$(filter %/, $(core-y) $(drivers-y) \
Kumar Galaff694e92016-06-16 16:16:46 -0500763 $(libs-y)))
Juan Manuel Cruz941059c2016-08-26 17:24:13 -0500764
765# Workaround for some make notdir implementations that require
766# the paramenter not to end in "/".
767zephyr-app-dir-root-name := $(patsubst %/,%, $(SOURCE_DIR))
768
Juan Manuel Cruz5bd398d2016-08-23 15:06:38 -0500769zephyr-app-dir-root := $(abspath $(patsubst %, %/.., $(SOURCE_DIR)))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600770
Juan Manuel Cruz5bd398d2016-08-23 15:06:38 -0500771zephyr-alldirs := $(sort $(zephyr-dirs) $(SOURCE_DIR) $(patsubst %/,%,$(filter %/, \
Anas Nashifce5dc6a2015-12-06 20:02:33 -0500772 $(core-) $(drivers-) $(libs-) $(app-))))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600773
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600774core-y := $(patsubst %/, %/built-in.o, $(core-y))
Juan Manuel Cruz941059c2016-08-26 17:24:13 -0500775app-y := $(patsubst %, %/built-in.o, $(notdir $(zephyr-app-dir-root-name)))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600776drivers-y := $(patsubst %/, %/built-in.o, $(drivers-y))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600777libs-y1 := $(patsubst %/, %/lib.a, $(libs-y))
778libs-y2 := $(patsubst %/, %/built-in.o, $(libs-y))
779libs-y := $(libs-y1) $(libs-y2)
780
Andrew Boied9a84c52016-07-26 14:45:14 -0700781# core-y must be last here. several arches use .gnu.linkonce magic
782# to register interrupt or exception handlers, and defaults under
783# arch/ (part of core-y) must be linked after drivers or libs.
Kumar Galaff694e92016-06-16 16:16:46 -0500784export KBUILD_ZEPHYR_MAIN := $(drivers-y) $(libs-y) $(core-y)
Anas Nashif65230942015-06-05 22:41:29 -0400785export LDFLAGS_zephyr
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600786
Anas Nashif369cf082015-05-17 08:55:21 -0400787ALL_LIBS += $(TOOLCHAIN_LIBS)
788export ALL_LIBS
789
Daniel Thompson608abd92016-09-29 17:49:51 -0700790zephyr-deps := $(KBUILD_LDS) $(KBUILD_ZEPHYR_MAIN) $(app-y) $(ALL_LIBS)
791
Anas Nashif06e78de2015-11-01 11:59:09 -0500792LINK_LIBS := $(foreach l,$(ALL_LIBS), -l$(l))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600793
Anas Nashif06e78de2015-11-01 11:59:09 -0500794OUTPUT_FORMAT ?= elf32-i386
795OUTPUT_ARCH ?= i386
796
Kumar Gala44b712d2016-06-03 10:52:12 -0500797quiet_cmd_ar_target = AR $@
Dmitriy Korovkin19f28842016-10-03 14:58:18 -0400798# Do not put lib.a into libzephyr.a. lib.a files are to be linked separately to
799# the final image
Dmitriy Korovkin9558d472016-09-09 13:59:17 -0400800cmd_ar_target = rm -f $@; $(AR) rcT$(KBUILD_ARFLAGS) $@ \
Dmitriy Korovkin19f28842016-10-03 14:58:18 -0400801 $(filter-out %/lib.a, $(KBUILD_ZEPHYR_MAIN))
Anas Nashiff4ed1082015-12-06 19:27:22 -0500802libzephyr.a: $(zephyr-deps)
Kumar Gala44b712d2016-06-03 10:52:12 -0500803 $(call cmd,ar_target)
Anas Nashiff4ed1082015-12-06 19:27:22 -0500804
Anas Nashif06e78de2015-11-01 11:59:09 -0500805quiet_cmd_create-lnk = LINK $@
806 cmd_create-lnk = \
807( \
808 echo $(LDFLAGS_zephyr); \
Anas Nashifd53c2372016-06-13 18:31:00 -0400809 echo "$(LINKFLAGPREFIX)-Map=$(KERNEL_NAME).map"; \
Anas Nashif06e78de2015-11-01 11:59:09 -0500810 echo "-L $(objtree)/include/generated"; \
811 echo "-u _OffsetAbsSyms -u _ConfigAbsSyms"; \
812 echo "-e __start"; \
Anas Nashif74326ec2016-05-01 17:44:15 -0400813 echo "$(LINKFLAGPREFIX)--start-group"; \
814 echo "$(LINKFLAGPREFIX)--whole-archive"; \
815 echo "$(KBUILD_ZEPHYR_APP)"; \
Kumar Galaff694e92016-06-16 16:16:46 -0500816 echo "$(app-y)"; \
Anas Nashiff4ed1082015-12-06 19:27:22 -0500817 echo "libzephyr.a"; \
Anas Nashif74326ec2016-05-01 17:44:15 -0400818 echo "$(LINKFLAGPREFIX)--no-whole-archive"; \
Dmitriy Korovkin19f28842016-10-03 14:58:18 -0400819 echo "$(filter %/lib.a, $(KBUILD_ZEPHYR_MAIN))"; \
Anas Nashif06e78de2015-11-01 11:59:09 -0500820 echo "$(objtree)/arch/$(ARCH)/core/offsets/offsets.o"; \
Anas Nashif74326ec2016-05-01 17:44:15 -0400821 echo "$(LINKFLAGPREFIX)--end-group"; \
Anas Nashif06e78de2015-11-01 11:59:09 -0500822 echo "$(LIB_INCLUDE_DIR) $(LINK_LIBS)"; \
823) > $@
824
Anas Nashifa0cf03b2016-05-14 21:56:35 -0400825$(KERNEL_NAME).lnk: $(zephyr-deps)
Anas Nashif06e78de2015-11-01 11:59:09 -0500826 $(call cmd,create-lnk)
827
Anas Nashif872676c2016-02-02 07:58:26 -0500828linker.cmd: $(zephyr-deps)
Anas Nashif06e78de2015-11-01 11:59:09 -0500829 $(Q)$(CC) -x assembler-with-cpp -nostdinc -undef -E -P \
830 $(LDFLAG_LINKERCMD) $(LD_TOOLCHAIN) -I$(srctree)/include \
Anas Nashif20fe6782016-01-31 04:46:37 -0500831 -I$(objtree)/include/generated $(EXTRA_LINKER_CMD_OPT) $(KBUILD_LDS) -o $@
Anas Nashif06e78de2015-11-01 11:59:09 -0500832
Andy Ross018b8312016-08-25 10:18:09 -0700833PREBUILT_KERNEL = $(KERNEL_NAME)_prebuilt.elf
Anas Nashif06e78de2015-11-01 11:59:09 -0500834
Andy Ross018b8312016-08-25 10:18:09 -0700835$(PREBUILT_KERNEL): $(zephyr-deps) libzephyr.a $(KBUILD_ZEPHYR_APP) $(app-y) linker.cmd $(KERNEL_NAME).lnk
Anas Nashif74326ec2016-05-01 17:44:15 -0400836 $(Q)$(CC) -T linker.cmd @$(KERNEL_NAME).lnk -o $@
Anas Nashif06e78de2015-11-01 11:59:09 -0500837
838quiet_cmd_gen_idt = SIDT $@
Andrew Boie2ee9aca2016-01-27 12:19:15 -0800839 cmd_gen_idt = \
840( \
841 $(OBJCOPY) -I $(OUTPUT_FORMAT) -O binary -j intList $< isrList.bin && \
842 $(GENIDT) -i isrList.bin -n $(CONFIG_IDT_NUM_VECTORS) -o staticIdt.bin \
Andrew Boie238fe1f2016-10-21 10:30:21 -0700843 -m irq_int_vector_map.bin \
Andrew Boie0d03e322016-07-26 11:36:31 -0700844 -l $(CONFIG_MAX_IRQ_LINES) $(GENIDT_EXTRA_ARGS) && \
Andrew Boie2ee9aca2016-01-27 12:19:15 -0800845 $(OBJCOPY) -I binary -B $(OUTPUT_ARCH) -O $(OUTPUT_FORMAT) \
846 --rename-section .data=staticIdt staticIdt.bin staticIdt.o && \
847 $(OBJCOPY) -I binary -B $(OUTPUT_ARCH) -O $(OUTPUT_FORMAT) \
Andrew Boie2ee9aca2016-01-27 12:19:15 -0800848 --rename-section .data=irq_int_vector_map irq_int_vector_map.bin \
849 irq_int_vector_map.o && \
Andrew Boie238fe1f2016-10-21 10:30:21 -0700850 rm staticIdt.bin irq_int_vector_map.bin isrList.bin \
Anas Nashif06e78de2015-11-01 11:59:09 -0500851)
852
Andy Ross018b8312016-08-25 10:18:09 -0700853staticIdt.o: $(PREBUILT_KERNEL)
Anas Nashif06e78de2015-11-01 11:59:09 -0500854 $(call cmd,gen_idt)
855
856quiet_cmd_lnk_elf = LINK $@
857 cmd_lnk_elf = \
858( \
Andrew Boie238fe1f2016-10-21 10:30:21 -0700859 $(CC) -T linker.cmd @$(KERNEL_NAME).lnk staticIdt.o \
860 irq_int_vector_map.o -o $@; \
Ido Yariva8c48ea2016-01-04 14:59:09 -0500861 ${OBJCOPY} --change-section-address intList=${CONFIG_PHYS_LOAD_ADDR} $@ elf.tmp;\
Anas Nashif06e78de2015-11-01 11:59:09 -0500862 $(OBJCOPY) -R intList elf.tmp $@; \
863 rm elf.tmp \
864)
865
Benjamin Walsh0107c802016-05-18 16:24:59 -0400866ASSERT_WARNING_STR := \
867 "\n ------------------------------------------------------------" \
868 "\n --- WARNING: __ASSERT() statements are globally ENABLED ---" \
869 "\n --- The kernel will run more slowly and uses more memory ---" \
870 "\n ------------------------------------------------------------\n\n" \
871
872WARN_ABOUT_ASSERT := $(if $(CONFIG_ASSERT),echo -e -n $(ASSERT_WARNING_STR),true)
873
Andrew Boiea5ae8892016-10-19 13:05:35 -0700874
875DEPRECATION_WARNING_STR := \
876 "\n WARNING: The board '$(BOARD)' is deprecated and will be" \
877 "\n removed in version $(CONFIG_BOARD_DEPRECATED)\n\n"
878
879WARN_ABOUT_DEPRECATION := $(if $(CONFIG_BOARD_DEPRECATED),echo -e \
880 -n $(DEPRECATION_WARNING_STR),true)
881
Anas Nashif06e78de2015-11-01 11:59:09 -0500882ifeq ($(ARCH),x86)
Andrew Boiecaf19b22016-10-21 10:39:43 -0700883$(KERNEL_ELF_NAME): staticIdt.o linker.cmd
Anas Nashif06e78de2015-11-01 11:59:09 -0500884 $(call cmd,lnk_elf)
Andy Ross4cc228b2016-10-03 12:20:39 -0700885 @$(srctree)/scripts/check_link_map.py $(KERNEL_NAME).map
Benjamin Walsh0107c802016-05-18 16:24:59 -0400886 @$(WARN_ABOUT_ASSERT)
Andrew Boiea5ae8892016-10-19 13:05:35 -0700887 @$(WARN_ABOUT_DEPRECATION)
Anas Nashif06e78de2015-11-01 11:59:09 -0500888else
Andy Ross018b8312016-08-25 10:18:09 -0700889$(KERNEL_ELF_NAME): $(PREBUILT_KERNEL)
890 @cp $(PREBUILT_KERNEL) $(KERNEL_ELF_NAME)
Benjamin Walsh0107c802016-05-18 16:24:59 -0400891 @$(WARN_ABOUT_ASSERT)
Andrew Boiea5ae8892016-10-19 13:05:35 -0700892 @$(WARN_ABOUT_DEPRECATION)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600893endif
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600894
Anas Nashif06e78de2015-11-01 11:59:09 -0500895
896quiet_cmd_gen_bin = BIN $@
897 cmd_gen_bin = \
898( \
899 $(OBJDUMP) -S $< > $(KERNEL_NAME).lst; \
900 $(OBJCOPY) -S -O binary -R .note -R .comment -R COMMON -R .eh_frame $< $@; \
901 $(STRIP) -s -o $(KERNEL_NAME).strip $<; \
902)
903
904$(KERNEL_BIN_NAME): $(KERNEL_ELF_NAME)
905 $(call cmd,gen_bin)
906
Kumar Galabbf736c2016-10-24 10:57:46 -0500907quiet_cmd_gen_hex = HEX $@
908 cmd_gen_hex = \
909( \
910 $(OBJCOPY) -S -O ihex -R .note -R .comment -R COMMON -R .eh_frame $< $@; \
911)
912
913$(KERNEL_HEX_NAME): $(KERNEL_ELF_NAME)
914 $(call cmd,gen_hex)
915
Anas Nashif6361be22016-05-22 12:34:06 -0400916$(KERNEL_STAT_NAME): $(KERNEL_BIN_NAME) $(KERNEL_ELF_NAME)
917 @$(READELF) -e $(KERNEL_ELF_NAME) > $@
918
919ram_report: $(KERNEL_STAT_NAME)
920 @$(srctree)/scripts/size_report -r -o $(O)
921rom_report: $(KERNEL_STAT_NAME)
922 @$(srctree)/scripts/size_report -F -o $(O)
923
Anas Nashif06e78de2015-11-01 11:59:09 -0500924zephyr: $(zephyr-deps) $(KERNEL_BIN_NAME)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600925
926# The actual objects are generated when descending,
927# make sure no implicit rule kicks in
Juan Manuel Cruz5bd398d2016-08-23 15:06:38 -0500928$(sort $(zephyr-deps)): $(zephyr-dirs) zephyr-app-dir ;
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600929
Anas Nashif65230942015-06-05 22:41:29 -0400930# Handle descending into subdirectories listed in $(zephyr-dirs)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600931# Preset locale variables to speed up the build process. Limit locale
932# tweaks to this spot to avoid wrong language settings when running
933# make menuconfig etc.
934# Error messages still appears in the original language
935
Anas Nashif65230942015-06-05 22:41:29 -0400936PHONY += $(zephyr-dirs)
937$(zephyr-dirs): prepare scripts
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600938 $(Q)$(MAKE) $(build)=$@
939
Juan Manuel Cruz5bd398d2016-08-23 15:06:38 -0500940PHONY += zephyr-app-dir
941zephyr-app-dir: prepare scripts
Juan Manuel Cruz941059c2016-08-26 17:24:13 -0500942 $(Q)$(MAKE) $(build)=$(notdir $(zephyr-app-dir-root-name)) \
943 srctree=$(zephyr-app-dir-root)
Kumar Galaff694e92016-06-16 16:16:46 -0500944
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600945# Things we need to do before we recursively start building the kernel
946# or the modules are listed in "prepare".
947# A multi level approach is used. prepareN is processed before prepareN-1.
948# archprepare is used in arch Makefiles and when processed asm symlink,
949# version.h and scripts_basic is processed / created.
950
951# Listed in dependency order
Juan Manuel Cruz0167ee42015-06-05 13:08:53 -0500952PHONY += prepare prepare1 prepare2 prepare3
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600953
954# prepare3 is used to check if we are building in a separate output directory,
955# and if so do:
956# 1) Check that make has not been executed in the kernel src $(srctree)
Juan Manuel Cruz8b16a172015-05-22 18:38:45 -0500957prepare3:
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600958ifneq ($(KBUILD_SRC),)
959 @$(kecho) ' Using $(srctree) as source for kernel'
Juan Manuel Cruzce3533a2015-02-23 16:48:02 -0600960 $(Q)if [ -f $(srctree)/.config ]; then \
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600961 echo >&2 " $(srctree) is not clean, please run 'make mrproper'"; \
962 echo >&2 " in the '$(srctree)' directory.";\
963 /bin/false; \
964 fi;
965endif
966
967# prepare2 creates a makefile if using a separate output directory
Juan Manuel Cruz8b16a172015-05-22 18:38:45 -0500968prepare2: prepare3 outputmakefile
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600969
Juan Manuel Cruz8b16a172015-05-22 18:38:45 -0500970prepare1: prepare2 $(version_h) \
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600971 include/config/auto.conf
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600972
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600973archprepare_common = $(strip \
Anas Nashifb2147172015-11-01 17:55:27 -0500974 prepare1 scripts_basic \
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600975 )
976
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600977
Juan Manuel Cruz0167ee42015-06-05 13:08:53 -0500978archprepare = $(strip \
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600979 $(archprepare_common) \
Juan Manuel Cruzba741fe2015-02-15 14:12:00 -0600980 )
981
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600982# All the preparing..
Juan Manuel Cruz0167ee42015-06-05 13:08:53 -0500983prepare: $(archprepare) FORCE
984 $(Q)$(MAKE) $(build)=.
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600985
986# Generate some files
987# ---------------------------------------------------------------------------
988
989# KERNELRELEASE can change from a few different places, meaning version.h
990# needs to be updated, so this check is forced on all builds
991
Anas Nashif71bed172015-06-23 19:10:06 -0400992VERSION_MAJOR_HEX=$(shell printf '%02x\n' ${VERSION_MAJOR})
993VERSION_MINOR_HEX=$(shell printf '%02x\n' ${VERSION_MINOR})
Anas Nashif4cf6c312015-06-23 19:19:45 -0400994PATCHLEVEL_HEX=$(shell printf '%02x\n' ${PATCHLEVEL})
Anas Nashif8ad48782015-06-23 10:29:17 -0400995VERSION_RESERVED_HEX=00
Anas Nashif4cf6c312015-06-23 19:19:45 -0400996KERNEL_VERSION_HEX=0x$(VERSION_MAJOR_HEX)$(VERSION_MINOR_HEX)$(PATCHLEVEL_HEX)
Juan Manuel Cruz6de34002015-02-15 21:46:45 -0600997
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -0600998define filechk_version.h
Juan Manuel Cruz6de34002015-02-15 21:46:45 -0600999 (echo "#ifndef _KERNEL_VERSION_H_"; \
1000 echo "#define _KERNEL_VERSION_H_"; \
1001 echo ;\
Anas Nashif4cf6c312015-06-23 19:19:45 -04001002 (echo \#define ZEPHYR_VERSION_CODE $(shell \
1003 expr $(VERSION_MAJOR) \* 65536 + 0$(VERSION_MINOR) \* 256 + 0$(PATCHLEVEL)); \
1004 echo '#define ZEPHYR_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))';); \
1005 echo ;\
Anas Nashif9c72cf52015-08-25 10:44:41 -04001006 echo "#define KERNELVERSION \\"; \
Anas Nashif71bed172015-06-23 19:10:06 -04001007 echo "$(KERNEL_VERSION_HEX)$(VERSION_RESERVED_HEX)"; \
Juan Manuel Cruz6de34002015-02-15 21:46:45 -06001008 echo "#define KERNEL_VERSION_NUMBER $(KERNEL_VERSION_HEX)"; \
Juan Manuel Cruz6de34002015-02-15 21:46:45 -06001009 echo "#define KERNEL_VERSION_MAJOR $(VERSION_MAJOR)"; \
1010 echo "#define KERNEL_VERSION_MINOR $(VERSION_MINOR)"; \
Anas Nashif4cf6c312015-06-23 19:19:45 -04001011 echo "#define KERNEL_PATCHLEVEL $(PATCHLEVEL)"; \
Juan Manuel Cruz6de34002015-02-15 21:46:45 -06001012 echo "#define KERNEL_VERSION_STRING \"$(KERNELVERSION)\""; \
1013 echo; \
1014 echo "#endif /* _KERNEL_VERSION_H_ */";)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001015endef
1016
1017$(version_h): $(srctree)/Makefile FORCE
1018 $(call filechk,version.h)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001019
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001020PHONY += headerdep
1021headerdep:
1022 $(Q)find $(srctree)/include/ -name '*.h' | xargs --max-args 1 \
1023 $(srctree)/scripts/headerdep.pl -I$(srctree)/include
1024
1025# ---------------------------------------------------------------------------
1026
1027PHONY += depend dep
1028depend dep:
1029 @echo '*** Warning: make $@ is unnecessary now.'
1030
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001031###
1032# Cleaning is done on three levels.
1033# make clean Delete most generated files
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001034# make mrproper Delete the current configuration, and all generated files
1035# make distclean Remove editor backup files, patch leftover files and the like
1036
1037# Directories & files removed with 'make clean'
1038CLEAN_DIRS += $(MODVERDIR)
1039
Anas Nashif8e9d8eb2015-05-29 11:48:25 -04001040CLEAN_FILES += misc/generated/sysgen/kernel_main.c \
Anas Nashif8b45f4e2015-10-21 07:24:39 -04001041 misc/generated/sysgen/sysgen.h \
Anas Nashif06e78de2015-11-01 11:59:09 -05001042 misc/generated/sysgen/prj.mdef \
Kumar Gala62a9f9c2016-07-09 09:30:52 -05001043 misc/generated/sysgen/micro_private_types.h \
1044 misc/generated/sysgen/kernel_main.h \
Anas Nashif06e78de2015-11-01 11:59:09 -05001045 .old_version .tmp_System.map .tmp_version \
1046 .tmp_* System.map *.lnk *.map *.elf *.lst \
Kumar Galabdd77642016-10-24 11:00:32 -05001047 *.bin *.hex *.stat *.strip staticIdt.o linker.cmd
Juan Manuel Cruzce3533a2015-02-23 16:48:02 -06001048
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001049# Directories & files removed with 'make mrproper'
1050MRPROPER_DIRS += include/config usr/include include/generated \
1051 arch/*/include/generated .tmp_objdiff
Anas Nashif1e8dc5a2015-04-24 18:32:55 -04001052MRPROPER_FILES += .config .config.old .version $(version_h) \
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001053 Module.symvers tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS \
1054 signing_key.priv signing_key.x509 x509.genkey \
1055 extra_certificates signing_key.x509.keyid \
1056 signing_key.x509.signer
1057
Anas Nashiff05ee8f2015-06-05 21:38:07 -04001058# clean - Delete most
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001059#
1060clean: rm-dirs := $(CLEAN_DIRS)
1061clean: rm-files := $(CLEAN_FILES)
Anas Nashif65230942015-06-05 22:41:29 -04001062clean-dirs := $(addprefix _clean_, . $(zephyr-alldirs) )
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001063
Juan Manuel Cruz5bd398d2016-08-23 15:06:38 -05001064PHONY += clean-dirs-target clean archclean zephyrclean
1065clean-dirs-target:
1066 $(Q)$(MAKE) $(clean)=$(patsubst _clean_%,%,$(clean-dirs))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001067
Anas Nashif06e78de2015-11-01 11:59:09 -05001068clean: archclean
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001069
1070# mrproper - Delete all generated files, including .config
1071#
1072mrproper: rm-dirs := $(wildcard $(MRPROPER_DIRS))
1073mrproper: rm-files := $(wildcard $(MRPROPER_FILES))
Juan Manuel Cruzce3533a2015-02-23 16:48:02 -06001074mrproper-dirs := $(addprefix _mrproper_,scripts)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001075
1076PHONY += $(mrproper-dirs) mrproper archmrproper
1077$(mrproper-dirs):
1078 $(Q)$(MAKE) $(clean)=$(patsubst _mrproper_%,%,$@)
1079
1080mrproper: clean archmrproper $(mrproper-dirs)
1081 $(call cmd,rmdirs)
1082 $(call cmd,rmfiles)
1083
1084# distclean
1085#
1086PHONY += distclean
1087
1088distclean: mrproper
1089 @find $(srctree) $(RCS_FIND_IGNORE) \
1090 \( -name '*.orig' -o -name '*.rej' -o -name '*~' \
1091 -o -name '*.bak' -o -name '#*#' -o -name '.*.orig' \
1092 -o -name '.*.rej' -o -name '*%' -o -name 'core' \) \
1093 -type f -print | xargs rm -f
1094
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001095# Brief documentation of the typical targets used
1096# ---------------------------------------------------------------------------
1097
Kumar Gala8d357602016-10-19 15:13:41 -05001098boards := $(wildcard $(srctree)/boards/*/*/*_defconfig)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001099boards := $(sort $(notdir $(boards)))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001100
Anas Nashifa77c7d82016-02-02 07:04:49 -05001101kconfig-help:
1102 @echo 'Configuration targets:'
1103 @echo ''
1104 @$(MAKE) -f $(srctree)/scripts/kconfig/Makefile help
1105 @echo ''
1106
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001107help:
1108 @echo 'Cleaning targets:'
Juan Manuel Cruza5a6ec32015-10-02 13:07:55 -05001109 @echo ' clean - Remove most generated files but keep configuration and backup files'
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001110 @echo ' mrproper - Remove all generated files + config + various backup files'
1111 @echo ' distclean - mrproper + remove editor backup and patch files'
Juan Manuel Cruza5a6ec32015-10-02 13:07:55 -05001112 @echo ' pristine - Remove the output directory with all generated files'
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001113 @echo ''
1114 @echo 'Configuration targets:'
Anas Nashifa77c7d82016-02-02 07:04:49 -05001115 @echo ''
1116 @echo ' run <make kconfig-help>'
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001117 @echo ''
1118 @echo 'Other generic targets:'
1119 @echo ' all - Build all targets marked with [*]'
Anas Nashif7d348b02016-01-31 08:22:18 -05001120 @echo '* zephyr - Build a zephyr application'
1121 @echo ' qemu - Build a zephyr application and run it in qemu'
Andrew Boiebe13e262016-06-23 11:33:55 -07001122 @echo ' qemugdb - Same as 'qemu' but start a GDB server on port 1234'
Anas Nashif7d348b02016-01-31 08:22:18 -05001123 @echo ' flash - Build and flash an application'
Anas Nashifa77c7d82016-02-02 07:04:49 -05001124 @echo ' debug - Build and debug an application using GDB'
Anas Nashif1b4742f2016-09-24 15:07:28 -04001125 @echo ' ram_report - Build and create RAM usage report'
1126 @echo ' rom_report - Build and create ROM usage report'
Juan Manuel Cruza5a6ec32015-10-02 13:07:55 -05001127 @echo ''
Anas Nashif10bb38c2015-12-17 08:54:35 -05001128 @echo 'Supported Boards:'
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001129 @echo ''
Anas Nashif7d348b02016-01-31 08:22:18 -05001130 @echo ' To build an image for one of the supported boards below, run:'
1131 @echo ''
1132 @echo ' make BOARD=<BOARD NAME>'
1133 @echo ' in the application directory.'
Anas Nashifa77c7d82016-02-02 07:04:49 -05001134 @echo ''
Anas Nashif7d348b02016-01-31 08:22:18 -05001135 @echo ' To flash the image (if supported), run:'
1136 @echo ''
1137 @echo ' make BOARD=<BOARD NAME> flash'
1138 @echo ''
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001139 @$(if $(boards), \
1140 $(foreach b, $(boards), \
Anas Nashifa77c7d82016-02-02 07:04:49 -05001141 printf " make BOARD=%-24s - Build for %s\\n" $(subst _defconfig,,$(b)) $(subst _defconfig,,$(b));) \
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001142 echo '')
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001143
Anas Nashifa77c7d82016-02-02 07:04:49 -05001144 @echo ''
1145 @echo 'Build flags:'
1146 @echo ''
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001147 @echo ' make V=0|1 [targets] 0 => quiet build (default), 1 => verbose build'
1148 @echo ' make V=2 [targets] 2 => give reason for rebuild of target'
1149 @echo ' make O=dir [targets] Locate all output files in "dir", including .config'
1150 @echo ' make C=1 [targets] Check all c source with $$CHECK (sparse by default)'
1151 @echo ' make C=2 [targets] Force check of all c source with $$CHECK'
1152 @echo ' make RECORDMCOUNT_WARN=1 [targets] Warn about ignored mcount sections'
1153 @echo ' make W=n [targets] Enable extra gcc checks, n=1,2,3 where'
1154 @echo ' 1: warnings which may be relevant and do not occur too often'
1155 @echo ' 2: warnings which occur quite often but may still be relevant'
1156 @echo ' 3: more obscure warnings, can most likely be ignored'
1157 @echo ' Multiple levels can be combined with W=12 or W=123'
1158 @echo ''
1159 @echo 'Execute "make" or "make all" to build all targets marked with [*] '
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001160
1161
1162help-board-dirs := $(addprefix help-,$(board-dirs))
1163
1164help-boards: $(help-board-dirs)
1165
Kumar Gala8d357602016-10-19 15:13:41 -05001166boards-per-dir = $(sort $(notdir $(wildcard $(srctree)/boards/*/$*/*_defconfig)))
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001167
1168$(help-board-dirs): help-%:
Anas Nashif3a87dae2015-12-06 09:51:33 -05001169 @echo 'Architecture specific targets ($(ARCH) $*):'
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001170 @$(if $(boards-per-dir), \
1171 $(foreach b, $(boards-per-dir), \
1172 printf " %-24s - Build for %s\\n" $*/$(b) $(subst _defconfig,,$(b));) \
1173 echo '')
1174
1175
1176# Documentation targets
1177# ---------------------------------------------------------------------------
Anas Nashif486a2a12015-07-30 07:06:40 -04001178%docs: FORCE
1179 $(Q)$(MAKE) -C doc htmldocs
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001180
Juan Manuel Cruz5bd398d2016-08-23 15:06:38 -05001181clean: clean-dirs-target
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001182 $(call cmd,rmdirs)
1183 $(call cmd,rmfiles)
1184 @find $(if $(KBUILD_EXTMOD), $(KBUILD_EXTMOD), .) $(RCS_FIND_IGNORE) \
Anas Nashif801874d2015-12-06 09:09:03 -05001185 \( -name '*.[oas]' -o -name '.*.cmd' \
1186 -o -name '*.dwo' -o -name '.*.d' -o -name '.*.tmp' \
Anas Nashif3bdae612015-12-06 19:38:53 -05001187 -o -name '.tmp_*.o.*' -o -name '*.gcno' \) -type f \
1188 -print | xargs rm -f
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001189
1190# Generate tags for editors
1191# ---------------------------------------------------------------------------
1192quiet_cmd_tags = GEN $@
1193 cmd_tags = $(CONFIG_SHELL) $(srctree)/scripts/tags.sh $@
1194
1195tags TAGS cscope gtags: FORCE
1196 $(call cmd,tags)
1197
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001198endif #ifeq ($(config-targets),1)
1199endif #ifeq ($(mixed-targets),1)
1200
Juan Manuel Cruzef07b962015-06-05 12:58:50 -05001201PHONY += checkstack kernelversion image_name
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001202
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001203CHECKSTACK_ARCH := $(ARCH)
Anas Nashif3a87dae2015-12-06 09:51:33 -05001204
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001205checkstack:
Anas Nashif3a87dae2015-12-06 09:51:33 -05001206 $(OBJDUMP) -d $(O)/$(KERNEL_ELF_NAME) | \
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001207 $(PERL) $(src)/scripts/checkstack.pl $(CHECKSTACK_ARCH)
1208
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001209kernelversion:
1210 @echo $(KERNELVERSION)
1211
1212image_name:
1213 @echo $(KBUILD_IMAGE)
1214
1215# Clear a bunch of variables before executing the submake
1216tools/: FORCE
1217 $(Q)mkdir -p $(objtree)/tools
1218 $(Q)$(MAKE) LDFLAGS= MAKEFLAGS="$(filter --j% -j,$(MAKEFLAGS))" O=$(objtree) subdir=tools -C $(src)/tools/
1219
1220tools/%: FORCE
1221 $(Q)mkdir -p $(objtree)/tools
1222 $(Q)$(MAKE) LDFLAGS= MAKEFLAGS="$(filter --j% -j,$(MAKEFLAGS))" O=$(objtree) subdir=tools -C $(src)/tools/ $*
1223
Anas Nashif3a87dae2015-12-06 09:51:33 -05001224QEMU_FLAGS = $(QEMU_FLAGS_$(ARCH)) -pidfile qemu.pid
Juan Manuel Cruze7de0f62015-04-30 14:14:29 -05001225
Fabrice Olivero35dcc0a2016-04-15 10:46:30 +02001226ifneq ($(QEMU_PTY),)
1227 QEMU_FLAGS += -serial pty
1228else
Andrew Boie84fed362015-07-16 10:59:01 -07001229ifneq ($(QEMU_PIPE),)
Andrew Boie22c3fa32015-07-29 14:25:29 -07001230 # Send console output to a pipe, used for running automated sanity tests
1231 QEMU_FLAGS += -serial pipe:$(QEMU_PIPE)
Andrew Boie84fed362015-07-16 10:59:01 -07001232else
Andrew Boie206d0b42016-06-29 13:33:05 -07001233 QEMU_FLAGS += -serial mon:stdio
Andrew Boie84fed362015-07-16 10:59:01 -07001234endif
Fabrice Olivero35dcc0a2016-04-15 10:46:30 +02001235endif
Andrew Boie84fed362015-07-16 10:59:01 -07001236
Anas Nashif65230942015-06-05 22:41:29 -04001237qemu: zephyr
Andrew Boie84fed362015-07-16 10:59:01 -07001238 $(if $(QEMU_PIPE),,@echo "To exit from QEMU enter: 'CTRL+a, x'")
Anas Nashif3a87dae2015-12-06 09:51:33 -05001239 @echo '[QEMU] CPU: $(QEMU_CPU_TYPE_$(ARCH))'
Andrew Boiee79c9602016-03-10 16:02:24 -08001240 $(if $(CONFIG_X86_IAMCU),python $(ZEPHYR_BASE)/scripts/qemu-machine-hack.py $(KERNEL_ELF_NAME))
Anas Nashif3a87dae2015-12-06 09:51:33 -05001241 $(Q)$(QEMU) $(QEMU_FLAGS) $(QEMU_EXTRA_FLAGS) -kernel $(KERNEL_ELF_NAME)
Juan Manuel Cruze7de0f62015-04-30 14:14:29 -05001242
Andrew Boiebe13e262016-06-23 11:33:55 -07001243qemugdb: QEMU_EXTRA_FLAGS += -s -S
1244qemugdb: qemu
1245
Kumar Gala8d357602016-10-19 15:13:41 -05001246-include $(srctree)/boards/$(ARCH)/$(BOARD_NAME)/Makefile.board
Anas Nashif78e1b0e2016-02-01 23:30:37 -05001247ifneq ($(FLASH_SCRIPT),)
Anas Nashif51be9a52016-01-15 12:18:53 -05001248flash: zephyr
1249 @echo "Flashing $(BOARD_NAME)"
1250 $(Q)$(CONFIG_SHELL) $(srctree)/scripts/support/$(FLASH_SCRIPT) flash
1251
Anas Nashifa49762c2016-02-01 22:24:21 -05001252debug: zephyr
1253 $(Q)$(CONFIG_SHELL) $(srctree)/scripts/support/$(FLASH_SCRIPT) debug
Anas Nashif78e1b0e2016-02-01 23:30:37 -05001254else
1255flash: FORCE
1256 @echo Flashing not supported with this board.
1257 @echo Please check the documentation for alternate instructions.
1258
1259debug: FORCE
1260 @echo Debugging not supported with this board.
1261 @echo Please check the documentation for alternate instructions.
1262endif
Anas Nashifa49762c2016-02-01 22:24:21 -05001263
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001264# Single targets
1265# ---------------------------------------------------------------------------
1266# Single targets are compatible with:
1267# - build with mixed source and output
1268# - build with separate output dir 'make O=...'
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001269#
1270# target-dir => where to store outputfile
1271# build-dir => directory in kernel source tree to use
1272
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001273 build-dir = $(patsubst %/,%,$(dir $@))
1274 target-dir = $(dir $@)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001275
1276%.s: %.c prepare scripts FORCE
1277 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1278%.i: %.c prepare scripts FORCE
1279 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1280%.o: %.c prepare scripts FORCE
1281 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
Peter Mitsis8e35cc82016-01-13 13:02:56 -05001282%.o: %.cpp prepare scripts FORCE
1283 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1284%.o: %.cxx prepare scripts FORCE
1285 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001286%.lst: %.c prepare scripts FORCE
1287 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1288%.s: %.S prepare scripts FORCE
1289 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1290%.o: %.S prepare scripts FORCE
1291 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1292%.symtypes: %.c prepare scripts FORCE
1293 $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
1294
1295# Modules
1296/: prepare scripts FORCE
Anas Nashiff05ee8f2015-06-05 21:38:07 -04001297 $(Q)$(MAKE) $(build)=$(build-dir)
Anas Nashif7b9455b2015-08-22 11:41:34 -04001298
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001299%/: prepare scripts FORCE
Anas Nashiff05ee8f2015-06-05 21:38:07 -04001300 $(Q)$(MAKE) $(build)=$(build-dir)
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001301
1302# FIXME Should go into a make.lib or something
1303# ===========================================================================
1304
1305quiet_cmd_rmdirs = $(if $(wildcard $(rm-dirs)),CLEAN $(wildcard $(rm-dirs)))
1306 cmd_rmdirs = rm -rf $(rm-dirs)
1307
1308quiet_cmd_rmfiles = $(if $(wildcard $(rm-files)),CLEAN $(wildcard $(rm-files)))
1309 cmd_rmfiles = rm -f $(rm-files)
1310
Juan Manuel Cruza9b1c742015-01-29 15:22:58 -06001311# read all saved command lines
1312
1313targets := $(wildcard $(sort $(targets)))
1314cmd_files := $(wildcard .*.cmd $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
1315
1316ifneq ($(cmd_files),)
1317 $(cmd_files): ; # Do not try to update included dependency files
1318 include $(cmd_files)
1319endif
1320
1321endif # skip-makefile
1322
1323PHONY += FORCE
1324FORCE:
1325
1326# Declare the contents of the .PHONY variable as phony. We keep that
1327# information in a variable so we can use it in if_changed and friends.
1328.PHONY: $(PHONY)